lib/Perl/Critic/Policy/Subroutines/RequireArgUnpacking.pm

filter: Statementstmt 38.8% 56 / 144 Branchbran 5.5% 6 / 108 Conditioncond 1.3% 1 / 74 MC/DCmcdc 0.0% 0 / 39 Subroutinesub 56.5% 13 / 23 Podpod 80.0% 4 / 5 totaltotal 20.3% 80 / 393 CC 82 SCAR 45.6
CC
82
Cov
19%
_is_delegation
272.0
_is_anon_sub
156.0
_is_unpack
110.0
CRAP 95.6
« lib/Perl/Critic/Policy/CodeLayout/ProhibitQuotedWordLists.pm ↑ Summary lib/Perl/Critic/Policy/ControlStructures/ProhibitMutatingListFunctions.pm »
1 package Perl::Critic::Policy::Subroutines::RequireArgUnpacking;
2  
3 40 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 40 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 40 ▶use Perl::Critic::Utils qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
10 :booleans :characters :classification hashify :severities
11 40 );
12 40 ▶use parent 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 40 our $VERSION = '1.156';
15  
16 #-----------------------------------------------------------------------------
17  
18 40 Readonly::Scalar my $AT => q{@};
19 40 Readonly::Scalar my $AT_ARG => q{@_}; ## no critic (InterpolationOfMetachars)
20 40 Readonly::Scalar my $DEREFERENCE => q{->};
21 40 Readonly::Scalar my $DOLLAR => q{$};
22 40 Readonly::Scalar my $DOLLAR_ARG => q{$_}; ## no critic (InterpolationOfMetaChars)
23  
24 40 Readonly::Scalar my $DESC => qq{Always unpack $AT_ARG first};
25 40 Readonly::Scalar my $EXPL => [178];
26  
27 #-----------------------------------------------------------------------------
28  
29 94 ▶sub supported_parameters {
Subroutine supported_parameters: called CC=1 SCAR=0.0
Pod undocumented
30 return (
31 {
32 94 name => 'short_subroutine_statements',
33 description =>
34 'The number of statements to allow without unpacking.',
35 default_string => '0',
36 behavior => 'integer',
37 integer_minimum => 0,
38 },
39 {
40 name => 'allow_subscripts',
41 description =>
42 'Should unpacking from array slices and elements be allowed?',
43 default_string => $FALSE,
44 behavior => 'boolean',
45 },
46 {
47 name => 'allow_delegation_to',
48 description =>
49 'Allow the usual delegation idiom to these namespaces/subroutines',
50 behavior => 'string list',
51 list_always_present_values => [ qw< SUPER:: NEXT:: > ],
52 },
53 {
54 name => 'allow_closures',
55 description => 'Allow unpacking by a closure',
56 default_string => $FALSE,
57 behavior => 'boolean',
58 },
59 );
60 }
61  
62 74 ▶sub default_severity { return $SEVERITY_HIGH }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
63 85 ▶sub default_themes { return qw( core pbp maintenance ) }
Subroutine default_themes: called CC=1 SCAR=0.0
Pod documented
64 33 ▶sub applies_to { return 'PPI::Statement::Sub' }
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
65  
66 #-----------------------------------------------------------------------------
67  
68 5 ▶sub violates {
Subroutine violates: called CC=14 SCAR=41.1
Pod documented
69 5 my ( $self, $elem, undef ) = @_;
70  
71 # forward declaration?
72 5 ▶ return if not $elem->block;
Branch: 1 branch
BranchTrueFalseTotal
unless $elem->block 0 5 5
73  
74 5 my @statements = $elem->block->schildren;
75  
76 # empty sub?
77 5 ▶ return if not @statements;
Branch: 1 branch
BranchTrueFalseTotal
unless @statements 0 5 5
78  
79 # Don't apply policy to short subroutines
80  
81 # Should we instead be doing a find() for PPI::Statement
82 # instances? That is, should we count all statements instead of
83 # just top-level statements?
84 5 ▶ return if $self->{_short_subroutine_statements} >= @statements;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_short_subroutine_statements"} >= @statements 0 5 5
85  
86 # look for explicit dereferences of @_, including '$_[0]'
87 # You may use "... = @_;" in the first paragraph of the sub
88 # Don't descend into nested or anonymous subs
89 5 my $state = 'unpacking'; # still in unpacking paragraph
90 5 for my $statement (@statements) {
91  
92 7 my @magic = _get_arg_symbols($statement);
93  
94 7 my $saw_unpack = $FALSE;
95  
96 MAGIC:
97 7 for my $magic (@magic) {
98 # allow conditional checks on the size of @_
99 0 ▶ next MAGIC if _is_size_check($magic);
Branch: 1 branch
BranchTrueFalseTotal
if _is_size_check($magic) 0 0 0
100  
101 0 ▶ if ('unpacking' eq $state) {
Branch: 1 branch
BranchTrueFalseTotal
if ("unpacking" eq $state) 0 0 0
102 0 ▶ if ($self->_is_unpack($magic)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_unpack($magic)) 0 0 0
103 0 $saw_unpack = $TRUE;
104 0 next MAGIC;
105 }
106 }
107  
108 # allow @$_[] construct in "... for ();"
109 # Check for "print @$_[] for ()" construct (rt39601)
110 next MAGIC
111 0 ▶ if _is_cast_of_array($magic) and _is_postfix_foreach($magic);
Branch: 1 branch
BranchTrueFalseTotal
if _is_cast_of_array($magic) and _is_postfix_foreach($magic) 0 0 0
Condition0%0/3
_is_cast_of_array($magic) and _is_postfix_foreach($magic)
!ll&&!rl&&r
000
MC/DC0%0/2
_is_cast_of_array($magic) and _is_postfix_foreach($magic)
_is_cast_of_array($magic) _is_postfix_foreach($magic)
Truth table0%0/3
A and B
A= _is_cast_of_array($magic)B= _is_postfix_foreach($magic)
ABresult
0X0
100
111
112  
113 # allow $$_[], which is equivalent to $_->[] and not a use
114 # of @_ at all.
115 next MAGIC
116 0 ▶ if _is_cast_of_scalar( $magic );
Branch: 1 branch
BranchTrueFalseTotal
if _is_cast_of_scalar($magic) 0 0 0
117  
118 # allow delegation of the form "$self->SUPER::foo( @_ );"
119 next MAGIC
120 0 ▶ if $self->_is_delegation( $magic );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_delegation($magic) 0 0 0
121  
122 # If we make it this far, it is a violation
123 0 return $self->violation( $DESC, $EXPL, $elem );
124 }
125 7 ▶ if (not $saw_unpack) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($saw_unpack) 7 0 7
126 7 $state = 'post_unpacking';
127 }
128 }
129 5 return; # OK
130 }
131  
132 0 ▶sub _is_unpack {
Subroutine _is_unpack: not called CC=10 SCAR=47.0
133 0 my ($self, $magic) = @_;
134  
135 0 my $prev = $magic->sprevious_sibling();
136 0 my $next = $magic->snext_sibling();
137  
138 # If we have a subscript, we're dealing with an array slice on @_
139 # or an array element of @_. See RT #34009.
140 0 ▶ if ( $next and $next->isa('PPI::Structure::Subscript') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next and $next->isa("PPI::Structure::Subscript")) 0 0 0
Condition0%0/3
$next and $next->isa("PPI::Structure::Subscript")
!ll&&!rl&&r
000
MC/DC0%0/2
$next and $next->isa("PPI::Structure::Subscript")
$next $next->isa("PPI::Structure::Subscript")
Truth table0%0/3
A and B
A= $nextB= $next->isa("PPI::Structure::Subscript")
ABresult
0X0
100
111
141 0 ▶ $self->{_allow_subscripts} or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"_allow_subscripts"} or return 0 0 0
142 0 $next = $next->snext_sibling;
143 }
144  
145 0 ▶ return $TRUE if
Branch: 1 branch
BranchTrueFalseTotal
if $prev and $prev->isa("PPI::Token::Operator") and is_assignment_operator($prev->content) and !$next || $next->isa("PPI::Token::Structure") && $Perl::Critic::Policy::Subroutines::RequireArgUnpacking::SCOLON eq $next->content 0 0 0
Condition0%0/15
$prev and $prev->isa("PPI::Token::Operator") and is_assignment_operator($prev->content) and !$next || $next->isa("PPI::Token::Structure") && $Perl::Critic::Policy::Subroutines::RequireArgUnpacking::SCOLON eq $next->content
!ll&&!rl&&r
000
$prev and $prev->isa("PPI::Token::Operator") and is_assignment_operator($prev->content)
!ll&&!rl&&r
000
$prev and $prev->isa("PPI::Token::Operator")
!ll&&!rl&&r
000
not $next or $next->isa("PPI::Token::Structure") and $Perl::Critic::Policy::Subroutines::RequireArgUnpacking::SCOLON eq $next->content
l!l&&r!l&&!r
000
$next->isa("PPI::Token::Structure") and $Perl::Critic::Policy::Subroutines::RequireArgUnpacking::SCOLON eq $next->content
!ll&&!rl&&r
000
MC/DC0%0/6
$prev and $prev->isa("PPI::Token::Operator") and is_assignment_operator($prev->content) and !$next || $next->isa("PPI::Token::Structure") && $Perl::Critic::Policy::Subroutines::RequireArgUnpacking::SCOLON eq $next->content
$prev $prev->isa("PPI::Token::Operator") is_assignment_operator($prev->content) not $next $next->isa("PPI::Token::Structure") $Perl::Critic::Policy::Subroutines::RequireArgUnpacking::SCOLON eq $next->content
Truth table0%0/7
A and B and C and not(D) or E and F
A= $prevB= $prev->isa("PPI::Token::Operator")C= is_assignment_operator($prev->content)D= not $nextE= $next->isa("PPI::Token::Structure")F= $Perl::Critic::Policy::Subroutines::RequireArgUnpacking::SCOLON eq $next->content
ABCDEFresult
0XXXXX0
10XXXX0
110XXX0
11100X0
1110100
1111XX1
1110111
146 $prev
147 and $prev->isa('PPI::Token::Operator')
148 and is_assignment_operator($prev->content())
149 and (
150 not $next
151 or $next->isa('PPI::Token::Structure')
152 and $SCOLON eq $next->content()
153 );
154 0 return;
155 }
156  
157 0 ▶sub _is_size_check {
Subroutine _is_size_check: not called CC=10 SCAR=47.0
158 0 my ($magic) = @_;
159  
160 # No size check on $_[0]. RT #34009.
161 0 ▶ $AT eq $magic->raw_type or return;
Branch: 1 branch
BranchTrueFalseTotal
$AT eq $magic->raw_type or return 0 0 0
162  
163 0 my $prev = $magic->sprevious_sibling;
164 0 my $next = $magic->snext_sibling;
165  
166 0 ▶ if ( $prev || $next ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prev or $next) 0 0 0
Condition0%0/3
$prev or $next
l!l&&r!l&&!r
000
MC/DC0%0/2
$prev or $next
$prev $next
Truth table0%0/3
A or B
A= $prevB= $next
ABresult
1X1
011
000
167  
168 0 ▶ return $TRUE
Branch: 1 branch
BranchTrueFalseTotal
if _legal_before_size_check($prev) and _legal_after_size_check($next) 0 0 0
Condition0%0/3
_legal_before_size_check($prev) and _legal_after_size_check($next)
!ll&&!rl&&r
000
MC/DC0%0/2
_legal_before_size_check($prev) and _legal_after_size_check($next)
_legal_before_size_check($prev) _legal_after_size_check($next)
Truth table0%0/3
A and B
A= _legal_before_size_check($prev)B= _legal_after_size_check($next)
ABresult
0X0
100
111
169 if _legal_before_size_check( $prev )
170 and _legal_after_size_check( $next );
171 }
172  
173 0 my $parent = $magic;
174 {
175 0 ▶ $parent = $parent->parent()
Branch: 1 branch
BranchTrueFalseTotal
$parent = $parent->parent or return 0 0 0
176 or return;
177 0 $prev = $parent->sprevious_sibling();
178 0 $next = $parent->snext_sibling();
179 0 ▶ $prev
Branch: 1 branch
BranchTrueFalseTotal
$prev or $next or redo 0 0 0
Condition0%0/3
$prev or $next
l!l&&r!l&&!r
000
MC/DC0%0/2
$prev or $next
$prev $next
Truth table0%0/3
A or B
A= $prevB= $next
ABresult
1X1
011
000
180 or $next
181 or redo;
182 } # until ( $prev || $next );
183  
184 0 ▶ return $TRUE
Branch: 1 branch
BranchTrueFalseTotal
if $parent->isa("PPI::Structure::Condition") 0 0 0
185 if $parent->isa( 'PPI::Structure::Condition' );
186  
187 0 return;
188 }
189  
190 {
191  
192 40 Readonly::Hash my %LEGAL_NEXT_OPER => hashify(
193 qw{ && || == != > >= < <= and or } );
194  
195 40 Readonly::Hash my %LEGAL_NEXT_STRUCT => hashify( qw{ ; } );
196  
197 0 ▶ sub _legal_after_size_check {
Subroutine _legal_after_size_check: not called CC=4 SCAR=30.0
198 0 my ( $next ) = @_;
199  
200 0 ▶ $next
Branch: 1 branch
BranchTrueFalseTotal
$next or return $Perl::Critic::Policy::Subroutines::RequireArgUnpacking::TRUE 0 0 0
201 or return $TRUE;
202  
203 $next->isa( 'PPI::Token::Operator' )
204 0 ▶ and return $LEGAL_NEXT_OPER{ $next->content() };
Branch: 1 branch
BranchTrueFalseTotal
$next->isa("PPI::Token::Operator") and return $LEGAL_NEXT_OPER{$next->content} 0 0 0
205  
206 $next->isa( 'PPI::Token::Structure' )
207 0 ▶ and return $LEGAL_NEXT_STRUCT{ $next->content() };
Branch: 1 branch
BranchTrueFalseTotal
$next->isa("PPI::Token::Structure") and return $LEGAL_NEXT_STRUCT{$next->content} 0 0 0
208  
209 0 return;
210 }
211 }
212  
213 {
214  
215 40 Readonly::Hash my %LEGAL_PREV_OPER => hashify(
216 qw{ && || ! == != > >= < <= and or not } );
217  
218 40 Readonly::Hash my %LEGAL_PREV_WORD => hashify(
219 qw{ if unless } );
220  
221 0 ▶ sub _legal_before_size_check {
Subroutine _legal_before_size_check: not called CC=4 SCAR=30.0
222 0 my ( $prev ) = @_;
223  
224 0 ▶ $prev
Branch: 1 branch
BranchTrueFalseTotal
$prev or return $Perl::Critic::Policy::Subroutines::RequireArgUnpacking::TRUE 0 0 0
225 or return $TRUE;
226  
227 $prev->isa( 'PPI::Token::Operator' )
228 0 ▶ and return $LEGAL_PREV_OPER{ $prev->content() };
Branch: 1 branch
BranchTrueFalseTotal
$prev->isa("PPI::Token::Operator") and return $LEGAL_PREV_OPER{$prev->content} 0 0 0
229  
230 $prev->isa( 'PPI::Token::Word' )
231 0 ▶ and return $LEGAL_PREV_WORD{ $prev->content() };
Branch: 1 branch
BranchTrueFalseTotal
$prev->isa("PPI::Token::Word") and return $LEGAL_PREV_WORD{$prev->content} 0 0 0
232  
233 0 return;
234 }
235  
236 }
237  
238 40 ▶sub _is_postfix_foreach {
Subroutine _is_postfix_foreach: not called CC=4 SCAR=28.1
239 0 my ($magic) = @_;
240  
241 0 my $sibling = $magic;
242 0 ▶ while ( $sibling = $sibling->snext_sibling ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sibling = $sibling->snext_sibling) 0 0 0
243 0 ▶ return $TRUE
Branch: 1 branch
BranchTrueFalseTotal
if $sibling->isa("PPI::Token::Word") and $sibling =~ / \A for (?:each)? \z /msx 0 0 0
Condition0%0/3
$sibling->isa("PPI::Token::Word") and $sibling =~ / \A for (?:each)? \z /msx
!ll&&!rl&&r
000
MC/DC0%0/2
$sibling->isa("PPI::Token::Word") and $sibling =~ / \A for (?:each)? \z /msx
$sibling->isa("PPI::Token::Word") $sibling =~ / \A for (?:each)? \z /msx
Truth table0%0/3
A and B
A= $sibling->isa("PPI::Token::Word")B= $sibling =~ / \A for (?:each)? \z /msx
ABresult
0X0
100
111
244 if
245 $sibling->isa('PPI::Token::Word')
246 and $sibling =~ m< \A for (?:each)? \z >xms;
247 }
248 0 return;
249 }
250  
251 0 ▶sub _is_cast_of_array {
Subroutine _is_cast_of_array: not called CC=4 SCAR=30.0
252 0 my ($magic) = @_;
253  
254 0 my $prev = $magic->sprevious_sibling;
255  
256 0 ▶ return $TRUE
Branch: 1 branch
BranchTrueFalseTotal
if $prev and $prev->content eq $AT and $prev->isa("PPI::Token::Cast") 0 0 0
Condition0%0/6
$prev and $prev->content eq $AT and $prev->isa("PPI::Token::Cast")
!ll&&!rl&&r
000
$prev and $prev->content eq $AT
!ll&&!rl&&r
000
MC/DC0%0/3
$prev and $prev->content eq $AT and $prev->isa("PPI::Token::Cast")
$prev $prev->content eq $AT $prev->isa("PPI::Token::Cast")
Truth table0%0/4
A and B and C
A= $prevB= $prev->content eq $ATC= $prev->isa("PPI::Token::Cast")
ABCresult
0XX0
10X0
1100
1111
257 if ( $prev && $prev->content() eq $AT )
258 and $prev->isa('PPI::Token::Cast');
259 0 return;
260 }
261  
262 # This subroutine recognizes (e.g.) $$_[0]. This is a use of $_ (equivalent to
263 # $_->[0]), not @_.
264  
265 0 ▶sub _is_cast_of_scalar {
Subroutine _is_cast_of_scalar: not called CC=6 SCAR=37.4
266 0 my ($magic) = @_;
267  
268 0 my $prev = $magic->sprevious_sibling;
269 0 my $next = $magic->snext_sibling;
270  
271 0 ▶ return $DOLLAR_ARG eq $magic->content() &&
Condition0%0/15
$DOLLAR_ARG eq $magic->content && $prev && $prev->isa('PPI::Token::Cast') && $DOLLAR eq $prev->content && $next && $next->isa('PPI::Structure::Subscript')
!ll&&!rl&&r
000
$DOLLAR_ARG eq $magic->content and $prev and $prev->isa("PPI::Token::Cast") and $DOLLAR eq $prev->content and $next
!ll&&!rl&&r
000
$DOLLAR_ARG eq $magic->content and $prev and $prev->isa("PPI::Token::Cast") and $DOLLAR eq $prev->content
!ll&&!rl&&r
000
$DOLLAR_ARG eq $magic->content and $prev and $prev->isa("PPI::Token::Cast")
!ll&&!rl&&r
000
$DOLLAR_ARG eq $magic->content and $prev
!ll&&!rl&&r
000
MC/DC0%0/6
$DOLLAR_ARG eq $magic->content && $prev && $prev->isa('PPI::Token::Cast') && $DOLLAR eq $prev->content && $next && $next->isa('PPI::Structure::Subscript')
$DOLLAR_ARG eq $magic->content $prev $prev->isa("PPI::Token::Cast") $DOLLAR eq $prev->content $next $next->isa('PPI::Structure::Subscript')
Truth table0%0/7
A and B and C and D and E && F
A= $DOLLAR_ARG eq $magic->contentB= $prevC= $prev->isa("PPI::Token::Cast")D= $DOLLAR eq $prev->contentE= $nextF= $next->isa('PPI::Structure::Subscript')
ABCDEFresult
0XXXXX0
10XXXX0
110XXX0
1110XX0
11110X0
1111100
1111111
272 $prev && $prev->isa('PPI::Token::Cast') &&
273 $DOLLAR eq $prev->content() &&
274 $next && $next->isa('PPI::Structure::Subscript');
275 }
276  
277 # A literal @_ is allowed as the argument for a delegation.
278 # An example of the idiom we are looking for is $self->SUPER::foo(@_).
279 # The argument list of (@_) is required; no other use of @_ is allowed.
280  
281 0 ▶sub _is_delegation {
Subroutine _is_delegation: not called CC=16 SCAR=56.1
282 0 my ($self, $magic) = @_;
283  
284 0 ▶ $AT_ARG eq $magic->content() or return; # Not a literal '@_'.
Branch: 1 branch
BranchTrueFalseTotal
$AT_ARG eq $magic->content or return 0 0 0
285 0 ▶ my $parent = $magic->parent() # Don't know what to do with
Branch: 1 branch
BranchTrueFalseTotal
my $parent = $magic->parent or return 0 0 0
286 or return; # orphans.
287 0 ▶ $parent->isa( 'PPI::Statement::Expression' )
Branch: 1 branch
BranchTrueFalseTotal
$parent->isa("PPI::Statement::Expression") or return 0 0 0
288 or return; # Parent must be expression.
289 0 ▶ 1 == $parent->schildren() # '@_' must stand alone in
Branch: 1 branch
BranchTrueFalseTotal
1 == $parent->schildren or return 0 0 0
290 or return; # its expression.
291 0 ▶ $parent = $parent->parent() # Still don't know what to do
Branch: 1 branch
BranchTrueFalseTotal
$parent = $parent->parent or return 0 0 0
292 or return; # with orphans.
293 0 ▶ $parent->isa ( 'PPI::Structure::List' )
Branch: 1 branch
BranchTrueFalseTotal
$parent->isa("PPI::Structure::List") or return 0 0 0
294 or return; # Parent must be a list.
295 0 ▶ 1 == $parent->schildren() # '@_' must stand alone in
Branch: 1 branch
BranchTrueFalseTotal
1 == $parent->schildren or return 0 0 0
296 or return; # the argument list.
297 0 ▶ my $subroutine_name = $parent->sprevious_sibling()
Branch: 1 branch
BranchTrueFalseTotal
my $subroutine_name = $parent->sprevious_sibling or return 0 0 0
298 or return; # Missing sub name.
299 0 ▶ if ( $subroutine_name->isa( 'PPI::Token::Word' ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($subroutine_name->isa('PPI::Token::Word')) { } 0 0 0
elsif ($self->{'_allow_closures'} and _is_dereference_operator($subroutine_name)) { } 0 0 0
300 0 ▶ $self->{_allow_delegation_to}{$subroutine_name}
Branch: 1 branch
BranchTrueFalseTotal
$self->{"_allow_delegation_to"}{$subroutine_name} and return 1 0 0 0
301 and return 1;
302 0 ▶ my ($subroutine_namespace) = $subroutine_name =~ m/ \A ( .* ::) \w+ \z /smx
Branch: 1 branch
BranchTrueFalseTotal
my($subroutine_namespace) = $subroutine_name =~ / \A ( .* ::) \w+ \z /msx or return 0 0 0
303 or return;
304 0 return $self->{_allow_delegation_to}{$subroutine_namespace};
305 ▶ } elsif ( $self->{_allow_closures} &&
Condition0%0/3
$self->{"_allow_closures"} and _is_dereference_operator($subroutine_name)
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"_allow_closures"} and _is_dereference_operator($subroutine_name)
$self->{"_allow_closures"} _is_dereference_operator($subroutine_name)
Truth table0%0/3
A and B
A= $self->{"_allow_closures"}B= _is_dereference_operator($subroutine_name)
ABresult
0X0
100
111
306 _is_dereference_operator( $subroutine_name ) ) {
307 0 my $prev_sib = $subroutine_name;
308 { # Single-iteration loop
309 0 ▶ $prev_sib = $prev_sib->sprevious_sibling()
Branch: 1 branch
BranchTrueFalseTotal
$prev_sib = $prev_sib->sprevious_sibling or return 0 0 0
310 or return;
311 0 ▶ ( $prev_sib->isa( 'PPI::Structure::Subscript' ||
Branch: 1 branch
BranchTrueFalseTotal
$prev_sib->isa("PPI::Structure::Subscript") and redo 0 0 0
312 _is_dereference_operator( $prev_sib ) ) )
313 and redo;
314 }
315 0 return $prev_sib->isa( 'PPI::Token::Symbol' );
316 }
317 0 return;
318 }
319  
320 0 ▶sub _is_dereference_operator {
Subroutine _is_dereference_operator: not called CC=3 SCAR=24.8
321 0 my ( $elem ) = @_;
322 0 ▶ $elem
Branch: 1 branch
BranchTrueFalseTotal
$elem or return 0 0 0
323 or return;
324 0 ▶ $elem->isa( 'PPI::Token::Operator' )
Branch: 1 branch
BranchTrueFalseTotal
$elem->isa("PPI::Token::Operator") or return 0 0 0
325 or return;
326 0 return $DEREFERENCE eq $elem->content();
327 }
328  
329  
330 7 ▶sub _get_arg_symbols {
Subroutine _get_arg_symbols: called CC=2 SCAR=7.6
331 7 my ($statement) = @_;
332  
333 0 ▶ return grep {$AT_ARG eq $_->symbol} @{$statement->find(\&_magic_finder) || []};
Condition50%1/2
$statement->find(\&Perl::Critic::Policy::Subroutines::RequireArgUnpacking::_magic_finder) || []
l!l
07
MC/DC0%0/1
$statement->find(\&Perl::Critic::Policy::Subroutines::RequireArgUnpacking::_magic_finder) || []
$statement->find(\&Perl::Critic::Policy::Subroutines::RequireArgUnpacking::_magic_finder)
Truth table50%1/2
A
A= $statement->find(\&Perl::Critic::Policy::Subroutines::RequireArgUnpacking::_magic_finder)
Aresult
11
00
334 }
335  
336 39 ▶sub _magic_finder {
Subroutine _magic_finder: called CC=5 SCAR=22.7
337 # Find all @_ and $_[\d+] not inside of nested subs
338 39 my (undef, $elem) = @_;
339 39 ▶ return $TRUE if $elem->isa('PPI::Token::Magic'); # match
Branch: 1 branch
BranchTrueFalseTotal
if $elem->isa("PPI::Token::Magic") 0 39 39
340  
341 39 ▶ if ($elem->isa('PPI::Structure::Block')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem->isa("PPI::Structure::Block")) 0 39 39
342 # don't descend into a nested named sub
343 0 ▶ return if $elem->statement->isa('PPI::Statement::Sub');
Branch: 1 branch
BranchTrueFalseTotal
if $elem->statement->isa("PPI::Statement::Sub") 0 0 0
344  
345 # don't descend into a nested anon sub, either.
346 0 ▶ return if _is_anon_sub( $elem );
Branch: 1 branch
BranchTrueFalseTotal
if _is_anon_sub($elem) 0 0 0
347  
348 }
349  
350 39 return $FALSE; # no match, descend
351 }
352  
353 # Detecting anonymous subs is hard, partly because PPI's parse of them, at
354 # least as of 1.220, appears to be a bit dodgy.
355 0 ▶sub _is_anon_sub {
Subroutine _is_anon_sub: not called CC=12 SCAR=50.5
356 0 my ( $elem ) = @_;
357  
358 # If we have no previous element, we can not be an anonymous sub.
359 0 ▶ my $prev = $elem->sprevious_sibling()
Branch: 1 branch
BranchTrueFalseTotal
my $prev = $elem->sprevious_sibling or return $Perl::Critic::Policy::Subroutines::RequireArgUnpacking::FALSE 0 0 0
360 or return $FALSE;
361  
362 # The simple case.
363 0 ▶ return $TRUE if $prev->isa( 'PPI::Token::Word' )
Branch: 1 branch
BranchTrueFalseTotal
if $prev->isa("PPI::Token::Word") and "sub" eq $prev->content 0 0 0
Condition0%0/3
$prev->isa("PPI::Token::Word") and "sub" eq $prev->content
!ll&&!rl&&r
000
MC/DC0%0/2
$prev->isa("PPI::Token::Word") and "sub" eq $prev->content
$prev->isa("PPI::Token::Word") "sub" eq $prev->content
Truth table0%0/3
A and B
A= $prev->isa("PPI::Token::Word")B= "sub" eq $prev->content
ABresult
0X0
100
111
364 and 'sub' eq $prev->content();
365  
366 # Skip possible subroutine attributes. These appear as words (the names)
367 # or lists (the arguments, if any), or actual attributes (depending on how
368 # PPI handles them). A colon is required before the first, and is optional
369 # in between.
370 0 ▶ while ( $prev->isa( 'PPI::Token::Attribute' )
Branch: 1 branch
BranchTrueFalseTotal
if ($prev->isa("PPI::Token::Attribute") or $prev->isa("PPI::Token::Operator") and ":" eq $prev->content) 0 0 0
Condition0%0/6
$prev->isa("PPI::Token::Attribute") or $prev->isa("PPI::Token::Operator") and ":" eq $prev->content
l!l&&r!l&&!r
000
$prev->isa("PPI::Token::Operator") and ":" eq $prev->content
!ll&&!rl&&r
000
MC/DC0%0/3
$prev->isa("PPI::Token::Attribute") or $prev->isa("PPI::Token::Operator") and ":" eq $prev->content
$prev->isa("PPI::Token::Attribute") $prev->isa("PPI::Token::Operator") ":" eq $prev->content
Truth table0%0/4
A or B and C
A= $prev->isa("PPI::Token::Attribute")B= $prev->isa("PPI::Token::Operator")C= ":" eq $prev->content
ABCresult
1XX1
0111
00X0
0100
371 or $prev->isa( 'PPI::Token::Operator' )
372 and q<:> eq $prev->content() ) {
373  
374 # Grab the previous significant sib. If there is none, we can not
375 # be an anonymous sub with attributes.
376 0 ▶ return $FALSE if not $prev = $prev->sprevious_sibling();
Branch: 1 branch
BranchTrueFalseTotal
unless $prev = $prev->sprevious_sibling 0 0 0
377 }
378  
379 # At this point we may have a prototype. Skip that too, but there needs to
380 # be something before it.
381 0 ▶ return $FALSE if $prev->isa( 'PPI::Token::Prototype' )
Branch: 1 branch
BranchTrueFalseTotal
if $prev->isa("PPI::Token::Prototype") and not $prev = $prev->sprevious_sibling 0 0 0
Condition0%0/3
$prev->isa("PPI::Token::Prototype") and not $prev = $prev->sprevious_sibling
!ll&&!rl&&r
000
MC/DC0%0/2
$prev->isa("PPI::Token::Prototype") and not $prev = $prev->sprevious_sibling
$prev->isa("PPI::Token::Prototype") not $prev = $prev->sprevious_sibling
Truth table0%0/3
A and not(B)
A= $prev->isa("PPI::Token::Prototype")B= not $prev = $prev->sprevious_sibling
ABresult
0X0
100
111
382 and not $prev = $prev->sprevious_sibling();
383  
384 # Finally, we can find out if we're a sub
385 0 ▶ return $TRUE if $prev->isa( 'PPI::Token::Word' )
Branch: 1 branch
BranchTrueFalseTotal
if $prev->isa("PPI::Token::Word") and "sub" eq $prev->content 0 0 0
Condition0%0/3
$prev->isa("PPI::Token::Word") and "sub" eq $prev->content
!ll&&!rl&&r
000
MC/DC0%0/2
$prev->isa("PPI::Token::Word") and "sub" eq $prev->content
$prev->isa("PPI::Token::Word") "sub" eq $prev->content
Truth table0%0/3
A and B
A= $prev->isa("PPI::Token::Word")B= "sub" eq $prev->content
ABresult
0X0
100
111
386 and 'sub' eq $prev->content();
387  
388 # We are out of options. At this point we can not possibly be an anon sub.
389 0 return $FALSE;
390 }
391  
392  
393 40 1;
394  
395 __END__
« lib/Perl/Critic/Policy/CodeLayout/ProhibitQuotedWordLists.pm ↑ Summary lib/Perl/Critic/Policy/ControlStructures/ProhibitMutatingListFunctions.pm »