Policy/CodeLayout/ProhibitIfIfSameLine.pm

filter: Statementstmt 97.9% 48 / 49 Branchbran 91.6% 11 / 12 Conditioncond 66.6% 8 / 12 MC/DCmcdc 42.8% 3 / 7 Subroutinesub 100% 12 / 12 Podpod 100% 1 / 1 totaltotal 89.2% 83 / 93 CC 12 SCAR 13.4
CC
12
Cov
92%
violates
7.1
_elems_any_separator
5.0
_compound_statement_is_if
2.1
CRAP 3.8
« Policy/Documentation/RequireLinkedURLs.pm ↑ Summary Policy/Compatibility/ProhibitUnixDevNull.pm »
1 # Copyright 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 # perlcritic -s ProhibitIfIfSameLine /usr/share/perl5/Pod/Simple.pm
18 # preceded by "return" so actually ok
19 # perlcritic -s ProhibitIfIfSameLine /usr/share/perl5/Tk/AbstractCanvas.pm
20 # two ifs one line
21  
22  
23 package Perl::Critic::Policy::CodeLayout::ProhibitIfIfSameLine;
24 40 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 40 ▶use Perl::Critic::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 40 ▶use base 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 40 our $VERSION = 100;
32  
33 # uncomment this to run the ### lines
34 # use Smart::Comments;
35  
36  
37 40 ▶use constant supported_parameters => ();
Subroutine BEGIN: called CC=1 SCAR=0.0
38 40 ▶use constant default_severity => $Perl::Critic::Utils::SEVERITY_MEDIUM;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 40 ▶use constant default_themes => qw(pulp bugs);
Subroutine BEGIN: called CC=1 SCAR=0.0
40 40 ▶use constant applies_to => ('PPI::Statement::Compound');
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 40 my %compound_type_is_if = (if => 1,
43 unless => 1);
44  
45 30 ▶sub violates {
Subroutine violates: called CC=7 SCAR=19.6
Pod documented
46 30 my ($self, $elem, $document) = @_;
47 ### ProhibitIfIfSameLine elem: "$elem"
48 ### type: $elem->type
49  
50 30 ▶ unless (_compound_statement_is_if($elem)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_compound_statement_is_if($elem)) 14 16 30
51 ### not an "if" ...
52 14 return;
53 }
54  
55 16 ▶ if (_elems_any_separator ($elem->child(0), $elem->schild(0))) {
Branch: 1 branch
BranchTrueFalseTotal
if (_elems_any_separator($elem->child(0), $elem->schild(0))) 0 16 16
56 ### leading whitespace in elem itself, so ok ...
57 0 return;
58 }
59  
60 16 ▶ my $prev = $elem->sprevious_sibling || return;
Condition100%2/2
$elem->sprevious_sibling || (return)
l!l
88
MC/DC100%1/1
$elem->sprevious_sibling || (return)
$elem->sprevious_sibling
Truth table100%2/2
A
A= $elem->sprevious_sibling
Aresult
11
00
61 8 ▶ unless ($prev->isa('PPI::Statement::Compound')
Branch: 1 branch
BranchTrueFalseTotal
unless ($prev->isa("PPI::Statement::Compound") and $compound_type_is_if{$prev->type}) 4 4 8
Condition66%2/3
$prev->isa("PPI::Statement::Compound") and $compound_type_is_if{$prev->type}
!ll&&!rl&&r
044
MC/DC50%1/2
$prev->isa("PPI::Statement::Compound") and $compound_type_is_if{$prev->type}
$prev->isa("PPI::Statement::Compound") $compound_type_is_if{$prev->type}
Truth table66%2/3
A and B
A= $prev->isa("PPI::Statement::Compound")B= $compound_type_is_if{$prev->type}
ABresult
0X0
100
111
62 && $compound_type_is_if{$prev->type}) {
63 ### not preceded by an "if", so ok ...
64 4 return;
65 }
66  
67 4 ▶ if (_elems_any_separator ($prev->next_sibling, $elem)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_elems_any_separator($prev->next_sibling, $elem)) 1 3 4
68 ### newlines after previous statement, so ok ...
69 1 return;
70 }
71  
72 3 return $self->violation
73 ('Put a newline in "} if (x)" so it doesn\'t look like possible \"elsif\"',
74 '',
75 $elem);
76 }
77  
78 # $elem is a PPI::Statement::Compound
79 # Return true if it's an "if" statement.
80 # Note this is not simply $elem->type eq "if", since type "if" includes
81 # "unless" statements, but _compound_statement_is_if() is true only on "if"
82 # statements.
83 #
84 30 ▶sub _compound_statement_is_if {
Subroutine _compound_statement_is_if: called CC=2 SCAR=7.2
85 30 my ($elem) = @_;
86 30 ▶ return (($elem->schild(0)||'') eq 'if');
Condition50%1/2
$elem->schild(0) || ''
l!l
300
MC/DC0%0/1
$elem->schild(0) || ''
$elem->schild(0)
Truth table50%1/2
A
A= $elem->schild(0)
Aresult
11
00
87 }
88  
89 # Return true if there is a suitable separator in $from or its following
90 # elements up to $to, but not including $to.
91 #
92 20 ▶sub _elems_any_separator {
Subroutine _elems_any_separator: called CC=5 SCAR=16.2
93 20 my ($from, $to) = @_;
94 20 for (;;) {
95 24 ▶ if ($from == $to) {
Branch: 1 branch
BranchTrueFalseTotal
if ($from == $to) 19 5 24
96 19 return 0;
97 }
98 5 ▶ if ($from =~ /\n/
Branch: 1 branch
BranchTrueFalseTotal
if ($from =~ /\n/ or $from->isa("PPI::Statement::Null")) 1 4 5
Condition66%2/3
$from =~ /\n/ or $from->isa("PPI::Statement::Null")
l!l&&r!l&&!r
014
MC/DC50%1/2
$from =~ /\n/ or $from->isa("PPI::Statement::Null")
$from =~ /\n/ $from->isa("PPI::Statement::Null")
Truth table66%2/3
A or B
A= $from =~ /\n/B= $from->isa("PPI::Statement::Null")
ABresult
1X1
011
000
99 || $from->isa('PPI::Statement::Null')) {
100 1 return 1;
101 }
102 4 ▶ $from = $from->next_sibling || return 0;
Condition50%1/2
$from->next_sibling || (return 0)
l!l
40
MC/DC0%0/1
$from->next_sibling || (return 0)
$from->next_sibling
Truth table50%1/2
A
A= $from->next_sibling
Aresult
11
00
103 }
104 }
105  
106 40 1;
107 __END__
« Policy/Documentation/RequireLinkedURLs.pm ↑ Summary Policy/Compatibility/ProhibitUnixDevNull.pm »