Policy/Community/MultidimensionalArrayEmulation.pm

filter: Statementstmt 97.9% 48 / 49 Branchbran 96.1% 25 / 26 Conditioncond 81.6% 49 / 60 MC/DCmcdc 62.0% 18 / 29 Subroutinesub 92.3% 12 / 13 Podpod 80.0% 4 / 5 totaltotal 85.7% 156 / 182 CC 33 SCAR 30.1
CC
33
Cov
90%
violates
30.2
__ANON__
5.0
default_themes
2.0
CRAP 20.3
« Policy/Community/DeprecatedFeatures.pm ↑ Summary Policy/Community/OverloadOptions.pm »
1 package Perl::Critic::Policy::Community::MultidimensionalArrayEmulation;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Perl::Critic::Utils qw(:severities :classification :ppi);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use parent 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use List::Util 'any';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 our $VERSION = 'v1.0.4';
12  
13 1 ▶use constant DESC => 'Use of multidimensional array emulation in hash subscript';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use constant EXPL => 'A list in a hash subscript used with the $ sigil triggers Perl 4 multidimensional array emulation. Nest structures using references instead.';
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 4 ▶sub supported_parameters { () }
Subroutine supported_parameters: called CC=1 SCAR=0.0
Pod undocumented
17 13 ▶sub default_severity { $SEVERITY_LOW }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
18 0 ▶sub default_themes { 'community' }
Subroutine default_themes: not called CC=1 SCAR=6.9
Pod documented
19 4 ▶sub applies_to { 'PPI::Structure::Subscript' }
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
20  
21 34 ▶sub violates {
Subroutine violates: called CC=29 SCAR=34.1
Pod documented
22 34 my ($self, $elem) = @_;
23 34 ▶ return () unless $elem->complete and $elem->braces eq '{}';
Branch: 1 branch
BranchTrueFalseTotal
unless $elem->complete and $elem->braces eq "{}" 4 30 34
Condition66%2/3
$elem->complete and $elem->braces eq "{}"
!ll&&!rl&&r
0430
MC/DC50%1/2
$elem->complete and $elem->braces eq "{}"
$elem->complete $elem->braces eq "{}"
Truth table66%2/3
A and B
A= $elem->completeB= $elem->braces eq "{}"
ABresult
0X0
100
111
24
25 30 my @contents = $elem->schildren;
26 30 ▶ @contents = $contents[0]->schildren if @contents == 1 and $contents[0]->isa('PPI::Statement::Expression');
Branch: 1 branch
BranchTrueFalseTotal
if @contents == 1 and $contents[0]->isa("PPI::Statement::Expression") 30 0 30
Condition33%1/3
@contents == 1 and $contents[0]->isa("PPI::Statement::Expression")
!ll&&!rl&&r
0030
MC/DC0%0/2
@contents == 1 and $contents[0]->isa("PPI::Statement::Expression")
@contents == 1 $contents[0]->isa("PPI::Statement::Expression")
Truth table33%1/3
A and B
A= @contents == 1B= $contents[0]->isa("PPI::Statement::Expression")
ABresult
0X0
100
111
27
28 # check for function call with no parentheses; following args won't trigger MAE
29 30 ▶ if (@contents > 1 and $contents[0]->isa('PPI::Token::Word') and !$contents[1]->isa('PPI::Structure::List')
Branch: 1 branch
BranchTrueFalseTotal
if (@contents > 1 and $contents[0]->isa("PPI::Token::Word") and not $contents[1]->isa("PPI::Structure::List") and not $contents[1]->isa("PPI::Token::Operator") && ($contents[1] eq "," || $contents[1] eq "=>")) 1 29 30
Condition86%13/15
@contents > 1 and $contents[0]->isa("PPI::Token::Word") and not $contents[1]->isa("PPI::Structure::List") and not $contents[1]->isa("PPI::Token::Operator") && ($contents[1] eq "," || $contents[1] eq "=>")
!ll&&!rl&&r
2631
@contents > 1 and $contents[0]->isa("PPI::Token::Word") and not $contents[1]->isa("PPI::Structure::List")
!ll&&!rl&&r
2424
@contents > 1 and $contents[0]->isa("PPI::Token::Word")
!ll&&!rl&&r
10146
$contents[1]->isa('PPI::Token::Operator') && ($contents[1] eq ',' || $contents[1] eq '=>')
!ll&&!rl&&r
103
$contents[1] eq "," or $contents[1] eq "=>"
l!l&&r!l&&!r
120
MC/DC66%4/6
@contents > 1 and $contents[0]->isa("PPI::Token::Word") and not $contents[1]->isa("PPI::Structure::List") and not $contents[1]->isa("PPI::Token::Operator") && ($contents[1] eq "," || $contents[1] eq "=>")
@contents > 1 $contents[0]->isa("PPI::Token::Word") not $contents[1]->isa("PPI::Structure::List") $contents[1]->isa('PPI::Token::Operator') $contents[1] eq "," $contents[1] eq "=>"
Truth table85%6/7
A and B and not(C) and not(D && E or F)
A= @contents > 1B= $contents[0]->isa("PPI::Token::Word")C= not $contents[1]->isa("PPI::Structure::List")D= $contents[1]->isa('PPI::Token::Operator')E= $contents[1] eq ","F= $contents[1] eq "=>"
ABCDEFresult
0XXXXX0
10XXXX0
110XXX0
11111X0
1111010
1110XX1
1111001
30 and !($contents[1]->isa('PPI::Token::Operator') and ($contents[1] eq ',' or $contents[1] eq '=>'))) {
31 1 return ();
32 }
33
34 # check if contains top level , or multi-word qw
35 51 ▶ return () unless any {
Subroutine __ANON__: called CC=5 SCAR=16.1
36 51 ▶ ($_->isa('PPI::Token::Operator') and ($_ eq ',' or $_ eq '=>')) or
Branch: 1 branch
BranchTrueFalseTotal
$_->isa("PPI::Token::Operator") and $_ eq "," || $_ eq "=>" or $_->isa("PPI::Token::QuoteLike::Words") and (my(@words) = $_->literal) > 1 18 33 51
Condition91%11/12
$_->isa('PPI::Token::Operator') && ($_ eq ',' || $_ eq '=>') || $_->isa('PPI::Token::QuoteLike::Words') && (my(@words) = $_->literal) > 1
l!l&&r!l&&!r
18330
$_->isa("PPI::Token::Operator") and $_ eq "," || $_ eq "=>"
!ll&&!rl&&r
32118
$_ eq "," or $_ eq "=>"
l!l&&r!l&&!r
1531
$_->isa("PPI::Token::QuoteLike::Words") and (my(@words) = $_->literal) > 1
!ll&&!rl&&r
3003
MC/DC80%4/5
$_->isa('PPI::Token::Operator') && ($_ eq ',' || $_ eq '=>') || $_->isa('PPI::Token::QuoteLike::Words') && (my(@words) = $_->literal) > 1
$_->isa("PPI::Token::Operator") $_ eq "," $_ eq "=>" $_->isa("PPI::Token::QuoteLike::Words") (my(@words) = $_->literal) > 1
Truth table62%5/8
A and B or C || D and E
A= $_->isa("PPI::Token::Operator")B= $_ eq ","C= $_ eq "=>"D= $_->isa("PPI::Token::QuoteLike::Words")E= (my(@words) = $_->literal) > 1
ABCDEresult
11XXX1
101XX1
0XX111
100111
0XX0X0
0XX100
1000X0
100100
37 ($_->isa('PPI::Token::QuoteLike::Words') and (my @words = $_->literal) > 1)
38 29 ▶ } @contents;
Branch: 1 branch
BranchTrueFalseTotal
unless &any(sub { $_->isa("PPI::Token::Operator") and $_ eq "," || $_ eq "=>" or $_->isa("PPI::Token::QuoteLike::Words") and (my(@words) = $_->literal) > 1; } , @contents) 8 21 29
39
40 # check if it's a postderef slice
41 21 my $prev = $elem->sprevious_sibling;
42 21 ▶ return () if $prev and $prev->isa('PPI::Token::Cast') and ($prev eq '@' or $prev eq '%');
Branch: 1 branch
BranchTrueFalseTotal
if $prev and $prev->isa("PPI::Token::Cast") and $prev eq "\@" || $prev eq "%" 2 19 21
Condition66%6/9
$prev and $prev->isa("PPI::Token::Cast") and $prev eq "\@" || $prev eq "%"
!ll&&!rl&&r
1902
$prev and $prev->isa("PPI::Token::Cast")
!ll&&!rl&&r
0192
$prev eq "\@" or $prev eq "%"
l!l&&r!l&&!r
110
MC/DC25%1/4
$prev and $prev->isa("PPI::Token::Cast") and $prev eq "\@" || $prev eq "%"
$prev $prev->isa("PPI::Token::Cast") $prev eq "\@" $prev eq "%"
Truth table60%3/5
A and B and C or D
A= $prevB= $prev->isa("PPI::Token::Cast")C= $prev eq "\@"D= $prev eq "%"
ABCDresult
0XXX0
10XX0
11000
111X1
11011
43
44 # check if it's a slice
45 19 my ($cast, $found_symbol);
46 19 $prev = $elem;
47 19 ▶ while ($prev = $prev->sprevious_sibling) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prev = $prev->sprevious_sibling) 34 17 51
48 34 ▶ last if $found_symbol and !$prev->isa('PPI::Token::Cast');
Branch: 1 branch
BranchTrueFalseTotal
if $found_symbol and not $prev->isa("PPI::Token::Cast") 1 33 34
Condition100%3/3
$found_symbol and not $prev->isa("PPI::Token::Cast")
!ll&&!rl&&r
2581
MC/DC100%2/2
$found_symbol and not $prev->isa("PPI::Token::Cast")
$found_symbol not $prev->isa("PPI::Token::Cast")
Truth table100%3/3
A and not(B)
A= $found_symbolB= not $prev->isa("PPI::Token::Cast")
ABresult
0X0
100
111
49 33 ▶ if ($prev->isa('PPI::Token::Symbol')) {
Branch: 3 branches
BranchTrueFalseTotal
if ($prev->isa('PPI::Token::Symbol')) { } 12 21 33
elsif ($prev->isa('PPI::Structure::Block')) { } 5 16 21
elsif ($found_symbol and $prev->isa('PPI::Token::Cast')) { } 8 8 16
50 12 $cast = $prev->raw_type;
51 12 $found_symbol = 1;
52 } elsif ($prev->isa('PPI::Structure::Block')) {
53 5 $found_symbol = 1;
54 ▶ } elsif ($found_symbol and $prev->isa('PPI::Token::Cast')) {
Condition66%2/3
$found_symbol and $prev->isa("PPI::Token::Cast")
!ll&&!rl&&r
808
MC/DC50%1/2
$found_symbol and $prev->isa("PPI::Token::Cast")
$found_symbol $prev->isa("PPI::Token::Cast")
Truth table66%2/3
A and B
A= $found_symbolB= $prev->isa("PPI::Token::Cast")
ABresult
0X0
100
111
55 8 $cast = $prev;
56 } else {
57 8 ▶ last unless $prev->isa('PPI::Structure::Subscript')
Branch: 1 branch
BranchTrueFalseTotal
unless $prev->isa("PPI::Structure::Subscript") or $prev->isa("PPI::Token::Operator") and $prev eq "->" 1 7 8
Condition83%5/6
$prev->isa("PPI::Structure::Subscript") or $prev->isa("PPI::Token::Operator") and $prev eq "->"
l!l&&r!l&&!r
251
$prev->isa("PPI::Token::Operator") and $prev eq "->"
!ll&&!rl&&r
105
MC/DC66%2/3
$prev->isa("PPI::Structure::Subscript") or $prev->isa("PPI::Token::Operator") and $prev eq "->"
$prev->isa("PPI::Structure::Subscript") $prev->isa("PPI::Token::Operator") $prev eq "->"
Truth table75%3/4
A or B and C
A= $prev->isa("PPI::Structure::Subscript")B= $prev->isa("PPI::Token::Operator")C= $prev eq "->"
ABCresult
1XX1
0111
00X0
0100
58 or ($prev->isa('PPI::Token::Operator') and $prev eq '->');
59 }
60 }
61 19 ▶ return () if $cast and ($cast eq '@' or $cast eq '%');
Branch: 1 branch
BranchTrueFalseTotal
if $cast and $cast eq "\@" || $cast eq "%" 6 13 19
Condition100%6/6
$cast and $cast eq "\@" || $cast eq "%"
!ll&&!rl&&r
2116
$cast eq "\@" or $cast eq "%"
l!l&&r!l&&!r
5111
MC/DC100%3/3
$cast and $cast eq "\@" || $cast eq "%"
$cast $cast eq "\@" $cast eq "%"
Truth table100%4/4
A and B or C
A= $castB= $cast eq "\@"C= $cast eq "%"
ABCresult
0XX0
1000
11X1
1011
62
63 13 return $self->violation(DESC, EXPL, $elem);
64 }
65  
66 1 1;
67  
68 =head1 NAME
69  
70 Perl::Critic::Policy::Community::MultidimensionalArrayEmulation - Don't use
71 multidimensional array emulation
72  
73 =head1 DESCRIPTION
74  
75 When used with the C<@> or C<%> sigils, a list in a hash subscript (C<{}>) will
76 access multiple elements of the hash as a slice. With the C<$> sigil however,
77 it accesses the single element at the key defined by joining the list with the
78 subscript separator C<$;>. This feature is known as
79 L<perldata/"Multi-dimensional array emulation"> and provided a way to emulate
80 a multidimensional structure before Perl 5 introduced references. Perl now
81 supports true multidimensional structures, so this feature is now unnecessary
82 in most cases.
83  
84 In Perl 5.34 or newer, or automatically under C<use v5.36> or newer,
85 L<feature/"The 'multidimensional' feature> can be disabled to remove this
86 syntax from the parser.
87  
88 $foo{$x,$y,$z} # not ok
89 $foo{qw(a b c)} # not ok
90 $foo{$x}{$y}{$z} # ok
91 @foo{$x,$y,$z} # ok
92  
93 =head1 AFFILIATION
94  
95 This policy is part of L<Perl::Critic::Community>.
96  
97 =head1 CONFIGURATION
98  
99 This policy is not configurable except for the standard options.
100  
101 =head1 AUTHOR
102  
103 Dan Book, C<dbook@cpan.org>
104  
105 =head1 COPYRIGHT AND LICENSE
106  
107 Copyright 2015, Dan Book.
108  
109 This library is free software; you may redistribute it and/or modify it under
110 the terms of the Artistic License version 2.0.
111  
112 =head1 SEE ALSO
113  
114 L<Perl::Critic>
« Policy/Community/DeprecatedFeatures.pm ↑ Summary Policy/Community/OverloadOptions.pm »