Implementation of ClassOverridesRole
[gitmo/Perl-Critic-Dynamic-Moose.git] / lib / Perl / Critic / Policy / DynamicMoose / ClassOverridesRole.pm
CommitLineData
042287b0 1package Perl::Critic::Policy::DynamicMoose::ClassOverridesRole;
2use Moose;
3extends 'Perl::Critic::Policy::DynamicMoose';
4
5use Perl::Critic::Utils ':severities';
6
34b35078 7Readonly::Scalar my $EXPL => q{Explicitly exclude overriden methods};
042287b0 8sub default_severity { $SEVERITY_MEDIUM }
9
425e287e 10# Class::MOP::Class has no roles
11sub applies_to_metaclass { 'Moose::Meta::Class' }
12
042287b0 13sub violates_metaclass {
34b35078 14 my $self = shift;
15 my $class = shift;
042287b0 16
34b35078 17 my @violations;
18
19 for my $application ($class->role_applications) {
20 my $role = $application->role;
21 for my $method ($role->get_method_list) {
22 next if $application->is_method_excluded($method);
23
24 my $method_object = $class->get_method($method)
25 or next;
26
27 # no metadata, should check source role to make sure it's the
28 # same as $role
29 next if $method_object->isa('Moose::Meta::Role::Method');
30
31 my $class_name = $class->name;
32 my $role_name = $role->name;
33
34 my $desc = "Class '$class_name' method '$method' implicitly overrides the same method from role '$role_name'";
35 push @violations, $self->violation($desc, $EXPL);
36 }
37 }
38
39 return @violations;
042287b0 40}
41
42no Moose;
43
441;
45
46__END__
47
48=head1 NAME
49
50Perl::Critic::Policy::DynamicMoose::ClassOverridesRole
51
52=head1 DESCRIPTION
53
54
55=head1 WARNING
56
57B<VERY IMPORTANT:> Most L<Perl::Critic> Policies (including all the ones that
58ship with Perl::Critic> use pure static analysis -- they never compile nor
59execute any of the code that they analyze. However, this policy is very
60different. It actually attempts to compile your code and then compares the
61subroutines mentioned in your code to those found in the symbol table.
62Therefore you should B<not> use this Policy on any code that you do not trust,
63or may have undesirable side-effects at compile-time (such as connecting to the
64network or mutating files).
65
66For this Policy to work, all the modules included in your code must be
67installed locally, and must compile without error.
68
69=head1 AUTHOR
70
71Shawn M Moore, C<sartak@bestpractical.com>
72
73=cut
74