blib/lib/Perl/Critic/Policy/ControlStructures/ProhibitMutatingListFunctionsStricter.pm

filter: Statementstmt 98.8% 262 / 265 Branchbran 91.2% 115 / 126 Conditioncond 83.9% 120 / 143 MC/DCmcdc 70.6% 53 / 75 Subroutinesub 100% 42 / 42 Podpod 100% 21 / 21 totaltotal 91.2% 613 / 672 CC 126 SCAR 23.9
CC
126
Cov
93%
violates
25.0
topic_owned_in
21.0
is_unsafe_call
15.0
CRAP 10.9
↑ Summary
1 package Perl::Critic::Policy::ControlStructures::ProhibitMutatingListFunctionsStricter 0.003;
2  
3 # ABSTRACT: Do not change $_ in a list function, directly or through a sub that the block calls.
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 2 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use re '/aa';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use List::Util qw{any first};
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Perl::Critic::Distribution ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Perl::Critic::Document ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use PPI::Statement ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Perl::Critic::Utils qw{ :severities is_function_call is_method_call is_perl_builtin first_arg };
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 2 ▶use parent qw{Perl::Critic::Policy::ControlStructures::ProhibitMutatingListFunctions};
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19  
20 2 Readonly::Scalar my $CORE => 'Perl::Critic::Policy::ControlStructures::ProhibitMutatingListFunctions';
21 2 Readonly::Scalar my $DESC => q{Don't modify $_ in list functions};
22 2 Readonly::Scalar my $DESC_CALLED => q{A sub that modifies $_ is called in a list function};
23 2 Readonly::Scalar my $EXPL => [114];
24 2 Readonly::Scalar my $DESC_UNRESOLVED => q{A sub or method that may modify $_ is called in a list function};
25 2 Readonly::Scalar my $EXPL_UNRESOLVED => q{Say "local ($_) = $_;" before the first call, so that a callee that writes $_ writes a copy};
26 2 Readonly::Scalar my $DESC_POSTFIX => q{A sub or method that may modify $_ is called in a statement with a postfix for};
27 2 Readonly::Scalar my $EXPL_POSTFIX => q{Write a block "for my $x (...) { ... }" instead, so that $_ is not the element};
28  
29 # The functions that make $_ an alias to each element, for report_unresolved_calls.
30 Readonly::Array my @ALIASING_FUNCS => (
31 qw{ map grep },
32 220 map { ( $_, "List::Util::$_" ) } qw{ first any all none notall },
33 70 map { ( $_, "List::MoreUtils::$_", "List::SomeUtils::$_" ) } qw{
34 any all none notall true false firstidx first_index lastidx last_index
35 firstval first_value lastval last_value firstres first_result lastres
36 last_result onlyidx only_index onlyval only_value onlyres only_result
37 indexes after after_incl before before_incl part insert_after
38 },
39 2 map { ( $_, "List::UtilsBy::$_" ) } qw{ max_by min_by },
40 );
41 2 Readonly::Hash my %EXPR_FORM => map { $_ => 1 } qw{ map grep };
42  
43 2 Readonly::Hash my %CHOPS => map { $_ => 1 } qw{ chop chomp };
44 2 Readonly::Hash my %LOOP => map { $_ => 1 } qw{ for foreach };
45 2 ▶Readonly::Hash my %LIST_FUNCTION => map { $_ => 1 } split m/\s+/xs, ( first { $_->{name} eq 'list_funcs' } $CORE->supported_parameters )->{default_string};
Subroutine __ANON__: called CC=1 SCAR=0.0
46  
47 # Change it when what the collector returns changes shape.
48 2 Readonly::Scalar my $DATA_FORMAT => 2;
49  
50 # The definition of what changes $_ is the core policy's, in a sub that it does
51 # not export. Said at load, not at the first file.
52 2 ▶my $core_side_effect = $CORE->can('_has_topic_side_effect') or die "$CORE has no _has_topic_side_effect, which this policy reuses\n";
Branch: 1 branch
BranchTrueFalseTotal
my $core_side_effect = $CORE->can("_has_topic_side_effect") or die "$CORE has no _has_topic_side_effect, which this policy reuses\n" 2 0 2
53  
54 # The part of a name after its last ::, which is all of a bare name.
55 465 ▶my $bare_name = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
56 465 my ($name) = @_;
57 465 my $at = rindex $name, '::';
58 465 ▶ return $at < 0 ? $name : substr $name, $at + 2;
Branch: 1 branch
BranchTrueFalseTotal
$at < 0 ? : 209 256 465
59 2 };
60  
61  
62 4 ▶sub supported_parameters {
Subroutine supported_parameters: called CC=1 SCAR=0.0
Pod documented
63 4 my ($class) = @_;
64 return (
65 4 $class->SUPER::supported_parameters(),
66 {
67 name => 'report_unresolved_calls',
68 description => q{Report a call in a list function block that this policy cannot read},
69 default_string => '0',
70 behavior => 'boolean',
71 },
72 {
73 name => 'report_postfix_for_calls',
74 description => q{Report a call in a statement with a postfix for},
75 default_string => '0',
76 behavior => 'boolean',
77 },
78 {
79 name => 'allow_calls',
80 description => q{Subs and methods that those two reports take as not writing $_},
81 default_string => q{can DOES isa VERSION},
82 behavior => 'string list',
83 },
84 );
85 }
86  
87  
88 4 ▶sub initialize_if_enabled {
Subroutine initialize_if_enabled: called CC=3 SCAR=11.0
Pod documented
89 4 my ( $self, $config ) = @_;
90  
91 4 my $enabled = $self->SUPER::initialize_if_enabled($config);
92  
93 # The list functions as the core policy configured them, from its
94 # list_funcs and add_list_funcs, and those that report_unresolved_calls adds.
95 4 ▶ $self->{_block_funcs} = { %{ $self->{_all_list_funcs} }, $self->{_report_unresolved_calls} ? ( map { $_ => 1 } @ALIASING_FUNCS ) : () };
Branch: 1 branch
BranchTrueFalseTotal
$self->{'_report_unresolved_calls'} ? : 3 1 4
96  
97 Perl::Critic::Distribution->register(
98 name => __PACKAGE__,
99 version => join( q{/}, $DATA_FORMAT, $CORE->VERSION, Perl::Critic::Distribution->stamp(__FILE__) // q{} ),
100 5 ▶ collect => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
101 5 my ($ppi) = @_;
102 5 my $doc = Perl::Critic::Document->new( -source => $ppi );
103 5 my $packages = packages_in($doc);
104 return {
105 1 mutating_subs => [ map { $_->[1] } grep { !$_->[0]->type } mutating_subs_in( $doc, $packages ) ],
106 5 subs => [ map { $_->[1] } grep { !$_->[0]->type } subs_in( $doc, $packages ) ],
107 };
108 },
109 4 ▶ );
Condition50%1/2
"Perl::Critic::Distribution"->stamp(__FILE__) // ""
l!l
40
MC/DC0%0/1
"Perl::Critic::Distribution"->stamp(__FILE__) // ""
"Perl::Critic::Distribution"->stamp(__FILE__)
Truth table50%1/2
A
A= "Perl::Critic::Distribution"->stamp(__FILE__)
Aresult
11
00
110 4 return $enabled;
111 }
112  
113  
114 1651 ▶sub violates {
Subroutine violates: called CC=25 SCAR=32.2
Pod documented
115 1651 my ( $self, $elem, $doc ) = @_;
116  
117 1651 my @core = $self->SUPER::violates( $elem, $doc );
118 1651 ▶ return @core if @core;
Branch: 1 branch
BranchTrueFalseTotal
if @core 6 1645 1651
119  
120 1645 ▶ if ( $self->{_report_postfix_for_calls} && ( my $statement = postfix_statement($elem) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_report_postfix_for_calls"} and my $statement = postfix_statement($elem)) 22 1623 1645
Condition100%3/3
$self->{"_report_postfix_for_calls"} and my $statement = postfix_statement($elem)
!ll&&!rl&&r
120242122
MC/DC100%2/2
$self->{"_report_postfix_for_calls"} and my $statement = postfix_statement($elem)
$self->{"_report_postfix_for_calls"} my $statement = postfix_statement($elem)
Truth table100%3/3
A and B
A= $self->{"_report_postfix_for_calls"}B= my $statement = postfix_statement($elem)
ABresult
0X0
100
111
121 22 my $known = $self->known_subs($doc);
122 22 ▶ my $call = $self->first_call( $known, 1, [ topic_owned_in( $statement, $self->{_block_funcs} ) ], grep { precedes( $_, $elem ) } $statement->schildren ) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $call = $self->first_call($known, 1, [topic_owned_in($statement, $self->{"_block_funcs"})], grep({precedes($_, $elem);} $statement->schildren)) or return 11 11 22
123 11 return $self->violation( $DESC_POSTFIX, $EXPL_POSTFIX, $call );
124 }
125  
126 1623 ▶ return if !$self->{_block_funcs}{$elem} || !is_function_call($elem);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_block_funcs"}{$elem} and is_function_call($elem) 1376 247 1623
Condition66%2/3
$self->{"_block_funcs"}{$elem} and is_function_call($elem)
!ll&&!rl&&r
13760247
MC/DC50%1/2
$self->{"_block_funcs"}{$elem} and is_function_call($elem)
$self->{"_block_funcs"}{$elem} is_function_call($elem)
Truth table66%2/3
A and B
A= $self->{"_block_funcs"}{$elem}B= is_function_call($elem)
ABresult
0X0
100
111
127 247 ▶ my $arg = first_arg($elem) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $arg = first_arg($elem) or return 247 0 247
128  
129 247 my @aliased;
130 my %expression;
131 247 ▶ if ( $arg->isa('PPI::Structure::Block') ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($arg->isa('PPI::Structure::Block')) { } 228 19 247
elsif (@aliased = expression_of($elem)) { } 18 1 19
132 228 ▶ return $self->violation( $DESC, $EXPL, $elem ) if chops_topic($arg) || ( !$self->{_all_list_funcs}{$elem} && $core_side_effect->( $arg, $doc ) );
Branch: 1 branch
BranchTrueFalseTotal
if chops_topic($arg) or not $self->{"_all_list_funcs"}{$elem} and &$core_side_effect($arg, $doc) 3 225 228
Condition100%6/6
chops_topic($arg) or not $self->{"_all_list_funcs"}{$elem} and &$core_side_effect($arg, $doc)
l!l&&r!l&&!r
21225
not $self->{"_all_list_funcs"}{$elem} and &$core_side_effect($arg, $doc)
!ll&&!rl&&r
213121
MC/DC100%3/3
chops_topic($arg) or not $self->{"_all_list_funcs"}{$elem} and &$core_side_effect($arg, $doc)
chops_topic($arg) not $self->{"_all_list_funcs"}{$elem} &$core_side_effect($arg, $doc)
Truth table100%4/4
A or not(B) and C
A= chops_topic($arg)B= not $self->{"_all_list_funcs"}{$elem}C= &$core_side_effect($arg, $doc)
ABCresult
1XX1
0111
00X0
0100
133 225 @aliased = aliased_statements($arg);
134 }
135 elsif ( @aliased = expression_of($elem) ) {
136 18 my $copy = PPI::Statement->new;
137 18 $copy->add_element( $_->clone ) for @aliased;
138 18 ▶ return $self->violation( $DESC, $EXPL, $elem ) if mutates_topic( $copy, $doc );
Branch: 1 branch
BranchTrueFalseTotal
if mutates_topic($copy, $doc) 3 15 18
139 15 %expression = map { Scalar::Util::refaddr($_) => 1 } @aliased;
140 }
141 else {
142 1 return;
143 }
144  
145 # An expression's owner is its statement, in which topic_owned_in also
146 # finds the expression itself.
147 240 my $known = $self->known_subs($doc);
148 240 ▶ my $owner = $arg->isa('PPI::Structure::Block') ? $arg : $elem->statement;
Branch: 1 branch
BranchTrueFalseTotal
$arg->isa('PPI::Structure::Block') ? : 225 15 240
149 240 my @owned = grep { !$expression{ Scalar::Util::refaddr($_) } } topic_owned_in( $owner, $self->{_block_funcs} );
150 240 my %owned = map { Scalar::Util::refaddr($_) => 1 } @owned;
151 240 ▶ foreach my $word ( map { $_->isa('PPI::Node') ? @{ $_->find('PPI::Token::Word') || [] } : $_ } @aliased ) {
Branch: 1 branch
BranchTrueFalseTotal
$_->isa('PPI::Node') ? : 396 15 411
Condition100%2/2
$_->find('PPI::Token::Word') || []
l!l
237159
MC/DC100%1/1
$_->find('PPI::Token::Word') || []
$_->find('PPI::Token::Word')
Truth table100%2/2
A
A= $_->find('PPI::Token::Word')
Aresult
11
00
152 315 ▶ next if !$word->isa('PPI::Token::Word') || !is_function_call($word) || is_inside( $word, \%owned );
Branch: 1 branch
BranchTrueFalseTotal
if not $word->isa("PPI::Token::Word") && is_function_call($word) or is_inside($word, \%owned) 72 243 315
Condition100%6/6
not $word->isa("PPI::Token::Word") && is_function_call($word) or is_inside($word, \%owned)
l!l&&r!l&&!r
4626243
$word->isa('PPI::Token::Word') && is_function_call($word)
!ll&&!rl&&r
244269
MC/DC100%3/3
not $word->isa("PPI::Token::Word") && is_function_call($word) or is_inside($word, \%owned)
$word->isa('PPI::Token::Word') is_function_call($word) is_inside($word, \%owned)
Truth table100%4/4
not(A && B) or C
A= $word->isa('PPI::Token::Word')B= is_function_call($word)C= is_inside($word, \%owned)
ABCresult
0XX1
10X1
1111
1100
153 243 ▶ return $self->violation( $DESC_CALLED, $EXPL, $elem ) if calls_sub_in( $word, $word->content, $known->{mutating}, $known->{packages} );
Branch: 1 branch
BranchTrueFalseTotal
if calls_sub_in($word, $word->content, $known->{"mutating"}, $known->{"packages"}) 81 162 243
154 }
155  
156 159 ▶ return if !$self->{_report_unresolved_calls};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_report_unresolved_calls"} 77 82 159
157 82 ▶ my $call = $self->first_call( $known, 0, \@owned, @aliased ) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $call = $self->first_call($known, 0, \@owned, @aliased) or return 53 29 82
158 53 return $self->violation( $DESC_UNRESOLVED, $EXPL_UNRESOLVED, $call );
159 }
160  
161  
162 104 ▶sub first_call {
Subroutine first_call: called CC=6 SCAR=17.9
Pod documented
163 104 my ( $self, $known, $mutating_too, $owned, @elements ) = @_;
164  
165 104 my %owned = map { Scalar::Util::refaddr($_) => 1 } @$owned;
166 104 ▶ foreach my $token ( map { $_->isa('PPI::Node') ? $_->tokens : $_ } @elements ) {
Branch: 1 branch
BranchTrueFalseTotal
$_->isa('PPI::Node') ? : 124 47 171
167 515 ▶ next if !$token->significant || is_inside( $token, \%owned );
Branch: 1 branch
BranchTrueFalseTotal
if not $token->significant or is_inside($token, \%owned) 186 329 515
Condition100%3/3
not $token->significant or is_inside($token, \%owned)
l!l&&r!l&&!r
12165329
MC/DC100%2/2
not $token->significant or is_inside($token, \%owned)
not $token->significant is_inside($token, \%owned)
Truth table100%3/3
not(A) or B
A= not $token->significantB= is_inside($token, \%owned)
ABresult
1X1
011
000
168 329 ▶ return $token if $self->is_unsafe_call( $token, $known, $mutating_too );
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_unsafe_call($token, $known, $mutating_too) 64 265 329
169 }
170 40 return;
171 }
172  
173  
174 329 ▶sub is_unsafe_call {
Subroutine is_unsafe_call: called CC=15 SCAR=27.1
Pod documented
175 329 my ( $self, $token, $known, $mutating_too ) = @_;
176  
177 329 my $name;
178 329 ▶ if ( $token->isa('PPI::Token::Word') ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($token->isa('PPI::Token::Word')) { } 129 200 329
elsif ($token->isa('PPI::Token::Symbol') and $token->raw_type eq '&') { } 1 199 200
elsif ($token->isa('PPI::Token::Cast')) { } 1 198 199
elsif ($token->isa('PPI::Token::Operator') and $token->content eq '->') { } 14 184 198
179 129 $name = $token->content;
180 129 ▶ return !$self->{_allow_calls}{$name} if is_method_call($token);
Branch: 1 branch
BranchTrueFalseTotal
if is_method_call($token) 10 119 129
181 119 ▶ return if !is_function_call($token) || $self->{_block_funcs}{$name};
Branch: 1 branch
BranchTrueFalseTotal
if not is_function_call($token) or $self->{"_block_funcs"}{$name} 22 97 119
Condition100%3/3
not is_function_call($token) or $self->{"_block_funcs"}{$name}
l!l&&r!l&&!r
21197
MC/DC100%2/2
not is_function_call($token) or $self->{"_block_funcs"}{$name}
not is_function_call($token) $self->{"_block_funcs"}{$name}
Truth table100%3/3
not(A) or B
A= not is_function_call($token)B= $self->{"_block_funcs"}{$name}
ABresult
1X1
011
000
182 97 ( my $core = $name ) =~ s/\ACORE::(?:GLOBAL::)?//xms;
183 97 ▶ return if is_perl_builtin($core);
Branch: 1 branch
BranchTrueFalseTotal
if is_perl_builtin($core) 34 63 97
184 }
185 ▶ elsif ( $token->isa('PPI::Token::Symbol') && $token->raw_type eq q{&} ) {
Condition100%3/3
$token->isa("PPI::Token::Symbol") and $token->raw_type eq "&"
!ll&&!rl&&r
126731
MC/DC100%2/2
$token->isa("PPI::Token::Symbol") and $token->raw_type eq "&"
$token->isa("PPI::Token::Symbol") $token->raw_type eq "&"
Truth table100%3/3
A and B
A= $token->isa("PPI::Token::Symbol")B= $token->raw_type eq "&"
ABresult
0X0
100
111
186 1 $name = substr $token->content, 1;
187 }
188 elsif ( $token->isa('PPI::Token::Cast') ) {
189 1 return $token->content eq q{&};
190 }
191 ▶ elsif ( $token->isa('PPI::Token::Operator') && $token->content eq q{->} ) {
Condition100%3/3
$token->isa("PPI::Token::Operator") and $token->content eq "->"
!ll&&!rl&&r
1592514
MC/DC100%2/2
$token->isa("PPI::Token::Operator") and $token->content eq "->"
$token->isa("PPI::Token::Operator") $token->content eq "->"
Truth table100%3/3
A and B
A= $token->isa("PPI::Token::Operator")B= $token->content eq "->"
ABresult
0X0
100
111
192 14 my $next = $token->snext_sibling;
193 14 ▶ return $next && ( $next->isa('PPI::Structure::List') || $next->isa('PPI::Token::Symbol') );
Condition83%5/6
$next && ($next->isa('PPI::Structure::List') || $next->isa('PPI::Token::Symbol'))
!ll&&!rl&&r
0113
$next->isa("PPI::Structure::List") or $next->isa("PPI::Token::Symbol")
l!l&&r!l&&!r
2111
MC/DC66%2/3
$next && ($next->isa('PPI::Structure::List') || $next->isa('PPI::Token::Symbol'))
$next $next->isa("PPI::Structure::List") $next->isa("PPI::Token::Symbol")
Truth table75%3/4
A && B or C
A= $nextB= $next->isa("PPI::Structure::List")C= $next->isa("PPI::Token::Symbol")
ABCresult
0XX0
1000
11X1
1011
194 }
195 else {
196 184 return;
197 }
198  
199 64 ▶ return $mutating_too && calls_sub_in( $token, $name, $known->{mutating}, $known->{packages} ) if calls_sub_in( $token, $name, $known->{defined}, $known->{packages} );
Branch: 1 branch
BranchTrueFalseTotal
if calls_sub_in($token, $name, $known->{"defined"}, $known->{"packages"}) 9 55 64
Condition100%3/3
$mutating_too && calls_sub_in($token, $name, $known->{'mutating'}, $known->{'packages'})
!ll&&!rl&&r
522
MC/DC100%2/2
$mutating_too && calls_sub_in($token, $name, $known->{'mutating'}, $known->{'packages'})
$mutating_too calls_sub_in($token, $name, $known->{'mutating'}, $known->{'packages'})
Truth table100%3/3
A && B
A= $mutating_tooB= calls_sub_in($token, $name, $known->{'mutating'}, $known->{'packages'})
ABresult
0X0
100
111
200 55 return !$self->{_allow_calls}{$name};
201 }
202  
203  
204 262 ▶sub known_subs {
Subroutine known_subs: called CC=9 SCAR=22.0
Pod documented
205 262 my ( $self, $doc ) = @_;
206  
207 # The document is held, not just its address, so the address cannot be
208 # handed to the next document while this still answers for it.
209 262 my $last = $self->{_known};
210 262 ▶ return $last->{subs} if $last && Scalar::Util::refaddr( $last->{doc} ) == Scalar::Util::refaddr($doc);
Branch: 1 branch
BranchTrueFalseTotal
if $last and builtin::refaddr($last->{"doc"}) == builtin::refaddr($doc) 22 240 262
Condition100%3/3
$last and builtin::refaddr($last->{"doc"}) == builtin::refaddr($doc)
!ll&&!rl&&r
423622
MC/DC100%2/2
$last and builtin::refaddr($last->{"doc"}) == builtin::refaddr($doc)
$last builtin::refaddr($last->{"doc"}) == builtin::refaddr($doc)
Truth table100%3/3
A and B
A= $lastB= builtin::refaddr($last->{"doc"}) == builtin::refaddr($doc)
ABresult
0X0
100
111
211  
212 240 my $packages = packages_in($doc);
213 240 my @here = subs_in( $doc, $packages );
214 my %subs = (
215 packages => $packages,
216 113 mutating => { local => { map { ( $_->[0]->name => 1 ) } mutating_subs_in( $doc, $packages ) }, remote => {} },
217 240 defined => { local => { map { ( $_->[0]->name => 1 ) } @here }, remote => { map { ( $_->[1] => 1 ) } @here } },
218 );
219  
220 240 my $filename = $doc->filename;
221 240 ▶ if ( my $dist = defined $filename && Perl::Critic::Distribution->for_file($filename) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $dist = defined $filename && "Perl::Critic::Distribution"->for_file($filename)) 6 234 240
Condition66%2/3
defined $filename && 'Perl::Critic::Distribution'->for_file($filename)
!ll&&!rl&&r
23406
MC/DC50%1/2
defined $filename && 'Perl::Critic::Distribution'->for_file($filename)
defined $filename 'Perl::Critic::Distribution'->for_file($filename)
Truth table66%2/3
A && B
A= defined $filenameB= 'Perl::Critic::Distribution'->for_file($filename)
ABresult
0X0
100
111
222 6 foreach my $file ( values %{ $dist->collected(__PACKAGE__) } ) {
223 30 $subs{mutating}{remote}{$_} = 1 for @{ $file->{mutating_subs} };
224 30 $subs{defined}{remote}{$_} = 1 for @{ $file->{subs} };
225 }
226 }
227 240 foreach my $set ( @subs{qw{ mutating defined }} ) {
228 480 $set->{bare} = { map { ( $bare_name->($_) => 1 ) } keys %{ $set->{remote} } };
229 }
230  
231 240 $self->{_known} = { doc => $doc, subs => \%subs };
232 240 return \%subs;
233 }
234  
235  
236 125 ▶sub precedes {
Subroutine precedes: called CC=3 SCAR=11.1
Pod documented
237 125 my ( $elem, $mark ) = @_;
238 125 my ( $line, $col ) = @{ $elem->location }[ 0, 1 ];
239 125 my ( $mline, $mcol ) = @{ $mark->location }[ 0, 1 ];
240 125 ▶ return $line < $mline || ( $line == $mline && $col < $mcol );
Condition66%4/6
$line < $mline || $line == $mline && $col < $mcol
l!l&&r!l&&!r
05768
$line == $mline and $col < $mcol
!ll&&!rl&&r
06857
MC/DC33%1/3
$line < $mline || $line == $mline && $col < $mcol
$line < $mline $line == $mline $col < $mcol
Truth table50%2/4
A || B and C
A= $line < $mlineB= $line == $mlineC= $col < $mcol
ABCresult
1XX1
0111
00X0
0100
241 }
242  
243  
244 663 ▶sub is_inside {
Subroutine is_inside: called CC=3 SCAR=11.0
Pod documented
245 663 my ( $elem, $owned ) = @_;
246 663 ▶ for ( my $up = $elem; $up; $up = $up->parent ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($up) 3185 572 3757
247 3185 ▶ return 1 if $owned->{ Scalar::Util::refaddr($up) };
Branch: 1 branch
BranchTrueFalseTotal
if $$owned{builtin::refaddr($up)} 91 3094 3185
248 }
249 572 return;
250 }
251  
252  
253 311 ▶sub calls_sub_in {
Subroutine calls_sub_in: called CC=4 SCAR=13.9
Pod documented
254 311 my ( $elem, $name, $set, $packages ) = @_;
255  
256 311 ▶ return 1 if $set->{local}{$name};
Branch: 1 branch
BranchTrueFalseTotal
if $set->{"local"}{$name} 84 227 311
257 227 ▶ return if !$set->{bare}{ $bare_name->($name) };
Branch: 1 branch
BranchTrueFalseTotal
unless $set->{"bare"}{&$bare_name($name)} 215 12 227
258 12 ▶ my $full = index( $name, '::' ) >= 0 ? $name : package_at( $elem, $packages ) . "::$name";
Branch: 1 branch
BranchTrueFalseTotal
index($name, '::') != -1 ? : 7 5 12
259 12 return $set->{remote}{$full};
260 }
261  
262  
263 443 ▶sub postfix_statement {
Subroutine postfix_statement: called CC=6 SCAR=18.6
Pod documented
264 443 my ($elem) = @_;
265  
266 443 ▶ return if !$LOOP{ $elem->content };
Branch: 1 branch
BranchTrueFalseTotal
unless $LOOP{$elem->content} 414 29 443
267 29 my $statement = $elem->parent;
268 29 ▶ return if !$statement || !$statement->isa('PPI::Statement') || $statement->isa('PPI::Statement::Compound');
Branch: 1 branch
BranchTrueFalseTotal
if not $statement && $statement->isa("PPI::Statement") or $statement->isa("PPI::Statement::Compound") 7 22 29
Condition50%3/6
not $statement && $statement->isa("PPI::Statement") or $statement->isa("PPI::Statement::Compound")
l!l&&r!l&&!r
0722
$statement && $statement->isa('PPI::Statement')
!ll&&!rl&&r
0029
MC/DC33%1/3
not $statement && $statement->isa("PPI::Statement") or $statement->isa("PPI::Statement::Compound")
$statement $statement->isa('PPI::Statement') $statement->isa("PPI::Statement::Compound")
Truth table50%2/4
not(A && B) or C
A= $statementB= $statement->isa('PPI::Statement')C= $statement->isa("PPI::Statement::Compound")
ABCresult
0XX1
10X1
1111
1100
269 22 ▶ return if $statement->schild(0) == $elem;
Branch: 1 branch
BranchTrueFalseTotal
if $statement->schild(0) == $elem 0 22 22
270 22 return $statement;
271 }
272  
273  
274 490 ▶sub subs_in {
Subroutine subs_in: called CC=3 SCAR=11.0
Pod documented
275 490 my ( $doc, $packages ) = @_;
276 return map {
277 386 my $name = $_->name;
278 386 ▶ [ $_, index( $name, '::' ) >= 0 ? $name : package_at( $_, $packages ) . "::$name" ]
Branch: 1 branch
BranchTrueFalseTotal
index($name, '::') != -1 ? : 0 386 386
279 490 ▶ } grep { $_->block } @{ $doc->find('PPI::Statement::Sub') || [] };
Condition100%2/2
$doc->find('PPI::Statement::Sub') || []
l!l
310180
MC/DC100%1/1
$doc->find('PPI::Statement::Sub') || []
$doc->find('PPI::Statement::Sub')
Truth table100%2/2
A
A= $doc->find('PPI::Statement::Sub')
Aresult
11
00
280 }
281  
282  
283 245 ▶sub mutating_subs_in {
Subroutine mutating_subs_in: called CC=5 SCAR=16.1
Pod documented
284 245 my ( $doc, $packages ) = @_;
285  
286 245 my @found;
287 245 foreach my $found ( subs_in( $doc, $packages ) ) {
288 193 my $block = $found->[0]->block;
289 66 ▶ next if any {
Subroutine __ANON__: called CC=2 SCAR=6.9
290 1 ▶ $_->type eq 'local' && any { $_ eq q{$_} }
Subroutine __ANON__: called CC=1 SCAR=0.0
291 66 ▶ $_->variables
Branch: 1 branch
BranchTrueFalseTotal
$_->type eq "local" and &any(sub { $_ eq "\$_"; } , $_->variables) 1 65 66
292 193 ▶ } @{ $block->find('PPI::Statement::Variable') || [] };
Branch: 1 branch
BranchTrueFalseTotal
if &any(sub { $_->type eq "local" and &any(sub { $_ eq "\$_"; } , $_->variables); } , @{$block->find("PPI::Statement::Variable") or [];}) 1 192 193
Condition100%2/2
$block->find('PPI::Statement::Variable') || []
l!l
66127
MC/DC100%1/1
$block->find('PPI::Statement::Variable') || []
$block->find('PPI::Statement::Variable')
Truth table100%2/2
A
A= $block->find('PPI::Statement::Variable')
Aresult
11
00
293 192 ▶ next if !mutates_topic( own_topic_removed($block), $doc );
Branch: 1 branch
BranchTrueFalseTotal
unless mutates_topic(own_topic_removed($block), $doc) 78 114 192
294  
295 114 push @found, $found;
296 }
297 245 return @found;
298 }
299  
300  
301 192 ▶sub own_topic_removed {
Subroutine own_topic_removed: called CC=3 SCAR=11.1
Pod documented
302 192 my ($block) = @_;
303  
304 192 my $copy = $block->clone;
305  
306 # An element inside one that is already gone is gone with it.
307 192 foreach my $elem ( topic_owned_in($copy) ) {
308 109 ▶ $elem->delete if $elem->top == $copy;
Branch: 1 branch
BranchTrueFalseTotal
if $elem->top == $copy 109 0 109
309 }
310 192 return $copy;
311 }
312  
313  
314 454 ▶sub topic_owned_in {
Subroutine topic_owned_in: called CC=21 SCAR=30.5
Pod documented
315 454 my ( $node, $list_functions ) = @_;
316 454 ▶ $list_functions //= \%LIST_FUNCTION;
Condition100%2/2
$list_functions //= \%LIST_FUNCTION
l!l
262192
MC/DC100%1/1
$list_functions //= \%LIST_FUNCTION
$list_functions
Truth table100%2/2
A
A= $list_functions
Aresult
11
00
317  
318 454 my @owned;
319 454 ▶ foreach my $statement ( @{ $node->find('PPI::Statement') || [] } ) {
Condition100%2/2
$node->find('PPI::Statement') || []
l!l
42925
MC/DC100%1/1
$node->find('PPI::Statement') || []
$node->find('PPI::Statement')
Truth table100%2/2
A
A= $node->find('PPI::Statement')
Aresult
11
00
320 1032 my @parts = $statement->schildren;
321 1032 ▶ if ( $statement->isa('PPI::Statement::Compound') ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($statement->isa('PPI::Statement::Compound')) { } 11 1021 1032
elsif ($statement->isa('PPI::Statement::Sub')) { } 22 999 1021
elsif (my $loop = &first(sub { $parts[$_]->isa('PPI::Token::Word') and $LOOP{$parts[$_]->content}; } , 1 .. $#parts)) { } 12 987 999
322 11 ▶ push @owned, grep { $_->isa('PPI::Structure::Block') } @parts[ 2 .. $#parts ] if $LOOP{ $parts[0] // q{} } && $parts[1] && $parts[1]->isa('PPI::Structure::List');
Branch: 1 branch
BranchTrueFalseTotal
if $LOOP{$parts[0] // ""} and $parts[1] and $parts[1]->isa("PPI::Structure::List") 6 5 11
Condition75%6/8
$LOOP{$parts[0] // ""} and $parts[1] and $parts[1]->isa("PPI::Structure::List")
!ll&&!rl&&r
236
$LOOP{$parts[0] // ""} and $parts[1]
!ll&&!rl&&r
209
$parts[0] // ""
l!l
110
MC/DC66%2/3
$LOOP{$parts[0] // ""} and $parts[1] and $parts[1]->isa("PPI::Structure::List")
$LOOP{$parts[0] // ""} $parts[1] $parts[1]->isa("PPI::Structure::List")
MC/DC0%0/1
$parts[0] // ""
$parts[0]
Truth table75%3/4
A and B and C
A= $LOOP{$parts[0] // ""}B= $parts[1]C= $parts[1]->isa("PPI::Structure::List")
ABCresult
0XX0
10X0
1100
1111
Truth table50%1/2
A
A= $parts[0]
Aresult
11
00
323 }
324 elsif ( $statement->isa('PPI::Statement::Sub') ) {
325 22 push @owned, $statement;
326 }
327 1288 ▶ elsif ( my $loop = first { $parts[$_]->isa('PPI::Token::Word') && $LOOP{ $parts[$_]->content } } 1 .. $#parts ) {
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$parts[$_]->isa("PPI::Token::Word") and $LOOP{$parts[$_]->content} 112 1176 1288
328 12 push @owned, @parts[ 0 .. $loop - 1 ];
329 }
330 }
331 454 ▶ foreach my $word ( @{ $node->find('PPI::Token::Word') || [] } ) {
Condition100%2/2
$node->find('PPI::Token::Word') || []
l!l
42727
MC/DC100%1/1
$node->find('PPI::Token::Word') || []
$node->find('PPI::Token::Word')
Truth table100%2/2
A
A= $node->find('PPI::Token::Word')
Aresult
11
00
332 809 my $next = $word->snext_sibling;
333 809 ▶ if ( $word->content eq 'sub' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($word->content eq "sub") 83 726 809
334 83 ▶ $next = $next->snext_sibling while $next && is_sub_header($next);
Branch: 1 branch
BranchTrueFalseTotal
$next and is_sub_header($next) and do { $next = $next->snext_sibling } 88 83 171
Condition66%2/3
$next and is_sub_header($next)
!ll&&!rl&&r
08388
MC/DC50%1/2
$next and is_sub_header($next)
$next is_sub_header($next)
Truth table66%2/3
A and B
A= $nextB= is_sub_header($next)
ABresult
0X0
100
111
335 }
336 809 ▶ push @owned, $next if $next && $next->isa('PPI::Structure::Block') && ( $word->content eq 'sub' || $list_functions->{ $word->content } );
Branch: 1 branch
BranchTrueFalseTotal
if $next and $next->isa("PPI::Structure::Block") and $word->content eq "sub" || $$list_functions{$word->content} 87 722 809
Condition100%9/9
$next and $next->isa("PPI::Structure::Block") and $word->content eq "sub" || $$list_functions{$word->content}
!ll&&!rl&&r
718487
$next and $next->isa("PPI::Structure::Block")
!ll&&!rl&&r
6865091
$word->content eq "sub" or $$list_functions{$word->content}
l!l&&r!l&&!r
61264
MC/DC100%4/4
$next and $next->isa("PPI::Structure::Block") and $word->content eq "sub" || $$list_functions{$word->content}
$next $next->isa("PPI::Structure::Block") $word->content eq "sub" $$list_functions{$word->content}
Truth table100%5/5
A and B and C or D
A= $nextB= $next->isa("PPI::Structure::Block")C= $word->content eq "sub"D= $$list_functions{$word->content}
ABCDresult
0XXX0
10XX0
11000
111X1
11011
337 809 ▶ push @owned, expression_of($word) if $list_functions->{ $word->content };
Branch: 1 branch
BranchTrueFalseTotal
if $$list_functions{$word->content} 45 764 809
338 }
339 454 return @owned;
340 }
341  
342  
343 64 ▶sub expression_of {
Subroutine expression_of: called CC=8 SCAR=21.1
Pod documented
344 64 my ($elem) = @_;
345  
346 64 ▶ return if !$EXPR_FORM{ $elem->content } || !is_function_call($elem);
Branch: 1 branch
BranchTrueFalseTotal
unless $EXPR_FORM{$elem->content} and is_function_call($elem) 2 62 64
Condition66%2/3
$EXPR_FORM{$elem->content} and is_function_call($elem)
!ll&&!rl&&r
2062
MC/DC50%1/2
$EXPR_FORM{$elem->content} and is_function_call($elem)
$EXPR_FORM{$elem->content} is_function_call($elem)
Truth table66%2/3
A and B
A= $EXPR_FORM{$elem->content}B= is_function_call($elem)
ABresult
0X0
100
111
347 62 my $arg = first_arg($elem);
348 62 ▶ return if !$arg || $arg->isa('PPI::Structure::Block');
Branch: 1 branch
BranchTrueFalseTotal
if not $arg or $arg->isa("PPI::Structure::Block") 25 37 62
Condition66%2/3
not $arg or $arg->isa("PPI::Structure::Block")
l!l&&r!l&&!r
02537
MC/DC50%1/2
not $arg or $arg->isa("PPI::Structure::Block")
not $arg $arg->isa("PPI::Structure::Block")
Truth table66%2/3
not(A) or B
A= not $argB= $arg->isa("PPI::Structure::Block")
ABresult
1X1
011
000
349  
350 37 my @parts;
351 37 ▶ for ( my $part = $arg; $part && !( $part->isa('PPI::Token::Operator') && $part->content eq q{,} ); $part = $part->snext_sibling ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($part and not $part->isa("PPI::Token::Operator") && $part->content eq ",") 61 37 98
Condition66%4/6
$part and not $part->isa("PPI::Token::Operator") && $part->content eq ","
!ll&&!rl&&r
03761
$part->isa('PPI::Token::Operator') && $part->content eq ','
!ll&&!rl&&r
61037
MC/DC33%1/3
$part and not $part->isa("PPI::Token::Operator") && $part->content eq ","
$part $part->isa('PPI::Token::Operator') $part->content eq ','
Truth table50%2/4
A and not(B && C)
A= $partB= $part->isa('PPI::Token::Operator')C= $part->content eq ','
ABCresult
0XX0
1110
10X1
1101
352 61 push @parts, $part;
353 }
354 37 return @parts;
355 }
356  
357  
358 171 ▶sub is_sub_header {
Subroutine is_sub_header: called CC=5 SCAR=16.1
Pod documented
359 171 my ($elem) = @_;
360 return
361 171 ▶ $elem->isa('PPI::Token::Prototype')
Condition91%11/12
$elem->isa('PPI::Token::Prototype') || $elem->isa('PPI::Token::Attribute') || $elem->isa('PPI::Structure::List') || $elem->isa('PPI::Token::Operator') && $elem->content eq ':'
l!l&&r!l&&!r
662283
$elem->isa("PPI::Token::Prototype") or $elem->isa("PPI::Token::Attribute") or $elem->isa("PPI::Structure::List")
l!l&&r!l&&!r
588105
$elem->isa("PPI::Token::Prototype") or $elem->isa("PPI::Token::Attribute")
l!l&&r!l&&!r
3622113
$elem->isa("PPI::Token::Operator") and $elem->content eq ":"
!ll&&!rl&&r
83022
MC/DC80%4/5
$elem->isa('PPI::Token::Prototype') || $elem->isa('PPI::Token::Attribute') || $elem->isa('PPI::Structure::List') || $elem->isa('PPI::Token::Operator') && $elem->content eq ':'
$elem->isa("PPI::Token::Prototype") $elem->isa("PPI::Token::Attribute") $elem->isa("PPI::Structure::List") $elem->isa("PPI::Token::Operator") $elem->content eq ":"
Truth table83%5/6
A or B or C || D and E
A= $elem->isa("PPI::Token::Prototype")B= $elem->isa("PPI::Token::Attribute")C= $elem->isa("PPI::Structure::List")D= $elem->isa("PPI::Token::Operator")E= $elem->content eq ":"
ABCDEresult
1XXXX1
01XXX1
001XX1
000111
0000X0
000100
362 || $elem->isa('PPI::Token::Attribute')
363 || $elem->isa('PPI::Structure::List')
364 || ( $elem->isa('PPI::Token::Operator') && $elem->content eq q{:} );
365 }
366  
367  
368 210 ▶sub mutates_topic {
Subroutine mutates_topic: called CC=2 SCAR=6.9
Pod documented
369 210 my ( $block, $doc ) = @_;
370 210 ▶ return 1 if $core_side_effect->( $block, $doc );
Branch: 1 branch
BranchTrueFalseTotal
if &$core_side_effect($block, $doc) 116 94 210
371 94 return chops_topic($block);
372 }
373  
374  
375 225 ▶sub aliased_statements {
Subroutine aliased_statements: called CC=5 SCAR=16.1
Pod documented
376 225 my ($block) = @_;
377  
378 225 my @statements;
379 225 foreach my $statement ( $block->schildren ) {
380 385 push @statements, $statement;
381 385 ▶ last if $statement->isa('PPI::Statement::Variable') && $statement->type eq 'local' && any { $_ eq q{$_} } $statement->variables;
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if $statement->isa("PPI::Statement::Variable") and $statement->type eq "local" and &any(sub { $_ eq "\$_"; } , $statement->variables) 9 376 385
Condition100%6/6
$statement->isa("PPI::Statement::Variable") and $statement->type eq "local" and &any(sub { $_ eq "\$_"; } , $statement->variables)
!ll&&!rl&&r
37429
$statement->isa("PPI::Statement::Variable") and $statement->type eq "local"
!ll&&!rl&&r
3482611
MC/DC100%3/3
$statement->isa("PPI::Statement::Variable") and $statement->type eq "local" and &any(sub { $_ eq "\$_"; } , $statement->variables)
$statement->isa("PPI::Statement::Variable") $statement->type eq "local" &any(sub { $_ eq "\$_"; } , $statement->variables)
Truth table100%4/4
A and B and C
A= $statement->isa("PPI::Statement::Variable")B= $statement->type eq "local"C= &any(sub { $_ eq "\$_"; } , $statement->variables)
ABCresult
0XX0
10X0
1100
1111
382 }
383 225 return @statements;
384 }
385  
386  
387 322 ▶sub chops_topic {
Subroutine chops_topic: called CC=2 SCAR=7.0
Pod documented
388 322 my ($block) = @_;
389 508 ▶ return any {
Subroutine __ANON__: called CC=5 SCAR=16.4
390 508 my $arg = first_arg($_);
391 508 ▶ $CHOPS{ $_->content } && is_function_call($_) && ( !$arg || ( $arg->isa('PPI::Token::Structure') && $arg->content eq q{;} ) )
Branch: 1 branch
BranchTrueFalseTotal
$CHOPS{$_->content} and is_function_call($_) and !$arg || $arg->isa("PPI::Token::Structure") && $arg->content eq ";" 4 504 508
Condition75%9/12
$CHOPS{$_->content} && is_function_call($_) && (!$arg || $arg->isa('PPI::Token::Structure') && $arg->content eq ';')
!ll&&!rl&&r
50413
$CHOPS{$_->content} and is_function_call($_)
!ll&&!rl&&r
50404
not $arg or $arg->isa("PPI::Token::Structure") and $arg->content eq ";"
l!l&&r!l&&!r
031
$arg->isa("PPI::Token::Structure") and $arg->content eq ";"
!ll&&!rl&&r
103
MC/DC40%2/5
$CHOPS{$_->content} && is_function_call($_) && (!$arg || $arg->isa('PPI::Token::Structure') && $arg->content eq ';')
$CHOPS{$_->content} is_function_call($_) not $arg $arg->isa("PPI::Token::Structure") $arg->content eq ";"
Truth table50%3/6
A and B && not(C) or D and E
A= $CHOPS{$_->content}B= is_function_call($_)C= not $argD= $arg->isa("PPI::Token::Structure")E= $arg->content eq ";"
ABCDEresult
0XXXX0
10XXX0
1100X0
110100
111XX1
110111
392 322 ▶ } @{ $block->find('PPI::Token::Word') || [] };
Condition100%2/2
$block->find('PPI::Token::Word') || []
l!l
31012
MC/DC100%1/1
$block->find('PPI::Token::Word') || []
$block->find('PPI::Token::Word')
Truth table100%2/2
A
A= $block->find('PPI::Token::Word')
Aresult
11
00
393 }
394  
395  
396 245 ▶sub packages_in {
Subroutine packages_in: called CC=2 SCAR=6.9
Pod documented
397 245 my ($ppi) = @_;
398 245 ▶ return $ppi->find('PPI::Statement::Package') || [];
Condition100%2/2
$ppi->find('PPI::Statement::Package') || []
l!l
12233
MC/DC100%1/1
$ppi->find('PPI::Statement::Package') || []
$ppi->find('PPI::Statement::Package')
Truth table100%2/2
A
A= $ppi->find('PPI::Statement::Package')
Aresult
11
00
399 }
400  
401  
402 391 ▶sub package_at {
Subroutine package_at: called CC=8 SCAR=25.3
Pod documented
403 391 my ( $elem, $packages ) = @_;
404  
405 391 my $package = 'main';
406 391 foreach my $statement (@$packages) {
407 61 ▶ my $block = first { $_->isa('PPI::Structure::Block') } $statement->schildren;
Subroutine __ANON__: called CC=1 SCAR=0.0
408 61 ▶ if ($block) {
Branch: 1 branch
BranchTrueFalseTotal
if ($block) 0 61 61
409 0 ▶ for ( my $up = $elem->parent; $up; $up = $up->parent ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($up) 0 0 0
410 0 ▶ return $statement->namespace if $up == $block;
Branch: 1 branch
BranchTrueFalseTotal
if $up == $block 0 0 0
411 }
412 0 next;
413 }
414 61 my ( $line, $col ) = @{ $statement->location }[ 0, 1 ];
415 61 my ( $at_line, $at_col ) = @{ $elem->location }[ 0, 1 ];
416 61 ▶ $package = $statement->namespace if $line < $at_line || ( $line == $at_line && $col < $at_col );
Branch: 1 branch
BranchTrueFalseTotal
if $line < $at_line or $line == $at_line and $col < $at_col 61 0 61
Condition50%3/6
$line < $at_line or $line == $at_line and $col < $at_col
l!l&&r!l&&!r
5920
$line == $at_line and $col < $at_col
!ll&&!rl&&r
002
MC/DC0%0/3
$line < $at_line or $line == $at_line and $col < $at_col
$line < $at_line $line == $at_line $col < $at_col
Truth table50%2/4
A or B and C
A= $line < $at_lineB= $line == $at_lineC= $col < $at_col
ABCresult
1XX1
0111
00X0
0100
417 }
418 391 return $package;
419 }
420  
421 2 1;
422  
423 __END__
↑ Summary