Policy/CodeLayout/RequireTrailingCommaAtNewline.pm

filter: Statementstmt 98.8% 86 / 87 Branchbran 90.0% 36 / 40 Conditioncond 76.7% 33 / 43 MC/DCmcdc 60.0% 15 / 25 Subroutinesub 100% 15 / 15 Podpod 100% 1 / 1 totaltotal 88.1% 186 / 211 CC 37 SCAR 23.9
CC
37
Cov
91%
_is_list_single_expression
16.5
violates
14.0
_is_preceded_by_array
9.4
CRAP 10.9
« Policy/ValuesAndExpressions/RequireNumericVersion.pm ↑ Summary Policy/Documentation/ProhibitDuplicateHeadings.pm »
1 # Copyright 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2019, 2021 Kevin Ryde
2  
3 # Perl-Critic-Pulp is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by the
5 # Free Software Foundation; either version 3, or (at your option) any later
6 # version.
7 #
8 # Perl-Critic-Pulp is distributed in the hope that it will be useful, but
9 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
11 # for more details.
12 #
13 # You should have received a copy of the GNU General Public License along
14 # with Perl-Critic-Pulp. If not, see <http://www.gnu.org/licenses/>.
15  
16  
17 package Perl::Critic::Policy::CodeLayout::RequireTrailingCommaAtNewline;
18 40 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 40 ▶use List::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 40 ▶use base 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
24 40 ▶use Perl::Critic::Utils qw(is_function_call is_method_call);
Subroutine BEGIN: called CC=1 SCAR=0.0
25 40 ▶use Perl::Critic::Pulp::Utils 'elem_is_comma_operator';
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 # uncomment this to run the ### lines
28 #use Smart::Comments;
29  
30 40 our $VERSION = 100;
31  
32 40 ▶use constant supported_parameters =>
Subroutine BEGIN: called CC=1 SCAR=0.0
33 ({ name => 'except_function_calls',
34 description => 'Don\'t demand a trailing comma in function call argument lists.',
35 behavior => 'boolean',
36 default_string => '0',
37 40 });
38 40 ▶use constant default_severity => $Perl::Critic::Utils::SEVERITY_LOW;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 40 ▶use constant default_themes => qw(pulp cosmetic);
Subroutine BEGIN: called CC=1 SCAR=0.0
40 40 ▶use constant applies_to => qw(PPI::Structure::List
Subroutine BEGIN: called CC=1 SCAR=0.0
41 40 PPI::Structure::Constructor);
42  
43 44 ▶sub violates {
Subroutine violates: called CC=14 SCAR=26.4
Pod documented
44 44 my ($self, $elem, $document) = @_;
45 ### elem: ref($elem)
46 ### content: "$elem"
47  
48 44 ▶ if ($self->{'_except_function_calls'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_except_function_calls"}) 4 40 44
49 4 my $prev;
50 4 ▶ if (($prev = $elem->sprevious_sibling)
Branch: 1 branch
BranchTrueFalseTotal
if ($prev = $elem->sprevious_sibling and $prev->isa("PPI::Token::Word") and is_function_call($prev) || is_method_call($prev)) 3 1 4
Condition66%6/9
$prev = $elem->sprevious_sibling and $prev->isa("PPI::Token::Word") and is_function_call($prev) || is_method_call($prev)
!ll&&!rl&&r
103
$prev = $elem->sprevious_sibling and $prev->isa("PPI::Token::Word")
!ll&&!rl&&r
013
is_function_call($prev) or is_method_call($prev)
l!l&&r!l&&!r
120
MC/DC25%1/4
$prev = $elem->sprevious_sibling and $prev->isa("PPI::Token::Word") and is_function_call($prev) || is_method_call($prev)
$prev = $elem->sprevious_sibling $prev->isa("PPI::Token::Word") is_function_call($prev) is_method_call($prev)
Truth table60%3/5
A and B and C or D
A= $prev = $elem->sprevious_siblingB= $prev->isa("PPI::Token::Word")C= is_function_call($prev)D= is_method_call($prev)
ABCDresult
0XXX0
10XX0
11000
111X1
11011
51 && $prev->isa('PPI::Token::Word')
52 && (is_function_call($prev) || is_method_call($prev))) {
53 ### is_function_call: !! is_function_call($prev)
54 ### is_method_call: !! is_method_call($prev)
55 3 return;
56 }
57 }
58  
59 41 my @children = $elem->children;
60 41 ▶ @children = map {$_->isa('PPI::Statement') ? $_->children : $_} @children;
Branch: 1 branch
BranchTrueFalseTotal
$_->isa('PPI::Statement') ? : 36 67 103
61 ### children: "@children"
62  
63 41 ▶ if (_is_list_single_expression($elem)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_list_single_expression($elem)) 9 32 41
64 ### an expression not a list as such ...
65 9 return;
66 }
67  
68 32 my $newline = 0;
69 32 my $after;
70 32 foreach my $child (reverse @children) {
71 84 ▶ if ($child->isa('PPI::Token::Whitespace')
Branch: 1 branch
BranchTrueFalseTotal
if ($child->isa('PPI::Token::Whitespace') or $child->isa('PPI::Token::Comment')) { } 56 28 84
Condition100%3/3
$child->isa("PPI::Token::Whitespace") or $child->isa("PPI::Token::Comment")
l!l&&r!l&&!r
53328
MC/DC100%2/2
$child->isa("PPI::Token::Whitespace") or $child->isa("PPI::Token::Comment")
$child->isa("PPI::Token::Whitespace") $child->isa("PPI::Token::Comment")
Truth table100%3/3
A or B
A= $child->isa("PPI::Token::Whitespace")B= $child->isa("PPI::Token::Comment")
ABresult
1X1
011
000
72 || $child->isa('PPI::Token::Comment')) {
73 ### HWS ...
74 56 ▶ $newline ||= ($child->content =~ /\n/);
Condition100%3/3
$newline ||= $child->content =~ /\n/
l!l&&r!l&&!r
62525
MC/DC100%2/2
$newline ||= $child->content =~ /\n/
$newline $child->content =~ /\n/
Truth table100%3/3
A ||= B
A= $newlineB= $child->content =~ /\n/
ABresult
1X1
011
000
75 ### $newline
76 56 $after = $child;
77 } else {
78 28 ▶ if ($newline && ! elem_is_comma_operator($child)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($newline and not elem_is_comma_operator($child)) 18 10 28
Condition100%3/3
$newline and not elem_is_comma_operator($child)
!ll&&!rl&&r
4618
MC/DC100%2/2
$newline and not elem_is_comma_operator($child)
$newline not elem_is_comma_operator($child)
Truth table100%3/3
A and not(B)
A= $newlineB= not elem_is_comma_operator($child)
ABresult
0X0
100
111
79 18 return $self->violation
80 ('Put a trailing comma at last of a list ending with a newline',
81 '',
82 $after);
83 }
84 10 last;
85 }
86 }
87  
88 14 return;
89 }
90  
91 # $elem is any PPI::Element
92 # Return true if it's a PPI::Structure::List which contains just a single
93 # expression. Any "," or "=>" in the list is multiple expressions, but also
94 # the various rules of the policy are applied as to what is list context
95 # (array assignments, function calls).
96 #
97 41 ▶sub _is_list_single_expression {
Subroutine _is_list_single_expression: called CC=16 SCAR=28.0
98 41 my ($elem) = @_;
99 41 ▶ $elem->isa('PPI::Structure::List')
Branch: 1 branch
BranchTrueFalseTotal
$elem->isa("PPI::Structure::List") or return 0 35 6 41
100 or return 0;
101  
102 35 my @children = $elem->schildren;
103 {
104 # eg. PPI::Structure::List
105 # PPI::Statement::Expression
106 # PPI::Token::Number '1'
107 # PPI::Token::Operator ','
108 # so descend through PPI::Statement::Expression
109 #
110 35 ▶ @children = map { $_->isa('PPI::Statement::Expression')
Branch: 1 branch
BranchTrueFalseTotal
$_->isa('PPI::Statement::Expression') ? : 30 0 30
111 ? ($_->schildren) : ($_)} @children;
112 35 ▶ if (List::Util::first {elem_is_comma_operator($_)} @children) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if (&List::Util::first(sub { elem_is_comma_operator($_); } , @children)) 13 22 35
113 ### contains comma operator, so not an expression ...
114 13 return 0;
115 }
116 }
117  
118 22 ▶ if (my $prev = $elem->sprevious_sibling) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $prev = $elem->sprevious_sibling) 22 0 22
119 22 ▶ if ($prev->isa('PPI::Token::Word')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($prev->isa('PPI::Token::Word')) { } 9 13 22
elsif ($prev->isa('PPI::Token::Operator') and $prev eq '=' and _is_preceded_by_array($prev)) { } 10 3 13
120 9 ▶ if ($prev eq 'return') {
Branch: 1 branch
BranchTrueFalseTotal
if ($prev eq "return") 2 7 9
121 ### return statement without commas, is reckoned a single expression ...
122 2 return 1;
123 }
124 7 ▶ if (is_function_call($prev)
Branch: 1 branch
BranchTrueFalseTotal
if (is_function_call($prev) or is_method_call($prev)) 7 0 7
Condition66%2/3
is_function_call($prev) or is_method_call($prev)
l!l&&r!l&&!r
520
MC/DC0%0/2
is_function_call($prev) or is_method_call($prev)
is_function_call($prev) is_method_call($prev)
Truth table66%2/3
A or B
A= is_function_call($prev)B= is_method_call($prev)
ABresult
1X1
011
000
125 || is_method_call($prev)) {
126 ### function or method call ...
127 7 ▶ if ($children[-1] && $children[-1]->isa('PPI::Token::HereDoc')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($children[-1] and $children[-1]->isa("PPI::Token::HereDoc")) 2 5 7
Condition100%3/3
$children[-1] and $children[-1]->isa("PPI::Token::HereDoc")
!ll&&!rl&&r
232
MC/DC100%2/2
$children[-1] and $children[-1]->isa("PPI::Token::HereDoc")
$children[-1] $children[-1]->isa("PPI::Token::HereDoc")
Truth table100%3/3
A and B
A= $children[-1]B= $children[-1]->isa("PPI::Token::HereDoc")
ABresult
0X0
100
111
128 2 return 1;
129 }
130 5 return 0;
131 }
132  
133 ▶ } elsif ($prev->isa('PPI::Token::Operator')
Condition50%3/6
$prev->isa("PPI::Token::Operator") and $prev eq "=" and _is_preceded_by_array($prev)
!ll&&!rl&&r
0310
$prev->isa("PPI::Token::Operator") and $prev eq "="
!ll&&!rl&&r
0013
MC/DC33%1/3
$prev->isa("PPI::Token::Operator") and $prev eq "=" and _is_preceded_by_array($prev)
$prev->isa("PPI::Token::Operator") $prev eq "=" _is_preceded_by_array($prev)
Truth table50%2/4
A and B and C
A= $prev->isa("PPI::Token::Operator")B= $prev eq "="C= _is_preceded_by_array($prev)
ABCresult
0XX0
10X0
1100
1111
134 && $prev eq '='
135 && _is_preceded_by_array($prev)) {
136 ### array assignment, not a single expression ...
137 10 ▶ if ($children[-1] && $children[-1]->isa('PPI::Token::HereDoc')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($children[-1] and $children[-1]->isa("PPI::Token::HereDoc")) 2 8 10
Condition100%3/3
$children[-1] and $children[-1]->isa("PPI::Token::HereDoc")
!ll&&!rl&&r
262
MC/DC100%2/2
$children[-1] and $children[-1]->isa("PPI::Token::HereDoc")
$children[-1] $children[-1]->isa("PPI::Token::HereDoc")
Truth table100%3/3
A and B
A= $children[-1]B= $children[-1]->isa("PPI::Token::HereDoc")
ABresult
0X0
100
111
138 2 return 1;
139 }
140 8 return 0;
141 }
142 }
143  
144 ### no commas, not a call, so is an expression
145 3 return 1;
146 }
147  
148 13 ▶sub _is_preceded_by_array {
Subroutine _is_preceded_by_array: called CC=9 SCAR=22.4
149 13 my ($elem) = @_;
150 ### _is_preceded_by_array: "$elem"
151  
152 13 ▶ my $prev = $elem->sprevious_sibling || return 0;
Condition50%1/2
$elem->sprevious_sibling || (return 0)
l!l
130
MC/DC0%0/1
$elem->sprevious_sibling || (return 0)
$elem->sprevious_sibling
Truth table50%1/2
A
A= $elem->sprevious_sibling
Aresult
11
00
153 13 ▶ while ($prev->isa('PPI::Structure::Subscript')
Branch: 1 branch
BranchTrueFalseTotal
if ($prev->isa("PPI::Structure::Subscript") or $prev->isa("PPI::Structure::Block")) 5 13 18
Condition100%3/3
$prev->isa("PPI::Structure::Subscript") or $prev->isa("PPI::Structure::Block")
l!l&&r!l&&!r
4113
MC/DC100%2/2
$prev->isa("PPI::Structure::Subscript") or $prev->isa("PPI::Structure::Block")
$prev->isa("PPI::Structure::Subscript") $prev->isa("PPI::Structure::Block")
Truth table100%3/3
A or B
A= $prev->isa("PPI::Structure::Subscript")B= $prev->isa("PPI::Structure::Block")
ABresult
1X1
011
000
154 || $prev->isa('PPI::Structure::Block')) {
155 ### skip: ref $prev
156 5 ▶ $prev = $prev->sprevious_sibling || return 0;
Condition50%1/2
$prev->sprevious_sibling || (return 0)
l!l
50
MC/DC0%0/1
$prev->sprevious_sibling || (return 0)
$prev->sprevious_sibling
Truth table50%1/2
A
A= $prev->sprevious_sibling
Aresult
11
00
157 }
158 ### prev: ref $prev
159 13 ▶ if ($prev->isa('PPI::Token::Symbol')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prev->isa("PPI::Token::Symbol")) 12 1 13
160 12 my $cast;
161 12 ▶ if (($cast = $prev->sprevious_sibling)
Branch: 1 branch
BranchTrueFalseTotal
if ($cast = $prev->sprevious_sibling and $cast->isa("PPI::Token::Cast")) 1 11 12
Condition66%2/3
$cast = $prev->sprevious_sibling and $cast->isa("PPI::Token::Cast")
!ll&&!rl&&r
1101
MC/DC50%1/2
$cast = $prev->sprevious_sibling and $cast->isa("PPI::Token::Cast")
$cast = $prev->sprevious_sibling $cast->isa("PPI::Token::Cast")
Truth table66%2/3
A and B
A= $cast = $prev->sprevious_siblingB= $cast->isa("PPI::Token::Cast")
ABresult
0X0
100
111
162 && $cast->isa('PPI::Token::Cast')) {
163 1 return ($cast->content eq '@');
164 }
165 ### raw_type: $prev->raw_type
166 11 return ($prev->raw_type eq '@');
167 }
168 1 ▶ if ($prev->isa('PPI::Token::Cast')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prev->isa("PPI::Token::Cast")) 1 0 1
169 1 return ($prev->content eq '@');
170 }
171 0 return 0;
172 }
173  
174 40 1;
175 __END__
« Policy/ValuesAndExpressions/RequireNumericVersion.pm ↑ Summary Policy/Documentation/ProhibitDuplicateHeadings.pm »