Policy/Subroutines/RequireSubOrder.pm

filter: Statementstmt 92.4% 135 / 146 Branchbran 84.8% 56 / 66 Conditioncond 78.7% 26 / 33 MC/DCmcdc 65.0% 13 / 20 Subroutinesub 95.0% 19 / 20 Podpod 66.6% 6 / 9 totaltotal 86.7% 255 / 294 CC 53 SCAR 23.2
CC
53
Cov
89%
violates
20.1
builder
13.1
_blockcalls
8.1
CRAP 10.1
« Policy/Variables/ProhibitTopicIterator.pm ↑ Summary Policy/Subroutines/RequireConsistentReturn.pm »
1 package Perl::Critic::Policy::Subroutines::RequireSubOrder;
2  
3 11 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 11 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 11 ▶use Perl::Critic::Utils qw/:severities :classification/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 11 ▶use base 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 11 our $VERSION = '0.1.4';
12  
13 11 Readonly::Scalar my $DESC => q{Place subroutines in dependency order};
14 11 Readonly::Scalar my $EXPL => undef;
15  
16 #-----------------------------------------------------------------------------
17  
18 8 ▶sub supported_parameters {
Subroutine supported_parameters: called CC=1 SCAR=0.0
Pod undocumented
19 return (
20 {
21 8 name => 'all',
22 description => 'Report all names a subroutine must appear after.',
23 default_string => '0',
24 behavior => 'boolean',
25 },
26 {
27 name => 'backwards',
28 description => 'Expect dependencies to follow their callers.',
29 default_string => '0',
30 behavior => 'boolean',
31 },
32 );
33 }
34  
35 47 ▶sub applies_to { return qw/PPI::Statement::Sub/ }
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
36 23 ▶sub default_severity { return $SEVERITY_LOW }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
37 0 ▶sub default_themes { return qw/cosmetic/ }
Subroutine default_themes: not called CC=1 SCAR=6.9
Pod documented
38  
39 #-----------------------------------------------------------------------------
40  
41 23 ▶sub invalid {
Subroutine invalid: called CC=3 SCAR=11.3
Pod undocumented
42 23 my ($self,$elem,$note)=@_;
43 23 ▶ $note//='';
Condition50%1/2
$note //= ""
l!l
230
MC/DC0%0/1
$note //= ""
$note
Truth table50%1/2
A
A= $note
Aresult
11
00
44 23 ▶ if($note) { $note=" ($note)" }
Branch: 1 branch
BranchTrueFalseTotal
if ($note) 23 0 23
45 23 return $self->violation(sprintf("%s%s",$DESC,$note),$EXPL,$elem);
46 }
47  
48 200 ▶sub _loc { my ($node)=@_; return join(' ',map {$_//'U'} @{$node->location()}) }
Subroutine _loc: called CC=2 SCAR=6.9
Condition100%2/2
$_ // "U"
l!l
800200
MC/DC100%1/1
$_ // "U"
$_
Truth table100%2/2
A
A= $_
Aresult
11
00
49  
50 123 ▶sub _blockcalls {
Subroutine _blockcalls: called CC=8 SCAR=20.9
51 123 my ($self,$package,$doc)=@_;
52 123 ▶ if(!$doc) { return }
Branch: 1 branch
BranchTrueFalseTotal
unless ($doc) 1 122 123
53 122 my %res;
54 122 ▶ foreach my $word (grep {is_function_call($_)&&!is_perl_builtin($_)} @{$doc->find('PPI::Token::Word')||[]}) {
Condition60%3/5
is_function_call($_) && !is_perl_builtin($_)
!ll&&!rl&&r
0046
$doc->find('PPI::Token::Word') || []
l!l
4379
MC/DC0%0/2
is_function_call($_) && !is_perl_builtin($_)
is_function_call($_) !is_perl_builtin($_)
MC/DC100%1/1
$doc->find('PPI::Token::Word') || []
$doc->find('PPI::Token::Word')
Truth table33%1/3
A && not(B)
A= is_function_call($_)B= !is_perl_builtin($_)
ABresult
0X0
100
111
Truth table100%2/2
A
A= $doc->find('PPI::Token::Word')
Aresult
11
00
55 46 my $name=$word->content();
56 46 ▶ if($name=~/^(.*)::([^:]+)$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($name =~ /^(.*)::([^:]+)$/) { } 9 37 46
elsif (not $name =~ /::/) { } 37 0 37
57 9 ▶ if($1 eq $package) { $res{$2}=undef }
Branch: 1 branch
BranchTrueFalseTotal
if ($1 eq $package) 7 2 9
58 }
59 37 elsif($name!~/::/) { $res{$name}=undef }
60 }
61 122 return keys(%res);
62 }
63  
64 # The Perl compiler fully scans the document for subroutine names, permitting out-of-order
65 # dependencies, so it's necessary here to fully map all subroutine names, and their packages, to
66 # determine if dependencies are declared within the document. Anything encountered but not
67 # declared is imported, via 'use' or 'do', and assumed declared prior to use. If the author
68 # calls 'do' too late, that's a runtime error not detected here. Likewise, anonymous subs are
69 # ignored since standard variable scoping controls their availability.
70  
71 11 my $cachekey;
72 8 ▶sub initialize_if_enabled {
Subroutine initialize_if_enabled: called CC=4 SCAR=15.7
Pod documented
73 8 my ($self,$config)=@_;
74 8 my $name='__REQUIRESUBORDER__';
75 8 my $suffix=int(rand(1e9));
76 8 my $retry=3;
77 0 ▶ while($retry&&exists($$self{"$name$suffix"})) { $suffix=int(rand(1e9)); $retry-- }
Branch: 1 branch
BranchTrueFalseTotal
if ($retry and exists $$self{"$name$suffix"}) 0 8 8
Condition33%1/3
$retry and exists $$self{"$name$suffix"}
!ll&&!rl&&r
080
MC/DC0%0/2
$retry and exists $$self{"$name$suffix"}
$retry exists $$self{"$name$suffix"}
Truth table33%1/3
A and B
A= $retryB= exists $$self{"$name$suffix"}
ABresult
0X0
100
111
78 0 ▶ if(exists($$self{"$name$suffix"})) { warn 'RequireSubOrder unable to build subroutine cache' }
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$self{"$name$suffix"}) { } 0 8 8
79 else {
80 8 $cachekey="$name$suffix";
81 8 $$self{$cachekey}={cache=>{},recent=>[]};
82 }
83 8 return 1;
84 }
85  
86 65 ▶sub builder {
Subroutine builder: called CC=13 SCAR=25.7
Pod undocumented
87 65 my ($self,$doc,$package)=@_;
88 65 ▶ $package//='main';
Condition100%2/2
$package //= "main"
l!l
1847
MC/DC100%1/1
$package //= "main"
$package
Truth table100%2/2
A
A= $package
Aresult
11
00
89 65 foreach my $node ($doc->children()) {
90 223 ▶ if($node->isa('PPI::Statement::Package')) {
Branch: 4 branches
BranchTrueFalseTotal
if ($node->isa('PPI::Statement::Package')) { } 39 184 223
elsif ($node->isa('PPI::Statement::Sub')) { } 100 84 184
elsif ($node->isa('PPI::Statement::Compound') or $node->isa('PPI::Structure::Block')) { } 12 72 84
elsif ($node->isa('PPI::Statement')) { } 23 49 72
91 39 ▶ if(my $block=$node->find_first('PPI::Structure::Block')) { $self->builder($block,$node->namespace()) }
Branch: 1 branch
BranchTrueFalseTotal
if (my $block = $node->find_first('PPI::Structure::Block')) { } 6 33 39
92 33 else { $package=$node->namespace() }
93 }
94 elsif($node->isa('PPI::Statement::Sub')) {
95 100 my $loc=_loc($node);
96 100 ▶ $$self{$cachekey}{sub}{$node->name()}//={};
Condition100%2/2
$self->{$cachekey}{"sub"}{$node->name} //= {}
l!l
1783
MC/DC100%1/1
$self->{$cachekey}{"sub"}{$node->name} //= {}
$self->{$cachekey}{"sub"}{$node->name}
Truth table100%2/2
A
A= $self->{$cachekey}{"sub"}{$node->name}
Aresult
11
00
97 100 my $csub=$$self{$cachekey}{sub}{$node->name()};
98 0 ▶ if(exists($$csub{$loc})) { warn "A declaration of subroutine @{[$node->name()]} has already been encountered at this location" }
Branch: 1 branch
BranchTrueFalseTotal
if (exists $csub->{$loc}) 0 100 100
99 100 $$csub{$loc}={
100 package=>$package,
101 node=>$node,
102 name=>$node->name(),
103 loc=>$node->location(),
104 calls=>[$self->_blockcalls($package,$node->block())],
105 };
106 100 $$self{$cachekey}{package}{$package}{$node->name()}{$loc}=$$csub{$loc};
107 }
108 12 ▶ elsif($node->isa('PPI::Statement::Compound')||$node->isa('PPI::Structure::Block')) { $self->builder($node,$package) }
Condition100%3/3
$node->isa("PPI::Statement::Compound") or $node->isa("PPI::Structure::Block")
l!l&&r!l&&!r
6672
MC/DC100%2/2
$node->isa("PPI::Statement::Compound") or $node->isa("PPI::Structure::Block")
$node->isa("PPI::Statement::Compound") $node->isa("PPI::Structure::Block")
Truth table100%3/3
A or B
A= $node->isa("PPI::Statement::Compound")B= $node->isa("PPI::Structure::Block")
ABresult
1X1
011
000
109 elsif($node->isa('PPI::Statement')) {
110 23 ▶ $$self{$cachekey}{outer}{"${package}::"}//={package=>$package,node=>$doc,calls=>{}};
Condition100%2/2
$self->{$cachekey}{"outer"}{"${package}::"} //= {"package", $package, "node", $doc, "calls", {}}
l!l
221
MC/DC100%1/1
$self->{$cachekey}{"outer"}{"${package}::"} //= {"package", $package, "node", $doc, "calls", {}}
$self->{$cachekey}{"outer"}{"${package}::"}
Truth table100%2/2
A
A= $self->{$cachekey}{"outer"}{"${package}::"}
Aresult
11
00
111 23 my $calls=$$self{$cachekey}{outer}{"${package}::"}{calls};
112 23 foreach my $call ($self->_blockcalls($package,$node)) { push @{$$calls{$call}},{loc=>$node->location(),call=>$call} }
113 }
114 # else do nothing
115 }
116 65 return 1;
117 }
118  
119 47 ▶sub prepare_to_scan_document {
Subroutine prepare_to_scan_document: called CC=2 SCAR=7.2
Pod documented
120 47 my ($self,$doc,$package)=@_;
121 0 ▶ if(!$cachekey) { return 0 }
Branch: 1 branch
BranchTrueFalseTotal
unless ($cachekey) 0 47 47
122 47 %{$$self{$cachekey}}=();
123 47 return $self->builder($doc,$package);
124 }
125  
126 44 ▶sub _cmploc {
Subroutine _cmploc: called CC=5 SCAR=17.8
127 44 my ($X,$Y)=@_;
128 0 ▶ if($$X[0]<$$Y[0]) { return -1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($X->[0] < $Y->[0]) 0 44 44
129 0 ▶ if($$X[0]>$$Y[0]) { return +1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($X->[0] > $Y->[0]) 0 44 44
130 44 ▶ if($$X[1]<$$Y[1]) { return -1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($X->[1] < $Y->[1]) 20 24 44
131 24 ▶ if($$X[1]>$$Y[1]) { return +1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($X->[1] > $Y->[1]) 24 0 24
132 0 return 0;
133 }
134  
135 84 ▶sub _sortloc {
Subroutine _sortloc: called CC=1 SCAR=0.0
136 84 my (@items)=@_;
137 84 return sort {_cmploc($$a{loc},$$b{loc})} @items;
138 }
139  
140 22 ▶sub _depends {
Subroutine _depends: called CC=4 SCAR=13.9
141 22 my ($self,$pkg,$name,$dep)=@_;
142 22 foreach my $definition (values %{$$self{$cachekey}{package}{$pkg}{$name}}) {
143 22 foreach my $name (@{$$definition{calls}}) {
144 3 ▶ if($name eq $dep) { return 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq $dep) 2 1 3
145 }
146 }
147 20 return 0;
148 }
149  
150 100 ▶sub violates {
Subroutine violates: called CC=20 SCAR=30.0
Pod documented
151 100 my ($self,$elem,undef)=@_;
152 100 my $sub=$$self{$cachekey}{sub}{$elem->name()}{_loc($elem)};
153 0 ▶ if(!$sub) { return }
Branch: 1 branch
BranchTrueFalseTotal
unless ($sub) 0 100 100
154 #
155 100 my @dependencies;
156 100 foreach my $dep (@{$$sub{calls}}) {
157 38 my @deps=_sortloc(values(%{$$self{$cachekey}{package}{$$sub{package}}{$dep}}));
158 38 ▶ if(@deps) {
Branch: 1 branch
BranchTrueFalseTotal
if (@deps) 34 4 38
159 34 ▶ if ( $$self{_backwards} && (_cmploc($deps[-1]{loc},$elem->location())<0)){ push @dependencies,$deps[0] }
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'_backwards'} and _cmploc($deps[-1]{'loc'}, $elem->location) < 0) { } 2 32 34
elsif (not $self->{'_backwards'} and _cmploc($deps[0]{'loc'}, $elem->location) > 0) { } 20 12 32
Condition66%2/3
$self->{"_backwards"} and _cmploc($deps[-1]{"loc"}, $elem->location) < 0
!ll&&!rl&&r
3202
MC/DC50%1/2
$self->{"_backwards"} and _cmploc($deps[-1]{"loc"}, $elem->location) < 0
$self->{"_backwards"} _cmploc($deps[-1]{"loc"}, $elem->location) < 0
Truth table66%2/3
A and B
A= $self->{"_backwards"}B= _cmploc($deps[-1]{"loc"}, $elem->location) < 0
ABresult
0X0
100
111
160 20 ▶ elsif(!$$self{_backwards} && (_cmploc($deps[0]{loc}, $elem->location())>0)){ push @dependencies,$deps[-1] }
Condition66%2/3
not $self->{"_backwards"} and _cmploc($deps[0]{"loc"}, $elem->location) > 0
!ll&&!rl&&r
01220
MC/DC50%1/2
not $self->{"_backwards"} and _cmploc($deps[0]{"loc"}, $elem->location) > 0
not $self->{"_backwards"} _cmploc($deps[0]{"loc"}, $elem->location) > 0
Truth table66%2/3
not(A) and B
A= not $self->{"_backwards"}B= _cmploc($deps[0]{"loc"}, $elem->location) > 0
ABresult
0X0
100
111
161 }
162 }
163 100 ▶ if(@dependencies) {
Branch: 1 branch
BranchTrueFalseTotal
if (@dependencies) 19 81 100
164 19 @dependencies=map {+{cyclic=>($self->_depends(@$_{qw/package name/},$$sub{name})?' (cyclic)':''),
165 22 ▶ package=>$$_{package},name=>$$_{name}}} _sortloc(@dependencies);
Branch: 1 branch
BranchTrueFalseTotal
$self->_depends(@$_{'package', 'name'}, $sub->{'name'}) ? : 2 20 22
166 19 ▶ if(!$$self{_all}) { @dependencies=$dependencies[-1] }
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_all"}) 18 1 19
167 19 @dependencies=join(q{, },map {sprintf("'%s::%s'%s",@$_{qw/package name cyclic/})} @dependencies);
168 19 ▶ if($$self{_backwards}) { return $self->invalid($elem,sprintf("move %s after '%s::%s'",$dependencies[0],$$sub{package},$elem->name())) }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_backwards'}) { } 2 17 19
169 17 else { return $self->invalid($elem,sprintf("move '%s::%s' after %s",$$sub{package},$elem->name(),$dependencies[0])) }
170 }
171 #
172 81 @dependencies=();
173 81 ▶ if(defined($$self{$cachekey}{outer}{"$$sub{package}::"})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{$cachekey}{"outer"}{qq[$sub->{"package"}::]}) 23 58 81
174 23 my @deps=_sortloc(@{$$self{$cachekey}{outer}{"$$sub{package}::"}{calls}{$$sub{name}}});
175 23 ▶ if(@deps) {
Branch: 1 branch
BranchTrueFalseTotal
if (@deps) 6 17 23
176 6 ▶ if ( $$self{_backwards}&&(_cmploc($deps[-1]{loc},$elem->location())>0)) { push @dependencies,$deps[-1] }
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'_backwards'} and _cmploc($deps[-1]{'loc'}, $elem->location) > 0) { } 1 5 6
elsif (not $self->{'_backwards'} and _cmploc($deps[0]{'loc'}, $elem->location) < 0) { } 3 3 6
Condition100%3/3
$self->{"_backwards"} and _cmploc($deps[-1]{"loc"}, $elem->location) > 0
!ll&&!rl&&r
411
MC/DC100%2/2
$self->{"_backwards"} and _cmploc($deps[-1]{"loc"}, $elem->location) > 0
$self->{"_backwards"} _cmploc($deps[-1]{"loc"}, $elem->location) > 0
Truth table100%3/3
A and B
A= $self->{"_backwards"}B= _cmploc($deps[-1]{"loc"}, $elem->location) > 0
ABresult
0X0
100
111
177 3 ▶ elsif(!$$self{_backwards}&&(_cmploc($deps[0]{loc}, $elem->location())<0)) { push @dependencies,$deps[0] }
Condition100%3/3
not $self->{"_backwards"} and _cmploc($deps[0]{"loc"}, $elem->location) < 0
!ll&&!rl&&r
113
MC/DC100%2/2
not $self->{"_backwards"} and _cmploc($deps[0]{"loc"}, $elem->location) < 0
not $self->{"_backwards"} _cmploc($deps[0]{"loc"}, $elem->location) < 0
Truth table100%3/3
not(A) and B
A= not $self->{"_backwards"}B= _cmploc($deps[0]{"loc"}, $elem->location) < 0
ABresult
0X0
100
111
178 }
179 23 ▶ if(@dependencies) {
Branch: 1 branch
BranchTrueFalseTotal
if (@dependencies) 4 19 23
180 4 @dependencies=_sortloc(@dependencies);
181 4 ▶ if($$self{_backwards}) { return $self->invalid($elem,sprintf("move '%s::%s' after caller on line %d",@$sub{qw/package name/},$dependencies[-1]{loc}[0])) }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_backwards'}) { } 1 3 4
182 3 else { return $self->invalid($elem,sprintf("move '%s::%s' before caller on line %d",@$sub{qw/package name/},$dependencies[0]{loc}[0])) }
183 }
184 }
185 77 return;
186 }
187  
188 #-----------------------------------------------------------------------------
189  
190 11 1;
191  
192 __END__
« Policy/Variables/ProhibitTopicIterator.pm ↑ Summary Policy/Subroutines/RequireConsistentReturn.pm »