Policy/Lax/ProhibitComplexMappings/LinesNotStatements.pm

filter: Statementstmt 87.5% 28 / 32 Branchbran 62.5% 10 / 16 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 87.5% 7 / 8 Podpod 100% 4 / 4 totaltotal 75.3% 49 / 65 CC 10 SCAR 22.2
CC
10
Cov
75%
violates
14.8
default_themes
2.0
BEGIN
1.0
CRAP 9.2
« Policy/Lax/ProhibitStringyEval/ExceptForRequire.pm ↑ Summary Policy/Lax/RequireExplicitPackage/ExceptForPragmata.pm »
1 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package Perl::Critic::Policy::Lax::ProhibitComplexMappings::LinesNotStatements 0.014;
4 # ABSTRACT: prohibit multiline maps, not multistatement maps
5  
6 #pod =head1 DESCRIPTION
7 #pod
8 #pod Yes, yes, don't go nuts with map and use it to implement the complex multi-pass
9 #pod fnordsort algorithm. But, come on, people! What's wrong with this:
10 #pod
11 #pod my @localparts = map { my $addr = $_; $addr =~ s/\@.+//; $addr } @addresses;
12 #pod
13 #pod Nothing, that's what!
14 #pod
15 #pod The assumption behind this module is that while the above is okay, the bellow
16 #pod is Right Out:
17 #pod
18 #pod my @localparts = map {
19 #pod my $addr = $_;
20 #pod $addr =~ s/\@.+//;
21 #pod $addr
22 #pod } @addresses;
23 #pod
24 #pod Beyond the fact that it's really ugly, it's just a short step from there to a
25 #pod few included loop structures and then -- oops! -- a return statement.
26 #pod Seriously, people, they're called subroutines. We've had them since Perl 3.
27 #pod
28 #pod =cut
29  
30 7 ▶use Perl::Critic::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 7 ▶use parent qw(Perl::Critic::Policy);
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 7 my $DESCRIPTION = q{The block given to map should fit on one line.};
34 7 my $EXPLANATION = "If it doesn't fit on one line, turn it into a subroutine.";
35  
36 1 ▶sub default_severity { $SEVERITY_MEDIUM }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
37 0 ▶sub default_themes { qw(lax complexity) }
Subroutine default_themes: not called CC=1 SCAR=6.9
Pod documented
38 6 ▶sub applies_to { 'PPI::Token::Word' }
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
39  
40 9 ▶sub violates {
Subroutine violates: called CC=10 SCAR=27.0
Pod documented
41 9 my ($self, $element, undef) = @_;
42  
43 9 ▶ return if $element ne 'map';
Branch: 1 branch
BranchTrueFalseTotal
if $element ne "map" 3 6 9
44 6 ▶ return if !is_function_call($element);
Branch: 1 branch
BranchTrueFalseTotal
unless is_function_call($element) 0 6 6
45  
46 6 my $sib = $element->snext_sibling();
47 6 ▶ return if !$sib;
Branch: 1 branch
BranchTrueFalseTotal
unless $sib 0 6 6
48  
49 6 my $arg = $sib;
50 6 ▶ if ($arg->isa('PPI::Structure::List')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg->isa("PPI::Structure::List")) 0 6 6
51 0 $arg = $arg->schild(0);
52  
53 # Forward looking: PPI might change in v1.200 so schild(0) is a
54 # PPI::Statement::Expression
55 0 ▶ if ($arg && $arg->isa('PPI::Statement::Expression')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg and $arg->isa("PPI::Statement::Expression")) 0 0 0
Condition0%0/3
$arg and $arg->isa("PPI::Statement::Expression")
!ll&&!rl&&r
000
MC/DC0%0/2
$arg and $arg->isa("PPI::Statement::Expression")
$arg $arg->isa("PPI::Statement::Expression")
Truth table0%0/3
A and B
A= $argB= $arg->isa("PPI::Statement::Expression")
ABresult
0X0
100
111
56 0 $arg = $arg->schild(0);
57 }
58 }
59  
60 # If it's not a block, it's an expression-style map, which is only one
61 # statement by definition
62 6 ▶ return if !$arg;
Branch: 1 branch
BranchTrueFalseTotal
unless $arg 0 6 6
63 6 ▶ return if !$arg->isa('PPI::Structure::Block');
Branch: 1 branch
BranchTrueFalseTotal
unless $arg->isa("PPI::Structure::Block") 1 5 6
64  
65 # The moment of truth: does the block contain any newlines?
66 5 ▶ return unless $arg =~ /[\x0d\x0a]/;
Branch: 1 branch
BranchTrueFalseTotal
unless $arg =~ /[\x0d\x0a]/ 4 1 5
67  
68 # more than one child statements
69 1 return $self->violation($DESCRIPTION, $EXPLANATION, $element);
70 }
71  
72 7 1;
73  
74 __END__
« Policy/Lax/ProhibitStringyEval/ExceptForRequire.pm ↑ Summary Policy/Lax/RequireExplicitPackage/ExceptForPragmata.pm »