Pulp/Utils.pm

filter: Statementstmt 94.5% 69 / 73 Branchbran 76.6% 23 / 30 Conditioncond 80.6% 25 / 31 MC/DCmcdc 60.0% 9 / 15 Subroutinesub 93.3% 14 / 15 Podpod 57.1% 4 / 7 totaltotal 84.2% 144 / 171 CC 27 SCAR 16.9
CC
27
Cov
87%
include_module_version
10.1
include_module_first_arg
7.1
parameter_parse_version
7.0
CRAP 5.4
« Policy/ValuesAndExpressions/ProhibitNullStatements.pm ↑ Summary Policy/Modules/ProhibitPOSIXimport.pm »
1 # Copyright 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2019, 2021 Kevin Ryde
2  
3 # This file is part of Perl-Critic-Pulp.
4  
5 # Perl-Critic-Pulp is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by the
7 # Free Software Foundation; either version 3, or (at your option) any later
8 # version.
9 #
10 # Perl-Critic-Pulp is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 # for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with Perl-Critic-Pulp. If not, see <http://www.gnu.org/licenses/>.
17  
18  
19 package Perl::Critic::Pulp::Utils;
20 41 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 41 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 41 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 41 ▶use version (); # but don't import qv()
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 41 our $VERSION = 100;
26  
27 41 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
28 41 our @EXPORT_OK = qw(parameter_parse_version
29 version_if_valid
30 include_module_version
31 elem_package
32 elem_in_BEGIN
33 elem_is_comma_operator
34 %COMMA);
35  
36 41 our %COMMA = (',' => 1,
37 '=>' => 1);
38  
39 92 ▶sub parameter_parse_version {
Subroutine parameter_parse_version: called CC=4 SCAR=19.4
Pod documented
40 92 my ($self, $parameter, $str) = @_;
41  
42 92 my $version;
43 92 ▶ if (defined $str && $str ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $str and $str ne "") 0 92 92
Condition33%1/3
defined $str and $str ne ""
!ll&&!rl&&r
9200
MC/DC0%0/2
defined $str and $str ne ""
defined $str $str ne ""
Truth table33%1/3
A and B
A= defined $strB= $str ne ""
ABresult
0X0
100
111
44 0 $version = version_if_valid ($str);
45 0 ▶ if (! defined $version) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $version) 0 0 0
46 0 $self->throw_parameter_value_exception
47 ($parameter->get_name,
48 $str,
49 undef, # source
50 'invalid version number string');
51 }
52 }
53 92 $self->__set_parameter_value ($parameter, $version);
54 }
55  
56 # return a version.pm object, or undef if $str is invalid
57 20 ▶sub version_if_valid {
Subroutine version_if_valid: called CC=2 SCAR=7.1
Pod undocumented
58 20 my ($str) = @_;
59 # this is a nasty hack to notice "not a number" warnings, and for version
60 # 0.81 possibly throwing errors too
61 20 my $good = 1;
62 20 my $version;
63 0 ▶ { local $SIG{'__WARN__'} = sub { $good = 0 };
Subroutine __ANON__: not called CC=1 SCAR=0.4
64 20 eval { $version = version->new($str) };
65 }
66 20 ▶ return ($good ? $version : undef);
Branch: 1 branch
BranchTrueFalseTotal
$good ? : 20 0 20
67 }
68  
69 # This regexp is what Perl's toke.c S_force_version() demands, as of
70 # versions 5.004 through 5.8.9. A version number in a "use" must start with
71 # a digit and then have only digits, dots and underscores. In particular
72 # other normal numeric forms like hex or exponential are not taken to be
73 # version numbers, and even omitting the 0 from a decimal like ".25" is not
74 # a version number.
75 #
76 41 our $use_module_version_number_re = qr/^v?[0-9][0-9._]*$/;
77  
78 416 ▶sub include_module_version {
Subroutine include_module_version: called CC=10 SCAR=23.1
Pod undocumented
79 416 my ($inc) = @_;
80  
81 # only a module style "use Foo", not a perl version num like "use 5.010"
82 416 ▶ defined ($inc->module) || return undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $inc->module or return undef 416 0 416
83  
84 416 ▶ my $ver = $inc->schild(2) || return undef;
Condition100%2/2
$inc->schild(2) || (return undef)
l!l
4079
MC/DC100%1/1
$inc->schild(2) || (return undef)
$inc->schild(2)
Truth table100%2/2
A
A= $inc->schild(2)
Aresult
11
00
85 # ENHANCE-ME: when PPI recognises v-strings may have to extend this
86 407 ▶ $ver->isa('PPI::Token::Number') || return undef;
Branch: 1 branch
BranchTrueFalseTotal
$ver->isa("PPI::Token::Number") or return undef 162 245 407
87  
88 162 ▶ $ver->content =~ $use_module_version_number_re or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$ver->content =~ /$Perl::Critic::Pulp::Utils::use_module_version_number_re/ or return undef 154 8 162
89  
90 # must be followed by whitespace, or comment, or end of statement, so
91 #
92 # use Foo 10 -3; <- version 10, arg -3
93 # use Foo 10-3; <- arg 7
94 #
95 # use Foo 10# <- version 10, arg -3
96 # -3;
97 #
98 154 ▶ if (my $after = $ver->next_sibling) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $after = $ver->next_sibling) 139 15 154
99 139 ▶ unless ($after->isa('PPI::Token::Whitespace')
Branch: 1 branch
BranchTrueFalseTotal
unless ($after->isa("PPI::Token::Whitespace") or $after->isa("PPI::Token::Comment") or $after->isa("PPI::Token::Structure") and $after eq ";") 26 113 139
Condition77%7/9
$after->isa("PPI::Token::Whitespace") or $after->isa("PPI::Token::Comment") or $after->isa("PPI::Token::Structure") and $after eq ";"
l!l&&r!l&&!r
615226
$after->isa("PPI::Token::Whitespace") or $after->isa("PPI::Token::Comment")
l!l&&r!l&&!r
61078
$after->isa("PPI::Token::Structure") and $after eq ";"
!ll&&!rl&&r
26052
MC/DC50%2/4
$after->isa("PPI::Token::Whitespace") or $after->isa("PPI::Token::Comment") or $after->isa("PPI::Token::Structure") and $after eq ";"
$after->isa("PPI::Token::Whitespace") $after->isa("PPI::Token::Comment") $after->isa("PPI::Token::Structure") $after eq ";"
Truth table60%3/5
A or B or C and D
A= $after->isa("PPI::Token::Whitespace")B= $after->isa("PPI::Token::Comment")C= $after->isa("PPI::Token::Structure")D= $after eq ";"
ABCDresult
1XXX1
01XX1
00111
000X0
00100
100 || $after->isa('PPI::Token::Comment')
101 || ($after->isa('PPI::Token::Structure')
102 && $after eq ';')) {
103 26 return undef;
104 }
105 }
106  
107 128 return $ver;
108 }
109  
110 # $inc is a PPI::Statement::Include.
111 # Return the element which is the start of the first argument to its
112 # import() or unimport(), for "use" or "no" respectively.
113 #
114 # A "require" is treated the same as "use" and "no", but arguments to it
115 # like "require Foo::Bar '-init';" is in fact a syntax error.
116 #
117 288 ▶sub include_module_first_arg {
Subroutine include_module_first_arg: called CC=7 SCAR=19.6
Pod undocumented
118 288 my ($inc) = @_;
119 288 ▶ defined ($inc->module) || return;
Branch: 1 branch
BranchTrueFalseTotal
defined $inc->module or return 288 0 288
120 288 my $arg;
121 288 ▶ if (my $ver = include_module_version ($inc)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ver = include_module_version($inc)) { } 72 216 288
122 72 $arg = $ver->snext_sibling;
123 } else {
124 # eg. "use Foo 'xxx'"
125 216 $arg = $inc->schild(2);
126 }
127 # don't return terminating ";"
128 288 ▶ if ($arg
Branch: 1 branch
BranchTrueFalseTotal
if ($arg and $arg->isa("PPI::Token::Structure") and $arg->content eq ";" and not $arg->snext_sibling) 41 247 288
Condition77%7/9
$arg and $arg->isa("PPI::Token::Structure") and $arg->content eq ";" and not $arg->snext_sibling
!ll&&!rl&&r
247041
$arg and $arg->isa("PPI::Token::Structure") and $arg->content eq ";"
!ll&&!rl&&r
247041
$arg and $arg->isa("PPI::Token::Structure")
!ll&&!rl&&r
1822941
MC/DC50%2/4
$arg and $arg->isa("PPI::Token::Structure") and $arg->content eq ";" and not $arg->snext_sibling
$arg $arg->isa("PPI::Token::Structure") $arg->content eq ";" not $arg->snext_sibling
Truth table60%3/5
A and B and C and not(D)
A= $argB= $arg->isa("PPI::Token::Structure")C= $arg->content eq ";"D= not $arg->snext_sibling
ABCDresult
0XXX0
10XX0
110X0
11100
11111
129 && $arg->isa('PPI::Token::Structure')
130 && $arg->content eq ';'
131 && ! $arg->snext_sibling) {
132 41 return;
133 }
134 247 return $arg;
135 }
136  
137 # Hack to set Perl::Critic::Violation location to $linenum in $doc_str.
138 # Have thought about validating _location and _source fields before mangling
139 # them, but hopefully there'll be a documented interface to use before long.
140 #
141 193 ▶sub _violation_override_linenum {
Subroutine _violation_override_linenum: called CC=1 SCAR=0.0
142 193 my ($violation, $doc_str, $linenum) = @_;
143  
144 # if ($violation->can('set_line_number_offset')) {
145 # $violation->set_line_number_offset ($linenum - 1);
146 # } else {
147  
148 193 bless $violation, 'Perl::Critic::Pulp::PodMinimumVersionViolation';
149 193 $violation->{_Pulp_linenum_offset} = $linenum - 1;
150 193 $violation->{'_source'} = _str_line_n ($doc_str, $linenum);
151  
152 193 return $violation;
153 }
154  
155 # starting contents of line number $n within $str
156 # $n==0 is the first line
157 199 ▶sub _str_line_n {
Subroutine _str_line_n: called CC=2 SCAR=7.1
158 199 my ($str, $n) = @_;
159 199 $n--;
160 199 ▶ return ($str =~ /^(.*\n){$n}(.*)/ ? $2 : '');
Branch: 1 branch
BranchTrueFalseTotal
$str =~ /^(.*\n){$n}(.*)/ ? : 199 0 199
161 }
162  
163 96 ▶sub elem_package {
Subroutine elem_package: called CC=4 SCAR=13.9
Pod documented
164 96 my ($elem) = @_;
165 96 for (;;) {
166 273 ▶ $elem = $elem->sprevious_sibling || $elem->parent
Condition100%5/5
$elem->sprevious_sibling || $elem->parent || (return undef)
l!l
2658
$elem->sprevious_sibling or $elem->parent
l!l&&r!l&&!r
179868
MC/DC100%2/2
$elem->sprevious_sibling || $elem->parent || (return undef)
$elem->sprevious_sibling $elem->parent
Truth table100%3/3
A or B
A= $elem->sprevious_siblingB= $elem->parent
ABresult
1X1
011
000
167 || return undef;
168 265 ▶ if ($elem->isa ('PPI::Statement::Package')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem->isa("PPI::Statement::Package")) 88 177 265
169 88 return $elem;
170 }
171 }
172 }
173  
174 12 ▶sub elem_in_BEGIN {
Subroutine elem_in_BEGIN: called CC=3 SCAR=11.0
Pod documented
175 12 my ($elem) = @_;
176 12 ▶ while ($elem = $elem->parent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem = $elem->parent) 29 3 32
177 29 ▶ if ($elem->isa('PPI::Statement::Scheduled')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem->isa("PPI::Statement::Scheduled")) 9 20 29
178 9 return ($elem->type eq 'BEGIN');
179 }
180 }
181 3 return 0;
182 }
183  
184 487 ▶sub elem_is_comma_operator {
Subroutine elem_is_comma_operator: called CC=2 SCAR=6.9
Pod documented
185 487 my ($elem) = @_;
186 return ($elem->isa('PPI::Token::Operator')
187 487 ▶ && $Perl::Critic::Pulp::Utils::COMMA{$elem});
Condition100%3/3
$elem->isa('PPI::Token::Operator') && $Perl::Critic::Pulp::Utils::COMMA{$elem}
!ll&&!rl&&r
28413190
MC/DC100%2/2
$elem->isa('PPI::Token::Operator') && $Perl::Critic::Pulp::Utils::COMMA{$elem}
$elem->isa('PPI::Token::Operator') $Perl::Critic::Pulp::Utils::COMMA{$elem}
Truth table100%3/3
A && B
A= $elem->isa('PPI::Token::Operator')B= $Perl::Critic::Pulp::Utils::COMMA{$elem}
ABresult
0X0
100
111
188 }
189  
190 41 1;
191 __END__
« Policy/ValuesAndExpressions/ProhibitNullStatements.pm ↑ Summary Policy/Modules/ProhibitPOSIXimport.pm »