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

filter: Statementstmt 21.8% 29 / 133 Branchbran 1.0% 1 / 98 Conditioncond 0.0% 0 / 73 MC/DCmcdc 0.0% 0 / 41 Subroutinesub 55.0% 11 / 20 Podpod 80.0% 4 / 5 totaltotal 12.1% 45 / 370 CC 78 SCAR 51.6
CC
78
Cov
10%
_unambiguous_goto
420.0
_get_method_name
240.0
_is_in_conditional_structure
210.0
CRAP 174.1
↑ Summary lib/Perl/Critic/Policy/ValuesAndExpressions/RequireConstantVersion.pm »
1 package Perl::Critic::Policy::RegularExpressions::ProhibitCaptureWithoutTest;
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 40 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 40 ▶use Perl::Critic::Utils qw{ :booleans :data_conversion :severities };
Subroutine BEGIN: called CC=1 SCAR=0.0
9 40 ▶use parent 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 40 our $VERSION = '1.156';
12  
13 #-----------------------------------------------------------------------------
14  
15 40 Readonly::Hash my %CONDITIONAL_OPERATOR => hashify( qw{ && || ? and or xor } );
16 40 Readonly::Hash my %UNAMBIGUOUS_CONTROL_TRANSFER => hashify(
17 qw< next last redo return > );
18  
19 40 Readonly::Scalar my $DESC => q{Capture variable used outside conditional};
20 40 Readonly::Scalar my $EXPL => [ 253 ];
21  
22 #-----------------------------------------------------------------------------
23  
24 91 ▶sub supported_parameters { return (
Subroutine supported_parameters: called CC=1 SCAR=0.0
Pod undocumented
25 {
26 91 name => 'exception_source',
27 description => 'Names of ways to generate exceptions',
28 behavior => 'string list',
29 list_always_present_values => [ qw{ die croak confess } ],
30 }
31 );
32 }
33 74 ▶sub default_severity { return $SEVERITY_MEDIUM }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
34 85 ▶sub default_themes { return qw(core pbp maintenance certrule ) }
Subroutine default_themes: called CC=1 SCAR=0.0
Pod documented
35 31 ▶sub applies_to { return 'PPI::Token::Magic' }
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
36  
37 #-----------------------------------------------------------------------------
38  
39 15 ▶sub violates {
Subroutine violates: called CC=4 SCAR=23.2
Pod documented
40 15 my ($self, $elem, undef) = @_;
41 # TODO named capture variables
42 15 ▶ return if $elem !~ m/\A \$[1-9] \z/xms;
Branch: 1 branch
BranchTrueFalseTotal
unless $elem =~ /\A \$[1-9] \z/msx 15 0 15
43 0 ▶ return if _is_in_conditional_expression($elem);
Branch: 1 branch
BranchTrueFalseTotal
if _is_in_conditional_expression($elem) 0 0 0
44 0 ▶ return if $self->_is_in_conditional_structure($elem);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_in_conditional_structure($elem) 0 0 0
45 0 return $self->violation( $DESC, $EXPL, $elem );
46 }
47  
48 0 ▶sub _is_in_conditional_expression {
Subroutine _is_in_conditional_expression: not called CC=7 SCAR=40.3
49 0 my $elem = shift;
50  
51 # simplistic check: is there a conditional operator between a match and
52 # the capture var?
53 0 my $psib = $elem->sprevious_sibling;
54 0 ▶ while ($psib) {
Branch: 1 branch
BranchTrueFalseTotal
if ($psib) 0 0 0
55 0 ▶ if ($psib->isa('PPI::Token::Operator')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($psib->isa("PPI::Token::Operator")) 0 0 0
56 0 my $op = $psib->content;
57 0 ▶ if ( $CONDITIONAL_OPERATOR{ $op } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($CONDITIONAL_OPERATOR{$op}) 0 0 0
58 0 $psib = $psib->sprevious_sibling;
59 0 ▶ while ($psib) {
Branch: 1 branch
BranchTrueFalseTotal
if ($psib) 0 0 0
60 0 ▶ return 1 if ($psib->isa('PPI::Token::Regexp::Match'));
Branch: 1 branch
BranchTrueFalseTotal
if $psib->isa("PPI::Token::Regexp::Match") 0 0 0
61 0 ▶ return 1 if ($psib->isa('PPI::Token::Regexp::Substitute'));
Branch: 1 branch
BranchTrueFalseTotal
if $psib->isa("PPI::Token::Regexp::Substitute") 0 0 0
62 0 $psib = $psib->sprevious_sibling;
63 }
64 0 return; # false
65 }
66 }
67 0 $psib = $psib->sprevious_sibling;
68 }
69  
70 0 return; # false
71 }
72  
73 0 ▶sub _is_in_conditional_structure {
Subroutine _is_in_conditional_structure: not called CC=14 SCAR=53.5
74 0 my ( $self, $elem ) = @_;
75  
76 0 my $stmt = $elem->statement();
77 0 ▶ while ($stmt && $elem->isa('PPI::Statement::Expression')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stmt and $elem->isa("PPI::Statement::Expression")) 0 0 0
Condition0%0/3
$stmt and $elem->isa("PPI::Statement::Expression")
!ll&&!rl&&r
000
MC/DC0%0/2
$stmt and $elem->isa("PPI::Statement::Expression")
$stmt $elem->isa("PPI::Statement::Expression")
Truth table0%0/3
A and B
A= $stmtB= $elem->isa("PPI::Statement::Expression")
ABresult
0X0
100
111
78 #return if _is_in_conditional_expression($stmt);
79 0 $stmt = $stmt->statement();
80 }
81 0 ▶ return if !$stmt;
Branch: 1 branch
BranchTrueFalseTotal
unless $stmt 0 0 0
82  
83 # Check if any previous statements in the same scope have regexp matches
84 0 my $psib = $stmt->sprevious_sibling;
85 0 ▶ while ($psib) {
Branch: 1 branch
BranchTrueFalseTotal
if ($psib) 0 0 0
86 0 ▶ if ( $psib->isa( 'PPI::Node' ) and
Branch: 1 branch
BranchTrueFalseTotal
if ($psib->isa("PPI::Node") and my $match = _find_exposed_match_or_substitute($psib)) 0 0 0
Condition0%0/3
$psib->isa("PPI::Node") and my $match = _find_exposed_match_or_substitute($psib)
!ll&&!rl&&r
000
MC/DC0%0/2
$psib->isa("PPI::Node") and my $match = _find_exposed_match_or_substitute($psib)
$psib->isa("PPI::Node") my $match = _find_exposed_match_or_substitute($psib)
Truth table0%0/3
A and B
A= $psib->isa("PPI::Node")B= my $match = _find_exposed_match_or_substitute($psib)
ABresult
0X0
100
111
87 my $match = _find_exposed_match_or_substitute( $psib ) ) {
88 0 ▶ return _is_control_transfer_to_left( $self, $match, $elem ) ||
Condition0%0/3
_is_control_transfer_to_left($self, $match, $elem) || _is_control_transfer_to_right($self, $match, $elem)
l!l&&r!l&&!r
000
MC/DC0%0/2
_is_control_transfer_to_left($self, $match, $elem) || _is_control_transfer_to_right($self, $match, $elem)
_is_control_transfer_to_left($self, $match, $elem) _is_control_transfer_to_right($self, $match, $elem)
Truth table0%0/3
A || B
A= _is_control_transfer_to_left($self, $match, $elem)B= _is_control_transfer_to_right($self, $match, $elem)
ABresult
1X1
011
000
89 _is_control_transfer_to_right( $self, $match, $elem );
90 }
91 0 $psib = $psib->sprevious_sibling;
92 }
93  
94 # Check for an enclosing 'if', 'unless', 'elsif', 'else', or 'when'
95 0 my $parent = $stmt->parent;
96 0 ▶ while ($parent) { # never false as long as we're inside a PPI::Document
Branch: 1 branch
BranchTrueFalseTotal
if ($parent) 0 0 0
97 0 ▶ if ($parent->isa('PPI::Statement::Compound') ||
Branch: 2 branches
BranchTrueFalseTotal
if ($parent->isa('PPI::Statement::Compound') or $parent->isa('PPI::Statement::When')) { } 0 0 0
elsif ($parent->isa('PPI::Structure')) { } 0 0 0
Condition0%0/3
$parent->isa("PPI::Statement::Compound") or $parent->isa("PPI::Statement::When")
l!l&&r!l&&!r
000
MC/DC0%0/2
$parent->isa("PPI::Statement::Compound") or $parent->isa("PPI::Statement::When")
$parent->isa("PPI::Statement::Compound") $parent->isa("PPI::Statement::When")
Truth table0%0/3
A or B
A= $parent->isa("PPI::Statement::Compound")B= $parent->isa("PPI::Statement::When")
ABresult
1X1
011
000
98 $parent->isa('PPI::Statement::When' )
99 ) {
100 0 return 1;
101 }
102 elsif ($parent->isa('PPI::Structure')) {
103 0 ▶ return 1 if _is_in_conditional_expression($parent);
Branch: 1 branch
BranchTrueFalseTotal
if _is_in_conditional_expression($parent) 0 0 0
104 0 ▶ return 1 if $self->_is_in_conditional_structure($parent);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_in_conditional_structure($parent) 0 0 0
105 0 $parent = $parent->parent;
106 }
107 else {
108 0 last;
109 }
110 }
111  
112 0 return; # fail
113 }
114  
115 # This subroutine returns true if there is a control transfer to the left of
116 # the match operation which would bypass the capture variable. The arguments
117 # are the match operation and the capture variable.
118 0 ▶sub _is_control_transfer_to_left {
Subroutine _is_control_transfer_to_left: not called CC=9 SCAR=45.0
119 0 my ( $self, $match, $elem ) = @_;
120 # If a regexp match is found, we succeed if a match failure
121 # appears to throw an exception, and fail otherwise. RT 36081
122 0 ▶ my $prev = $match->sprevious_sibling() or return;
Branch: 1 branch
BranchTrueFalseTotal
my $prev = $match->sprevious_sibling or return 0 0 0
123 0 ▶ while ( not ( $prev->isa( 'PPI::Token::Word' ) &&
Branch: 1 branch
BranchTrueFalseTotal
unless ($prev->isa("PPI::Token::Word") and "unless" eq $prev->content) 0 0 0
Condition0%0/3
$prev->isa("PPI::Token::Word") and "unless" eq $prev->content
!ll&&!rl&&r
000
MC/DC0%0/2
$prev->isa("PPI::Token::Word") and "unless" eq $prev->content
$prev->isa("PPI::Token::Word") "unless" eq $prev->content
Truth table0%0/3
A and B
A= $prev->isa("PPI::Token::Word")B= "unless" eq $prev->content
ABresult
0X0
100
111
124 q<unless> eq $prev->content() ) ) {
125 0 ▶ $prev = $prev->sprevious_sibling() or return;
Branch: 1 branch
BranchTrueFalseTotal
$prev = $prev->sprevious_sibling or return 0 0 0
126 }
127 # In this case we analyze the first thing to appear in the parent of the
128 # 'unless'. This is the simplest case, and it will not be hard to dream up
129 # cases where this is insufficient (e.g. do {something(); die} unless ...)
130 0 ▶ my $parent = $prev->parent() or return;
Branch: 1 branch
BranchTrueFalseTotal
my $parent = $prev->parent or return 0 0 0
131 0 ▶ my $first = $parent->schild( 0 ) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $first = $parent->schild(0) or return 0 0 0
132 0 ▶ if ( my $method = _get_method_name( $first ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $method = _get_method_name($first)) 0 0 0
133 # Methods can also be exception sources.
134 0 return $self->{_exception_source}{ $method->content() };
135 }
136 0 ▶ return $self->{_exception_source}{ $first->content() } ||
Condition0%0/3
$self->{'_exception_source'}{$first->content} || _unambiguous_control_transfer($first, $elem)
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'_exception_source'}{$first->content} || _unambiguous_control_transfer($first, $elem)
$self->{'_exception_source'}{$first->content} _unambiguous_control_transfer($first, $elem)
Truth table0%0/3
A || B
A= $self->{'_exception_source'}{$first->content}B= _unambiguous_control_transfer($first, $elem)
ABresult
1X1
011
000
137 _unambiguous_control_transfer( $first, $elem );
138 }
139  
140 # This subroutine returns true if there is a control transfer to the right of
141 # the match operation which would bypass the capture variable. The arguments
142 # are the match operation and the capture variable.
143 0 ▶sub _is_control_transfer_to_right {
Subroutine _is_control_transfer_to_right: not called CC=7 SCAR=40.3
144 0 my ( $self, $match, $elem ) = @_;
145 # If a regexp match is found, we succeed if a match failure
146 # appears to throw an exception, and fail otherwise. RT 36081
147 0 ▶ my $oper = $match->snext_sibling() or return; # fail
Branch: 1 branch
BranchTrueFalseTotal
my $oper = $match->snext_sibling or return 0 0 0
148 0 my $oper_content = $oper->content();
149 # We do not check '//' because a match failure does not
150 # return an undefined value.
151 0 ▶ q{or} eq $oper_content
Branch: 1 branch
BranchTrueFalseTotal
"or" eq $oper_content or "||" eq $oper_content or return 0 0 0
Condition0%0/3
"or" eq $oper_content or "||" eq $oper_content
l!l&&r!l&&!r
000
MC/DC0%0/2
"or" eq $oper_content or "||" eq $oper_content
"or" eq $oper_content "||" eq $oper_content
Truth table0%0/3
A or B
A= "or" eq $oper_contentB= "||" eq $oper_content
ABresult
1X1
011
000
152 or q{||} eq $oper_content
153 or return; # fail
154 0 ▶ my $next = $oper->snext_sibling() or return; # fail
Branch: 1 branch
BranchTrueFalseTotal
my $next = $oper->snext_sibling or return 0 0 0
155 0 ▶ if ( my $method = _get_method_name( $next ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $method = _get_method_name($next)) 0 0 0
156 # Methods can also be exception sources.
157 0 return $self->{_exception_source}{ $method->content() };
158 }
159 0 ▶ return $self->{_exception_source}{ $next->content() } ||
Condition0%0/3
$self->{'_exception_source'}{$next->content} || _unambiguous_control_transfer($next, $elem)
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'_exception_source'}{$next->content} || _unambiguous_control_transfer($next, $elem)
$self->{'_exception_source'}{$next->content} _unambiguous_control_transfer($next, $elem)
Truth table0%0/3
A || B
A= $self->{'_exception_source'}{$next->content}B= _unambiguous_control_transfer($next, $elem)
ABresult
1X1
011
000
160 _unambiguous_control_transfer( $next, $elem );
161 }
162  
163 # Given a PPI::Node, find the last regexp match or substitution that is
164 # in-scope to the node's next sibling.
165 0 ▶sub _find_exposed_match_or_substitute { # RT 36081
Subroutine _find_exposed_match_or_substitute: not called CC=6 SCAR=37.4
166 0 my $elem = shift;
167 FIND_REGEXP_NOT_IN_BLOCK:
168 0 foreach my $regexp ( reverse @{ $elem->find(
169 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
170 0 ▶ return $_[1]->isa( 'PPI::Token::Regexp::Substitute' )
Condition0%0/3
$_[1]->isa('PPI::Token::Regexp::Substitute') || $_[1]->isa('PPI::Token::Regexp::Match')
l!l&&r!l&&!r
000
MC/DC0%0/2
$_[1]->isa('PPI::Token::Regexp::Substitute') || $_[1]->isa('PPI::Token::Regexp::Match')
$_[1]->isa('PPI::Token::Regexp::Substitute') $_[1]->isa('PPI::Token::Regexp::Match')
Truth table0%0/3
A || B
A= $_[1]->isa('PPI::Token::Regexp::Substitute')B= $_[1]->isa('PPI::Token::Regexp::Match')
ABresult
1X1
011
000
171 || $_[1]->isa( 'PPI::Token::Regexp::Match' );
172 }
173 0 ▶ ) || [] } ) {
Condition0%0/2
$elem->find(sub { return $_[1]->isa('PPI::Token::Regexp::Substitute') || $_[1]->isa('PPI::Token::Regexp::Match'); } ) || []
l!l
00
MC/DC0%0/1
$elem->find(sub { return $_[1]->isa('PPI::Token::Regexp::Substitute') || $_[1]->isa('PPI::Token::Regexp::Match'); } ) || []
$elem->find(sub { return $_[1]->isa('PPI::Token::Regexp::Substitute') || $_[1]->isa('PPI::Token::Regexp::Match'); } )
Truth table0%0/2
A
A= $elem->find(sub { return $_[1]->isa('PPI::Token::Regexp::Substitute') || $_[1]->isa('PPI::Token::Regexp::Match'); } )
Aresult
11
00
174 0 my $parent = $regexp->parent();
175 0 ▶ while ( $parent != $elem ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent != $elem) 0 0 0
176 0 ▶ $parent->isa( 'PPI::Structure::Block' )
Branch: 1 branch
BranchTrueFalseTotal
$parent->isa("PPI::Structure::Block") and next FIND_REGEXP_NOT_IN_BLOCK 0 0 0
177 and next FIND_REGEXP_NOT_IN_BLOCK;
178 0 ▶ $parent = $parent->parent()
Branch: 1 branch
BranchTrueFalseTotal
$parent = $parent->parent or next FIND_REGEXP_NOT_IN_BLOCK 0 0 0
179 or next FIND_REGEXP_NOT_IN_BLOCK;
180 }
181 0 return $regexp;
182 }
183 0 return;
184 }
185  
186 # If the argument introduces a method call, return the method name;
187 # otherwise just return.
188 0 ▶sub _get_method_name {
Subroutine _get_method_name: not called CC=15 SCAR=54.8
189 0 my ( $elem ) = @_;
190 # We fail unless the element we were given looks like it might be an
191 # object or a class name.
192 0 ▶ $elem or return;
Branch: 1 branch
BranchTrueFalseTotal
$elem or return 0 0 0
193 (
194 0 ▶ $elem->isa( 'PPI::Token::Symbol' ) &&
Branch: 1 branch
BranchTrueFalseTotal
$elem->isa("PPI::Token::Symbol") and "\$" eq $elem->raw_type or $elem->isa("PPI::Token::Word") and $elem->content =~ / \A [\w:]+ \z /msx or return 0 0 0
Condition0%0/9
$elem->isa("PPI::Token::Symbol") and "\$" eq $elem->raw_type or $elem->isa("PPI::Token::Word") and $elem->content =~ / \A [\w:]+ \z /msx
l!l&&r!l&&!r
000
$elem->isa("PPI::Token::Symbol") and "\$" eq $elem->raw_type
!ll&&!rl&&r
000
$elem->isa("PPI::Token::Word") and $elem->content =~ / \A [\w:]+ \z /msx
!ll&&!rl&&r
000
MC/DC0%0/4
$elem->isa("PPI::Token::Symbol") and "\$" eq $elem->raw_type or $elem->isa("PPI::Token::Word") and $elem->content =~ / \A [\w:]+ \z /msx
$elem->isa("PPI::Token::Symbol") "\$" eq $elem->raw_type $elem->isa("PPI::Token::Word") $elem->content =~ / \A [\w:]+ \z /msx
Truth table0%0/7
A and B or C and D
A= $elem->isa("PPI::Token::Symbol")B= "\$" eq $elem->raw_typeC= $elem->isa("PPI::Token::Word")D= $elem->content =~ / \A [\w:]+ \z /msx
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
195 q<$> eq $elem->raw_type() ||
196 $elem->isa( 'PPI::Token::Word' ) &&
197 $elem->content() =~ m/ \A [\w:]+ \z /smx
198 ) or return;
199 # We skip over all the subscripts and '->' operators to the right of
200 # the original element, failing if we run out of objects.
201 0 my $prior;
202 0 ▶ my $next = $elem->snext_sibling() or return;
Branch: 1 branch
BranchTrueFalseTotal
my $next = $elem->snext_sibling or return 0 0 0
203 0 ▶ while ( $next->isa( 'PPI::Token::Subscript' ) ||
Branch: 1 branch
BranchTrueFalseTotal
if ($next->isa("PPI::Token::Subscript") or $next->isa("PPI::Token::Operator") and "->" eq $next->content) 0 0 0
Condition0%0/6
$next->isa("PPI::Token::Subscript") or $next->isa("PPI::Token::Operator") and "->" eq $next->content
l!l&&r!l&&!r
000
$next->isa("PPI::Token::Operator") and "->" eq $next->content
!ll&&!rl&&r
000
MC/DC0%0/3
$next->isa("PPI::Token::Subscript") or $next->isa("PPI::Token::Operator") and "->" eq $next->content
$next->isa("PPI::Token::Subscript") $next->isa("PPI::Token::Operator") "->" eq $next->content
Truth table0%0/4
A or B and C
A= $next->isa("PPI::Token::Subscript")B= $next->isa("PPI::Token::Operator")C= "->" eq $next->content
ABCresult
1XX1
0111
00X0
0100
204 $next->isa( 'PPI::Token::Operator' ) &&
205 q{->} eq $next->content() ) {
206 0 $prior = $next;
207 0 ▶ $next = $next->snext_sibling or return; # fail
Branch: 1 branch
BranchTrueFalseTotal
$next = $next->snext_sibling or return 0 0 0
208 }
209 # A method call must have a '->' operator before it.
210 0 ▶ ( $prior &&
Branch: 1 branch
BranchTrueFalseTotal
$prior and $prior->isa("PPI::Token::Operator") and "->" eq $prior->content or return 0 0 0
Condition0%0/6
$prior and $prior->isa("PPI::Token::Operator") and "->" eq $prior->content
!ll&&!rl&&r
000
$prior and $prior->isa("PPI::Token::Operator")
!ll&&!rl&&r
000
MC/DC0%0/3
$prior and $prior->isa("PPI::Token::Operator") and "->" eq $prior->content
$prior $prior->isa("PPI::Token::Operator") "->" eq $prior->content
Truth table0%0/4
A and B and C
A= $priorB= $prior->isa("PPI::Token::Operator")C= "->" eq $prior->content
ABCresult
0XX0
10X0
1100
1111
211 $prior->isa( 'PPI::Token::Operator' ) &&
212 q{->} eq $prior->content()
213 ) or return;
214 # Anything other than a PPI::Token::Word can not be statically
215 # recognized as a method name.
216 0 ▶ $next->isa( 'PPI::Token::Word' ) or return;
Branch: 1 branch
BranchTrueFalseTotal
$next->isa("PPI::Token::Word") or return 0 0 0
217 # Whatever we have left at this point looks very like a method name.
218 0 return $next;
219 }
220  
221 # Determine whether the given element represents an unambiguous transfer of
222 # control around anything that follows it in the same block. The arguments are
223 # the element to check, and the capture variable that is the subject of this
224 # call to the policy.
225 0 ▶sub _unambiguous_control_transfer { # RT 36081.
Subroutine _unambiguous_control_transfer: not called CC=3 SCAR=24.8
226 0 my ( $xfer, $elem ) = @_;
227  
228 0 my $content = $xfer->content();
229  
230 # Anything in the hash is always a transfer of control.
231 0 ▶ return $TRUE if $UNAMBIGUOUS_CONTROL_TRANSFER{ $content };
Branch: 1 branch
BranchTrueFalseTotal
if $UNAMBIGUOUS_CONTROL_TRANSFER{$content} 0 0 0
232  
233 # A goto is not unambiguous on the face of it, but at least some forms of
234 # it can be accepted.
235 0 ▶ q<goto> eq $content
Branch: 1 branch
BranchTrueFalseTotal
"goto" eq $content and return _unambiguous_goto($xfer, $elem) 0 0 0
236 and return _unambiguous_goto( $xfer, $elem );
237  
238 # Anything left at this point is _not_ an unambiguous transfer of control
239 # around whatever follows it.
240 0 return;
241 }
242  
243 # Determine whether the given goto represents an unambiguous transfer of
244 # control around anything that follows it in the same block. The arguments are
245 # the element to check, and the capture variable that is the subject of this
246 # call to the policy.
247 0 ▶sub _unambiguous_goto {
Subroutine _unambiguous_goto: not called CC=20 SCAR=60.4
248 0 my ( $xfer, $elem ) = @_;
249  
250 # A goto without a target?
251 0 ▶ my $target = $xfer->snext_sibling() or return;
Branch: 1 branch
BranchTrueFalseTotal
my $target = $xfer->snext_sibling or return 0 0 0
252  
253 # The co-routine form of goto is an unambiguous transfer of control.
254 0 ▶ $target->isa( 'PPI::Token::Symbol' )
Branch: 1 branch
BranchTrueFalseTotal
$target->isa("PPI::Token::Symbol") and "&" eq $target->raw_type and return $Perl::Critic::Policy::RegularExpressions::ProhibitCaptureWithoutTest::TRUE 0 0 0
Condition0%0/3
$target->isa("PPI::Token::Symbol") and "&" eq $target->raw_type
!ll&&!rl&&r
000
MC/DC0%0/2
$target->isa("PPI::Token::Symbol") and "&" eq $target->raw_type
$target->isa("PPI::Token::Symbol") "&" eq $target->raw_type
Truth table0%0/3
A and B
A= $target->isa("PPI::Token::Symbol")B= "&" eq $target->raw_type
ABresult
0X0
100
111
255 and q<&> eq $target->raw_type()
256 and return $TRUE;
257  
258 # The label form of goto is an unambiguous transfer of control,
259 # provided the label does not occur between the goto and the capture
260 # variable.
261 0 ▶ if ( $target->isa( 'PPI::Token::Word' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($target->isa("PPI::Token::Word")) 0 0 0
262  
263 # We need to search in our most-local block, or the document if
264 # there is no enclosing block.
265 0 my $container = $target;
266 0 ▶ while ( my $parent = $container->parent() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $parent = $container->parent) 0 0 0
267 0 $container = $parent;
268 0 ▶ $container->isa( 'PPI::Structure::Block' ) and last;
Branch: 1 branch
BranchTrueFalseTotal
$container->isa("PPI::Structure::Block") and last 0 0 0
269 }
270  
271 # We search the container for our label. If we find it, we return
272 # true if it occurs before the goto or after the capture variable,
273 # otherwise we return false. If we do not find it we return true.
274 # Note that perl does not seem to consider duplicate labels an
275 # error, but also seems to take the first one in the relevant
276 # scope when this happens.
277 0 my $looking_for = qr/ \A @{[ $target->content() ]} \s* : \z /smx;
278 0 ▶ my ($start_line, $start_char) = @{ $xfer->location() || [] };
Condition0%0/2
$xfer->location || []
l!l
00
MC/DC0%0/1
$xfer->location || []
$xfer->location
Truth table0%0/2
A
A= $xfer->location
Aresult
11
00
279 0 ▶ defined $start_line or return; # document not indexed.
Branch: 1 branch
BranchTrueFalseTotal
defined $start_line or return 0 0 0
280 0 ▶ my ($end_line, $end_char) = @{ $elem->location() || [] };
Condition0%0/2
$elem->location || []
l!l
00
MC/DC0%0/1
$elem->location || []
$elem->location
Truth table0%0/2
A
A= $elem->location
Aresult
11
00
281 0 foreach my $label (
282 0 ▶ @{ $container->find( 'PPI::Token::Label' ) || [] } )
Condition0%0/2
$container->find('PPI::Token::Label') || []
l!l
00
MC/DC0%0/1
$container->find('PPI::Token::Label') || []
$container->find('PPI::Token::Label')
Truth table0%0/2
A
A= $container->find('PPI::Token::Label')
Aresult
11
00
283 {
284 0 ▶ $label->content() =~ m/$looking_for/smx or next;
Branch: 1 branch
BranchTrueFalseTotal
$label->content =~ /$looking_for/msx or next 0 0 0
285 0 ▶ my ( $line, $char ) = @{ $label->location() || [] };
Condition0%0/2
$label->location || []
l!l
00
MC/DC0%0/1
$label->location || []
$label->location
Truth table0%0/2
A
A= $label->location
Aresult
11
00
286 0 ▶ return $TRUE
Branch: 1 branch
BranchTrueFalseTotal
if $line < $start_line or $line == $start_line and $char < $start_char 0 0 0
Condition0%0/6
$line < $start_line or $line == $start_line and $char < $start_char
l!l&&r!l&&!r
000
$line == $start_line and $char < $start_char
!ll&&!rl&&r
000
MC/DC0%0/3
$line < $start_line or $line == $start_line and $char < $start_char
$line < $start_line $line == $start_line $char < $start_char
Truth table0%0/4
A or B and C
A= $line < $start_lineB= $line == $start_lineC= $char < $start_char
ABCresult
1XX1
0111
00X0
0100
287 if $line < $start_line ||
288 $line == $start_line && $char < $start_char;
289 0 ▶ return $TRUE
Branch: 1 branch
BranchTrueFalseTotal
if $line > $end_line or $line == $end_line and $char > $end_char 0 0 0
Condition0%0/6
$line > $end_line or $line == $end_line and $char > $end_char
l!l&&r!l&&!r
000
$line == $end_line and $char > $end_char
!ll&&!rl&&r
000
MC/DC0%0/3
$line > $end_line or $line == $end_line and $char > $end_char
$line > $end_line $line == $end_line $char > $end_char
Truth table0%0/4
A or B and C
A= $line > $end_lineB= $line == $end_lineC= $char > $end_char
ABCresult
1XX1
0111
00X0
0100
290 if $line > $end_line ||
291 $line == $end_line && $char > $end_char;
292 0 return;
293 }
294 0 return $TRUE;
295 }
296  
297 # Any other form of goto can not be statically analyzed, and so is not
298 # an unambiguous transfer of control around the capture variable.
299 0 return;
300 }
301  
302 40 1;
303  
304 #-----------------------------------------------------------------------------
305  
306 __END__
↑ Summary lib/Perl/Critic/Policy/ValuesAndExpressions/RequireConstantVersion.pm »