Policy/CodeLayout/RequireKRParens.pm

filter: Statementstmt 98.2% 55 / 56 Branchbran 92.8% 26 / 28 Conditioncond 66.6% 18 / 27 MC/DCmcdc 47.0% 8 / 17 Subroutinesub 92.3% 12 / 13 Podpod 100% 4 / 4 totaltotal 84.8% 123 / 145 CC 24 SCAR 16.4
CC
24
Cov
89%
_violates_subroutine
8.0
_violates_compound
7.2
_violates_condition
6.0
CRAP 5.2
« Policy/ControlStructures/ProhibitMultipleSubscripts.pm ↑ Summary Policy/ValuesAndExpressions/ProhibitListsInMultiConstants.pm »
1 package Perl::Critic::Policy::CodeLayout::RequireKRParens;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use parent qw[ Perl::Critic::Policy ];
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Perl::Critic::Utils qw[ :severities :classification ];
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use constant PBP_PAGE => 9;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 12 ▶sub default_severity { return $SEVERITY_LOW }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
10 0 ▶sub default_themes { return qw[ cosmetic pbp ] }
Subroutine default_themes: not called CC=1 SCAR=6.9
Pod documented
11  
12 11 ▶sub applies_to {
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
13 11 return qw[
14 PPI::Token::Word
15 PPI::Structure::Condition
16 PPI::Statement::Compound
17 ];
18 }
19  
20 37 ▶sub violates {
Subroutine violates: called CC=3 SCAR=11.0
Pod documented
21 37 my ($self, $elem, $doc) = @_;
22  
23 37 ▶ goto &_violates_subroutine if $elem->isa('PPI::Token::Word');
Branch: 1 branch
BranchTrueFalseTotal
if $elem->isa("PPI::Token::Word") 27 10 37
24 10 ▶ goto &_violates_condition if $elem->isa('PPI::Structure::Condition');
Branch: 1 branch
BranchTrueFalseTotal
if $elem->isa("PPI::Structure::Condition") 5 5 10
25 5 goto &_violates_compound; # PPI::Statement::Compound
26 }
27  
28 5 ▶sub _violates_condition {
Subroutine _violates_condition: called CC=6 SCAR=18.0
29 5 my ($self, $elem, $doc) = @_;
30  
31 5 my @violations;
32 5 my $prev = $elem->previous_sibling;
33 5 ▶ if (ref $prev and not $prev->isa('PPI::Token::Whitespace')) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $prev and not $prev->isa("PPI::Token::Whitespace")) 3 2 5
Condition66%2/3
ref $prev and not $prev->isa("PPI::Token::Whitespace")
!ll&&!rl&&r
023
MC/DC50%1/2
ref $prev and not $prev->isa("PPI::Token::Whitespace")
ref $prev not $prev->isa("PPI::Token::Whitespace")
Truth table66%2/3
A and not(B)
A= ref $prevB= not $prev->isa("PPI::Token::Whitespace")
ABresult
0X0
100
111
34 3 push @violations,
35 $self->violation('No whitespace before opening condition parenthesis', PBP_PAGE, $elem);
36 }
37  
38 5 my $next = $elem->next_sibling; # end of statement means it's a trailing condition
39 5 ▶ if (ref $next and not $next->isa('PPI::Token::Whitespace') and not _is_end_of_statement($next)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $next and not $next->isa("PPI::Token::Whitespace") and not _is_end_of_statement($next)) 2 3 5
Condition83%5/6
ref $next and not $next->isa("PPI::Token::Whitespace") and not _is_end_of_statement($next)
!ll&&!rl&&r
122
ref $next and not $next->isa("PPI::Token::Whitespace")
!ll&&!rl&&r
014
MC/DC66%2/3
ref $next and not $next->isa("PPI::Token::Whitespace") and not _is_end_of_statement($next)
ref $next not $next->isa("PPI::Token::Whitespace") not _is_end_of_statement($next)
Truth table75%3/4
A and not(B) and not(C)
A= ref $nextB= not $next->isa("PPI::Token::Whitespace")C= not _is_end_of_statement($next)
ABCresult
0XX0
10X0
1100
1111
40 2 push @violations,
41 $self->violation('No whitespace after closing condition parenthesis', PBP_PAGE, $elem);
42 }
43  
44 5 return @violations;
45 }
46  
47 27 ▶sub _violates_subroutine {
Subroutine _violates_subroutine: called CC=8 SCAR=20.8
48 27 my ($self, $elem, $doc) = @_;
49 27 ▶ return if is_perl_builtin($elem);
Branch: 1 branch
BranchTrueFalseTotal
if is_perl_builtin($elem) 3 24 27
50 24 ▶ return if not is_function_call($elem) and not is_method_call($elem);
Branch: 1 branch
BranchTrueFalseTotal
unless is_function_call($elem) or is_method_call($elem) 12 12 24
Condition100%3/3
is_function_call($elem) or is_method_call($elem)
l!l&&r!l&&!r
10212
MC/DC100%2/2
is_function_call($elem) or is_method_call($elem)
is_function_call($elem) is_method_call($elem)
Truth table100%3/3
A or B
A= is_function_call($elem)B= is_method_call($elem)
ABresult
1X1
011
000
51  
52 # Check for calls with no parentheses
53 12 my $parens = $elem->snext_sibling();
54 12 ▶ return if ref $parens and not $parens->isa('PPI::Structure::List');
Branch: 1 branch
BranchTrueFalseTotal
if ref $parens and not $parens->isa("PPI::Structure::List") 1 11 12
Condition66%2/3
ref $parens and not $parens->isa("PPI::Structure::List")
!ll&&!rl&&r
0111
MC/DC50%1/2
ref $parens and not $parens->isa("PPI::Structure::List")
ref $parens not $parens->isa("PPI::Structure::List")
Truth table66%2/3
A and not(B)
A= ref $parensB= not $parens->isa("PPI::Structure::List")
ABresult
0X0
100
111
55  
56 11 my $next_sib = $elem->next_sibling();
57 11 ▶ if (ref $next_sib and $next_sib->isa('PPI::Token::Whitespace')) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $next_sib and $next_sib->isa("PPI::Token::Whitespace")) 4 7 11
Condition66%2/3
ref $next_sib and $next_sib->isa("PPI::Token::Whitespace")
!ll&&!rl&&r
074
MC/DC50%1/2
ref $next_sib and $next_sib->isa("PPI::Token::Whitespace")
ref $next_sib $next_sib->isa("PPI::Token::Whitespace")
Truth table66%2/3
A and B
A= ref $next_sibB= $next_sib->isa("PPI::Token::Whitespace")
ABresult
0X0
100
111
58 4 return $self->violation('Whitespace between subroutine name and its opening parenthesis',
59 PBP_PAGE, $elem);
60 }
61  
62 7 return;
63 }
64  
65 5 ▶sub _violates_compound {
Subroutine _violates_compound: called CC=7 SCAR=19.7
66 5 my ($self, $elem, $doc) = @_;
67 5 my $type = $elem->type;
68  
69 5 ▶ my $parens =
Branch: 2 branches
BranchTrueFalseTotal
$type eq 'foreach' ? : 1 4 5
$type eq 'for' ? : 1 3 4
70 $type eq 'foreach' ? $elem->find_first('PPI::Structure::List')
71 : $type eq 'for' ? $elem->find_first('PPI::Structure::For')
72 : return; # if and while are handled in _violates_condition()
73  
74 2 my @violations;
75 2 my $prev = $parens->previous_sibling;
76 2 ▶ if (ref $prev and not $prev->isa('PPI::Token::Whitespace')) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $prev and not $prev->isa("PPI::Token::Whitespace")) 2 0 2
Condition33%1/3
ref $prev and not $prev->isa("PPI::Token::Whitespace")
!ll&&!rl&&r
002
MC/DC0%0/2
ref $prev and not $prev->isa("PPI::Token::Whitespace")
ref $prev not $prev->isa("PPI::Token::Whitespace")
Truth table33%1/3
A and not(B)
A= ref $prevB= not $prev->isa("PPI::Token::Whitespace")
ABresult
0X0
100
111
77 2 push @violations,
78 $self->violation("No whitespace before opening $type parenthesis", PBP_PAGE, $parens);
79 }
80  
81 2 my $next = $parens->next_sibling;
82 2 ▶ if (ref $next and not $next->isa('PPI::Token::Whitespace')) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $next and not $next->isa("PPI::Token::Whitespace")) 1 1 2
Condition66%2/3
ref $next and not $next->isa("PPI::Token::Whitespace")
!ll&&!rl&&r
011
MC/DC50%1/2
ref $next and not $next->isa("PPI::Token::Whitespace")
ref $next not $next->isa("PPI::Token::Whitespace")
Truth table66%2/3
A and not(B)
A= ref $nextB= not $next->isa("PPI::Token::Whitespace")
ABresult
0X0
100
111
83 1 push @violations,
84 $self->violation("No whitespace after closing $type parenthesis", PBP_PAGE, $parens);
85 }
86  
87 2 return @violations;
88 }
89  
90 4 ▶sub _is_end_of_statement {
Subroutine _is_end_of_statement: called CC=4 SCAR=14.6
91 4 my ($elem) = @_;
92 4 ▶ return if not ref $elem;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $elem 0 4 4
93 4 ▶ return if not $elem->isa('PPI::Token::Structure');
Branch: 1 branch
BranchTrueFalseTotal
unless $elem->isa("PPI::Token::Structure") 2 2 4
94 2 ▶ return ($elem eq '}' or $elem eq ';');
Condition33%1/3
$elem eq '}' || $elem eq ';'
l!l&&r!l&&!r
020
MC/DC0%0/2
$elem eq '}' || $elem eq ';'
$elem eq '}' $elem eq ';'
Truth table33%1/3
A || B
A= $elem eq '}'B= $elem eq ';'
ABresult
1X1
011
000
95 }
96  
97 2 1;
98 __END__
« Policy/ControlStructures/ProhibitMultipleSubscripts.pm ↑ Summary Policy/ValuesAndExpressions/ProhibitListsInMultiConstants.pm »