| 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 = 99; | |||||||||||||||||||||||||||
| 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 | sub violates { | ||||||||||||||||||||||||||||
| 46 | 30 | ▶ | my ($self, $elem, $document) = @_; | ||||||||||||||||||||||||||
|
Subroutine
violates: called CC=7 SCAR=19.6
Pod
documented
| |||||||||||||||||||||||||||||
| 47 | ### ProhibitIfIfSameLine elem: "$elem" | ||||||||||||||||||||||||||||
| 48 | ### type: $elem->type | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 30 | ▶ | unless (_compound_statement_is_if($elem)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | ### not an "if" ... | ||||||||||||||||||||||||||||
| 52 | 14 | return; | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 16 | ▶ | if (_elems_any_separator ($elem->child(0), $elem->schild(0))) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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)
MC/DC100%1/1
$elem->sprevious_sibling || (return)
$elem->sprevious_sibling
Truth table100%2/2
A
A= $elem->sprevious_sibling
| |||||||||||||||||||||||||||||
| 61 | 8 | ▶ | unless ($prev->isa('PPI::Statement::Compound') | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$prev->isa("PPI::Statement::Compound") and $compound_type_is_if{$prev->type}
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}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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 | sub _compound_statement_is_if { | ||||||||||||||||||||||||||||
| 85 | 30 | ▶ | my ($elem) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_compound_statement_is_if: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||
| 86 | 30 | ▶ | return (($elem->schild(0)||'') eq 'if'); | ||||||||||||||||||||||||||
|
Condition50%1/2
$elem->schild(0) || ''
MC/DC0%0/1
$elem->schild(0) || ''
$elem->schild(0)
Truth table50%1/2
A
A= $elem->schild(0)
| |||||||||||||||||||||||||||||
| 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 | sub _elems_any_separator { | ||||||||||||||||||||||||||||
| 93 | 20 | ▶ | my ($from, $to) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_elems_any_separator: called CC=5 SCAR=16.2
| |||||||||||||||||||||||||||||
| 94 | 20 | for (;;) { | |||||||||||||||||||||||||||
| 95 | 24 | ▶ | if ($from == $to) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 19 | return 0; | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | 5 | ▶ | if ($from =~ /\n/ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$from =~ /\n/ or $from->isa("PPI::Statement::Null")
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")
| |||||||||||||||||||||||||||||
| 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)
MC/DC0%0/1
$from->next_sibling || (return 0)
$from->next_sibling
Truth table50%1/2
A
A= $from->next_sibling
| |||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 40 | 1; | |||||||||||||||||||||||||||
| 107 | __END__ | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | =for stopwords Ryde | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | =head1 NAME | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | Perl::Critic::Policy::CodeLayout::ProhibitIfIfSameLine - don't put if after if on same line | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | This policy is part of the L<C<Perl::Critic::Pulp>|Perl::Critic::Pulp> | ||||||||||||||||||||||||||||
| 118 | add-on. It asks you to not to write an C<if> statement on the same line as | ||||||||||||||||||||||||||||
| 119 | a preceding C<if>. | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | if ($x) { | ||||||||||||||||||||||||||||
| 122 | ... | ||||||||||||||||||||||||||||
| 123 | } if ($y) { # bad | ||||||||||||||||||||||||||||
| 124 | ... | ||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | if ($x) { | ||||||||||||||||||||||||||||
| 128 | ... | ||||||||||||||||||||||||||||
| 129 | } elsif ($y) { # was "elsif" intended ? | ||||||||||||||||||||||||||||
| 130 | ... | ||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | The idea is that an C<if> in the layout of an C<elsif> may be either a | ||||||||||||||||||||||||||||
| 134 | mistake or will be confusing to a human reader. On that basis this policy | ||||||||||||||||||||||||||||
| 135 | is under the "bugs" theme and medium severity (see L<Perl::Critic/POLICY | ||||||||||||||||||||||||||||
| 136 | THEMES>). | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | =head2 Unless | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | An C<unless...if> is treated the same. Perl allows C<unless ... elsif> and | ||||||||||||||||||||||||||||
| 141 | so the same potential confusion with an C<elsif> layout arises. | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | unless ($x) { | ||||||||||||||||||||||||||||
| 144 | ... | ||||||||||||||||||||||||||||
| 145 | } if ($y) { # bad | ||||||||||||||||||||||||||||
| 146 | ... | ||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | unless ($x) { | ||||||||||||||||||||||||||||
| 150 | ... | ||||||||||||||||||||||||||||
| 151 | } elsif ($y) { # maybe meant to be "elsif" like this ? | ||||||||||||||||||||||||||||
| 152 | ... | ||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | Whether C<unless ... elsif> is a good idea at all is another matter. | ||||||||||||||||||||||||||||
| 156 | Sometimes it suits a combination of conditions. | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | =head2 Statement Modifiers | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | This policy only applies to a statement followed by a statement. An C<if> | ||||||||||||||||||||||||||||
| 161 | as a statement modifier is not affected. It's usual to put that on the same | ||||||||||||||||||||||||||||
| 162 | line as the statement it modifies. | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | do { | ||||||||||||||||||||||||||||
| 165 | ... | ||||||||||||||||||||||||||||
| 166 | } if ($x); # ok, statement modifier | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | =head2 All One Line | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | Two C<if> statements written on the same line will trigger the policy. | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | if(1){one;} if(2){two;} # bad | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | Perhaps there could be an exception or option when both statements are | ||||||||||||||||||||||||||||
| 175 | entirely on the one line, or some such, for code which is trying to be | ||||||||||||||||||||||||||||
| 176 | compact. | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | =head2 Disabling | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | As always if you don't care about this then you can disable | ||||||||||||||||||||||||||||
| 181 | C<ProhibitIfIfSameLine> from your F<.perlcriticrc> (see | ||||||||||||||||||||||||||||
| 182 | L<Perl::Critic/CONFIGURATION>), | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | [-CodeLayout::ProhibitIfIfSameLine] | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | L<Perl::Critic::Pulp>, L<Perl::Critic> | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | L<Perl::Critic::Policy::ControlStructures::ProhibitCascadingIfElse>, | ||||||||||||||||||||||||||||
| 191 | L<Perl::Critic::Policy::ControlStructures::ProhibitUnlessBlocks> | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | =head1 HOME PAGE | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | L<http://user42.tuxfamily.org/perl-critic-pulp/index.html> | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | Copyright 2013, 2014, 2015, 2016, 2017, 2019, 2021 Kevin Ryde | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | Perl-Critic-Pulp is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||
| 202 | under the terms of the GNU General Public License as published by the Free | ||||||||||||||||||||||||||||
| 203 | Software Foundation; either version 3, or (at your option) any later | ||||||||||||||||||||||||||||
| 204 | version. | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | Perl-Critic-Pulp is distributed in the hope that it will be useful, but | ||||||||||||||||||||||||||||
| 207 | WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | ||||||||||||||||||||||||||||
| 208 | or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | ||||||||||||||||||||||||||||
| 209 | more details. | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | You should have received a copy of the GNU General Public License along with | ||||||||||||||||||||||||||||
| 212 | Perl-Critic-Pulp. If not, see <http://www.gnu.org/licenses/>. | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | =cut | ||||||||||||||||||||||||||||