lib/Perl/Critic/Policy/RegularExpressions/ProhibitUnusedCapture.pm

filter: Statementstmt 15.2% 47 / 308 Branchbran 0.4% 1 / 224 Conditioncond 0.0% 0 / 83 MC/DCmcdc 0.0% 0 / 52 Subroutinesub 30.2% 13 / 43 Podpod 80.0% 4 / 5 totaltotal 9.0% 65 / 715 CC 152 SCAR 49.0
CC
152
Cov
8%
_enough_assignments
380.0
_mark_magic
306.0
_is_in_slurpy_array_context
240.0
CRAP 133.8
« lib/Perl/Critic/Policy/Subroutines/ProhibitAmpersandSigils.pm ↑ Summary lib/Perl/Critic/Policy/ValuesAndExpressions/ProhibitMagicNumbers.pm »
1 package Perl::Critic::Policy::RegularExpressions::ProhibitUnusedCapture;
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 List::SomeUtils qw(none);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 40 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 40 ▶use Scalar::Util qw(refaddr);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 40 ▶use Perl::Critic::Utils qw{
Subroutine BEGIN: called CC=1 SCAR=0.0
12 :booleans :characters :severities hashify precedence_of
13 split_nodes_on_comma
14 40 };
15 40 ▶use parent 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 40 our $VERSION = '1.156';
18  
19 #-----------------------------------------------------------------------------
20  
21 40 Readonly::Scalar my $SPLIT => q{split};
22 40 Readonly::Scalar my $WHILE => q{while};
23  
24 40 Readonly::Hash my %ZERO_BASED_CAPTURE_REFERENCE =>
25 hashify( qw< ${^CAPTURE} > );
26 # TODO: additional logic to prevent ${^CAPTURE_ALL}[n] from being recognized
27 # as a use of capture variable n.
28 40 Readonly::Hash my %CAPTURE_REFERENCE => (
29 hashify( qw< $+ $- ${^CAPTURE_ALL} > ),
30 %ZERO_BASED_CAPTURE_REFERENCE );
31 40 Readonly::Hash my %CAPTURE_REFERENCE_ENGLISH => (
32 hashify( qw{ $LAST_PAREN_MATCH $LAST_MATCH_START $LAST_MATCH_END } ),
33 %CAPTURE_REFERENCE );
34 40 Readonly::Hash my %CAPTURE_ARRAY => hashify( qw< @- @+ @{^CAPTURE} > );
35 40 Readonly::Hash my %CAPTURE_ARRAY_ENGLISH => (
36 hashify( qw< @LAST_MATCH_START @LAST_MATCH_END > ),
37 %CAPTURE_ARRAY );
38 40 Readonly::Hash my %CAPTURE_HASH => hashify( qw< %- %+ %{^CAPTURE} >);
39 40 Readonly::Hash my %CAPTURE_HASH_ENGLISH => (
40 hashify( qw< %LAST_PAREN_MATCH > ),
41 %CAPTURE_HASH );
42  
43  
44 40 Readonly::Scalar my $DESC => q{Only use a capturing group if you plan to use the captured value};
45 40 Readonly::Scalar my $EXPL => [252];
46  
47 #-----------------------------------------------------------------------------
48  
49 90 ▶sub supported_parameters { return qw() }
Subroutine supported_parameters: called CC=1 SCAR=0.0
Pod undocumented
50 74 ▶sub default_severity { return $SEVERITY_MEDIUM }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
51 85 ▶sub default_themes { return qw( core pbp maintenance ) }
Subroutine default_themes: called CC=1 SCAR=0.0
Pod documented
52 31 ▶sub applies_to {
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
53 31 return qw< PPI::Token::Regexp::Match PPI::Token::Regexp::Substitute >;
54 }
55  
56 #-----------------------------------------------------------------------------
57  
58 40 Readonly::Scalar my $NUM_CAPTURES_FOR_GLOBAL => 100; # arbitrarily large number
59  
60 1 ▶sub violates {
Subroutine violates: called CC=15 SCAR=53.0
Pod documented
61 1 my ( $self, $elem, $doc ) = @_;
62  
63 # optimization: don't bother parsing the regexp if there are no parens
64 1 ▶ return if 0 > index $elem->content(), '(';
Branch: 1 branch
BranchTrueFalseTotal
if index($elem->content, "(") == -1 1 0 1
65  
66 0 ▶ my $re = $doc->ppix_regexp_from_element( $elem ) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $re = $doc->ppix_regexp_from_element($elem) or return 0 0 0
67 0 ▶ $re->failures() and return;
Branch: 1 branch
BranchTrueFalseTotal
$re->failures and return 0 0 0
68  
69 0 ▶ my $ncaptures = $re->max_capture_number() or return;
Branch: 1 branch
BranchTrueFalseTotal
my $ncaptures = $re->max_capture_number or return 0 0 0
70  
71 0 my @captures = ( undef ) x $ncaptures; # List of expected captures
72  
73 0 my %named_captures; # List of expected named captures.
74 # Unlike the numbered capture logic, %named_captures
75 # entries are made undefined when a use of the name is
76 # found. Otherwise two hashes would be needed, one to
77 # become defined when a use is found, and one to hold
78 # the mapping of name to number.
79 0 ▶ foreach my $struct ( @{ $re->find( 'PPIx::Regexp::Structure::NamedCapture'
Condition0%0/2
$re->find('PPIx::Regexp::Structure::NamedCapture') || []
l!l
00
MC/DC0%0/1
$re->find('PPIx::Regexp::Structure::NamedCapture') || []
$re->find('PPIx::Regexp::Structure::NamedCapture')
Truth table0%0/2
A
A= $re->find('PPIx::Regexp::Structure::NamedCapture')
Aresult
11
00
80 ) || [] } ) {
81 # There can be more than one capture with the same name, so we need to
82 # record all of them. There will be duplications if the 'branch reset'
83 # "(?| ... )" pattern is used, but this is benign given how numbered
84 # captures are recorded.
85 0 ▶ push @{ $named_captures{ $struct->name() } ||= [] }, $struct->number();
Condition0%0/2
$named_captures{$struct->name} ||= []
l!l
00
MC/DC0%0/1
$named_captures{$struct->name} ||= []
$named_captures{$struct->name}
Truth table0%0/2
A
A= $named_captures{$struct->name}
Aresult
11
00
86 }
87  
88 # Look for references to the capture in the regex itself
89 0 ▶ return if _enough_uses_in_regexp( $re, \@captures, \%named_captures, $doc );
Branch: 1 branch
BranchTrueFalseTotal
if _enough_uses_in_regexp($re, \@captures, \%named_captures, $doc) 0 0 0
90  
91 0 ▶ if ( $re->modifier_asserted( 'g' )
Branch: 1 branch
BranchTrueFalseTotal
if ($re->modifier_asserted("g") and not _check_if_in_while_condition_or_block($elem)) 0 0 0
Condition0%0/3
$re->modifier_asserted("g") and not _check_if_in_while_condition_or_block($elem)
!ll&&!rl&&r
000
MC/DC0%0/2
$re->modifier_asserted("g") and not _check_if_in_while_condition_or_block($elem)
$re->modifier_asserted("g") not _check_if_in_while_condition_or_block($elem)
Truth table0%0/3
A and not(B)
A= $re->modifier_asserted("g")B= not _check_if_in_while_condition_or_block($elem)
ABresult
0X0
100
111
92 and not _check_if_in_while_condition_or_block( $elem ) ) {
93 0 $ncaptures = $NUM_CAPTURES_FOR_GLOBAL;
94 0 $#captures = $ncaptures - 1;
95 }
96  
97 0 ▶ if ( !%named_captures ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (%named_captures) 0 0 0
98 0 ▶ return if _enough_assignments($elem, \@captures);
Branch: 1 branch
BranchTrueFalseTotal
if _enough_assignments($elem, \@captures) 0 0 0
99 0 ▶ return if _is_in_slurpy_array_context($elem);
Branch: 1 branch
BranchTrueFalseTotal
if _is_in_slurpy_array_context($elem) 0 0 0
100 }
101 0 ▶ return if _enough_magic($elem, $re, \@captures, \%named_captures, $doc);
Branch: 1 branch
BranchTrueFalseTotal
if _enough_magic($elem, $re, \@captures, \%named_captures, $doc) 0 0 0
102  
103 0 return $self->violation( $DESC, $EXPL, $elem );
104 }
105  
106 # Find uses of both numbered and named capture variables in the regexp itself.
107 # Return true if all are used.
108 0 ▶sub _enough_uses_in_regexp {
Subroutine _enough_uses_in_regexp: not called CC=9 SCAR=45.0
109 0 my ( $re, $captures, $named_captures, $doc ) = @_;
110  
111 # Look for references to the capture in the regex itself. Note that this
112 # will also find backreferences in the replacement string of s///.
113 0 ▶ foreach my $token ( @{ $re->find( 'PPIx::Regexp::Token::Reference' )
Condition0%0/2
$re->find('PPIx::Regexp::Token::Reference') || []
l!l
00
MC/DC0%0/1
$re->find('PPIx::Regexp::Token::Reference') || []
$re->find('PPIx::Regexp::Token::Reference')
Truth table0%0/2
A
A= $re->find('PPIx::Regexp::Token::Reference')
Aresult
11
00
114 || [] } ) {
115 0 ▶ if ( $token->is_named() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token->is_named) { } 0 0 0
116 0 _record_named_capture( $token->name(), $captures, $named_captures );
117 } else {
118 0 _record_numbered_capture( $token->absolute(), $captures );
119 }
120 }
121  
122 0 ▶ foreach my $token ( @{ $re->find(
Condition0%0/2
$re->find('PPIx::Regexp::Token::Code') || []
l!l
00
MC/DC0%0/1
$re->find('PPIx::Regexp::Token::Code') || []
$re->find('PPIx::Regexp::Token::Code')
Truth table0%0/2
A
A= $re->find('PPIx::Regexp::Token::Code')
Aresult
11
00
123 'PPIx::Regexp::Token::Code' ) || [] } ) {
124 0 ▶ my $ppi = $token->ppi() or next;
Branch: 1 branch
BranchTrueFalseTotal
my $ppi = $token->ppi or next 0 0 0
125 0 _check_node_children( $ppi, {
126 regexp => $re,
127 numbered_captures => $captures,
128 named_captures => $named_captures,
129 document => $doc,
130 }, _make_regexp_checker() );
131 }
132  
133 0 ▶ return ( none {not defined} @{$captures} )
Subroutine __ANON__: not called CC=1 SCAR=6.9
Condition0%0/3
not %{$named_captures;} or &none(sub { defined $_; } , values %{$named_captures;})
l!l&&r!l&&!r
000
MC/DC0%0/2
not %{$named_captures;} or &none(sub { defined $_; } , values %{$named_captures;})
not %{$named_captures;} &none(sub { defined $_; } , values %{$named_captures;})
Truth table0%0/3
not(A) or B
A= not %{$named_captures;}B= &none(sub { defined $_; } , values %{$named_captures;})
ABresult
1X1
011
000
134 0 && ( !%{$named_captures} ||
135 0 ▶ none {defined} values %{$named_captures} );
Subroutine __ANON__: not called CC=1 SCAR=6.9
Condition0%0/3
&none(sub { not defined $_; } , @{$captures;}) && (!%{$named_captures;} || &none(sub { defined $_; } , values %{$named_captures;}))
!ll&&!rl&&r
000
MC/DC0%0/2
&none(sub { not defined $_; } , @{$captures;}) && (!%{$named_captures;} || &none(sub { defined $_; } , values %{$named_captures;}))
&none(sub { not defined $_; } , @{$captures;}) (!%{$named_captures;} || &none(sub { defined $_; } , values %{$named_captures;}))
Truth table0%0/3
A && B
A= &none(sub { not defined $_; } , @{$captures;})B= (!%{$named_captures;} || &none(sub { defined $_; } , values %{$named_captures;}))
ABresult
0X0
100
111
136 }
137  
138 0 ▶sub _enough_assignments {
Subroutine _enough_assignments: not called CC=19 SCAR=59.4
139 0 my ($elem, $captures) = @_;
140  
141 # look backward for the assignment operator
142 0 my $psib = $elem->sprevious_sibling;
143 SIBLING:
144 0 while (1) {
145 0 ▶ return if !$psib;
Branch: 1 branch
BranchTrueFalseTotal
unless $psib 0 0 0
146 0 ▶ if ($psib->isa('PPI::Token::Operator')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($psib->isa("PPI::Token::Operator")) 0 0 0
147 0 ▶ last SIBLING if q{=} eq $psib->content;
Branch: 1 branch
BranchTrueFalseTotal
if "=" eq $psib->content 0 0 0
148 0 ▶ return if q{!~} eq $psib->content;
Branch: 1 branch
BranchTrueFalseTotal
if "!~" eq $psib->content 0 0 0
149 }
150 0 $psib = $psib->sprevious_sibling;
151 }
152  
153 0 $psib = $psib->sprevious_sibling;
154 0 ▶ return if !$psib; # syntax error: '=' at the beginning of a statement???
Branch: 1 branch
BranchTrueFalseTotal
unless $psib 0 0 0
155  
156 0 ▶ if ($psib->isa('PPI::Token::Symbol')) {
Branch: 3 branches
BranchTrueFalseTotal
if ($psib->isa('PPI::Token::Symbol')) { } 0 0 0
elsif ($psib->isa('PPI::Structure::Block')) { } 0 0 0
elsif ($psib->isa('PPI::Structure::List')) { } 0 0 0
157 # @foo = m/(foo)/
158 # @$foo = m/(foo)/
159 # %foo = m/(foo)/
160 # %$foo = m/(foo)/
161 0 ▶ return $TRUE if _symbol_is_slurpy($psib);
Branch: 1 branch
BranchTrueFalseTotal
if _symbol_is_slurpy($psib) 0 0 0
162  
163 } elsif ($psib->isa('PPI::Structure::Block')) {
164 # @{$foo} = m/(foo)/
165 # %{$foo} = m/(foo)/
166 0 ▶ return $TRUE if _is_preceded_by_array_or_hash_cast($psib);
Branch: 1 branch
BranchTrueFalseTotal
if _is_preceded_by_array_or_hash_cast($psib) 0 0 0
167  
168 } elsif ($psib->isa('PPI::Structure::List')) {
169 # () = m/(foo)/
170 # ($foo) = m/(foo)/
171 # ($foo,$bar) = m/(foo)(bar)/
172 # (@foo) = m/(foo)(bar)/
173 # ($foo,@foo) = m/(foo)(bar)/
174 # ($foo,@$foo) = m/(foo)(bar)/
175 # ($foo,@{$foo}) = m/(foo)(bar)/
176  
177 0 my @args = $psib->schildren;
178 0 ▶ return $TRUE if not @args; # empty list (perhaps the "goatse" operator) is slurpy
Branch: 1 branch
BranchTrueFalseTotal
unless @args 0 0 0
179  
180 # Forward looking: PPI might change in v1.200 so schild(0) is a
181 # PPI::Statement::Expression.
182 0 ▶ if ( 1 == @args && $args[0]->isa('PPI::Statement::Expression') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (1 == @args and $args[0]->isa("PPI::Statement::Expression")) 0 0 0
Condition0%0/3
1 == @args and $args[0]->isa("PPI::Statement::Expression")
!ll&&!rl&&r
000
MC/DC0%0/2
1 == @args and $args[0]->isa("PPI::Statement::Expression")
1 == @args $args[0]->isa("PPI::Statement::Expression")
Truth table0%0/3
A and B
A= 1 == @argsB= $args[0]->isa("PPI::Statement::Expression")
ABresult
0X0
100
111
183 0 @args = $args[0]->schildren;
184 }
185  
186 0 my @parts = split_nodes_on_comma(@args);
187 PART:
188 0 for my $i (0 .. $#parts) {
189 0 ▶ if (1 == @{$parts[$i]}) {
Branch: 1 branch
BranchTrueFalseTotal
if (1 == @{$parts[$i];}) 0 0 0
190 0 my $var = $parts[$i]->[0];
191 0 ▶ if ($var->isa('PPI::Token::Symbol') || $var->isa('PPI::Token::Cast')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($var->isa("PPI::Token::Symbol") or $var->isa("PPI::Token::Cast")) 0 0 0
Condition0%0/3
$var->isa("PPI::Token::Symbol") or $var->isa("PPI::Token::Cast")
l!l&&r!l&&!r
000
MC/DC0%0/2
$var->isa("PPI::Token::Symbol") or $var->isa("PPI::Token::Cast")
$var->isa("PPI::Token::Symbol") $var->isa("PPI::Token::Cast")
Truth table0%0/3
A or B
A= $var->isa("PPI::Token::Symbol")B= $var->isa("PPI::Token::Cast")
ABresult
1X1
011
000
192 0 ▶ return $TRUE if _has_array_sigil($var);
Branch: 1 branch
BranchTrueFalseTotal
if _has_array_sigil($var) 0 0 0
193 }
194 }
195 0 _record_numbered_capture( $i + 1, $captures );
196 # ith variable capture
197 }
198 }
199  
200 0 ▶ return none {not defined} @{$captures};
Subroutine __ANON__: not called CC=1 SCAR=6.9
201 }
202  
203 0 ▶sub _symbol_is_slurpy {
Subroutine _symbol_is_slurpy: not called CC=4 SCAR=30.0
204 0 my ($symbol) = @_;
205  
206 0 ▶ return $TRUE if _has_array_sigil($symbol);
Branch: 1 branch
BranchTrueFalseTotal
if _has_array_sigil($symbol) 0 0 0
207 0 ▶ return $TRUE if _has_hash_sigil($symbol);
Branch: 1 branch
BranchTrueFalseTotal
if _has_hash_sigil($symbol) 0 0 0
208 0 ▶ return $TRUE if _is_preceded_by_array_or_hash_cast($symbol);
Branch: 1 branch
BranchTrueFalseTotal
if _is_preceded_by_array_or_hash_cast($symbol) 0 0 0
209 0 return;
210 }
211  
212 0 ▶sub _has_array_sigil {
Subroutine _has_array_sigil: not called CC=1 SCAR=6.9
213 0 my ($elem) = @_; # Works on PPI::Token::Symbol and ::Cast
214  
215 0 return q{@} eq substr $elem->content, 0, 1;
216 }
217  
218 0 ▶sub _has_hash_sigil {
Subroutine _has_hash_sigil: not called CC=1 SCAR=6.9
219 0 my ($elem) = @_; # Works on PPI::Token::Symbol and ::Cast
220  
221 0 return q{%} eq substr $elem->content, 0, 1;
222 }
223  
224 0 ▶sub _is_preceded_by_array_or_hash_cast {
Subroutine _is_preceded_by_array_or_hash_cast: not called CC=5 SCAR=34.0
225 0 my ($elem) = @_;
226 0 my $psib = $elem->sprevious_sibling;
227 0 my $cast;
228 0 ▶ while ($psib && $psib->isa('PPI::Token::Cast')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($psib and $psib->isa("PPI::Token::Cast")) 0 0 0
Condition0%0/3
$psib and $psib->isa("PPI::Token::Cast")
!ll&&!rl&&r
000
MC/DC0%0/2
$psib and $psib->isa("PPI::Token::Cast")
$psib $psib->isa("PPI::Token::Cast")
Truth table0%0/3
A and B
A= $psibB= $psib->isa("PPI::Token::Cast")
ABresult
0X0
100
111
229 0 $cast = $psib;
230 0 $psib = $psib->sprevious_sibling;
231 }
232 0 ▶ return if !$cast;
Branch: 1 branch
BranchTrueFalseTotal
unless $cast 0 0 0
233 0 my $sigil = substr $cast->content, 0, 1;
234 0 ▶ return q{@} eq $sigil || q{%} eq $sigil;
Condition0%0/3
'@' eq $sigil || '%' eq $sigil
l!l&&r!l&&!r
000
MC/DC0%0/2
'@' eq $sigil || '%' eq $sigil
'@' eq $sigil '%' eq $sigil
Truth table0%0/3
A || B
A= '@' eq $sigilB= '%' eq $sigil
ABresult
1X1
011
000
235 }
236  
237 0 ▶sub _is_in_slurpy_array_context {
Subroutine _is_in_slurpy_array_context: not called CC=15 SCAR=54.8
238 0 my ($elem) = @_;
239  
240 # return true is the result of the regexp is passed to a subroutine.
241 # doesn't check for array context due to assignment.
242  
243 # look backward for explicit regex operator
244 0 my $psib = $elem->sprevious_sibling;
245 0 ▶ if ($psib && $psib->content eq q{=~}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($psib and $psib->content eq "=~") 0 0 0
Condition0%0/3
$psib and $psib->content eq "=~"
!ll&&!rl&&r
000
MC/DC0%0/2
$psib and $psib->content eq "=~"
$psib $psib->content eq "=~"
Truth table0%0/3
A and B
A= $psibB= $psib->content eq "=~"
ABresult
0X0
100
111
246 # Track back through value
247 0 $psib = _skip_lhs($psib);
248 }
249  
250 0 ▶ if (!$psib) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($psib) 0 0 0
251 0 my $parent = $elem->parent;
252 0 ▶ return if !$parent;
Branch: 1 branch
BranchTrueFalseTotal
unless $parent 0 0 0
253 0 ▶ if ($parent->isa('PPI::Statement')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent->isa("PPI::Statement")) 0 0 0
254 0 $parent = $parent->parent;
255 0 ▶ return if !$parent;
Branch: 1 branch
BranchTrueFalseTotal
unless $parent 0 0 0
256 }
257  
258 # Return true if we have a list that isn't part of a foreach loop.
259 # TECHNICAL DEBT: This code is basically shared with
260 # RequireCheckingReturnValueOfEval. I don't want to put this code
261 # into Perl::Critic::Utils::*, but I don't have time to sort out
262 # PPIx::Utilities::Structure::List yet.
263 0 ▶ if ( $parent->isa('PPI::Structure::List') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent->isa("PPI::Structure::List")) 0 0 0
264 0 ▶ my $parent_statement = $parent->statement() or return $TRUE;
Branch: 1 branch
BranchTrueFalseTotal
my $parent_statement = $parent->statement or return $Perl::Critic::Policy::RegularExpressions::ProhibitUnusedCapture::TRUE 0 0 0
265 0 ▶ return $TRUE if not
Branch: 1 branch
BranchTrueFalseTotal
unless $parent_statement->isa("PPI::Statement::Compound") 0 0 0
266 $parent_statement->isa('PPI::Statement::Compound');
267 0 ▶ return $TRUE if $parent_statement->type() ne 'foreach';
Branch: 1 branch
BranchTrueFalseTotal
if $parent_statement->type ne "foreach" 0 0 0
268 }
269  
270 0 ▶ return $TRUE if $parent->isa('PPI::Structure::Constructor');
Branch: 1 branch
BranchTrueFalseTotal
if $parent->isa("PPI::Structure::Constructor") 0 0 0
271 0 ▶ if ($parent->isa('PPI::Structure::Block')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent->isa("PPI::Structure::Block")) 0 0 0
272 0 ▶ return $TRUE
Branch: 1 branch
BranchTrueFalseTotal
if builtin::refaddr($elem->statement) eq builtin::refaddr([$parent->schildren]->[-1]) 0 0 0
273 if
274 refaddr($elem->statement)
275 eq refaddr([$parent->schildren]->[-1]);
276 }
277 0 return;
278 }
279 0 ▶ if ($psib->isa('PPI::Token::Operator')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($psib->isa("PPI::Token::Operator")) 0 0 0
280 # Most operators kill slurpiness (except assignment, which is handled elsewhere).
281 0 return q{,} eq $psib->content;
282 }
283 0 return $TRUE;
284 }
285  
286 0 ▶sub _skip_lhs {
Subroutine _skip_lhs: not called CC=1 SCAR=6.9
287 0 my ($elem) = @_;
288  
289 # TODO: better implementation to handle casts, expressions, subcalls, etc.
290 0 $elem = $elem->sprevious_sibling();
291  
292 0 return $elem;
293 }
294  
295 0 ▶sub _enough_magic {
Subroutine _enough_magic: not called CC=3 SCAR=24.8
296 0 my ($elem, $re, $captures, $named_captures, $doc) = @_;
297  
298 0 _check_for_magic($elem, $re, $captures, $named_captures, $doc);
299  
300 0 ▶ return ( none {not defined} @{$captures} )
Subroutine __ANON__: not called CC=1 SCAR=6.9
Condition0%0/3
not %{$named_captures;} or &none(sub { defined $_; } , values %{$named_captures;})
l!l&&r!l&&!r
000
MC/DC0%0/2
not %{$named_captures;} or &none(sub { defined $_; } , values %{$named_captures;})
not %{$named_captures;} &none(sub { defined $_; } , values %{$named_captures;})
Truth table0%0/3
not(A) or B
A= not %{$named_captures;}B= &none(sub { defined $_; } , values %{$named_captures;})
ABresult
1X1
011
000
301 0 && ( !%{$named_captures} ||
302 0 ▶ none {defined} values %{$named_captures} );
Subroutine __ANON__: not called CC=1 SCAR=6.9
Condition0%0/3
&none(sub { not defined $_; } , @{$captures;}) && (!%{$named_captures;} || &none(sub { defined $_; } , values %{$named_captures;}))
!ll&&!rl&&r
000
MC/DC0%0/2
&none(sub { not defined $_; } , @{$captures;}) && (!%{$named_captures;} || &none(sub { defined $_; } , values %{$named_captures;}))
&none(sub { not defined $_; } , @{$captures;}) (!%{$named_captures;} || &none(sub { defined $_; } , values %{$named_captures;}))
Truth table0%0/3
A && B
A= &none(sub { not defined $_; } , @{$captures;})B= (!%{$named_captures;} || &none(sub { defined $_; } , values %{$named_captures;}))
ABresult
0X0
100
111
303 }
304  
305 # void return
306 0 ▶sub _check_for_magic {
Subroutine _check_for_magic: not called CC=7 SCAR=40.3
307 0 my ($elem, $re, $captures, $named_captures, $doc) = @_;
308  
309 # Search for $1..$9 in :
310 # * the rest of this statement
311 # * subsequent sibling statements
312 # * if this is in a conditional boolean, the if/else bodies of the conditional
313 # * if this is in a while/for condition, the loop body
314 # But NO intervening regexps!
315  
316 # Package up the usual arguments for _check_rest_of_statement().
317 0 my $arg = {
318 regexp => $re,
319 numbered_captures => $captures,
320 named_captures => $named_captures,
321 document => $doc,
322 };
323  
324 # Capture whether or not the regular expression is negated -- that
325 # is, whether it is preceded by the '!~' binding operator.
326 0 ▶ if ( my $prior_token = $elem->sprevious_sibling() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $prior_token = $elem->sprevious_sibling) 0 0 0
327 0 ▶ $arg->{negated} = $prior_token->isa( 'PPI::Token::Operator' ) &&
Condition0%0/3
$prior_token->isa('PPI::Token::Operator') && '!~' eq $prior_token->content
!ll&&!rl&&r
000
MC/DC0%0/2
$prior_token->isa('PPI::Token::Operator') && '!~' eq $prior_token->content
$prior_token->isa('PPI::Token::Operator') '!~' eq $prior_token->content
Truth table0%0/3
A && B
A= $prior_token->isa('PPI::Token::Operator')B= '!~' eq $prior_token->content
ABresult
0X0
100
111
328 q<!~> eq $prior_token->content();
329 }
330  
331 0 ▶ return if ! _check_rest_of_statement( $elem, $arg );
Branch: 1 branch
BranchTrueFalseTotal
unless _check_rest_of_statement($elem, $arg) 0 0 0
332  
333 0 my $parent = $elem->parent();
334 0 ▶ while ($parent && ! $parent->isa('PPI::Statement::Sub')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent and not $parent->isa("PPI::Statement::Sub")) 0 0 0
Condition0%0/3
$parent and not $parent->isa("PPI::Statement::Sub")
!ll&&!rl&&r
000
MC/DC0%0/2
$parent and not $parent->isa("PPI::Statement::Sub")
$parent not $parent->isa("PPI::Statement::Sub")
Truth table0%0/3
A and not(B)
A= $parentB= not $parent->isa("PPI::Statement::Sub")
ABresult
0X0
100
111
335 0 ▶ return if ! _check_rest_of_statement( $parent, $arg );
Branch: 1 branch
BranchTrueFalseTotal
unless _check_rest_of_statement($parent, $arg) 0 0 0
336 0 $parent = $parent->parent();
337 }
338  
339 0 return;
340 }
341  
342 # Check if we are in the condition or block of a 'while'
343 0 ▶sub _check_if_in_while_condition_or_block {
Subroutine _check_if_in_while_condition_or_block: not called CC=10 SCAR=47.0
344 0 my ( $elem ) = @_;
345 0 ▶ $elem or return;
Branch: 1 branch
BranchTrueFalseTotal
$elem or return 0 0 0
346  
347 0 ▶ my $parent = $elem->parent() or return;
Branch: 1 branch
BranchTrueFalseTotal
my $parent = $elem->parent or return 0 0 0
348 0 ▶ $parent->isa( 'PPI::Statement' ) or return;
Branch: 1 branch
BranchTrueFalseTotal
$parent->isa("PPI::Statement") or return 0 0 0
349  
350 0 ▶ my $item = $parent = $parent->parent() or return;
Branch: 1 branch
BranchTrueFalseTotal
my $item = $parent = $parent->parent or return 0 0 0
351 0 ▶ if ( $item->isa( 'PPI::Structure::Block' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item->isa("PPI::Structure::Block")) 0 0 0
352 0 ▶ $item = $item->sprevious_sibling() or return;
Branch: 1 branch
BranchTrueFalseTotal
$item = $item->sprevious_sibling or return 0 0 0
353 }
354 0 ▶ $item->isa( 'PPI::Structure::Condition' ) or return;
Branch: 1 branch
BranchTrueFalseTotal
$item->isa("PPI::Structure::Condition") or return 0 0 0
355  
356 0 ▶ $item = $item->sprevious_sibling() or return;
Branch: 1 branch
BranchTrueFalseTotal
$item = $item->sprevious_sibling or return 0 0 0
357 0 ▶ $item->isa( 'PPI::Token::Word' ) or return;
Branch: 1 branch
BranchTrueFalseTotal
$item->isa("PPI::Token::Word") or return 0 0 0
358  
359 0 return $WHILE eq $item->content();
360 }
361  
362 {
363 # Shortcut operators '||', '//', and 'or' can cause everything after
364 # them to be skipped. 'and' trumps '||' and '//', and causes things
365 # to be evaluated again. The value is true to skip, false to cancel
366 # skipping.
367 40 Readonly::Hash my %SHORTCUT_OPERATOR => (
368 q<||> => $FALSE,
369 q<//> => $FALSE,
370 and => $TRUE,
371 or => $FALSE,
372 );
373  
374 # RT #38942
375 # The issue in the ticket is that in something like
376 # if ( /(a)/ || /(b)/ ) {
377 # say $1
378 # }
379 # the capture variable can come from either /(a)/ or /(b)/. If we
380 # don't take into account the short-cutting nature of the '||' we
381 # erroneously conclude that the capture in /(a)/ is not used. So we
382 # need to skip every regular expression after an alternation.
383 #
384 # The trick is that we want to still mark magic variables, because
385 # of code like
386 # my $foo = $1 || $2;
387 # so we can't just ignore everything after an alternation.
388 #
389 # To do all this correctly, we have to track precedence, and start
390 # paying attention again if an 'and' is found after a '||'.
391  
392 # Subroutine _make_regexp_checker() manufactures a snippet of code
393 # which is used to track regular expressions. It takes one optional
394 # argument, which is the snippet used to track the parent object's
395 # regular expressions.
396 #
397 # The snippet is passed each token encountered, and returns true if
398 # the scan for capture variables is to be stopped. This will happen
399 # if the token is a regular expression which is _not_ to the right
400 # of an alternation operator ('||', '//', or 'or'), or it _is_ to
401 # the right of an 'and', without an intervening alternation
402 # operator.
403 #
404 # If _make_regexp_checker() was passed a snippet which
405 # returns false on encountering a regular expression, the returned
406 # snippet always returns false, for the benefit of code like
407 # /(a)/ || ( /(b)/ || /(c)/ ).
408  
409 0 ▶ sub _make_regexp_checker {
Subroutine _make_regexp_checker: not called CC=3 SCAR=24.8
410 0 my ( $parent ) = @_;
411  
412 $parent
413 and not $parent->()
414 0 ▶ and return sub { return $FALSE };
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$parent and not &$parent() and return sub { return $Perl::Critic::Policy::RegularExpressions::ProhibitUnusedCapture::FALSE; } 0 0 0
Condition0%0/3
$parent and not &$parent()
!ll&&!rl&&r
000
MC/DC0%0/2
$parent and not &$parent()
$parent not &$parent()
Truth table0%0/3
A and not(B)
A= $parentB= not &$parent()
ABresult
0X0
100
111
415  
416 0 my $check = $TRUE;
417 0 my $precedence = 0;
418  
419 0 ▶ return sub {
Subroutine __ANON__: not called CC=9 SCAR=45.0
420 0 my ( $elem ) = @_;
421  
422 0 ▶ $elem or return $check;
Branch: 1 branch
BranchTrueFalseTotal
$elem or return $check 0 0 0
423  
424 0 ▶ if ( $elem->isa( 'PPI::Token::Regexp' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem->isa("PPI::Token::Regexp")) 0 0 0
425 0 ▶ return _regexp_is_in_split( $elem ) ? $FALSE : $check;
Branch: 1 branch
BranchTrueFalseTotal
_regexp_is_in_split($elem) ? : 0 0 0
426 }
427  
428 0 ▶ if ( $elem->isa( 'PPI::Token::Structure' )
Branch: 1 branch
BranchTrueFalseTotal
if ($elem->isa("PPI::Token::Structure") and ";" eq $elem->content) 0 0 0
Condition0%0/3
$elem->isa("PPI::Token::Structure") and ";" eq $elem->content
!ll&&!rl&&r
000
MC/DC0%0/2
$elem->isa("PPI::Token::Structure") and ";" eq $elem->content
$elem->isa("PPI::Token::Structure") ";" eq $elem->content
Truth table0%0/3
A and B
A= $elem->isa("PPI::Token::Structure")B= ";" eq $elem->content
ABresult
0X0
100
111
429 && q<;> eq $elem->content() ) {
430 0 $check = $TRUE;
431 0 $precedence = 0;
432 0 return $FALSE;
433 }
434  
435 0 ▶ $elem->isa( 'PPI::Token::Operator' )
Branch: 1 branch
BranchTrueFalseTotal
$elem->isa("PPI::Token::Operator") or return $Perl::Critic::Policy::RegularExpressions::ProhibitUnusedCapture::FALSE 0 0 0
436 or return $FALSE;
437  
438 0 my $content = $elem->content();
439 0 ▶ defined( my $oper_check = $SHORTCUT_OPERATOR{$content} )
Branch: 1 branch
BranchTrueFalseTotal
defined(my $oper_check = $SHORTCUT_OPERATOR{$content}) or return $Perl::Critic::Policy::RegularExpressions::ProhibitUnusedCapture::FALSE 0 0 0
440 or return $FALSE;
441  
442 0 my $oper_precedence = precedence_of( $content );
443 0 ▶ $oper_precedence >= $precedence
Branch: 1 branch
BranchTrueFalseTotal
$oper_precedence >= $precedence or return $Perl::Critic::Policy::RegularExpressions::ProhibitUnusedCapture::FALSE 0 0 0
444 or return $FALSE;
445  
446 0 $precedence = $oper_precedence;
447 0 $check = $oper_check;
448  
449 0 return $FALSE;
450 0 };
451 }
452 }
453  
454 # Argument is regexp.
455 # True if it is the regexp in a split()
456 40 ▶sub _regexp_is_in_split {
Subroutine _regexp_is_in_split: not called CC=6 SCAR=36.0
457 0 my ( $elem ) = @_;
458  
459 0 my $prev;
460 0 ▶ if ( ! ( $prev = $elem->sprevious_sibling() ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($prev = $elem->sprevious_sibling) 0 0 0
461 # Maybe we have split( /.../, ... )
462 0 ▶ my $stmt = $elem->statement()
Branch: 1 branch
BranchTrueFalseTotal
my $stmt = $elem->statement or return $Perl::Critic::Policy::RegularExpressions::ProhibitUnusedCapture::FALSE 0 0 0
463 or return $FALSE;
464 0 ▶ $stmt->parent()
Branch: 1 branch
BranchTrueFalseTotal
$stmt->parent or return $Perl::Critic::Policy::RegularExpressions::ProhibitUnusedCapture::FALSE 0 0 0
465 or return $FALSE;
466 0 ▶ $prev = $elem->sprevious_sibling()
Branch: 1 branch
BranchTrueFalseTotal
$prev = $elem->sprevious_sibling or return $Perl::Critic::Policy::RegularExpressions::ProhibitUnusedCapture::FALSE 0 0 0
467 or return $FALSE;
468 }
469 0 ▶ return $prev->isa( 'PPI::Token::Word' ) && $SPLIT eq $prev->content();
Condition0%0/3
$prev->isa('PPI::Token::Word') && $SPLIT eq $prev->content
!ll&&!rl&&r
000
MC/DC0%0/2
$prev->isa('PPI::Token::Word') && $SPLIT eq $prev->content
$prev->isa('PPI::Token::Word') $SPLIT eq $prev->content
Truth table0%0/3
A && B
A= $prev->isa('PPI::Token::Word')B= $SPLIT eq $prev->content
ABresult
0X0
100
111
470 }
471  
472  
473 # false if we hit another regexp
474 # The arguments are:
475 # $elem - The PPI::Element whose siblings are to be checked;
476 # $arg - A hash reference containing the following keys:
477 # regexp => the relevant PPIx::Regexp object;
478 # numbered_captures => a reference to the array used to track the
479 # use of numbered captures;
480 # named_captures => a reference to the hash used to track the
481 # use of named captures;
482 # negated => true if the regexp was bound to its target with the
483 # '!~' operator;
484 # document => a reference to the Perl::Critic::Document;
485 # Converted to passing the arguments everyone gets in a hash because of
486 # the need to add the 'negated' argument, which would put us at six
487 # arguments.
488 0 ▶sub _check_rest_of_statement {
Subroutine _check_rest_of_statement: not called CC=10 SCAR=47.0
489 0 my ( $elem, $arg ) = @_;
490  
491 0 my $checker = _make_regexp_checker();
492 0 my $nsib = $elem->snext_sibling;
493  
494 # If we are an if (or elsif) and the result of the regexp is
495 # negated, we skip the first block found. RT #69867
496 0 ▶ if ( $arg->{negated} && _is_condition_of_if_statement( $elem ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg->{"negated"} and _is_condition_of_if_statement($elem)) 0 0 0
Condition0%0/3
$arg->{"negated"} and _is_condition_of_if_statement($elem)
!ll&&!rl&&r
000
MC/DC0%0/2
$arg->{"negated"} and _is_condition_of_if_statement($elem)
$arg->{"negated"} _is_condition_of_if_statement($elem)
Truth table0%0/3
A and B
A= $arg->{"negated"}B= _is_condition_of_if_statement($elem)
ABresult
0X0
100
111
497 0 ▶ while ( $nsib && ! $nsib->isa( 'PPI::Structure::Block' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nsib and not $nsib->isa("PPI::Structure::Block")) 0 0 0
Condition0%0/3
$nsib and not $nsib->isa("PPI::Structure::Block")
!ll&&!rl&&r
000
MC/DC0%0/2
$nsib and not $nsib->isa("PPI::Structure::Block")
$nsib not $nsib->isa("PPI::Structure::Block")
Truth table0%0/3
A and not(B)
A= $nsibB= not $nsib->isa("PPI::Structure::Block")
ABresult
0X0
100
111
498 0 $nsib = $nsib->snext_sibling();
499 }
500 0 ▶ $nsib and $nsib = $nsib->snext_sibling();
Branch: 1 branch
BranchTrueFalseTotal
$nsib and $nsib = $nsib->snext_sibling 0 0 0
501 }
502  
503 0 ▶ while ($nsib) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nsib) 0 0 0
504 0 ▶ return if $checker->($nsib);
Branch: 1 branch
BranchTrueFalseTotal
if &$checker($nsib) 0 0 0
505 0 ▶ if ($nsib->isa('PPI::Node')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nsib->isa('PPI::Node')) { } 0 0 0
506 0 ▶ return if ! _check_node_children($nsib, $arg, $checker );
Branch: 1 branch
BranchTrueFalseTotal
unless _check_node_children($nsib, $arg, $checker) 0 0 0
507 } else {
508 _mark_magic( $nsib, $arg->{regexp}, $arg->{numbered_captures},
509 0 $arg->{named_captures}, $arg->{document} );
510 }
511 0 $nsib = $nsib->snext_sibling;
512 }
513 0 return $TRUE;
514 }
515  
516 {
517  
518 40 Readonly::Hash my %IS_IF_STATEMENT => hashify( qw{ if elsif } );
519  
520 # Return true if the argument is the condition of an if or elsif
521 # statement, otherwise return false.
522 0 ▶ sub _is_condition_of_if_statement {
Subroutine _is_condition_of_if_statement: not called CC=5 SCAR=34.0
523 0 my ( $elem ) = @_;
524 0 ▶ $elem
Branch: 1 branch
BranchTrueFalseTotal
$elem and $elem->isa("PPI::Structure::Condition") or return $Perl::Critic::Policy::RegularExpressions::ProhibitUnusedCapture::FALSE 0 0 0
Condition0%0/3
$elem and $elem->isa("PPI::Structure::Condition")
!ll&&!rl&&r
000
MC/DC0%0/2
$elem and $elem->isa("PPI::Structure::Condition")
$elem $elem->isa("PPI::Structure::Condition")
Truth table0%0/3
A and B
A= $elemB= $elem->isa("PPI::Structure::Condition")
ABresult
0X0
100
111
525 and $elem->isa( 'PPI::Structure::Condition' )
526 or return $FALSE;
527 0 ▶ my $psib = $elem->sprevious_sibling()
Branch: 1 branch
BranchTrueFalseTotal
my $psib = $elem->sprevious_sibling or return $Perl::Critic::Policy::RegularExpressions::ProhibitUnusedCapture::FALSE 0 0 0
528 or return $FALSE;
529 0 ▶ $psib->isa( 'PPI::Token::Word' )
Branch: 1 branch
BranchTrueFalseTotal
$psib->isa("PPI::Token::Word") or return $Perl::Critic::Policy::RegularExpressions::ProhibitUnusedCapture::FALSE 0 0 0
530 or return $FALSE;
531 0 return $IS_IF_STATEMENT{ $psib->content() };
532  
533 }
534 }
535  
536 # false if we hit another regexp
537 # The arguments are:
538 # $elem - The PPI::Node whose children are to be checked;
539 # $arg - A hash reference containing the following keys:
540 # regexp => the relevant PPIx::Regexp object;
541 # numbered_captures => a reference to the array used to track the
542 # use of numbered captures;
543 # named_captures => a reference to the hash used to track the
544 # use of named captures;
545 # document => a reference to the Perl::Critic::Document;
546 # $parent_checker - The parent's regexp checking code snippet,
547 # manufactured by _make_regexp_checker(). This argument is not in
548 # the $arg hash because that hash is shared among levels of the
549 # parse tree, whereas the regexp checker is not.
550 # TODO the things in the $arg hash are widely shared among the various
551 # pieces/parts of this policy; maybe more subroutines should use this
552 # hash rather than passing all this stuff around as individual
553 # arguments. This particular subroutine got the hash-reference treatment
554 # because Subroutines::ProhibitManyArgs started complaining when the
555 # checker argument was added.
556 40 ▶sub _check_node_children {
Subroutine _check_node_children: not called CC=5 SCAR=32.3
557 0 my ($elem, $arg, $parent_checker) = @_;
558  
559 # caveat: this will descend into subroutine definitions...
560  
561 0 my $checker = _make_regexp_checker($parent_checker);
562 0 for my $child ($elem->schildren) {
563 0 ▶ return if $checker->($child);
Branch: 1 branch
BranchTrueFalseTotal
if &$checker($child) 0 0 0
564 0 ▶ if ($child->isa('PPI::Node')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($child->isa('PPI::Node')) { } 0 0 0
565 0 ▶ return if ! _check_node_children($child, $arg, $checker);
Branch: 1 branch
BranchTrueFalseTotal
unless _check_node_children($child, $arg, $checker) 0 0 0
566 } else {
567 _mark_magic($child, $arg->{regexp},
568 $arg->{numbered_captures}, $arg->{named_captures},
569 0 $arg->{document});
570 }
571 }
572 0 return $TRUE;
573 }
574  
575 0 ▶sub _mark_magic {
Subroutine _mark_magic: not called CC=17 SCAR=57.2
576 0 my ($elem, $re, $captures, $named_captures, $doc) = @_;
577  
578 # If we're a double-quotish element, we need to grub through its
579 # content. RT #38942
580 0 ▶ if ( _is_double_quotish_element( $elem ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_double_quotish_element($elem)) 0 0 0
581 0 _mark_magic_in_content(
582 $elem->content(), $re, $captures, $named_captures, $doc );
583 0 return;
584 }
585  
586 # Ditto a here document, though the logic is different. RT #38942
587 0 ▶ if ( $elem->isa( 'PPI::Token::HereDoc' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem->isa("PPI::Token::HereDoc")) 0 0 0
588 0 ▶ $elem->content() =~ m/ \A << ~? \s* ' /sxm
Branch: 1 branch
BranchTrueFalseTotal
$elem->content =~ / \A << ~? \s* ' /msx or _mark_magic_in_content(join($Perl::Critic::Policy::RegularExpressions::ProhibitUnusedCapture::EMPTY, $elem->heredoc), $re, $captures, $named_captures, $doc) 0 0 0
589 or _mark_magic_in_content(
590 join( $EMPTY, $elem->heredoc() ), $re, $captures,
591 $named_captures, $doc );
592 0 return;
593 }
594  
595 # Only interested in magic, or known English equivalent.
596 0 my $content = $elem->content();
597 0 ▶ my ( $capture_ref, $capture_array, $capture_hash ) = $doc->uses_module( 'English' ) ?
Branch: 1 branch
BranchTrueFalseTotal
$doc->uses_module('English') ? : 0 0 0
598 ( \%CAPTURE_REFERENCE_ENGLISH, \%CAPTURE_ARRAY_ENGLISH, \%CAPTURE_HASH_ENGLISH ) :
599 ( \%CAPTURE_REFERENCE, \%CAPTURE_ARRAY, \%CAPTURE_HASH );
600 $elem->isa( 'PPI::Token::Magic' )
601 or $capture_ref->{$content}
602 or $capture_array->{$content}
603 0 ▶ or $capture_hash->{$content}
Branch: 1 branch
BranchTrueFalseTotal
$elem->isa("PPI::Token::Magic") or $capture_ref->{$content} or $capture_array->{$content} or $capture_hash->{$content} or return 0 0 0
Condition0%0/9
$elem->isa("PPI::Token::Magic") or $capture_ref->{$content} or $capture_array->{$content} or $capture_hash->{$content}
l!l&&r!l&&!r
000
$elem->isa("PPI::Token::Magic") or $capture_ref->{$content} or $capture_array->{$content}
l!l&&r!l&&!r
000
$elem->isa("PPI::Token::Magic") or $capture_ref->{$content}
l!l&&r!l&&!r
000
MC/DC0%0/4
$elem->isa("PPI::Token::Magic") or $capture_ref->{$content} or $capture_array->{$content} or $capture_hash->{$content}
$elem->isa("PPI::Token::Magic") $capture_ref->{$content} $capture_array->{$content} $capture_hash->{$content}
Truth table0%0/5
A or B or C or D
A= $elem->isa("PPI::Token::Magic")B= $capture_ref->{$content}C= $capture_array->{$content}D= $capture_hash->{$content}
ABCDresult
1XXX1
01XX1
001X1
00011
00000
604 or return;
605  
606 0 ▶ if ( $content =~ m/ \A \$ ( \d+ ) /xms ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($content =~ / \A \$ ( \d+ ) /msx) { } 0 0 0
elsif ($capture_array->{$content}) { } 0 0 0
elsif ($capture_hash->{$content}) { } 0 0 0
elsif ($capture_ref->{$content}) { } 0 0 0
607  
608 # Record if we see $1, $2, $3, ...
609 0 my $num = $1;
610 0 ▶ if (0 < $num) { # don't mark $0
Branch: 1 branch
BranchTrueFalseTotal
if (0 < $num) 0 0 0
611 # Only mark the captures we really need -- don't mark superfluous magic vars
612 0 ▶ if ($num <= @{$captures}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num <= @{$captures;}) 0 0 0
613 0 _record_numbered_capture( $num, $captures );
614 }
615 }
616 } elsif ( $capture_array->{$content} ) { # GitHub #778
617 0 foreach my $num ( 1 .. @{$captures} ) {
618 0 _record_numbered_capture( $num, $captures );
619 }
620 } elsif ( $capture_hash->{$content} ) {
621 0 foreach my $name ( keys %{$named_captures} ) {
622 0 _record_named_capture( $name, $captures, $named_captures );
623 }
624 } elsif ( $capture_ref->{$content} ) {
625 0 _mark_magic_subscripted_code( $elem, $re, $captures, $named_captures );
626 }
627 0 return;
628 }
629  
630 # Record a named capture referenced by a hash or array found in code.
631 # The arguments are:
632 # $elem - The element that represents a subscripted capture variable;
633 # $re - The PPIx::Regexp object;
634 # $captures - A reference to the numbered capture array;
635 # $named_captures - A reference to the named capture hash.
636 0 ▶sub _mark_magic_subscripted_code {
Subroutine _mark_magic_subscripted_code: not called CC=3 SCAR=24.8
637 0 my ( $elem, $re, $captures, $named_captures ) = @_;
638 0 ▶ my $subscr = $elem->snext_sibling() or return;
Branch: 1 branch
BranchTrueFalseTotal
my $subscr = $elem->snext_sibling or return 0 0 0
639 0 ▶ $subscr->isa( 'PPI::Structure::Subscript' ) or return;
Branch: 1 branch
BranchTrueFalseTotal
$subscr->isa("PPI::Structure::Subscript") or return 0 0 0
640 0 my $subval = $subscr->content();
641 0 _record_subscripted_capture(
642 $elem->content(), $subval, $re, $captures, $named_captures );
643 0 return;
644 }
645  
646 # Find capture variables in the content of a double-quotish thing, and
647 # record their use. RT #38942. The arguments are:
648 # $content - The content() ( or heredoc() in the case of a here
649 # document) to be analyzed;
650 # $re - The PPIx::Regexp object;
651 # $captures - A reference to the numbered capture array;
652 # $named_captures - A reference to the named capture hash.
653 0 ▶sub _mark_magic_in_content {
Subroutine _mark_magic_in_content: not called CC=11 SCAR=48.8
654 0 my ( $content, $re, $captures, $named_captures, $doc ) = @_;
655  
656 0 ▶ my ( $capture_ref, $capture_array ) = $doc->uses_module( 'English' ) ?
Branch: 1 branch
BranchTrueFalseTotal
$doc->uses_module('English') ? : 0 0 0
657 ( \%CAPTURE_REFERENCE_ENGLISH, \%CAPTURE_ARRAY_ENGLISH ) :
658 ( \%CAPTURE_REFERENCE, \%CAPTURE_ARRAY );
659  
660 0 ▶ while ( $content =~ m< ( [\$\@] (?:
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ / ( [\$\@] (?: [{] \^? (?: \w+ | . ) [}] | \w+ | . ) ) /gmsx) 0 0 0
661 [{] \^? (?: \w+ | . ) [}] | \w+ | . ) ) >sxmg ) {
662 0 my $name = $1;
663 0 ▶ $name =~ s/ \A ( [\$\@] ) [{] (?! \^ ) /$1/sxm
Branch: 1 branch
BranchTrueFalseTotal
$name =~ s/ \A ( [\$\@] ) [{] (?! \^ ) /$1/msx and $name =~ s/ [}] \z //msx 0 0 0
664 and $name =~ s/ [}] \z //sxm;
665  
666 0 ▶ if ( $name =~ m/ \A \$ ( \d+ ) \z /sxm ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($name =~ / \A \$ ( \d+ ) \z /msx) { } 0 0 0
elsif ($capture_array->{$name}) { } 0 0 0
elsif ($capture_ref->{$name} and $content =~ / \G ( [{] [^}]+ [}] | [[] [^]] []] ) /cgmsx) { } 0 0 0
667  
668 0 my $num = $1;
669 0 < $num
670 0 ▶ and $num <= @{ $captures }
Branch: 1 branch
BranchTrueFalseTotal
0 < $num and $num <= @{$captures;} and _record_numbered_capture($num, $captures) 0 0 0
Condition0%0/3
0 < $num and $num <= @{$captures;}
!ll&&!rl&&r
000
MC/DC0%0/2
0 < $num and $num <= @{$captures;}
0 < $num $num <= @{$captures;}
Truth table0%0/3
A and B
A= 0 < $numB= $num <= @{$captures;}
ABresult
0X0
100
111
671 and _record_numbered_capture( $num, $captures );
672  
673 } elsif ( $capture_array->{$name} ) { # GitHub #778
674 0 foreach my $num ( 1 .. @{$captures} ) {
675 0 _record_numbered_capture( $num, $captures );
676 }
677  
678 ▶ } elsif ( $capture_ref->{$name} &&
Condition0%0/3
$capture_ref->{$name} and $content =~ / \G ( [{] [^}]+ [}] | [[] [^]] []] ) /cgmsx
!ll&&!rl&&r
000
MC/DC0%0/2
$capture_ref->{$name} and $content =~ / \G ( [{] [^}]+ [}] | [[] [^]] []] ) /cgmsx
$capture_ref->{$name} $content =~ / \G ( [{] [^}]+ [}] | [[] [^]] []] ) /cgmsx
Truth table0%0/3
A and B
A= $capture_ref->{$name}B= $content =~ / \G ( [{] [^}]+ [}] | [[] [^]] []] ) /cgmsx
ABresult
0X0
100
111
679 $content =~ m/ \G ( [{] [^}]+ [}] | [[] [^]] []] ) /smxgc )
680 {
681 0 _record_subscripted_capture(
682 $name, $1, $re, $captures, $named_captures );
683  
684 }
685 }
686 0 return;
687 }
688  
689 # Return true if the given element is double-quotish. Always returns
690 # false for a PPI::Token::HereDoc, since they're a different beast.
691 # RT #38942.
692 0 ▶sub _is_double_quotish_element {
Subroutine _is_double_quotish_element: not called CC=5 SCAR=34.0
693 0 my ( $elem ) = @_;
694  
695 0 ▶ $elem or return;
Branch: 1 branch
BranchTrueFalseTotal
$elem or return 0 0 0
696  
697 0 my $content = $elem->content();
698  
699 0 ▶ if ( $elem->isa( 'PPI::Token::QuoteLike::Command' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem->isa("PPI::Token::QuoteLike::Command")) 0 0 0
700 0 return $content !~ m/ \A qx \s* ' /sxm;
701 }
702  
703 0 foreach my $class ( qw{
704 PPI::Token::Quote::Double
705 PPI::Token::Quote::Interpolate
706 PPI::Token::QuoteLike::Backtick
707 PPI::Token::QuoteLike::Readline
708 } ) {
709 0 ▶ $elem->isa( $class ) and return $TRUE;
Branch: 1 branch
BranchTrueFalseTotal
$elem->isa($class) and return $Perl::Critic::Policy::RegularExpressions::ProhibitUnusedCapture::TRUE 0 0 0
710 }
711  
712 0 return $FALSE;
713 }
714  
715 # Record a subscripted capture, either hash dereference or array
716 # dereference. We assume that an array represents a numbered capture and
717 # a hash represents a named capture, since we have to handle (e.g.) both
718 # @+ and %+.
719 0 ▶sub _record_subscripted_capture {
Subroutine _record_subscripted_capture: not called CC=5 SCAR=34.0
720 0 my ( $variable_name, $suffix, $re, $captures, $named_captures ) = @_;
721 0 ▶ if ( $suffix =~ m/ \A [{] ( .*? ) [}] /smx ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($suffix =~ / \A [{] ( .*? ) [}] /msx) { } 0 0 0
elsif ($suffix =~ / \A [[] \s* ( [-+]? \d+ ) \s* []] /msx) { } 0 0 0
722 0 ( my $name = $1 ) =~ s/ \A ( ["'] ) ( .*? ) \1 \z /$2/smx;
723 0 _record_named_capture( $name, $captures, $named_captures );
724 } elsif ( $suffix =~ m/ \A [[] \s* ( [-+]? \d+ ) \s* []] /smx ) {
725 # GitHub #778
726 # Mostly capture numbers encountered here are 1-based (e.g. @+, @-).
727 # But @{^CAPTURE} is zero-based, so we need to tweak the subscript
728 # before we record the capture number.
729 0 my $num = $1 + 0;
730 $num >= 0
731 0 ▶ and $ZERO_BASED_CAPTURE_REFERENCE{$variable_name}
Branch: 1 branch
BranchTrueFalseTotal
$num >= 0 and $ZERO_BASED_CAPTURE_REFERENCE{$variable_name} and ++$num 0 0 0
Condition0%0/3
$num >= 0 and $ZERO_BASED_CAPTURE_REFERENCE{$variable_name}
!ll&&!rl&&r
000
MC/DC0%0/2
$num >= 0 and $ZERO_BASED_CAPTURE_REFERENCE{$variable_name}
$num >= 0 $ZERO_BASED_CAPTURE_REFERENCE{$variable_name}
Truth table0%0/3
A and B
A= $num >= 0B= $ZERO_BASED_CAPTURE_REFERENCE{$variable_name}
ABresult
0X0
100
111
732 and $num++;
733 0 _record_numbered_capture( $num, $captures, $re );
734 }
735 0 return;
736 }
737  
738 # Because a named capture is also one or more numbered captures, the recording
739 # of the use of a named capture seemed complex enough to wrap in a subroutine.
740 0 ▶sub _record_named_capture {
Subroutine _record_named_capture: not called CC=3 SCAR=24.8
741 0 my ( $name, $captures, $named_captures ) = @_;
742 0 ▶ defined ( my $numbers = $named_captures->{$name} ) or return;
Branch: 1 branch
BranchTrueFalseTotal
defined(my $numbers = $named_captures->{$name}) or return 0 0 0
743 0 foreach my $capnum ( @{ $numbers } ) {
744 0 _record_numbered_capture( $capnum, $captures );
745 }
746 0 $named_captures->{$name} = undef;
747 0 return;
748 }
749  
750 0 ▶sub _record_numbered_capture {
Subroutine _record_numbered_capture: not called CC=4 SCAR=30.0
751 0 my ( $number, $captures, $re ) = @_;
752 0 ▶ $re and $number < 0
Branch: 1 branch
BranchTrueFalseTotal
$re and $number < 0 and $number = $re->max_capture_number + $number + 1 0 0 0
Condition0%0/3
$re and $number < 0
!ll&&!rl&&r
000
MC/DC0%0/2
$re and $number < 0
$re $number < 0
Truth table0%0/3
A and B
A= $reB= $number < 0
ABresult
0X0
100
111
753 and $number = $re->max_capture_number() + $number + 1;
754 0 ▶ return if $number <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $number <= 0 0 0 0
755 0 $captures->[ $number - 1 ] = 1;
756 0 return;
757 }
758  
759 40 1;
760  
761 __END__
« lib/Perl/Critic/Policy/Subroutines/ProhibitAmpersandSigils.pm ↑ Summary lib/Perl/Critic/Policy/ValuesAndExpressions/ProhibitMagicNumbers.pm »