| 1 | package Perl::Critic::Policy::Modules::RequireExplicitInclusion; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use base 'Perl::Critic::Policy'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use List::MoreUtils qw( any ); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Readonly; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Perl::Critic::Utils qw( | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | :characters | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | :severities | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | &hashify | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | &is_class_name | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | &is_function_call | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | &is_perl_builtin | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | &is_qualified_name | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | &policy_short_name | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 1 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 1 | ▶ | use Perl::Critic::Exception::Configuration::Option::Policy::ParameterValue qw( | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | throw_policy_value | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 1 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 1 | ▶ | use Perl::Critic::StricterSubs::Utils qw( | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | &get_package_names_from_include_statements | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | &get_package_names_from_package_statements | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 1 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 1 | our $VERSION = '0.08'; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 1 | my $expl = | |||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 'Without importing a package, it is unlikely that references to things inside it even exist.'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 28 | ▶ | sub supported_parameters { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
supported_parameters: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | return ( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 28 | name => 'ignore_modules', | |||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | description => 'The names of modules to ignore if a violation is found', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | default_string => q{}, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | parser => \&_parse_modules, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 27 | ▶ | sub default_severity { return $SEVERITY_HIGH } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
default_severity: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 0 | ▶ | sub default_themes { return qw( strictersubs bugs ) } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
default_themes: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 28 | ▶ | sub applies_to { return 'PPI::Document' } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
applies_to: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 1 | Readonly my $MODULE_NAME_REGEX => | |||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | qr{ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | \b | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | [[:alpha:]_] | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | (?: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | (?: \w | :: )* | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | \w | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | )? | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | \b | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | }xms; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 1 | Readonly my $REGULAR_EXPRESSION_REGEX => qr{ [/] ( [^/]+ ) [/] }xms; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | # It's kind of unfortunate that I had to put capturing parentheses in the | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | # component regexes above, because they're not visible here and so make | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | # figuring out the positions of captures hard. Too bad we can't make the | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | # minimum perl version 5.10. :] | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 1 | Readonly my $MODULES_REGEX => | |||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | qr{ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | \A | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | \s* | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | (?: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | ( $MODULE_NAME_REGEX ) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | | $REGULAR_EXPRESSION_REGEX | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | ) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | \s* | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | }xms; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 28 | ▶ | sub _parse_modules { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_parse_modules: called CC=6 SCAR=18.3
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 28 | my ($self, $parameter, $config_string) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 28 | ▶ | my $module_specifications = $config_string // $parameter->get_default_string(); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$config_string // $parameter->get_default_string
MC/DC0%0/2
$config_string // $parameter->get_default_string
$config_string
$parameter->get_default_string
Truth table66%2/3
A // B
A= $config_stringB= $parameter->get_default_string
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 28 | ▶ | return if not $module_specifications; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 2 | ▶ | return if $module_specifications =~ m{ \A \s* \z }xms; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 2 | ▶ | while ( $module_specifications =~ s{ $MODULES_REGEX }{}xms ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 2 | my ($module, $regex_string) = ($1, $2); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 2 | $self->_handle_module_specification( | |||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | module => $module, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | regex_string => $regex_string, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | option_name => 'ignore_modules', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | option_value => $config_string, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 2 | ▶ | if ($module_specifications) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 0 | throw_policy_value | |||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | policy => $self->get_short_name(), | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | option_name => 'ignore_modules', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | option_value => $config_string, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | message_suffix => | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | qq{contains unparseable data: "$module_specifications"}; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 2 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 2 | ▶ | sub _handle_module_specification { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_handle_module_specification: called CC=5 SCAR=16.3
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | 2 | my ($self, %arguments) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 2 | ▶ | if ( my $regex_string = $arguments{regex_string} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | # These are module name patterns (e.g. /Acme/) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | 1 | my $actual_regex; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | 1 | eval { $actual_regex = qr/$regex_string/; 1 } ## no critic (ExtendedFormatting, LineBoundaryMatching, DotMatchAnything) | |||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | or throw_policy_value | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | policy => $self->get_short_name(), | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | option_name => $arguments{option_name}, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | option_value => $arguments{option_value}, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 1 | ▶ | message_suffix => | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | qq{contains an invalid regular expression: "$regex_string"}; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | # Can't use a hash due to stringification, so this is an AoA. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 1 | ▶ | $self->{_ignore_modules_regexes} ||= []; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"_ignore_modules_regexes"} ||= []
MC/DC0%0/1
$self->{"_ignore_modules_regexes"} ||= []
$self->{"_ignore_modules_regexes"}
Truth table50%1/2
A
A= $self->{"_ignore_modules_regexes"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | push | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | 1 | @{ $self->{_ignore_modules_regexes} }, | |||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | $actual_regex; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | # These are literal module names (e.g. Acme::Foo) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | 1 | ▶ | $self->{_ignore_modules} ||= {}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"_ignore_modules"} ||= {}
MC/DC0%0/1
$self->{"_ignore_modules"} ||= {}
$self->{"_ignore_modules"}
Truth table50%1/2
A
A= $self->{"_ignore_modules"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | 1 | $self->{_ignore_modules}{ $arguments{module} } = undef; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | 2 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | 28 | ▶ | sub violates { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
violates: called CC=3 SCAR=13.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | 28 | my ($self, undef, $doc) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | 28 | my @declared_packages = get_package_names_from_package_statements($doc); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | 28 | ▶ | if ( @declared_packages > 1 ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | 0 | ▶ | my $fname = $doc->filename() || 'unknown'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$doc->filename || 'unknown'
MC/DC0%0/1
$doc->filename || 'unknown'
$doc->filename
Truth table0%0/2
A
A= $doc->filename
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | 0 | my $pname = policy_short_name(__PACKAGE__); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | 0 | warn qq{$pname: Cannot cope with multiple packages in file "$fname"\n}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | 28 | my @included_packages = get_package_names_from_include_statements($doc); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | 28 | my @builtin_packages = ( qw(main UNIVERSAL CORE CORE::GLOBAL utf8), $EMPTY ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | 28 | my %all_packages = | |||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | hashify( @declared_packages, @included_packages, @builtin_packages ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | 28 | my @violations = ( | |||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | $self->_find_subroutine_call_violations( $doc, \%all_packages ), | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | $self->_find_class_method_call_violations( $doc, \%all_packages ), | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | $self->_find_symbol_violations( $doc, \%all_packages ), | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | 28 | return @violations; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | 28 | ▶ | sub _find_qualified_subroutine_calls { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_find_qualified_subroutine_calls: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | 28 | my $doc = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | my $calls = | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | $doc->find( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | 1088 | ▶ | sub { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | 1088 | my (undef, $elem) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | return | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | 1088 | ▶ | $elem->isa('PPI::Token::Word') | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%6/6
$elem->isa('PPI::Token::Word') && is_qualified_name($elem->content) && is_function_call($elem)
$elem->isa("PPI::Token::Word") and is_qualified_name($elem->content)
MC/DC100%3/3
$elem->isa('PPI::Token::Word') && is_qualified_name($elem->content) && is_function_call($elem)
$elem->isa("PPI::Token::Word")
is_qualified_name($elem->content)
is_function_call($elem)
Truth table100%4/4
A and B && C
A= $elem->isa("PPI::Token::Word")B= is_qualified_name($elem->content)C= is_function_call($elem)
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | && is_qualified_name( $elem->content() ) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | && is_function_call( $elem ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | 28 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | 28 | ▶ | return @{$calls} if $calls; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | 22 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | 28 | ▶ | sub _find_class_method_calls { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_find_class_method_calls: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | 28 | my $doc = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | my $calls = | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | $doc->find( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | 1088 | ▶ | sub { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | 1088 | my (undef, $elem) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | return | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | 1088 | ▶ | $elem->isa('PPI::Token::Word') | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%9/9
$elem->isa('PPI::Token::Word') && is_class_name($elem) && !is_perl_builtin($elem) && '__PACKAGE__' ne $elem->content
$elem->isa("PPI::Token::Word") and is_class_name($elem) and not is_perl_builtin($elem)
$elem->isa("PPI::Token::Word") and is_class_name($elem)
MC/DC100%4/4
$elem->isa('PPI::Token::Word') && is_class_name($elem) && !is_perl_builtin($elem) && '__PACKAGE__' ne $elem->content
$elem->isa("PPI::Token::Word")
is_class_name($elem)
not is_perl_builtin($elem)
'__PACKAGE__' ne $elem->content
Truth table100%5/5
A and B and not(C) && D
A= $elem->isa("PPI::Token::Word")B= is_class_name($elem)C= not is_perl_builtin($elem)D= '__PACKAGE__' ne $elem->content
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | && is_class_name( $elem ) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | && !is_perl_builtin( $elem ) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | && '__PACKAGE__' ne $elem->content(); # RT 43314, 44609 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | # From a design standpoint we should filter later, but | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | # the violation code is generic. The patch included with | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | # 44609, or adding '__PACKAGE__ to @builtin_packages, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | # would have also allowed, willy-nilly, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | # __PACKAGE__::foo() or $__PACKAGE__::foo, neither of | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | # which is correct. So I just hid __PACKAGE__->foo() from | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | # the violation logic. Mea culpa! Tom Wyant | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | 28 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | 28 | ▶ | return @{$calls} if $calls; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | 21 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | 28 | ▶ | sub _find_qualified_symbols { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_find_qualified_symbols: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | 28 | my $doc = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | my $symbols = | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | $doc->find( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | 1088 | ▶ | sub { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | 1088 | my (undef, $elem) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | return | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | 1088 | ▶ | $elem->isa('PPI::Token::Symbol') | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$elem->isa('PPI::Token::Symbol') && is_qualified_name($elem->canonical)
MC/DC100%2/2
$elem->isa('PPI::Token::Symbol') && is_qualified_name($elem->canonical)
$elem->isa('PPI::Token::Symbol')
is_qualified_name($elem->canonical)
Truth table100%3/3
A && B
A= $elem->isa('PPI::Token::Symbol')B= is_qualified_name($elem->canonical)
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | && is_qualified_name( $elem->canonical() ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | 28 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | 28 | ▶ | return @{$symbols} if $symbols; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | 10 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | 21 | ▶ | sub _extract_package_from_class_method_call { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_extract_package_from_class_method_call: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | # Class method calls look like "Foo::Bar->baz()" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | # So the package name will be the entire word, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | # which should be everything to the left of "->" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | 21 | my $word = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | # Remove trailing double colon, which is allowed and can be used for | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | # disambiguation. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | 21 | $word =~ s/::$//xms; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | 21 | return $word; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | 15 | ▶ | sub _extract_package_from_subroutine_call { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_extract_package_from_subroutine_call: called CC=2 SCAR=7.6
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | # Subroutine calls look like "Foo::Bar::baz()" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | # So the package name will be everything up | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | # to (but not including) the last "::". | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | 15 | my $word = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | 15 | ▶ | if ($word->content() =~ m/\A ( .* ) :: [^:]* \z/xms) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | 15 | return $1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | 32 | ▶ | sub _extract_package_from_symbol { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_extract_package_from_symbol: called CC=2 SCAR=7.6
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | # Qualified symbols look like "$Foo::Bar::baz" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | # So the package name will be everything between | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | # the sigil and the last "::". | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | 32 | my $symbol = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | 32 | ▶ | if ($symbol->canonical() =~ m/\A [\$*@%&] ( .* ) :: [^:]+ \z/xms) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | 32 | return $1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | 84 | ▶ | sub _find_violations { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_find_violations: called CC=8 SCAR=20.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | 84 | my ($self, $doc, $included_packages, $finder, $package_extractor) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | 84 | my @violations; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | 84 | for my $call ( $finder->( $doc ) ) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | 68 | my $package = $package_extractor->( $call ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | 68 | ▶ | next if exists $included_packages->{ $package }; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | 34 | ▶ | next if $self->_is_ignored_module( $package ); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | 32 | ▶ | next if exists $self->{_ignore_modules}->{ $package }; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | 30 | ▶ | if ( not ( $call eq 'STDIN' || $call eq 'STDOUT' || $call eq 'STDERR' ) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$call eq "STDIN" or $call eq "STDOUT" or $call eq "STDERR"
$call eq "STDIN" or $call eq "STDOUT"
MC/DC100%3/3
$call eq "STDIN" or $call eq "STDOUT" or $call eq "STDERR"
$call eq "STDIN"
$call eq "STDOUT"
$call eq "STDERR"
Truth table100%4/4
A or B or C
A= $call eq "STDIN"B= $call eq "STDOUT"C= $call eq "STDERR"
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 311 | 27 | my $desc = qq{Use of "$call" without including "$package"}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | 27 | push @violations, $self->violation( $desc, $expl, $call ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 313 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 314 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 316 | 84 | return @violations; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 317 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 320 | 34 | ▶ | sub _is_ignored_module { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_is_ignored_module: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 321 | 34 | my ($self, $package) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 323 | 34 | ▶ | my $ignore_hash = ($self->{_ignore_modules} //= {}); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->{"_ignore_modules"} //= {}
MC/DC100%1/1
$self->{"_ignore_modules"} //= {}
$self->{"_ignore_modules"}
Truth table100%2/2
A
A= $self->{"_ignore_modules"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 324 | 34 | ▶ | return if $ignore_hash->{ $package }; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 326 | 34 | ▶ | if ( my $ignore_regex_list = $self->{_ignore_modules_regexes} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 327 | 2 | ▶ | return any { $package =~ /$_/smx } @{$ignore_regex_list}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 328 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 330 | 32 | return 0; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 331 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 333 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 335 | 28 | ▶ | sub _find_subroutine_call_violations { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_find_subroutine_call_violations: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 336 | 28 | my ($self, $doc, $packages) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 337 | 28 | my $finder = \&_find_qualified_subroutine_calls; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 338 | 28 | my $extractor = \&_extract_package_from_subroutine_call; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 339 | 28 | return $self->_find_violations( $doc, $packages, $finder, $extractor ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 340 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 342 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 344 | 28 | ▶ | sub _find_class_method_call_violations { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_find_class_method_call_violations: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 345 | 28 | my ($self, $doc, $packages) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 346 | 28 | my $finder = \&_find_class_method_calls; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 347 | 28 | my $extractor = \&_extract_package_from_class_method_call; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 348 | 28 | return $self->_find_violations( $doc, $packages, $finder, $extractor ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 349 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 351 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 353 | 28 | ▶ | sub _find_symbol_violations { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_find_symbol_violations: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 354 | 28 | my ($self, $doc, $packages) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 355 | 28 | my $finder = \&_find_qualified_symbols; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 356 | 28 | my $extractor = \&_extract_package_from_symbol; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 357 | 28 | return $self->_find_violations( $doc, $packages, $finder, $extractor ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 358 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 360 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 362 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 364 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||