Policy/References/ProhibitRefChecks.pm

filter: Statementstmt 98.9% 98 / 99 Branchbran 100% 48 / 48 Conditioncond 83.3% 35 / 42 MC/DCmcdc 69.5% 16 / 23 Subroutinesub 100% 14 / 14 Podpod 50.0% 4 / 8 totaltotal 91.8% 215 / 234 CC 42 SCAR 27.7
CC
42
Cov
96%
violates
27.3
decompose
10.0
eqne
5.0
CRAP 16.0
↑ Summary Policy/References/ProhibitUnnecessaryRefs.pm »
1 package Perl::Critic::Policy::References::ProhibitRefChecks;
2  
3 11 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 11 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 11 ▶use Perl::Critic::Utils qw/:severities :classification precedence_of/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 11 ▶use base 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 11 our $VERSION = '0.1.4';
12  
13 11 Readonly::Scalar my $DESC => q{Do not perform manual ref checks};
14 11 Readonly::Scalar my $EXPL => undef; # [ ];
15 11 Readonly::Scalar my $UPREC => precedence_of('-e'); # named unary function precedence
16  
17 ####-----------------------------------------------------------------------------
18  
19 11 ▶sub supported_parameters {
Subroutine supported_parameters: called CC=1 SCAR=0.0
Pod undocumented
20 return (
21 {
22 11 name => 'eq',
23 description => 'Reference types that may be checked via string equality.',
24 default_string => '',
25 behavior => 'string list',
26 },
27 {
28 name => 'ne',
29 description => 'Reference types that may be checked via string inequality.',
30 default_string => '',
31 behavior => 'string list',
32 },
33 {
34 name => 'regexp',
35 description => 'Permit regular expression comparisons.',
36 default_string => '0',
37 behavior => 'boolean',
38 },
39 {
40 name => 'bareref',
41 description => 'Permit a bare if(ref) style check.',
42 default_string => '0',
43 behavior => 'boolean',
44 },
45 );
46 }
47  
48 7818 ▶sub applies_to { return 'PPI::Token::Word' }
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
49 5768 ▶sub default_severity { return $SEVERITY_MEDIUM }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
50 1 ▶sub default_themes { return qw/cosmetic maintenance performance/ }
Subroutine default_themes: called CC=1 SCAR=0.0
Pod documented
51  
52 #-----------------------------------------------------------------------------
53  
54 5768 ▶sub invalid {
Subroutine invalid: called CC=3 SCAR=11.0
Pod undocumented
55 5768 my ($self,$elem,$note)=@_;
56 5768 ▶ $note//='';
Condition100%2/2
$note //= ""
l!l
57671
MC/DC100%1/1
$note //= ""
$note
Truth table100%2/2
A
A= $note
Aresult
11
00
57 5768 ▶ if($note) { $note=" ($note)" }
Branch: 1 branch
BranchTrueFalseTotal
if ($note) 5767 1 5768
58 5768 return $self->violation(sprintf("%s%s",$DESC,$note),$EXPL,$elem);
59 }
60  
61 9225 ▶sub eqne {
Subroutine eqne: called CC=5 SCAR=16.1
Pod undocumented
62 9225 my ($node)=@_;
63 9225 ▶ if(!$node) { return }
Branch: 1 branch
BranchTrueFalseTotal
unless ($node) 6916 2309 9225
64 2309 ▶ if(!$node->isa('PPI::Token::Operator')) { return }
Branch: 1 branch
BranchTrueFalseTotal
unless ($node->isa("PPI::Token::Operator")) 1 2308 2309
65 2308 my $op=$node->content();
66 2308 ▶ if(($op eq 'eq')||($op eq 'ne')) { return 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq "eq" or $op eq "ne") 257 2051 2308
Condition100%3/3
$op eq "eq" or $op eq "ne"
l!l&&r!l&&!r
1291282051
MC/DC100%2/2
$op eq "eq" or $op eq "ne"
$op eq "eq" $op eq "ne"
Truth table100%3/3
A or B
A= $op eq "eq"B= $op eq "ne"
ABresult
1X1
011
000
67 2051 return;
68 }
69  
70 9000 ▶sub decompose {
Subroutine decompose: called CC=10 SCAR=23.0
Pod undocumented
71 9000 my ($elem)=@_;
72 9000 my %operator=map {$_=>undef} (qw/eq ne !~ =~ cmp/);
73 9000 my ($node,$operator,$rhs)=($elem,undef,undef);
74 9000 ▶ while($node) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node) 31620 9000 40620
75 31620 ▶ if($node->isa('PPI::Token::Operator') && exists($operator{$node->content()})) {
Branch: 2 branches
BranchTrueFalseTotal
if ($node->isa('PPI::Token::Operator') and exists $operator{$node->content}) { } 7964 23656 31620
elsif ($node->isa('PPI::Token::Operator') and precedence_of($node) > $UPREC) { } 516 23140 23656
Condition100%3/3
$node->isa("PPI::Token::Operator") and exists $operator{$node->content}
!ll&&!rl&&r
2220314537964
MC/DC100%2/2
$node->isa("PPI::Token::Operator") and exists $operator{$node->content}
$node->isa("PPI::Token::Operator") exists $operator{$node->content}
Truth table100%3/3
A and B
A= $node->isa("PPI::Token::Operator")B= exists $operator{$node->content}
ABresult
0X0
100
111
76 7964 $operator=$node->content();
77 7964 $rhs=$node->snext_sibling();
78 7964 $node=0;
79 }
80 516 ▶ elsif($node->isa('PPI::Token::Operator') && (precedence_of($node)>$UPREC)) { $node=0 }
Condition100%3/3
$node->isa("PPI::Token::Operator") and precedence_of($node) > $UPREC
!ll&&!rl&&r
22203937516
MC/DC100%2/2
$node->isa("PPI::Token::Operator") and precedence_of($node) > $UPREC
$node->isa("PPI::Token::Operator") precedence_of($node) > $UPREC
Truth table100%3/3
A and B
A= $node->isa("PPI::Token::Operator")B= precedence_of($node) > $UPREC
ABresult
0X0
100
111
81 23140 else { $node=$node->snext_sibling() }
82 }
83 9000 ▶ if(!$rhs) { return ($operator) }
Branch: 1 branch
BranchTrueFalseTotal
unless ($rhs) 1036 7964 9000
84 7964 ▶ if($rhs->isa('PPI::Token::Quote')) { return ($operator,lc($rhs->string())) }
Branch: 1 branch
BranchTrueFalseTotal
if ($rhs->isa("PPI::Token::Quote")) 6927 1037 7964
85 1037 ▶ if($rhs->isa('PPI::Token::Word') && ($rhs->content() eq 'ref')) { return ($operator,'ref') }
Branch: 1 branch
BranchTrueFalseTotal
if ($rhs->isa("PPI::Token::Word") and $rhs->content eq "ref") 260 777 1037
Condition66%2/3
$rhs->isa("PPI::Token::Word") and $rhs->content eq "ref"
!ll&&!rl&&r
7770260
MC/DC50%1/2
$rhs->isa("PPI::Token::Word") and $rhs->content eq "ref"
$rhs->isa("PPI::Token::Word") $rhs->content eq "ref"
Truth table66%2/3
A and B
A= $rhs->isa("PPI::Token::Word")B= $rhs->content eq "ref"
ABresult
0X0
100
111
86 777 return ($operator,$rhs->content());
87 }
88  
89 13576 ▶sub violates {
Subroutine violates: called CC=27 SCAR=33.1
Pod documented
90 13576 my ($self,$elem,undef)=@_;
91 13576 ▶ if(!$elem->isa('PPI::Token::Word')) { return }
Branch: 1 branch
BranchTrueFalseTotal
unless ($elem->isa("PPI::Token::Word")) 1 13575 13576
92 13575 ▶ if(!is_perl_builtin($elem)) { return }
Branch: 1 branch
BranchTrueFalseTotal
unless (is_perl_builtin($elem)) 4345 9230 13575
93 9230 ▶ if(!is_function_call($elem)) { return }
Branch: 1 branch
BranchTrueFalseTotal
unless (is_function_call($elem)) 2 9228 9230
94 9228 ▶ if($elem->content() ne 'ref') { return }
Branch: 1 branch
BranchTrueFalseTotal
if ($elem->content ne "ref") 3 9225 9228
95  
96 # Already handled.
97 # No support for ('quoted' eq ref($x)) at this time.
98 9225 ▶ if(eqne($elem->sprevious_sibling())){ return }
Branch: 1 branch
BranchTrueFalseTotal
if (eqne($elem->sprevious_sibling)) 257 8968 9225
99  
100 8968 ▶ $$self{_eq} //={};
Condition50%1/2
$self->{"_eq"} //= {}
l!l
89680
MC/DC0%0/1
$self->{"_eq"} //= {}
$self->{"_eq"}
Truth table50%1/2
A
A= $self->{"_eq"}
Aresult
11
00
101 8968 ▶ $$self{_ne} //={};
Condition50%1/2
$self->{"_ne"} //= {}
l!l
89680
MC/DC0%0/1
$self->{"_ne"} //= {}
$self->{"_ne"}
Truth table50%1/2
A
A= $self->{"_ne"}
Aresult
11
00
102 8968 ▶ $$self{_regexp} //=0;
Condition50%1/2
$self->{"_regexp"} //= 0
l!l
89680
MC/DC0%0/1
$self->{"_regexp"} //= 0
$self->{"_regexp"}
Truth table50%1/2
A
A= $self->{"_regexp"}
Aresult
11
00
103 8968 ▶ $$self{_bareref}//=0;
Condition50%1/2
$self->{"_bareref"} //= 0
l!l
89680
MC/DC0%0/1
$self->{"_bareref"} //= 0
$self->{"_bareref"}
Truth table50%1/2
A
A= $self->{"_bareref"}
Aresult
11
00
104  
105 # Without options, 'ref' should never be called.
106 8968 ▶ if(!%{$$self{_eq}} && !%{$$self{_ne}} && !$$self{_regexp} && !$$self{_bareref}) { return $self->invalid($elem) }
Branch: 1 branch
BranchTrueFalseTotal
unless (%{$self->{"_eq"};} or %{$self->{"_ne"};} or $self->{"_regexp"} or $self->{"_bareref"}) 1 8967 8968
Condition100%9/9
%{$self->{"_eq"};} or %{$self->{"_ne"};} or $self->{"_regexp"} or $self->{"_bareref"}
l!l&&r!l&&!r
81997681
%{$self->{"_eq"};} or %{$self->{"_ne"};} or $self->{"_regexp"}
l!l&&r!l&&!r
7815384769
%{$self->{"_eq"};} or %{$self->{"_ne"};}
l!l&&r!l&&!r
461532001153
MC/DC100%4/4
%{$self->{"_eq"};} or %{$self->{"_ne"};} or $self->{"_regexp"} or $self->{"_bareref"}
%{$self->{"_eq"};} %{$self->{"_ne"};} $self->{"_regexp"} $self->{"_bareref"}
Truth table100%5/5
A or B or C or D
A= %{$self->{"_eq"};}B= %{$self->{"_ne"};}C= $self->{"_regexp"}D= $self->{"_bareref"}
ABCDresult
1XXX1
01XX1
001X1
00011
00000
107  
108 8967 my ($operator,$rhs)=decompose($elem);
109  
110 8967 ▶ if(!$operator) {
Branch: 4 branches
BranchTrueFalseTotal
if (not $operator) { } 1028 7939 8967
elsif ($operator eq 'eq') { } 3586 4353 7939
elsif ($operator eq 'ne') { } 3584 769 4353
elsif ($operator eq '=~' or $operator eq '!~') { } 768 1 769
111 1028 ▶ if(!$$self{_bareref}) { return $self->invalid($elem,'bare ref check') }
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_bareref"}) 260 768 1028
112 768 return;
113 }
114 elsif($operator eq 'eq') {
115 3586 ▶ $$self{_eqfold}//={map {lc($_)=>undef} keys(%{$$self{_eq}//{}})};
Condition75%3/4
$self->{"_eqfold"} //= {map({lc $_, undef;} keys %{$self->{"_eq"} // {};})}
l!l
35806
$self->{"_eq"} // {}
l!l
60
MC/DC100%1/1
$self->{"_eqfold"} //= {map({lc $_, undef;} keys %{$self->{"_eq"} // {};})}
$self->{"_eqfold"}
MC/DC0%0/1
$self->{"_eq"} // {}
$self->{"_eq"}
Truth table100%2/2
A
A= $self->{"_eqfold"}
Aresult
11
00
Truth table50%1/2
A
A= $self->{"_eq"}
Aresult
11
00
116 3586 ▶ if(!exists($$self{_eqfold}{$rhs})) { return $self->invalid($elem,$rhs) }
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"_eqfold"}{$rhs}) 2562 1024 3586
117 1024 return;
118 }
119 elsif($operator eq 'ne') {
120 3584 ▶ $$self{_nefold}//={map {lc($_)=>undef} keys(%{$$self{_ne}//{}})};
Condition75%3/4
$self->{"_nefold"} //= {map({lc $_, undef;} keys %{$self->{"_ne"} // {};})}
l!l
35795
$self->{"_ne"} // {}
l!l
50
MC/DC100%1/1
$self->{"_nefold"} //= {map({lc $_, undef;} keys %{$self->{"_ne"} // {};})}
$self->{"_nefold"}
MC/DC0%0/1
$self->{"_ne"} // {}
$self->{"_ne"}
Truth table100%2/2
A
A= $self->{"_nefold"}
Aresult
11
00
Truth table50%1/2
A
A= $self->{"_ne"}
Aresult
11
00
121 3584 ▶ if(!exists($$self{_nefold}{$rhs})) { return $self->invalid($elem,$rhs) }
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"_nefold"}{$rhs}) 2560 1024 3584
122 1024 return;
123 }
124 ▶ elsif(($operator eq '=~')||($operator eq '!~')) {
Condition100%3/3
$operator eq "=~" or $operator eq "!~"
l!l&&r!l&&!r
3843841
MC/DC100%2/2
$operator eq "=~" or $operator eq "!~"
$operator eq "=~" $operator eq "!~"
Truth table100%3/3
A or B
A= $operator eq "=~"B= $operator eq "!~"
ABresult
1X1
011
000
125 768 ▶ if(!$$self{_regexp}) { return $self->invalid($elem,$rhs) }
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_regexp"}) 384 384 768
126 384 return;
127 }
128 else {
129 1 return $self->invalid($elem,$rhs);
130 }
131 0 return;
132 }
133  
134 #-----------------------------------------------------------------------------
135  
136 11 1;
137  
138 __END__
↑ Summary Policy/References/ProhibitUnnecessaryRefs.pm »