blib/lib/Perl/Critic/Policy/Variables/RequireNoAutovivification.pm

filter: Statementstmt 99.6% 265 / 266 Branchbran 95.1% 158 / 166 Conditioncond 78.0% 89 / 114 MC/DCmcdc 62.6% 42 / 67 Subroutinesub 97.4% 38 / 39 Podpod 100% 5 / 5 totaltotal 90.8% 597 / 657 CC 127 SCAR 22.0
CC
127
Cov
94%
_governed
15.2
_reads_through_element
14.1
_context
13.1
CRAP 9.0
↑ Summary
1 package Perl::Critic::Policy::Variables::RequireNoAutovivification 0.001;
2  
3 # ABSTRACT: Say no autovivification where a read through an element would create it.
4  
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 ▶use re '/aa';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 ▶use List::Util qw{ any };
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Scalar::Util qw{ refaddr };
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 2 ▶use Perl::Critic::Utils qw{ :severities is_assignment_operator is_function_call is_perl_builtin };
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use parent qw{Perl::Critic::Policy};
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19  
20 2 Readonly::Scalar my $DESC => q{Read through an element without "no autovivification"};
21  
22 2 Readonly::Hash my %EXPL => (
23 fetch => q{Say "no autovivification;" in this scope: a read through an element creates the element when it is missing ('fetch')},
24 exists => q{Say "no autovivification;" in this scope: exists creates every level above the one it tests ('exists')},
25 delete => q{Say "no autovivification;" in this scope: delete creates every level above the one it deletes ('delete')},
26 );
27  
28 2 Readonly::Scalar my $PRAGMA => 'autovivification';
29  
30 # What "no autovivification" turns on without arguments, and every category
31 # that "use autovivification" can take away.
32 2 Readonly::Array my @DEFAULT_CATEGORIES => qw{ fetch exists delete };
33 2 Readonly::Hash my %CATEGORY => map { $_ => 1 } @DEFAULT_CATEGORIES, 'store';
34  
35 # Builtins whose arguments are all lvalues, so perl creates the levels above
36 # them whatever the pragma says. Perl passes the arguments of sprintf that way too.
37 2 Readonly::Hash my %CHANGES_EVERY_ARGUMENT => map { $_ => 1 } qw{ chomp chop sprintf local map grep };
38  
39 # Builtins whose first argument is an lvalue, and the rest are read.
40 2 Readonly::Hash my %CHANGES_FIRST_ARGUMENT => map { $_ => 1 } qw{
41 push unshift splice pop shift each pos undef vec
42 read sysread recv open opendir sysopen tie untie tied lock
43 };
44  
45 # Builtins that take one argument and need a category of their own for it.
46 2 Readonly::Hash my %OWN_CATEGORY => ( exists => 'exists', delete => 'delete', keys => 'fetch', values => 'fetch' );
47  
48 # Statement modifiers, which is_function_call does not count as calls.
49 2 Readonly::Hash my %MODIFIER => ( if => 'fetch', unless => 'fetch', while => 'fetch', until => 'fetch', for => 'lvalue', foreach => 'lvalue' );
50  
51 2 Readonly::Hash my %STEP => map { $_ => 1 } qw{ ++ -- };
52 2 Readonly::Hash my %BINDING => map { $_ => 1 } qw{ =~ !~ };
53 2 Readonly::Hash my %COMMA => map { $_ => 1 } ( q{,}, '=>' );
54 2 Readonly::Hash my %POSTFIX => map { $_ => 1 } qw{ @* %* };
55 2 Readonly::Hash my %AGGREGATE_CAST => map { $_ => 1 } qw{ @ % };
56  
57  
58 3 ▶sub supported_parameters {
Subroutine supported_parameters: called CC=1 SCAR=0.0
Pod documented
59 return (
60 {
61 3 name => 'equivalent_modules',
62 description => q{Modules whose use counts as "no autovivification"},
63 default_string => q{},
64 behavior => 'string list',
65 },
66 );
67 }
68  
69 63 ▶sub default_severity { return $SEVERITY_MEDIUM }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
70 0 ▶sub default_themes { return qw{ bugs } }
Subroutine default_themes: not called CC=1 SCAR=6.9
Pod documented
71 130 ▶sub applies_to { return 'PPI::Document' }
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
72  
73  
74 130 ▶sub violates {
Subroutine violates: called CC=2 SCAR=6.9
Pod documented
75 130 my ( $self, $elem, $doc ) = @_;
76  
77 130 ▶ my @hazards = map { _hazard($_) } _terms($doc) or return;
Branch: 1 branch
BranchTrueFalseTotal
my(@hazards) = map({_hazard($_);} _terms($doc)) or return 85 45 130
78  
79 85 my @pragmas = $self->_pragmas($doc);
80 85 @hazards = grep { !_is_covered( $_, \@pragmas ) } @hazards;
81  
82 # A read inside a cast is part of the expression that the cast is, so only
83 # the cast is reported when both are.
84 85 my %blocks = map { refaddr($_) => 1 } grep { $_->isa('PPI::Structure::Block') } map { @{ $_->{pieces} } } @hazards;
85 85 @hazards = grep { !_is_inside( $_->{pieces}[0], \%blocks ) } @hazards;
86  
87 85 return map { $self->violation( $DESC, $EXPL{ $_->{category} }, $_->{pieces}[0] ) } @hazards;
88 }
89  
90 # Every term in $doc: the pieces of one variable, cast or call, and the
91 # subscripts, arrows and method calls after it, in order.
92 130 ▶sub _terms {
Subroutine _terms: called CC=2 SCAR=7.0
93 130 my ($doc) = @_;
94  
95 # 0 rather than undef for an element that is not a head: undef would tell
96 # find not to look inside it.
97 130 ▶ my $heads = $doc->find( sub { _is_head( $_[1] ) ? 1 : 0 } ) or return;
Subroutine __ANON__: called CC=2 SCAR=7.0
Branch: 2 branches
BranchTrueFalseTotal
my $heads = $doc->find(sub { _is_head($_[1]) ? 1 : 0; } ) or return 130 0 130
_is_head($_[1]) ? : 259 2932 3191
98 130 return map { [ _term_from($_) ] } @$heads;
99 }
100  
101 # Whether $elem starts a term. A symbol after a cast is part of the cast's
102 # term, and a cast after an arrow is part of the term before it.
103 3212 ▶sub _is_head {
Subroutine _is_head: called CC=10 SCAR=23.1
104 3212 my ($elem) = @_;
105  
106 3212 ▶ if ( $elem->isa('PPI::Token::Symbol') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem->isa("PPI::Token::Symbol")) 257 2955 3212
107 257 return !_is_dereference( $elem->sprevious_sibling );
108 }
109 2955 ▶ if ( _is_dereference($elem) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_dereference($elem)) 25 2930 2955
110 25 ▶ return if _is_operator( $elem->sprevious_sibling, '->' );
Branch: 1 branch
BranchTrueFalseTotal
if _is_operator($elem->sprevious_sibling, "->") 2 23 25
111 23 my $next = $elem->snext_sibling;
112 23 ▶ return $next && ( $next->isa('PPI::Token::Symbol') || $next->isa('PPI::Structure::Block') );
Condition50%3/6
$next && ($next->isa('PPI::Token::Symbol') || $next->isa('PPI::Structure::Block'))
!ll&&!rl&&r
0023
$next->isa("PPI::Token::Symbol") or $next->isa("PPI::Structure::Block")
l!l&&r!l&&!r
2210
MC/DC0%0/3
$next && ($next->isa('PPI::Token::Symbol') || $next->isa('PPI::Structure::Block'))
$next $next->isa("PPI::Token::Symbol") $next->isa("PPI::Structure::Block")
Truth table50%2/4
A && B or C
A= $nextB= $next->isa("PPI::Token::Symbol")C= $next->isa("PPI::Structure::Block")
ABCresult
0XX0
1000
11X1
1011
113 }
114 2930 ▶ if ( $elem->isa('PPI::Token::Word') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem->isa("PPI::Token::Word")) 507 2423 2930
115 507 ▶ return unless is_function_call($elem);
Branch: 1 branch
BranchTrueFalseTotal
unless is_function_call($elem) 342 165 507
116 165 my $list = $elem->snext_sibling;
117 165 ▶ return $list && $list->isa('PPI::Structure::List') && _is_operator( $list->snext_sibling, '->' );
Condition100%6/6
$list && $list->isa('PPI::Structure::List') && _is_operator($list->snext_sibling, '->')
!ll&&!rl&&r
146172
$list and $list->isa("PPI::Structure::List")
!ll&&!rl&&r
114519
MC/DC100%3/3
$list && $list->isa('PPI::Structure::List') && _is_operator($list->snext_sibling, '->')
$list $list->isa("PPI::Structure::List") _is_operator($list->snext_sibling, '->')
Truth table100%4/4
A and B && C
A= $listB= $list->isa("PPI::Structure::List")C= _is_operator($list->snext_sibling, '->')
ABCresult
0XX0
10X0
1100
1111
118 }
119 2423 return;
120 }
121  
122 # The pieces of the term that starts at $head.
123 288 ▶sub _term_from {
Subroutine _term_from: called CC=12 SCAR=24.9
124 288 my ($head) = @_;
125  
126 288 my @pieces = ($head);
127 288 ▶ push @pieces, $head->snext_sibling unless $head->isa('PPI::Token::Symbol');
Branch: 1 branch
BranchTrueFalseTotal
unless $head->isa("PPI::Token::Symbol") 33 255 288
128  
129 288 ▶ while ( my $next = $pieces[-1]->snext_sibling ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $next = $pieces[-1]->snext_sibling) 483 76 559
130 483 ▶ if ( $next->isa('PPI::Structure::Subscript') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next->isa("PPI::Structure::Subscript")) 254 229 483
131 254 push @pieces, $next;
132 254 next;
133 }
134 229 ▶ last unless _is_operator( $next, '->' );
Branch: 1 branch
BranchTrueFalseTotal
unless _is_operator($next, "->") 209 20 229
135  
136 20 ▶ my $after = $next->snext_sibling or last;
Branch: 1 branch
BranchTrueFalseTotal
my $after = $next->snext_sibling or last 20 0 20
137 20 ▶ if ( $after->isa('PPI::Structure::Subscript') || $after->isa('PPI::Token::Cast') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($after->isa("PPI::Structure::Subscript") or $after->isa("PPI::Token::Cast")) 15 5 20
Condition100%3/3
$after->isa("PPI::Structure::Subscript") or $after->isa("PPI::Token::Cast")
l!l&&r!l&&!r
1325
MC/DC100%2/2
$after->isa("PPI::Structure::Subscript") or $after->isa("PPI::Token::Cast")
$after->isa("PPI::Structure::Subscript") $after->isa("PPI::Token::Cast")
Truth table100%3/3
A or B
A= $after->isa("PPI::Structure::Subscript")B= $after->isa("PPI::Token::Cast")
ABresult
1X1
011
000
138 15 push @pieces, $next, $after;
139 15 next;
140 }
141 5 ▶ last unless $after->isa('PPI::Token::Word');
Branch: 1 branch
BranchTrueFalseTotal
unless $after->isa("PPI::Token::Word") 1 4 5
142  
143 # A method call. Its arguments are part of the term, and the arrow
144 # after them is what decides whether the term goes on.
145 4 my $args = $after->snext_sibling;
146 4 my @call = ( $next, $after );
147 4 ▶ push @call, $args if $args && $args->isa('PPI::Structure::List');
Branch: 1 branch
BranchTrueFalseTotal
if $args and $args->isa("PPI::Structure::List") 1 3 4
Condition66%2/3
$args and $args->isa("PPI::Structure::List")
!ll&&!rl&&r
031
MC/DC50%1/2
$args and $args->isa("PPI::Structure::List")
$args $args->isa("PPI::Structure::List")
Truth table66%2/3
A and B
A= $argsB= $args->isa("PPI::Structure::List")
ABresult
0X0
100
111
148 4 ▶ last unless _is_operator( $call[-1]->snext_sibling, '->' );
Branch: 1 branch
BranchTrueFalseTotal
unless _is_operator($call[-1]->snext_sibling, "->") 2 2 4
149 2 push @pieces, @call;
150 }
151 288 return @pieces;
152 }
153  
154 # The read that the term @$pieces is, with the category it needs, or nothing
155 # when it is not a read through an element.
156 259 ▶sub _hazard {
Subroutine _hazard: called CC=5 SCAR=16.1
157 259 my ($pieces) = @_;
158  
159 259 ▶ my $through = _reads_through_element($pieces) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $through = _reads_through_element($pieces) or return 126 133 259
160 126 my $context = _context($pieces);
161 126 ▶ return if $context eq 'lvalue';
Branch: 1 branch
BranchTrueFalseTotal
if $context eq "lvalue" 36 90 126
162  
163 # Of an aggregate that an element refers to, only keys and values vivify:
164 # anything else that reads it dies under strict refs.
165 90 ▶ if ( $context eq 'aggregate' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($context eq 'aggregate') { } 7 83 90
elsif ($through eq 'deref') { } 2 81 83
166 7 $context = 'fetch';
167 }
168 elsif ( $through eq 'deref' ) {
169 2 return;
170 }
171 88 return { pieces => $pieces, category => $context };
172 }
173  
174 # 'subscript' when a subscript in @$pieces is applied to an element, 'deref'
175 # when the term is an aggregate dereference of an element, and false
176 # otherwise.
177 259 ▶sub _reads_through_element {
Subroutine _reads_through_element: called CC=14 SCAR=26.4
178 259 my ($pieces) = @_;
179  
180 259 my ( $head, @rest ) = @$pieces;
181  
182 259 my $element = 0;
183 259 ▶ if ( $head->isa('PPI::Token::Cast') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($head->isa("PPI::Token::Cast")) 23 236 259
184 23 my $inner = shift @rest;
185 23 ▶ if ( $inner->isa('PPI::Structure::Block') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($inner->isa("PPI::Structure::Block")) 21 2 23
186 21 $element = _holds_element($inner);
187 21 ▶ return $element && $AGGREGATE_CAST{ $head->content } ? 'deref' : 0 unless @rest;
Branch: 2 branches
BranchTrueFalseTotal
unless @rest 17 4 21
$element && $AGGREGATE_CAST{$head->content} ? : 16 1 17
Condition66%2/3
$element and $AGGREGATE_CAST{$head->content}
!ll&&!rl&&r
1016
MC/DC50%1/2
$element and $AGGREGATE_CAST{$head->content}
$element $AGGREGATE_CAST{$head->content}
Truth table66%2/3
A and B
A= $elementB= $AGGREGATE_CAST{$head->content}
ABresult
0X0
100
111
188 }
189 }
190  
191 242 foreach my $index ( 0 .. $#rest ) {
192 260 my $piece = $rest[$index];
193 260 ▶ if ( $piece->isa('PPI::Structure::Subscript') ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($piece->isa('PPI::Structure::Subscript')) { } 238 22 260
elsif ($piece->isa('PPI::Token::Cast')) { } 2 20 22
elsif (not _is_operator($piece, '->')) { } 4 16 20
194 238 ▶ return 'subscript' if $element;
Branch: 1 branch
BranchTrueFalseTotal
if $element 109 129 238
195 129 $element = 1;
196 }
197 elsif ( $piece->isa('PPI::Token::Cast') ) {
198 2 ▶ return 'deref' if $element && $POSTFIX{ $piece->content } && $index == $#rest;
Branch: 1 branch
BranchTrueFalseTotal
if $element and $POSTFIX{$piece->content} and $index == $#rest 1 1 2
Condition66%4/6
$element and $POSTFIX{$piece->content} and $index == $#rest
!ll&&!rl&&r
101
$element and $POSTFIX{$piece->content}
!ll&&!rl&&r
011
MC/DC33%1/3
$element and $POSTFIX{$piece->content} and $index == $#rest
$element $POSTFIX{$piece->content} $index == $#rest
Truth table50%2/4
A and B and C
A= $elementB= $POSTFIX{$piece->content}C= $index == $#rest
ABCresult
0XX0
10X0
1100
1111
199 }
200 elsif ( !_is_operator( $piece, '->' ) ) {
201 4 $element = 0;
202 }
203 }
204 132 return 0;
205 }
206  
207 # Whether $block holds one term, and that term ends in a subscript.
208 21 ▶sub _holds_element {
Subroutine _holds_element: called CC=5 SCAR=17.3
209 21 my ($block) = @_;
210  
211 21 my @statements = $block->schildren;
212 21 ▶ return 0 unless @statements == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @statements == 1 0 21 21
213  
214 21 my @children = $statements[0]->schildren;
215 21 ▶ return 0 unless @children && _is_head( $children[0] );
Branch: 1 branch
BranchTrueFalseTotal
unless @children and _is_head($children[0]) 0 21 21
Condition33%1/3
@children and _is_head($children[0])
!ll&&!rl&&r
0021
MC/DC0%0/2
@children and _is_head($children[0])
@children _is_head($children[0])
Truth table33%1/3
A and B
A= @childrenB= _is_head($children[0])
ABresult
0X0
100
111
216  
217 21 my @pieces = _term_from( $children[0] );
218 21 ▶ return @pieces == @children && $pieces[-1]->isa('PPI::Structure::Subscript');
Condition66%2/3
@pieces == @children && $pieces[-1]->isa('PPI::Structure::Subscript')
!ll&&!rl&&r
0219
MC/DC50%1/2
@pieces == @children && $pieces[-1]->isa('PPI::Structure::Subscript')
@pieces == @children $pieces[-1]->isa('PPI::Structure::Subscript')
Truth table66%2/3
A && B
A= @pieces == @childrenB= $pieces[-1]->isa('PPI::Structure::Subscript')
ABresult
0X0
100
111
219 }
220  
221 # How the term @$pieces is used: 'lvalue', 'fetch', 'aggregate' for the
222 # argument of keys or values, 'exists' or 'delete'.
223 139 ▶sub _context {
Subroutine _context: called CC=13 SCAR=25.7
224 139 my ($pieces) = @_;
225  
226 139 my $next = $pieces->[-1]->snext_sibling;
227 139 ▶ if ($next) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next) 102 37 139
228 102 ▶ return 'lvalue' if is_assignment_operator($next) || _is_step($next);
Branch: 1 branch
BranchTrueFalseTotal
if is_assignment_operator($next) or _is_step($next) 8 94 102
Condition100%3/3
is_assignment_operator($next) or _is_step($next)
l!l&&r!l&&!r
7194
MC/DC100%2/2
is_assignment_operator($next) or _is_step($next)
is_assignment_operator($next) _is_step($next)
Truth table100%3/3
A or B
A= is_assignment_operator($next)B= _is_step($next)
ABresult
1X1
011
000
229 94 ▶ return 'lvalue' if _is_binding($next) && _changes( $next->snext_sibling );
Branch: 1 branch
BranchTrueFalseTotal
if _is_binding($next) and _changes($next->snext_sibling) 2 92 94
Condition100%3/3
_is_binding($next) and _changes($next->snext_sibling)
!ll&&!rl&&r
9112
MC/DC100%2/2
_is_binding($next) and _changes($next->snext_sibling)
_is_binding($next) _changes($next->snext_sibling)
Truth table100%3/3
A and B
A= _is_binding($next)B= _changes($next->snext_sibling)
ABresult
0X0
100
111
230  
231 92 my $method = $next->snext_sibling;
232 92 ▶ return 'lvalue' if _is_operator( $next, '->' ) && $method && $method->isa('PPI::Token::Word');
Branch: 1 branch
BranchTrueFalseTotal
if _is_operator($next, "->") and $method and $method->isa("PPI::Token::Word") 1 91 92
Condition66%4/6
_is_operator($next, "->") and $method and $method->isa("PPI::Token::Word")
!ll&&!rl&&r
9101
_is_operator($next, "->") and $method
!ll&&!rl&&r
9101
MC/DC33%1/3
_is_operator($next, "->") and $method and $method->isa("PPI::Token::Word")
_is_operator($next, "->") $method $method->isa("PPI::Token::Word")
Truth table50%2/4
A and B and C
A= _is_operator($next, "->")B= $methodC= $method->isa("PPI::Token::Word")
ABCresult
0XX0
10X0
1100
1111
233 }
234  
235 128 my $prev = $pieces->[0]->sprevious_sibling;
236 128 ▶ if ($prev) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prev) 98 30 128
237 98 ▶ return 'lvalue' if $prev->isa('PPI::Token::Cast') && $prev->content eq q{\\};
Branch: 1 branch
BranchTrueFalseTotal
if $prev->isa("PPI::Token::Cast") and $prev->content eq "\\" 1 97 98
Condition66%2/3
$prev->isa("PPI::Token::Cast") and $prev->content eq "\\"
!ll&&!rl&&r
9701
MC/DC50%1/2
$prev->isa("PPI::Token::Cast") and $prev->content eq "\\"
$prev->isa("PPI::Token::Cast") $prev->content eq "\\"
Truth table66%2/3
A and B
A= $prev->isa("PPI::Token::Cast")B= $prev->content eq "\\"
ABresult
0X0
100
111
238 97 ▶ return 'lvalue' if _is_step($prev);
Branch: 1 branch
BranchTrueFalseTotal
if _is_step($prev) 1 96 97
239 }
240 126 return _governed($pieces);
241 }
242  
243 # The context that what comes before the term in its statement gives it, or
244 # else the context of the parens or cast around the statement.
245 126 ▶sub _governed {
Subroutine _governed: called CC=15 SCAR=27.2
246 126 my ($pieces) = @_;
247  
248 126 my $first = $pieces->[0];
249 126 my $commas = 0;
250 126 ▶ for ( my $prev = $first->sprevious_sibling; $prev; $prev = $prev->sprevious_sibling ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prev) 107 31 138
251 107 ▶ if ( $prev->isa('PPI::Token::Operator') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prev->isa("PPI::Token::Operator")) 53 54 107
252 53 ▶ return 'fetch' unless $COMMA{ $prev->content };
Branch: 1 branch
BranchTrueFalseTotal
unless $COMMA{$prev->content} 49 4 53
253 4 $commas++;
254 4 next;
255 }
256 54 ▶ next unless $prev->isa('PPI::Token::Word');
Branch: 1 branch
BranchTrueFalseTotal
unless $prev->isa("PPI::Token::Word") 7 47 54
257  
258 47 my $modifier = $MODIFIER{ $prev->content };
259 47 ▶ return $modifier if $modifier;
Branch: 1 branch
BranchTrueFalseTotal
if $modifier 3 44 47
260 44 ▶ next unless is_function_call($prev);
Branch: 1 branch
BranchTrueFalseTotal
unless is_function_call($prev) 1 43 44
261  
262 43 my $immediate = refaddr($prev) == refaddr( $first->sprevious_sibling );
263 43 return _argument_of( $prev, $immediate, $commas, [ $prev, @$pieces ] );
264 }
265  
266 31 my $statement = $first->parent;
267 31 ▶ my $container = $statement && $statement->parent or return 'fetch';
Branch: 1 branch
BranchTrueFalseTotal
my $container = $statement && $statement->parent or return "fetch" 31 0 31
Condition33%1/3
$statement && $statement->parent
!ll&&!rl&&r
0031
MC/DC0%0/2
$statement && $statement->parent
$statement $statement->parent
Truth table33%1/3
A && B
A= $statementB= $statement->parent
ABresult
0X0
100
111
268 31 my $whole = !$first->sprevious_sibling;
269  
270 31 ▶ if ( $container->isa('PPI::Structure::List') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($container->isa("PPI::Structure::List")) 18 13 31
271 18 my $compound = $container->parent;
272 18 ▶ return 'lvalue' if $compound->isa('PPI::Statement::Compound') && $compound->type eq 'foreach';
Branch: 1 branch
BranchTrueFalseTotal
if $compound->isa("PPI::Statement::Compound") and $compound->type eq "foreach" 3 15 18
Condition66%2/3
$compound->isa("PPI::Statement::Compound") and $compound->type eq "foreach"
!ll&&!rl&&r
1503
MC/DC50%1/2
$compound->isa("PPI::Statement::Compound") and $compound->type eq "foreach"
$compound->isa("PPI::Statement::Compound") $compound->type eq "foreach"
Truth table66%2/3
A and B
A= $compound->isa("PPI::Statement::Compound")B= $compound->type eq "foreach"
ABresult
0X0
100
111
273 15 return _argument_of_list( $container, $whole, $commas );
274 }
275 13 ▶ if ( $container->isa('PPI::Structure::Block') && $container->schildren == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($container->isa("PPI::Structure::Block") and $container->schildren == 1) 10 3 13
Condition66%2/3
$container->isa("PPI::Structure::Block") and $container->schildren == 1
!ll&&!rl&&r
3010
MC/DC50%1/2
$container->isa("PPI::Structure::Block") and $container->schildren == 1
$container->isa("PPI::Structure::Block") $container->schildren == 1
Truth table66%2/3
A and B
A= $container->isa("PPI::Structure::Block")B= $container->schildren == 1
ABresult
0X0
100
111
276 10 my $cast = $container->sprevious_sibling;
277 10 ▶ return _context( [ _term_from($cast) ] ) if _is_dereference($cast);
Branch: 1 branch
BranchTrueFalseTotal
if _is_dereference($cast) 8 2 10
278 }
279 5 return 'fetch';
280 }
281  
282 # The context of a statement in the parens $list. $whole is whether the term
283 # is all of the statement, and $commas how many arguments are before it.
284 15 ▶sub _argument_of_list {
Subroutine _argument_of_list: called CC=12 SCAR=25.2
285 15 my ( $list, $whole, $commas ) = @_;
286  
287 15 my $before = $list->sprevious_sibling;
288 15 ▶ if ($before) {
Branch: 1 branch
BranchTrueFalseTotal
if ($before) 12 3 15
289 12 ▶ return 'lvalue' if _is_operator( $before, '->' );
Branch: 1 branch
BranchTrueFalseTotal
if _is_operator($before, "->") 1 11 12
290 11 ▶ return 'lvalue' if $before->isa('PPI::Token::Symbol') && index( $before->content, q{&} ) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $before->isa("PPI::Token::Symbol") and index($before->content, "&") == 0 1 10 11
Condition66%2/3
$before->isa("PPI::Token::Symbol") and index($before->content, "&") == 0
!ll&&!rl&&r
1001
MC/DC50%1/2
$before->isa("PPI::Token::Symbol") and index($before->content, "&") == 0
$before->isa("PPI::Token::Symbol") index($before->content, "&") == 0
Truth table66%2/3
A and B
A= $before->isa("PPI::Token::Symbol")B= index($before->content, "&") == 0
ABresult
0X0
100
111
291 10 ▶ if ( $before->isa('PPI::Token::Word') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($before->isa("PPI::Token::Word")) 9 1 10
292 9 ▶ return 'lvalue' if _is_operator( $before->sprevious_sibling, '->' );
Branch: 1 branch
BranchTrueFalseTotal
if _is_operator($before->sprevious_sibling, "->") 1 8 9
293 8 ▶ return _argument_of( $before, $whole, $commas, [ $before, $list ] ) if is_function_call($before);
Branch: 1 branch
BranchTrueFalseTotal
if is_function_call($before) 8 0 8
294 }
295 }
296  
297 # Parens that only group: what is in them is used the way they are.
298 4 my $outer = _context( [$list] );
299 4 ▶ return $outer if $outer eq 'lvalue' || $outer eq 'fetch';
Branch: 1 branch
BranchTrueFalseTotal
if $outer eq "lvalue" or $outer eq "fetch" 2 2 4
Condition100%3/3
$outer eq "lvalue" or $outer eq "fetch"
l!l&&r!l&&!r
112
MC/DC100%2/2
$outer eq "lvalue" or $outer eq "fetch"
$outer eq "lvalue" $outer eq "fetch"
Truth table100%3/3
A or B
A= $outer eq "lvalue"B= $outer eq "fetch"
ABresult
1X1
011
000
300 2 ▶ return $whole && !$commas ? $outer : 'fetch';
Branch: 1 branch
BranchTrueFalseTotal
$whole && !$commas ? : 2 0 2
Condition33%1/3
$whole and not $commas
!ll&&!rl&&r
002
MC/DC0%0/2
$whole and not $commas
$whole not $commas
Truth table33%1/3
A and not(B)
A= $wholeB= not $commas
ABresult
0X0
100
111
301 }
302  
303 # The context of an argument of the call $word. $immediate is whether the
304 # argument comes first and alone, $commas how many arguments are before it, and
305 # $call the call up to the argument, for scalar, which passes on the context it
306 # is in.
307 51 ▶sub _argument_of {
Subroutine _argument_of: called CC=10 SCAR=23.1
308 51 my ( $word, $immediate, $commas, $call ) = @_;
309  
310 51 my $name = $word->content;
311 51 ▶ if ( my $own = $OWN_CATEGORY{$name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $own = $OWN_CATEGORY{$name}) 26 25 51
312 26 ▶ return 'fetch' unless $immediate;
Branch: 1 branch
BranchTrueFalseTotal
unless $immediate 0 26 26
313 26 ▶ return $own eq 'fetch' ? 'aggregate' : $own;
Branch: 1 branch
BranchTrueFalseTotal
$own eq 'fetch' ? : 7 19 26
314 }
315 25 ▶ return _context($call) if $name eq 'scalar' && $immediate;
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "scalar" and $immediate 1 24 25
Condition66%2/3
$name eq "scalar" and $immediate
!ll&&!rl&&r
2401
MC/DC50%1/2
$name eq "scalar" and $immediate
$name eq "scalar" $immediate
Truth table66%2/3
A and B
A= $name eq "scalar"B= $immediate
ABresult
0X0
100
111
316 24 ▶ return 'lvalue' if $CHANGES_EVERY_ARGUMENT{$name};
Branch: 1 branch
BranchTrueFalseTotal
if $CHANGES_EVERY_ARGUMENT{$name} 5 19 24
317 19 ▶ return $commas ? 'fetch' : 'lvalue' if $CHANGES_FIRST_ARGUMENT{$name};
Branch: 2 branches
BranchTrueFalseTotal
if $CHANGES_FIRST_ARGUMENT{$name} 8 11 19
$commas ? : 1 7 8
318 11 ▶ return is_perl_builtin($word) ? 'fetch' : 'lvalue';
Branch: 1 branch
BranchTrueFalseTotal
is_perl_builtin($word) ? : 8 3 11
319 }
320  
321 # The pragmas in $doc that turn categories on or off, in order: each is the
322 # statement, whether it turns them on, and the categories.
323 85 ▶sub _pragmas {
Subroutine _pragmas: called CC=12 SCAR=24.8
324 85 my ( $self, $doc ) = @_;
325  
326 85 ▶ my $includes = $doc->find('PPI::Statement::Include') or return;
Branch: 1 branch
BranchTrueFalseTotal
my $includes = $doc->find("PPI::Statement::Include") or return 41 44 85
327 41 my %equivalent = %{ $self->{_equivalent_modules} };
328  
329 41 my @pragmas;
330 41 foreach my $include (@$includes) {
331 48 my $module = $include->module;
332 48 my $type = $include->type;
333 48 ▶ if ( $type eq 'use' && $equivalent{$module} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "use" and $equivalent{$module}) 1 47 48
Condition100%3/3
$type eq "use" and $equivalent{$module}
!ll&&!rl&&r
4071
MC/DC100%2/2
$type eq "use" and $equivalent{$module}
$type eq "use" $equivalent{$module}
Truth table100%3/3
A and B
A= $type eq "use"B= $equivalent{$module}
ABresult
0X0
100
111
334 1 push @pragmas, { statement => $include, on => 1, categories => [@DEFAULT_CATEGORIES] };
335 1 next;
336 }
337 47 ▶ next unless $module eq $PRAGMA && ( $type eq 'use' || $type eq 'no' );
Branch: 1 branch
BranchTrueFalseTotal
unless $module eq $PRAGMA and $type eq "use" || $type eq "no" 2 45 47
Condition100%6/6
$module eq $PRAGMA and $type eq "use" || $type eq "no"
!ll&&!rl&&r
1145
$type eq "use" or $type eq "no"
l!l&&r!l&&!r
6391
MC/DC100%3/3
$module eq $PRAGMA and $type eq "use" || $type eq "no"
$module eq $PRAGMA $type eq "use" $type eq "no"
Truth table100%4/4
A and B or C
A= $module eq $PRAGMAB= $type eq "use"C= $type eq "no"
ABCresult
0XX0
1000
11X1
1011
338  
339 45 my $on = $type eq 'no';
340 45 my @words = _argument_words($include);
341 45 ▶ my @categories = !@words ? () : !defined $words[0] ? ( $on ? @DEFAULT_CATEGORIES : keys %CATEGORY ) : grep { $CATEGORY{$_} } @words;
Branch: 3 branches
BranchTrueFalseTotal
!@words ? : 2 43 45
!defined($words[0]) ? : 24 19 43
$on ? : 20 4 24
342 45 ▶ push @pragmas, { statement => $include, on => $on, categories => \@categories } if @categories;
Branch: 1 branch
BranchTrueFalseTotal
if @categories 41 4 45
343 }
344 41 return @pragmas;
345 }
346  
347 # The words of the arguments of $include. A lone undef when there are no
348 # arguments, or arguments that cannot be read without running them, both of
349 # which mean the defaults. An empty list when the arguments are an empty list,
350 # since then perl does not call unimport or import at all.
351 45 ▶sub _argument_words {
Subroutine _argument_words: called CC=10 SCAR=23.0
352 45 my ($include) = @_;
353  
354 45 my @arguments = $include->arguments;
355 45 ▶ return (undef) unless @arguments;
Branch: 1 branch
BranchTrueFalseTotal
unless @arguments 21 24 45
356  
357 24 my @words;
358 24 foreach my $argument ( map { _flatten($_) } @arguments ) {
359 27 ▶ if ( $argument->isa('PPI::Token::QuoteLike::Words') ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($argument->isa('PPI::Token::QuoteLike::Words')) { } 16 11 27
elsif ($argument->isa('PPI::Token::Quote')) { } 5 6 11
elsif ($argument->isa('PPI::Token::Word')) { } 2 4 6
elsif (not $argument->isa('PPI::Token::Operator') && $COMMA{$argument->content}) { } 2 2 4
360 16 push @words, $argument->literal;
361 }
362 elsif ( $argument->isa('PPI::Token::Quote') ) {
363 5 ▶ return (undef) if $argument->can('interpolations') && $argument->interpolations;
Branch: 1 branch
BranchTrueFalseTotal
if $argument->can("interpolations") and $argument->interpolations 1 4 5
Condition100%3/3
$argument->can("interpolations") and $argument->interpolations
!ll&&!rl&&r
311
MC/DC100%2/2
$argument->can("interpolations") and $argument->interpolations
$argument->can("interpolations") $argument->interpolations
Truth table100%3/3
A and B
A= $argument->can("interpolations")B= $argument->interpolations
ABresult
0X0
100
111
364 4 push @words, $argument->string;
365 }
366 elsif ( $argument->isa('PPI::Token::Word') ) {
367 2 push @words, $argument->content;
368 }
369 ▶ elsif ( !( $argument->isa('PPI::Token::Operator') && $COMMA{ $argument->content } ) ) {
Condition66%2/3
$argument->isa('PPI::Token::Operator') && $COMMA{$argument->content}
!ll&&!rl&&r
202
MC/DC50%1/2
$argument->isa('PPI::Token::Operator') && $COMMA{$argument->content}
$argument->isa('PPI::Token::Operator') $COMMA{$argument->content}
Truth table66%2/3
A && B
A= $argument->isa('PPI::Token::Operator')B= $COMMA{$argument->content}
ABresult
0X0
100
111
370 2 return (undef);
371 }
372 }
373 21 return @words;
374 }
375  
376 # $elem, or the significant tokens inside it when it is a list.
377 29 ▶sub _flatten {
Subroutine _flatten: called CC=2 SCAR=6.9
378 29 my ($elem) = @_;
379  
380 29 ▶ return $elem unless $elem->isa('PPI::Structure::List');
Branch: 1 branch
BranchTrueFalseTotal
unless $elem->isa("PPI::Structure::List") 27 2 29
381 2 return map { _flatten($_) } map { $_->schildren } $elem->schildren;
382 }
383  
384 # Whether the pragmas in scope at $hazard turn on the category it needs.
385 88 ▶sub _is_covered {
Subroutine _is_covered: called CC=4 SCAR=13.9
386 88 my ( $hazard, $pragmas ) = @_;
387  
388 88 my $at = $hazard->{pieces}[0];
389 88 my %on;
390 88 foreach my $pragma ( grep { _is_in_scope( $_->{statement}, $at ) } @$pragmas ) {
391 38 foreach my $category ( @{ $pragma->{categories} } ) {
392 85 ▶ if ( $pragma->{on} ) { $on{$category} = 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($pragma->{'on'}) { } 71 14 85
393 14 else { delete $on{$category} }
394 }
395 }
396 88 return $on{ $hazard->{category} };
397 }
398  
399 # Whether $statement is in a block around $elem, and before it. The lexical
400 # scope of a pragma is the rest of the block that it is in.
401 43 ▶sub _is_in_scope {
Subroutine _is_in_scope: called CC=4 SCAR=13.9
402 43 my ( $statement, $elem ) = @_;
403  
404 43 my $scope = refaddr( $statement->parent );
405 43 ▶ return unless any { refaddr($_) == $scope } _ancestors($elem);
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
unless &any(sub { builtin::refaddr($_) == $scope; } , _ancestors($elem)) 4 39 43
406  
407 39 my ( $statement_line, $statement_column ) = @{ $statement->location }[ 0, 1 ];
408 39 my ( $line, $column ) = @{ $elem->location }[ 0, 1 ];
409 39 ▶ return $statement_line < $line || ( $statement_line == $line && $statement_column < $column );
Condition66%4/6
$statement_line < $line || $statement_line == $line && $statement_column < $column
l!l&&r!l&&!r
0381
$statement_line == $line and $statement_column < $column
!ll&&!rl&&r
0138
MC/DC33%1/3
$statement_line < $line || $statement_line == $line && $statement_column < $column
$statement_line < $line $statement_line == $line $statement_column < $column
Truth table50%2/4
A || B and C
A= $statement_line < $lineB= $statement_line == $lineC= $statement_column < $column
ABCresult
1XX1
0111
00X0
0100
410 }
411  
412 # Whether an ancestor of $elem is one of %$blocks, which are keyed by refaddr.
413 64 ▶sub _is_inside {
Subroutine _is_inside: called CC=1 SCAR=0.0
414 64 my ( $elem, $blocks ) = @_;
415  
416 64 ▶ return any { $blocks->{ refaddr($_) } } _ancestors($elem);
Subroutine __ANON__: called CC=1 SCAR=0.0
417 }
418  
419 107 ▶sub _ancestors {
Subroutine _ancestors: called CC=2 SCAR=6.9
420 107 my ($elem) = @_;
421  
422 107 my @ancestors;
423 107 ▶ for ( my $parent = $elem->parent; $parent; $parent = $parent->parent ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent) 270 107 377
424 270 push @ancestors, $parent;
425 }
426 107 return @ancestors;
427 }
428  
429 # Whether $elem is a cast that dereferences, which \ does not.
430 3222 ▶sub _is_dereference {
Subroutine _is_dereference: called CC=3 SCAR=11.0
431 3222 my ($elem) = @_;
432  
433 3222 ▶ return $elem && $elem->isa('PPI::Token::Cast') && $elem->content ne q{\\};
Condition100%6/6
$elem && $elem->isa('PPI::Token::Cast') && $elem->content ne '\\'
!ll&&!rl&&r
3185235
$elem and $elem->isa("PPI::Token::Cast")
!ll&&!rl&&r
77310837
MC/DC100%3/3
$elem && $elem->isa('PPI::Token::Cast') && $elem->content ne '\\'
$elem $elem->isa("PPI::Token::Cast") $elem->content ne '\\'
Truth table100%4/4
A and B && C
A= $elemB= $elem->isa("PPI::Token::Cast")C= $elem->content ne '\\'
ABCresult
0XX0
10X0
1100
1111
434 }
435  
436 410 ▶sub _is_operator {
Subroutine _is_operator: called CC=3 SCAR=11.0
437 410 my ( $elem, $operator ) = @_;
438  
439 410 ▶ return $elem && $elem->isa('PPI::Token::Operator') && $elem->content eq $operator;
Condition100%6/6
$elem && $elem->isa('PPI::Token::Operator') && $elem->content eq $operator
!ll&&!rl&&r
23413145
$elem and $elem->isa("PPI::Token::Operator")
!ll&&!rl&&r
10224176
MC/DC100%3/3
$elem && $elem->isa('PPI::Token::Operator') && $elem->content eq $operator
$elem $elem->isa("PPI::Token::Operator") $elem->content eq $operator
Truth table100%4/4
A and B && C
A= $elemB= $elem->isa("PPI::Token::Operator")C= $elem->content eq $operator
ABCresult
0XX0
10X0
1100
1111
440 }
441  
442 192 ▶sub _is_step {
Subroutine _is_step: called CC=2 SCAR=6.9
443 192 my ($elem) = @_;
444  
445 192 ▶ return $elem->isa('PPI::Token::Operator') && $STEP{ $elem->content };
Condition100%3/3
$elem->isa('PPI::Token::Operator') && $STEP{$elem->content}
!ll&&!rl&&r
127632
MC/DC100%2/2
$elem->isa('PPI::Token::Operator') && $STEP{$elem->content}
$elem->isa('PPI::Token::Operator') $STEP{$elem->content}
Truth table100%3/3
A && B
A= $elem->isa('PPI::Token::Operator')B= $STEP{$elem->content}
ABresult
0X0
100
111
446 }
447  
448 94 ▶sub _is_binding {
Subroutine _is_binding: called CC=2 SCAR=6.9
449 94 my ($elem) = @_;
450  
451 94 ▶ return $elem->isa('PPI::Token::Operator') && $BINDING{ $elem->content };
Condition100%3/3
$elem->isa('PPI::Token::Operator') && $BINDING{$elem->content}
!ll&&!rl&&r
8473
MC/DC100%2/2
$elem->isa('PPI::Token::Operator') && $BINDING{$elem->content}
$elem->isa('PPI::Token::Operator') $BINDING{$elem->content}
Truth table100%3/3
A && B
A= $elem->isa('PPI::Token::Operator')B= $BINDING{$elem->content}
ABresult
0X0
100
111
452 }
453  
454 # Whether the right side of a binding changes the string that it is bound to.
455 3 ▶sub _changes {
Subroutine _changes: called CC=3 SCAR=11.0
456 3 my ($elem) = @_;
457  
458 3 ▶ return $elem && ( $elem->isa('PPI::Token::Regexp::Substitute') || $elem->isa('PPI::Token::Regexp::Transliterate') );
Condition83%5/6
$elem && ($elem->isa('PPI::Token::Regexp::Substitute') || $elem->isa('PPI::Token::Regexp::Transliterate'))
!ll&&!rl&&r
012
$elem->isa("PPI::Token::Regexp::Substitute") or $elem->isa("PPI::Token::Regexp::Transliterate")
l!l&&r!l&&!r
111
MC/DC66%2/3
$elem && ($elem->isa('PPI::Token::Regexp::Substitute') || $elem->isa('PPI::Token::Regexp::Transliterate'))
$elem $elem->isa("PPI::Token::Regexp::Substitute") $elem->isa("PPI::Token::Regexp::Transliterate")
Truth table75%3/4
A && B or C
A= $elemB= $elem->isa("PPI::Token::Regexp::Substitute")C= $elem->isa("PPI::Token::Regexp::Transliterate")
ABCresult
0XX0
1000
11X1
1011
459 }
460  
461 2 1;
462  
463 __END__
↑ Summary