| 1 | package MooseX::Role::WarnOnConflict; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # ABSTRACT: Warn if classes override role methods without excluding them | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 2 | our $VERSION = '0.01'; | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use MooseX::Meta::Role::WarnOnConflict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use Moose::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use Moose::Exporter; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 2 | Moose::Exporter->setup_import_methods( also => 'Moose::Role' ); | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 1 | ▶ | sub init_meta { | ||||||||||||||||||||||||||
|
Subroutine
init_meta: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 16 | 1 | my ( $class, %opt ) = @_; | |||||||||||||||||||||||||||
| 17 | 1 | return Moose::Role->init_meta( ## | |||||||||||||||||||||||||||
| 18 | %opt, ## | ||||||||||||||||||||||||||||
| 19 | metaclass => 'MooseX::Meta::Role::WarnOnConflict' | ||||||||||||||||||||||||||||
| 20 | ); | ||||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | package # Hide from PAUSE | ||||||||||||||||||||||||||||
| 24 | MooseX::Meta::Role::Application::ToClass::WarnOnConflig; | ||||||||||||||||||||||||||||
| 25 | 2 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | 2 | ▶ | use Carp 'carp'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | 2 | our @CARP_NOT = (__PACKAGE__); | |||||||||||||||||||||||||||
| 28 | 2 | extends 'Moose::Meta::Role::Application::ToClass'; | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 3 | ▶ | sub apply_methods { | ||||||||||||||||||||||||||
|
Subroutine
apply_methods: called CC=11 SCAR=24.9
Pod
documented
| |||||||||||||||||||||||||||||
| 31 | 3 | my ( $self, $role, $class ) = @_; | |||||||||||||||||||||||||||
| 32 | 3 | my @implicitly_overridden; | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 3 | METHOD: foreach my $method_name ( $role->get_method_list ) { | |||||||||||||||||||||||||||
| 35 | 6 | ▶ | next METHOD if 'meta' eq $method_name; # Moose auto-exports this | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | 3 | ▶ | unless ( $self->is_method_excluded($method_name) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | # it if it has one already | ||||||||||||||||||||||||||||
| 39 | 1 | ▶ | if ( | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$class->has_method($method_name) and $class->get_method($method_name)->body != $role->get_method($method_name)->body
MC/DC0%0/2
$class->has_method($method_name) and $class->get_method($method_name)->body != $role->get_method($method_name)->body
$class->has_method($method_name)
$class->get_method($method_name)->body != $role->get_method($method_name)->body
Truth table33%1/3
A and B
A= $class->has_method($method_name)B= $class->get_method($method_name)->body != $role->get_method($method_name)->body
| |||||||||||||||||||||||||||||
| 40 | $class->has_method($method_name) | ||||||||||||||||||||||||||||
| 41 | && | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | # and if they are not the same thing ... | ||||||||||||||||||||||||||||
| 44 | $class->get_method($method_name)->body != | ||||||||||||||||||||||||||||
| 45 | $role->get_method($method_name)->body | ||||||||||||||||||||||||||||
| 46 | ) | ||||||||||||||||||||||||||||
| 47 | { | ||||||||||||||||||||||||||||
| 48 | 1 | push @implicitly_overridden => $method_name; | |||||||||||||||||||||||||||
| 49 | 1 | next; | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | else { | ||||||||||||||||||||||||||||
| 52 | # add method to the required methods | ||||||||||||||||||||||||||||
| 53 | 0 | $role->add_required_methods($method_name); | |||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | # add it, although it could be overridden | ||||||||||||||||||||||||||||
| 56 | 0 | $class->add_method( $method_name, | |||||||||||||||||||||||||||
| 57 | $role->get_method($method_name) ); | ||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 2 | ▶ | if ( $self->is_method_aliased($method_name) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 1 | my $aliased_method_name = $self->get_method_aliases->{$method_name}; | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | # it if it has one already | ||||||||||||||||||||||||||||
| 65 | 1 | ▶ | if ( | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$class->has_method($aliased_method_name) and $class->get_method($aliased_method_name)->body != $role->get_method($method_name)->body
MC/DC0%0/2
$class->has_method($aliased_method_name) and $class->get_method($aliased_method_name)->body != $role->get_method($method_name)->body
$class->has_method($aliased_method_name)
$class->get_method($aliased_method_name)->body != $role->get_method($method_name)->body
Truth table33%1/3
A and B
A= $class->has_method($aliased_method_name)B= $class->get_method($aliased_method_name)->body != $role->get_method($method_name)->body
| |||||||||||||||||||||||||||||
| 66 | $class->has_method($aliased_method_name) | ||||||||||||||||||||||||||||
| 67 | && | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | # and if they are not the same thing ... | ||||||||||||||||||||||||||||
| 70 | $class->get_method($aliased_method_name)->body != | ||||||||||||||||||||||||||||
| 71 | $role->get_method($method_name)->body | ||||||||||||||||||||||||||||
| 72 | ) | ||||||||||||||||||||||||||||
| 73 | { | ||||||||||||||||||||||||||||
| 74 | 1 | my $class_name = $class->name; | |||||||||||||||||||||||||||
| 75 | 1 | my $role_name = $role->name; | |||||||||||||||||||||||||||
| 76 | 1 | carp( | |||||||||||||||||||||||||||
| 77 | "$class_name should not alias $role_name '$method_name' to '$aliased_method_name' if a local method of the same name exists" | ||||||||||||||||||||||||||||
| 78 | ); | ||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | 1 | $class->add_method( $aliased_method_name, | |||||||||||||||||||||||||||
| 81 | $role->get_method($method_name) ); | ||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 3 | ▶ | if (@implicitly_overridden) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | 1 | ▶ | my $s = @implicitly_overridden > 1 ? "s" : ""; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 1 | my $class_name = $class->name; | |||||||||||||||||||||||||||
| 89 | 1 | my $role_name = $role->name; | |||||||||||||||||||||||||||
| 90 | 1 | my $methods = join ', ' => @implicitly_overridden; | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | # we use \n because we have no hope of guessing the right stack frame, | ||||||||||||||||||||||||||||
| 93 | # it's almost certainly never going to be the one above us | ||||||||||||||||||||||||||||
| 94 | 1 | carp(<<" END_ERROR"); | |||||||||||||||||||||||||||
| 95 | The class $class_name has implicitly overridden the method$s ($methods) from | ||||||||||||||||||||||||||||
| 96 | role $role_name. If this is intentional, please exclude the method$s from | ||||||||||||||||||||||||||||
| 97 | composition to silence this warning (see Moose::Cookbook::Roles::Recipe2) | ||||||||||||||||||||||||||||
| 98 | END_ERROR | ||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | # we must reset the cache here since | ||||||||||||||||||||||||||||
| 102 | # we are just aliasing methods, otherwise | ||||||||||||||||||||||||||||
| 103 | # the modifiers go wonky. | ||||||||||||||||||||||||||||
| 104 | 3 | $class->reset_package_cache_flag; | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 2 | 1; | |||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | __END__ | ||||||||||||||||||||||||||||