lib/Perl/Critic/Utils/PPI.pm

filter: Statementstmt 78.8% 71 / 90 Branchbran 59.7% 43 / 72 Conditioncond 27.9% 12 / 43 MC/DCmcdc 5.0% 1 / 20 Subroutinesub 88.2% 15 / 17 Podpod 100% 10 / 10 totaltotal 60.3% 152 / 252 CC 52 SCAR 29.1
CC
52
Cov
61%
get_constant_name_element_from_declaring_statement
72.0
is_ppi_constant_element
30.2
get_next_element_in_same_simple_statement
16.1
CRAP 18.4
« lib/Perl/Critic/Policy/TestingAndDebugging/RequireUseStrict.pm ↑ Summary lib/Perl/Critic/Policy/ValuesAndExpressions/ProhibitComplexVersion.pm »
1 package Perl::Critic::Utils::PPI;
2  
3 40 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 40 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 40 ▶use Scalar::Util qw< blessed >;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 40 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 40 our $VERSION = '1.156';
14  
15 #-----------------------------------------------------------------------------
16  
17 40 our @EXPORT_OK = qw(
18 is_ppi_expression_or_generic_statement
19 is_ppi_generic_statement
20 is_ppi_statement_subclass
21 is_ppi_simple_statement
22 is_ppi_constant_element
23 is_subroutine_declaration
24 is_in_subroutine
25 get_constant_name_element_from_declaring_statement
26 get_next_element_in_same_simple_statement
27 get_previous_module_used_on_same_line
28 );
29  
30 40 our %EXPORT_TAGS = (
31 all => \@EXPORT_OK,
32 );
33  
34 #-----------------------------------------------------------------------------
35  
36 302 ▶sub is_ppi_expression_or_generic_statement {
Subroutine is_ppi_expression_or_generic_statement: called CC=5 SCAR=16.1
Pod documented
37 302 my $element = shift;
38  
39 302 ▶ return if not $element;
Branch: 1 branch
BranchTrueFalseTotal
unless $element 1 301 302
40 301 ▶ return if not $element->isa('PPI::Statement');
Branch: 1 branch
BranchTrueFalseTotal
unless $element->isa("PPI::Statement") 1 300 301
41 300 ▶ return 1 if $element->isa('PPI::Statement::Expression');
Branch: 1 branch
BranchTrueFalseTotal
if $element->isa("PPI::Statement::Expression") 90 210 300
42  
43 210 my $element_class = blessed($element);
44  
45 210 ▶ return if not $element_class;
Branch: 1 branch
BranchTrueFalseTotal
unless $element_class 0 210 210
46 210 return $element_class eq 'PPI::Statement';
47 }
48  
49 #-----------------------------------------------------------------------------
50  
51 65 ▶sub is_ppi_generic_statement {
Subroutine is_ppi_generic_statement: called CC=3 SCAR=11.0
Pod documented
52 65 my $element = shift;
53  
54 65 my $element_class = blessed($element);
55  
56 65 ▶ return if not $element_class;
Branch: 1 branch
BranchTrueFalseTotal
unless $element_class 1 64 65
57 64 ▶ return if not $element->isa('PPI::Statement');
Branch: 1 branch
BranchTrueFalseTotal
unless $element->isa("PPI::Statement") 23 41 64
58  
59 41 return $element_class eq 'PPI::Statement';
60 }
61  
62 #-----------------------------------------------------------------------------
63  
64 312 ▶sub is_ppi_statement_subclass {
Subroutine is_ppi_statement_subclass: called CC=3 SCAR=11.0
Pod documented
65 312 my $element = shift;
66  
67 312 my $element_class = blessed($element);
68  
69 312 ▶ return if not $element_class;
Branch: 1 branch
BranchTrueFalseTotal
unless $element_class 1 311 312
70 311 ▶ return if not $element->isa('PPI::Statement');
Branch: 1 branch
BranchTrueFalseTotal
unless $element->isa("PPI::Statement") 1 310 311
71  
72 310 return $element_class ne 'PPI::Statement';
73 }
74  
75 #-----------------------------------------------------------------------------
76  
77 # Can not use hashify() here because Perl::Critic::Utils already depends on
78 # this module.
79 40 Readonly::Hash my %SIMPLE_STATEMENT_CLASS => map { $_ => 1 } qw<
80 PPI::Statement
81 PPI::Statement::Break
82 PPI::Statement::Include
83 PPI::Statement::Null
84 PPI::Statement::Package
85 PPI::Statement::Variable
86 >;
87  
88 189 ▶sub is_ppi_simple_statement {
Subroutine is_ppi_simple_statement: called CC=3 SCAR=11.7
Pod documented
89 189 ▶ my $element = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $element = shift() or return 189 0 189
90  
91 189 ▶ my $element_class = blessed( $element ) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $element_class = builtin::blessed($element) or return 189 0 189
92  
93 189 return $SIMPLE_STATEMENT_CLASS{ $element_class };
94 }
95  
96 #-----------------------------------------------------------------------------
97  
98 27 ▶sub is_ppi_constant_element {
Subroutine is_ppi_constant_element: called CC=9 SCAR=34.1
Pod documented
99 27 ▶ my $element = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $element = shift() or return 27 0 27
100  
101 27 ▶ blessed( $element ) or return;
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($element) or return 27 0 27
102  
103 # TODO implement here documents once PPI::Token::HereDoc grows the
104 # necessary PPI::Token::Quote interface.
105 return
106 27 ▶ $element->isa( 'PPI::Token::Number' )
Condition22%4/18
$element->isa('PPI::Token::Number') || $element->isa('PPI::Token::Quote::Literal') || $element->isa('PPI::Token::Quote::Single') || $element->isa('PPI::Token::QuoteLike::Words') || ($element->isa('PPI::Token::Quote::Double') || $element->isa('PPI::Token::Quote::Interpolate')) && !($element->string =~ / (?: \A | [^\\] ) (?: \\\\)* [\$\@] /msx)
l!l&&r!l&&!r
2700
$element->isa("PPI::Token::Number") or $element->isa("PPI::Token::Quote::Literal") or $element->isa("PPI::Token::Quote::Single") or $element->isa("PPI::Token::QuoteLike::Words")
l!l&&r!l&&!r
2700
$element->isa("PPI::Token::Number") or $element->isa("PPI::Token::Quote::Literal") or $element->isa("PPI::Token::Quote::Single")
l!l&&r!l&&!r
2700
$element->isa("PPI::Token::Number") or $element->isa("PPI::Token::Quote::Literal")
l!l&&r!l&&!r
2700
$element->isa("PPI::Token::Quote::Double") || $element->isa("PPI::Token::Quote::Interpolate") and not $element->string =~ / (?: \A | [^\\] ) (?: \\\\)* [\$\@] /msx
!ll&&!rl&&r
000
$element->isa("PPI::Token::Quote::Double") or $element->isa("PPI::Token::Quote::Interpolate")
l!l&&r!l&&!r
000
MC/DC0%0/7
$element->isa('PPI::Token::Number') || $element->isa('PPI::Token::Quote::Literal') || $element->isa('PPI::Token::Quote::Single') || $element->isa('PPI::Token::QuoteLike::Words') || ($element->isa('PPI::Token::Quote::Double') || $element->isa('PPI::Token::Quote::Interpolate')) && !($element->string =~ / (?: \A | [^\\] ) (?: \\\\)* [\$\@] /msx)
$element->isa("PPI::Token::Number") $element->isa("PPI::Token::Quote::Literal") $element->isa("PPI::Token::Quote::Single") $element->isa("PPI::Token::QuoteLike::Words") $element->isa("PPI::Token::Quote::Double") $element->isa("PPI::Token::Quote::Interpolate") not $element->string =~ / (?: \A | [^\\] ) (?: \\\\)* [\$\@] /msx
Truth table11%1/9
A or B or C or D || E or F and not(G)
A= $element->isa("PPI::Token::Number")B= $element->isa("PPI::Token::Quote::Literal")C= $element->isa("PPI::Token::Quote::Single")D= $element->isa("PPI::Token::QuoteLike::Words")E= $element->isa("PPI::Token::Quote::Double")F= $element->isa("PPI::Token::Quote::Interpolate")G= not $element->string =~ / (?: \A | [^\\] ) (?: \\\\)* [\$\@] /msx
ABCDEFGresult
1XXXXXX1
01XXXXX1
001XXXX1
0001XXX1
00001X11
00000111
000000X0
00001X00
00000100
107 || $element->isa( 'PPI::Token::Quote::Literal' )
108 || $element->isa( 'PPI::Token::Quote::Single' )
109 || $element->isa( 'PPI::Token::QuoteLike::Words' )
110 || (
111 $element->isa( 'PPI::Token::Quote::Double' )
112 || $element->isa( 'PPI::Token::Quote::Interpolate' ) )
113 && $element->string() !~ m< (?: \A | [^\\] ) (?: \\\\)* [\$\@] >smx
114 ;
115 }
116  
117 #-----------------------------------------------------------------------------
118  
119 54 ▶sub is_subroutine_declaration {
Subroutine is_subroutine_declaration: called CC=7 SCAR=19.5
Pod documented
120 54 my $element = shift;
121  
122 54 ▶ return if not $element;
Branch: 1 branch
BranchTrueFalseTotal
unless $element 1 53 54
123  
124 53 ▶ return 1 if $element->isa('PPI::Statement::Sub');
Branch: 1 branch
BranchTrueFalseTotal
if $element->isa("PPI::Statement::Sub") 4 49 53
125  
126 49 ▶ if ( is_ppi_generic_statement($element) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_ppi_generic_statement($element)) 4 45 49
127 4 my $first_element = $element->first_element();
128  
129 4 ▶ return 1 if
Branch: 1 branch
BranchTrueFalseTotal
if $first_element and $first_element->isa("PPI::Token::Word") and $first_element->content eq "sub" 3 1 4
Condition66%4/6
$first_element and $first_element->isa("PPI::Token::Word") and $first_element->content eq "sub"
!ll&&!rl&&r
103
$first_element and $first_element->isa("PPI::Token::Word")
!ll&&!rl&&r
013
MC/DC33%1/3
$first_element and $first_element->isa("PPI::Token::Word") and $first_element->content eq "sub"
$first_element $first_element->isa("PPI::Token::Word") $first_element->content eq "sub"
Truth table50%2/4
A and B and C
A= $first_elementB= $first_element->isa("PPI::Token::Word")C= $first_element->content eq "sub"
ABCresult
0XX0
10X0
1100
1111
130 $first_element
131 and $first_element->isa('PPI::Token::Word')
132 and $first_element->content() eq 'sub';
133 }
134  
135 46 return;
136 }
137  
138 #-----------------------------------------------------------------------------
139  
140 15 ▶sub is_in_subroutine {
Subroutine is_in_subroutine: called CC=5 SCAR=16.1
Pod documented
141 15 my ($element) = @_;
142  
143 15 ▶ return if not $element;
Branch: 1 branch
BranchTrueFalseTotal
unless $element 2 13 15
144 13 ▶ return 1 if is_subroutine_declaration($element);
Branch: 1 branch
BranchTrueFalseTotal
if is_subroutine_declaration($element) 0 13 13
145  
146 13 ▶ while ( $element = $element->parent() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($element = $element->parent) 35 9 44
147 35 ▶ return 1 if is_subroutine_declaration($element);
Branch: 1 branch
BranchTrueFalseTotal
if is_subroutine_declaration($element) 4 31 35
148 }
149  
150 9 return;
151 }
152  
153 #-----------------------------------------------------------------------------
154  
155 0 ▶sub get_constant_name_element_from_declaring_statement {
Subroutine get_constant_name_element_from_declaring_statement: not called CC=8 SCAR=42.8
Pod documented
156 0 my ($element) = @_;
157  
158 0 warnings::warnif(
159 'deprecated',
160 'Perl::Critic::Utils::PPI::get_constant_name_element_from_declaring_statement() is deprecated. Use PPIx::Utils::Traversal::get_constant_name_elements_from_declaring_statement() instead.',
161 );
162  
163 0 ▶ return if not $element;
Branch: 1 branch
BranchTrueFalseTotal
unless $element 0 0 0
164 0 ▶ return if not $element->isa('PPI::Statement');
Branch: 1 branch
BranchTrueFalseTotal
unless $element->isa("PPI::Statement") 0 0 0
165  
166 0 ▶ if ( $element->isa('PPI::Statement::Include') ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($element->isa('PPI::Statement::Include')) { } 0 0 0
elsif (is_ppi_generic_statement($element) and $element->schild(0)->content =~ / \A Readonly \b /msx) { } 0 0 0
167 0 my $pragma;
168 0 ▶ if ( $pragma = $element->pragma() and $pragma eq 'constant' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pragma = $element->pragma and $pragma eq "constant") 0 0 0
Condition0%0/3
$pragma = $element->pragma and $pragma eq "constant"
!ll&&!rl&&r
000
MC/DC0%0/2
$pragma = $element->pragma and $pragma eq "constant"
$pragma = $element->pragma $pragma eq "constant"
Truth table0%0/3
A and B
A= $pragma = $element->pragmaB= $pragma eq "constant"
ABresult
0X0
100
111
169 0 return _constant_name_from_constant_pragma($element);
170 }
171 }
172 ▶ elsif (
Condition0%0/3
is_ppi_generic_statement($element) and $element->schild(0)->content =~ / \A Readonly \b /msx
!ll&&!rl&&r
000
MC/DC0%0/2
is_ppi_generic_statement($element) and $element->schild(0)->content =~ / \A Readonly \b /msx
is_ppi_generic_statement($element) $element->schild(0)->content =~ / \A Readonly \b /msx
Truth table0%0/3
A and B
A= is_ppi_generic_statement($element)B= $element->schild(0)->content =~ / \A Readonly \b /msx
ABresult
0X0
100
111
173 is_ppi_generic_statement($element)
174 and $element->schild(0)->content() =~ m< \A Readonly \b >xms
175 ) {
176 0 return $element->schild(2);
177 }
178  
179 0 return;
180 }
181  
182 0 ▶sub _constant_name_from_constant_pragma {
Subroutine _constant_name_from_constant_pragma: not called CC=3 SCAR=24.8
183 0 my ($include) = @_;
184  
185 0 ▶ my @arguments = $include->arguments() or return;
Branch: 1 branch
BranchTrueFalseTotal
my(@arguments) = $include->arguments or return 0 0 0
186  
187 0 my $follower = $arguments[0];
188 0 ▶ return if not defined $follower;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $follower 0 0 0
189  
190 0 return $follower;
191 }
192  
193 #-----------------------------------------------------------------------------
194  
195 81 ▶sub get_next_element_in_same_simple_statement {
Subroutine get_next_element_in_same_simple_statement: called CC=7 SCAR=27.8
Pod documented
196 81 ▶ my $element = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $element = shift() or return 81 0 81
197  
198 81 ▶ while ( $element and (
Branch: 1 branch
BranchTrueFalseTotal
if ($element and !is_ppi_simple_statement($element) || $element->parent && $element->parent->isa("PPI::Structure::List")) 81 0 81
Condition22%2/9
$element and !is_ppi_simple_statement($element) || $element->parent && $element->parent->isa("PPI::Structure::List")
!ll&&!rl&&r
0081
not is_ppi_simple_statement($element) or $element->parent and $element->parent->isa("PPI::Structure::List")
l!l&&r!l&&!r
8100
$element->parent and $element->parent->isa("PPI::Structure::List")
!ll&&!rl&&r
000
MC/DC0%0/4
$element and !is_ppi_simple_statement($element) || $element->parent && $element->parent->isa("PPI::Structure::List")
$element not is_ppi_simple_statement($element) $element->parent $element->parent->isa("PPI::Structure::List")
Truth table20%1/5
A and not(B) or C and D
A= $elementB= not is_ppi_simple_statement($element)C= $element->parentD= $element->parent->isa("PPI::Structure::List")
ABCDresult
0XXX0
100X0
10100
11XX1
10111
199 not is_ppi_simple_statement( $element )
200 or $element->parent()
201 and $element->parent()->isa( 'PPI::Structure::List' ) ) ) {
202 81 my $next;
203 81 ▶ $next = $element->snext_sibling() and return $next;
Branch: 1 branch
BranchTrueFalseTotal
$next = $element->snext_sibling and return $next 81 0 81
204 0 $element = $element->parent();
205 }
206 0 return;
207  
208 }
209  
210 #-----------------------------------------------------------------------------
211  
212 27 ▶sub get_previous_module_used_on_same_line {
Subroutine get_previous_module_used_on_same_line: called CC=9 SCAR=26.1
Pod documented
213 27 ▶ my $element = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $element = shift() or return 27 0 27
214  
215 27 ▶ my ( $line ) = @{ $element->location() || []};
Condition50%1/2
$element->location || []
l!l
270
MC/DC0%0/1
$element->location || []
$element->location
Truth table50%1/2
A
A= $element->location
Aresult
11
00
216  
217 27 ▶ while (not is_ppi_simple_statement( $element )) {
Branch: 1 branch
BranchTrueFalseTotal
unless (is_ppi_simple_statement($element)) 27 27 54
218 27 ▶ $element = $element->parent() or return;
Branch: 1 branch
BranchTrueFalseTotal
$element = $element->parent or return 27 0 27
219 }
220  
221 27 ▶ while ( $element = $element->sprevious_sibling() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($element = $element->sprevious_sibling) 27 0 27
222 27 ▶ ( @{ $element->location() || []} )[0] == $line or return;
Branch: 1 branch
BranchTrueFalseTotal
(@{$element->location or [];})[0] == $line or return 0 27 27
Condition50%1/2
$element->location || []
l!l
270
MC/DC0%0/1
$element->location || []
$element->location
Truth table50%1/2
A
A= $element->location
Aresult
11
00
223 0 ▶ $element->isa( 'PPI::Statement::Include' )
Branch: 1 branch
BranchTrueFalseTotal
$element->isa("PPI::Statement::Include") and return $element->schild(1) 0 0 0
224 and return $element->schild( 1 );
225 }
226  
227 0 return;
228 }
229  
230 #-----------------------------------------------------------------------------
231  
232 40 1;
233  
234 __END__
« lib/Perl/Critic/Policy/TestingAndDebugging/RequireUseStrict.pm ↑ Summary lib/Perl/Critic/Policy/ValuesAndExpressions/ProhibitComplexVersion.pm »