Perl/Lint/Policy/ValuesAndExpressions/RequireConstantVersion.pm

filter: Statementstmt 39.0% 39 / 100 Branchbran 17.8% 15 / 84 Conditioncond 12.1% 4 / 33 MC/DCmcdc 5.5% 1 / 18 Subroutinesub 85.7% 6 / 7 Podpod 0.0% 0 / 1 totaltotal 26.7% 65 / 243 CC 54 SCAR 70.5
CC
54
Cov
27%
evaluate
1335.7
_is_interpolation
12.0
BEGIN
1.0
CRAP 1158.3
« Perl/Lint/Policy/ControlStructures/ProhibitUnreachableCode.pm ↑ Summary Perl/Lint/Policy/Subroutines/ProtectPrivateSubs.pm »
1 package Perl::Lint::Policy::ValuesAndExpressions::RequireConstantVersion;
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use Perl::Lint::Constants::Type;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use parent "Perl::Lint::Policy";
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 DESC => '$VERSION value must be a constant',
9 EXPL => 'Computed $VERSION may tie the code to a single repository, or cause spooky action from a distance',
10 3 };
11  
12 2 ▶sub evaluate {
Subroutine evaluate: called CC=52 SCAR=72.0
Pod undocumented
13 2 my ($class, $file, $tokens, $src, $args) = @_;
14  
15 2 my $is_used_version = 0;
16 2 ▶ if (my $this_packages_arg = $args->{require_constant_version}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $this_packages_arg = $args->{"require_constant_version"}) 0 2 2
17 0 $is_used_version = $this_packages_arg->{allow_version_without_use_on_same_line};
18 }
19  
20 2 my @violations;
21  
22 2 my $is_version_assigner = 0;
23  
24 2 ▶ TOP: for (my $i = 0, my $token_type, my $token_data; my $token = $tokens->[$i]; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $token = $tokens->[$i]) 50 2 52
25 50 $token_type = $token->{type};
26 50 $token_data = $token->{data};
27  
28 # `use version;` declared?
29 50 ▶ if ($token_type == USED_NAME && $token_data eq 'version') {
Branch: 1 branch
BranchTrueFalseTotal
if ($token_type == 94 and $token_data eq "version") 0 50 50
Condition66%2/3
$token_type == 94 and $token_data eq "version"
!ll&&!rl&&r
4280
MC/DC0%0/2
$token_type == 94 and $token_data eq "version"
$token_type == 94 $token_data eq "version"
Truth table66%2/3
A and B
A= $token_type == 94B= $token_data eq "version"
ABresult
0X0
100
111
30 0 $is_used_version = 1;
31 0 next;
32 }
33  
34 # in assigning context?
35 50 ▶ if ($token_type == ASSIGN) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token_type == 65) 1 49 50
36 1 $is_version_assigner = 1;
37 1 next;
38 }
39  
40 # reset context information
41 49 ▶ if ($token_type == SEMI_COLON) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token_type == 106) 11 38 49
42 11 $is_version_assigner = 0;
43 11 next;
44 }
45  
46 38 ▶ if ($token_type == BUILTIN_FUNC) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token_type == 70) 1 37 38
47 1 ▶ $token = $tokens->[++$i] or last;
Branch: 1 branch
BranchTrueFalseTotal
$token = $$tokens[++$i] or last 1 0 1
48 1 ▶ if ($token->{type} == LEFT_PAREN) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token->{"type"} == 107) 0 1 1
49 # skip tokens which are surrounded by parenthesis
50 0 my $lpnum = 1;
51 0 ▶ for ($i++; $token = $tokens->[$i]; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token = $tokens->[$i]) 0 0 0
52 0 $token_type = $token->{type};
53  
54 0 ▶ if ($token_type == LEFT_PAREN) {
Branch: 2 branches
BranchTrueFalseTotal
if ($token_type == 107) { } 0 0 0
elsif ($token_type == 108) { } 0 0 0
55 0 $lpnum++;
56 }
57 elsif ($token_type == RIGHT_PAREN) {
58 0 ▶ last if --$lpnum <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if --$lpnum <= 0 0 0 0
59 }
60 }
61 }
62 # else: skip a token (means NOP)
63 }
64  
65 38 ▶ if ($token_type != GLOBAL_VAR && $token_type != VAR) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token_type != 194 and $token_type != 166) 36 2 38
Condition66%2/3
$token_type != 194 and $token_type != 166
!ll&&!rl&&r
0236
MC/DC50%1/2
$token_type != 194 and $token_type != 166
$token_type != 194 $token_type != 166
Truth table66%2/3
A and B
A= $token_type != 194B= $token_type != 166
ABresult
0X0
100
111
66 36 next;
67 }
68  
69 2 ▶ if ($token_data ne '$VERSION') {
Branch: 1 branch
BranchTrueFalseTotal
if ($token_data ne "\$VERSION") 2 0 2
70 2 next;
71 }
72  
73 0 ▶ if ($is_version_assigner) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_version_assigner) 0 0 0
74 # skip this!
75 0 $is_version_assigner = 0;
76 0 next;
77 }
78  
79 0 my $is_invalid = 0;
80 0 my $is_var_assigned = 0;
81  
82 # check assigning context or not
83 0 ▶ for ($i++; $token = $tokens->[$i]; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token = $tokens->[$i]) 0 0 0
84 0 $token_type = $token->{type};
85  
86 0 ▶ if ($token_type == ASSIGN || $token_type == OR_EQUAL) {
Branch: 3 branches
BranchTrueFalseTotal
if ($token_type == 65 or $token_type == 57) { } 0 0 0
elsif ($token_type == 35) { } 0 0 0
elsif ($token_type == 106) { } 0 0 0
Condition0%0/3
$token_type == 65 or $token_type == 57
l!l&&r!l&&!r
000
MC/DC0%0/2
$token_type == 65 or $token_type == 57
$token_type == 65 $token_type == 57
Truth table0%0/3
A or B
A= $token_type == 65B= $token_type == 57
ABresult
1X1
011
000
87 0 last;
88 }
89 elsif ($token_type == REG_OK) {
90 0 $is_invalid = 1;
91 0 last;
92 }
93 elsif ($token_type == SEMI_COLON) {
94 0 next TOP;
95 }
96 }
97  
98 0 ▶ if ($is_invalid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_invalid) 0 0 0
99 0 goto JUDGEMENT;
100 }
101  
102 0 ▶ for ($i++; $token = $tokens->[$i]; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token = $tokens->[$i]) 0 0 0
103 0 $token_type = $token->{type};
104 0 $token_data = $token->{data};
105  
106 0 ▶ if ($token_type == SEMI_COLON) {
Branch: 7 branches
BranchTrueFalseTotal
if ($token_type == 106) { } 0 0 0
elsif ($token_type == 172) { } 0 0 0
elsif ($token_type == 146) { } 0 0 0
elsif ($token_type == 70 or $token_type == 87 or $token_type == 19 or $token_type == 127 or $token_type == 35 or $token_type == 111) { } 0 0 0
elsif ($token_type == 65) { } 0 0 0
elsif ($token_type == 166 or $token_type == 194) { } 0 0 0
elsif ($token_type == 122) { } 0 0 0
107 0 last;
108 }
109 elsif ($token_type == STRING) {
110 0 ▶ if ($is_invalid = $class->_is_interpolation($token_data)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_invalid = $class->_is_interpolation($token_data)) 0 0 0
111 0 last;
112 }
113 }
114 elsif ($token_type == REG_DOUBLE_QUOTE) {
115 0 $i += 2; # skip delimiter
116 0 ▶ $token = $tokens->[$i] or last;
Branch: 1 branch
BranchTrueFalseTotal
$token = $tokens->[$i] or last 0 0 0
117 0 ▶ if ($is_invalid = $class->_is_interpolation($token->{data})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_invalid = $class->_is_interpolation($token->{"data"})) 0 0 0
118 0 last;
119 }
120 }
121 ▶ elsif (
Condition0%0/15
$token_type == 70 or $token_type == 87 or $token_type == 19 or $token_type == 127 or $token_type == 35 or $token_type == 111
l!l&&r!l&&!r
000
$token_type == 70 or $token_type == 87 or $token_type == 19 or $token_type == 127 or $token_type == 35
l!l&&r!l&&!r
000
$token_type == 70 or $token_type == 87 or $token_type == 19 or $token_type == 127
l!l&&r!l&&!r
000
$token_type == 70 or $token_type == 87 or $token_type == 19
l!l&&r!l&&!r
000
$token_type == 70 or $token_type == 87
l!l&&r!l&&!r
000
MC/DC0%0/6
$token_type == 70 or $token_type == 87 or $token_type == 19 or $token_type == 127 or $token_type == 35 or $token_type == 111
$token_type == 70 $token_type == 87 $token_type == 19 $token_type == 127 $token_type == 35 $token_type == 111
Truth table0%0/7
A or B or C or D or E or F
A= $token_type == 70B= $token_type == 87C= $token_type == 19D= $token_type == 127E= $token_type == 35F= $token_type == 111
ABCDEFresult
1XXXXX1
01XXXX1
001XXX1
0001XX1
00001X1
0000011
0000000
122 $token_type == BUILTIN_FUNC ||
123 $token_type == DO || # do {...}
124 $token_type == STRING_MUL || # "a" x 42
125 $token_type == NAMESPACE || # call other package
126 $token_type == REG_OK || # =~
127 $token_type == LEFT_BRACKET # access element of array
128 ) {
129 0 $is_invalid = 1;
130 0 last;
131 }
132 elsif ($token_type == ASSIGN) {
133 0 $is_var_assigned = 0;
134 }
135 ▶ elsif ($token_type == VAR || $token_type == GLOBAL_VAR) {
Condition0%0/3
$token_type == 166 or $token_type == 194
l!l&&r!l&&!r
000
MC/DC0%0/2
$token_type == 166 or $token_type == 194
$token_type == 166 $token_type == 194
Truth table0%0/3
A or B
A= $token_type == 166B= $token_type == 194
ABresult
1X1
011
000
136 0 $is_var_assigned = 1;
137 }
138 elsif ($token_type == KEY) {
139 0 ▶ if ($token_data eq 'qv') { # for `qv(...)` notation
Branch: 2 branches
BranchTrueFalseTotal
if ($token_data eq 'qv') { } 0 0 0
elsif ($token_data eq 'version') { } 0 0 0
140 0 ▶ if (!$is_used_version) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_used_version) 0 0 0
141 0 $is_invalid = 1;
142 0 last;
143 }
144 }
145 elsif ($token_data eq 'version') { # for `version->new(...)` notation
146 0 ▶ if (!$is_used_version) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_used_version) 0 0 0
147 0 $is_invalid = 1;
148 0 last;
149 }
150  
151 0 ▶ $token = $tokens->[++$i] or last;
Branch: 1 branch
BranchTrueFalseTotal
$token = $$tokens[++$i] or last 0 0 0
152 0 ▶ if ($token->{type} != POINTER) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token->{"type"} != 125) 0 0 0
153 0 next;
154 }
155  
156 0 ▶ $token = $tokens->[++$i] or last;
Branch: 1 branch
BranchTrueFalseTotal
$token = $$tokens[++$i] or last 0 0 0
157 0 ▶ if ($token->{type} != METHOD && $token->{data} ne 'new') {
Branch: 1 branch
BranchTrueFalseTotal
if ($token->{"type"} != 64 and $token->{"data"} ne "new") 0 0 0
Condition0%0/3
$token->{"type"} != 64 and $token->{"data"} ne "new"
!ll&&!rl&&r
000
MC/DC0%0/2
$token->{"type"} != 64 and $token->{"data"} ne "new"
$token->{"type"} != 64 $token->{"data"} ne "new"
Truth table0%0/3
A and B
A= $token->{"type"} != 64B= $token->{"data"} ne "new"
ABresult
0X0
100
111
158 0 next;
159 }
160 }
161 else { # for others
162 0 $is_invalid = 1;
163 0 last;
164 }
165 }
166 }
167  
168 JUDGEMENT:
169 0 ▶ if ($is_invalid || $is_var_assigned) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_invalid or $is_var_assigned) 0 0 0
Condition0%0/3
$is_invalid or $is_var_assigned
l!l&&r!l&&!r
000
MC/DC0%0/2
$is_invalid or $is_var_assigned
$is_invalid $is_var_assigned
Truth table0%0/3
A or B
A= $is_invalidB= $is_var_assigned
ABresult
1X1
011
000
170 push @violations, {
171 filename => $file,
172 line => $token->{line},
173 0 description => DESC,
174 explanation => EXPL,
175 policy => __PACKAGE__,
176 };
177 }
178 }
179  
180 2 return \@violations;
181 }
182  
183 0 ▶sub _is_interpolation {
Subroutine _is_interpolation: not called CC=3 SCAR=24.8
184 0 my ($class, $str) = @_;
185  
186 0 ▶ while ($str =~ /(\\*)(\$\S+)/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /(\\*)(\$\S+)/cg) 0 0 0
187 0 ▶ if (length($1) % 2 == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length($1) % 2 == 0) { } 0 0 0
188 # sigil is not escaped
189 # interpolated!
190 0 return 1;
191 }
192 else {
193 # sigil is escaped
194 0 next;
195 }
196 }
197  
198 0 return;
199 }
200  
201 3 1;
202  
« Perl/Lint/Policy/ControlStructures/ProhibitUnreachableCode.pm ↑ Summary Perl/Lint/Policy/Subroutines/ProtectPrivateSubs.pm »