| 1 | package Perl::Critic::Policy::Variables::ProhibitExcessiveNames; | ||||||||||||||||||||||||||||||||
| 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/; | ||||||||||||||||||||||||||||||
|
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{Use concise names}; | |||||||||||||||||||||||||||||||
| 14 | 11 | Readonly::Scalar my $EXPL => undef; | |||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||
| 16 | 100 | ▶ | sub applies_to { return qw/PPI::Statement::Variable PPI::Statement::Sub/ } | ||||||||||||||||||||||||||||||
|
Subroutine
applies_to: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 17 | 52 | ▶ | sub default_severity { return $SEVERITY_LOW } | ||||||||||||||||||||||||||||||
|
Subroutine
default_severity: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 18 | 0 | ▶ | sub default_themes { return qw/cosmetic/ } | ||||||||||||||||||||||||||||||
|
Subroutine
default_themes: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||
| 20 | 13 | ▶ | sub supported_parameters { | ||||||||||||||||||||||||||||||
|
Subroutine
supported_parameters: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 21 | return ( | ||||||||||||||||||||||||||||||||
| 22 | { | ||||||||||||||||||||||||||||||||
| 23 | 13 | name => 'subs', | |||||||||||||||||||||||||||||||
| 24 | description => 'Report excessive subroutine names.', | ||||||||||||||||||||||||||||||||
| 25 | default_string => '1', | ||||||||||||||||||||||||||||||||
| 26 | behavior => 'boolean', | ||||||||||||||||||||||||||||||||
| 27 | }, | ||||||||||||||||||||||||||||||||
| 28 | { | ||||||||||||||||||||||||||||||||
| 29 | name => 'vars', | ||||||||||||||||||||||||||||||||
| 30 | description => 'Report excessive variable names.', | ||||||||||||||||||||||||||||||||
| 31 | default_string => '1', | ||||||||||||||||||||||||||||||||
| 32 | behavior => 'boolean', | ||||||||||||||||||||||||||||||||
| 33 | }, | ||||||||||||||||||||||||||||||||
| 34 | { | ||||||||||||||||||||||||||||||||
| 35 | name => 'length', | ||||||||||||||||||||||||||||||||
| 36 | description => 'Maximum identify length.', | ||||||||||||||||||||||||||||||||
| 37 | default_string => '15', | ||||||||||||||||||||||||||||||||
| 38 | behavior => 'integer', | ||||||||||||||||||||||||||||||||
| 39 | integer_minimum=> 0, | ||||||||||||||||||||||||||||||||
| 40 | }, | ||||||||||||||||||||||||||||||||
| 41 | { | ||||||||||||||||||||||||||||||||
| 42 | name => 'words', | ||||||||||||||||||||||||||||||||
| 43 | description => 'Maximum number of words.', | ||||||||||||||||||||||||||||||||
| 44 | default_string => '3', | ||||||||||||||||||||||||||||||||
| 45 | behavior => 'integer', | ||||||||||||||||||||||||||||||||
| 46 | integer_minimum=> 0, | ||||||||||||||||||||||||||||||||
| 47 | }, | ||||||||||||||||||||||||||||||||
| 48 | { | ||||||||||||||||||||||||||||||||
| 49 | name => 'smallest', | ||||||||||||||||||||||||||||||||
| 50 | description => 'Smallest word length in a multi-word.', | ||||||||||||||||||||||||||||||||
| 51 | default_string => '4', | ||||||||||||||||||||||||||||||||
| 52 | behavior => 'integer', | ||||||||||||||||||||||||||||||||
| 53 | integer_minimum=> 0, | ||||||||||||||||||||||||||||||||
| 54 | }, | ||||||||||||||||||||||||||||||||
| 55 | { | ||||||||||||||||||||||||||||||||
| 56 | name => 'lpw', | ||||||||||||||||||||||||||||||||
| 57 | description => 'Length-per-word maximum average.', | ||||||||||||||||||||||||||||||||
| 58 | default_string => '7', | ||||||||||||||||||||||||||||||||
| 59 | behavior => 'integer', | ||||||||||||||||||||||||||||||||
| 60 | integer_minimum=> 0, | ||||||||||||||||||||||||||||||||
| 61 | }, | ||||||||||||||||||||||||||||||||
| 62 | { | ||||||||||||||||||||||||||||||||
| 63 | name => 'short', | ||||||||||||||||||||||||||||||||
| 64 | description => 'Always allowed at-or-below this length.', | ||||||||||||||||||||||||||||||||
| 65 | default_string => '6', | ||||||||||||||||||||||||||||||||
| 66 | behavior => 'integer', | ||||||||||||||||||||||||||||||||
| 67 | integer_minimum=> 0, | ||||||||||||||||||||||||||||||||
| 68 | }, | ||||||||||||||||||||||||||||||||
| 69 | ); | ||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||
| 72 | 147 | ▶ | sub desigil { | ||||||||||||||||||||||||||||||
|
Subroutine
desigil: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 73 | 147 | my ($name)=@_; | |||||||||||||||||||||||||||||||
| 74 | 147 | $name=~s/^[\%\@\$\&\*]//; | |||||||||||||||||||||||||||||||
| 75 | 147 | $name=~s/.*:://; | |||||||||||||||||||||||||||||||
| 76 | 147 | return $name; | |||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||
| 79 | 133 | ▶ | sub splitter { | ||||||||||||||||||||||||||||||
|
Subroutine
splitter: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 80 | 133 | my ($name)=@_; | |||||||||||||||||||||||||||||||
| 81 | 133 | $name=~s/^[\%\@\$\&\*]//; | |||||||||||||||||||||||||||||||
| 82 | 133 | $name=~s/.*:://; | |||||||||||||||||||||||||||||||
| 83 | 133 | $name=~s/^_+//; | |||||||||||||||||||||||||||||||
| 84 | 133 | ▶ | if($name=~/^[A-Z]+$/) { return $name } | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 85 | 131 | ▶ | if($name=~/_/) { return split(/_+/,$name) } | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 86 | 96 | return split(/(?=[A-Z])/,$name) | |||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||
| 89 | 147 | ▶ | sub happy { | ||||||||||||||||||||||||||||||
|
Subroutine
happy: called CC=12 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 90 | 147 | my ($name,$w,$L,$lpw,$small,$short)=@_; | |||||||||||||||||||||||||||||||
| 91 | 147 | $name=desigil($name); | |||||||||||||||||||||||||||||||
| 92 | 147 | my $NL=length($name); | |||||||||||||||||||||||||||||||
| 93 | 147 | ▶ | if($short&&($NL<=$short)) { return 1 } | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$short and $NL <= $short
MC/DC100%2/2
$short and $NL <= $short
$short
$NL <= $short
Truth table100%3/3
A and B
A= $shortB= $NL <= $short
| |||||||||||||||||||||||||||||||||
| 94 | 121 | ▶ | if($L&&($NL>$L)) { return 0 } | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$L and $NL > $L
MC/DC100%2/2
$L and $NL > $L
$L
$NL > $L
Truth table100%3/3
A and B
A= $LB= $NL > $L
| |||||||||||||||||||||||||||||||||
| 95 | 111 | my @S=splitter($name); | |||||||||||||||||||||||||||||||
| 96 | 111 | ▶ | if($w&&($#S>=$w)) { return 0 } | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$w and $#S >= $w
MC/DC100%2/2
$w and $#S >= $w
$w
$#S >= $w
Truth table100%3/3
A and B
A= $wB= $#S >= $w
| |||||||||||||||||||||||||||||||||
| 97 | 101 | ▶ | if($#S) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 98 | 78 | ▶ | if($lpw&&($NL>$lpw*(1+$#S))) { return 0 } | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$lpw and $NL > $lpw * (1 + $#S)
MC/DC100%2/2
$lpw and $NL > $lpw * (1 + $#S)
$lpw
$NL > $lpw * (1 + $#S)
Truth table100%3/3
A and B
A= $lpwB= $NL > $lpw * (1 + $#S)
| |||||||||||||||||||||||||||||||||
| 99 | 65 | ▶ | if($small) { foreach my $word (grep {length($_)<$small} @S) { return 0 } } | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||
| 101 | 59 | return 1; | |||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||
| 104 | 52 | ▶ | sub invalid { | ||||||||||||||||||||||||||||||
|
Subroutine
invalid: called CC=3 SCAR=11.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 105 | 52 | my ($self,$elem,$note)=@_; | |||||||||||||||||||||||||||||||
| 106 | 52 | ▶ | $note//=''; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$note //= ""
MC/DC0%0/1
$note //= ""
$note
Truth table50%1/2
A
A= $note
| |||||||||||||||||||||||||||||||||
| 107 | 52 | ▶ | if($note) { $note=" ($note)" } | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 108 | 52 | return $self->violation(sprintf("%s%s",$DESC,$note),$EXPL,$elem); | |||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||
| 111 | 100 | ▶ | sub violates { | ||||||||||||||||||||||||||||||
|
Subroutine
violates: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 112 | 100 | my ($self,$elem,undef)=@_; | |||||||||||||||||||||||||||||||
| 113 | 100 | my %violations; | |||||||||||||||||||||||||||||||
| 114 | 100 | ▶ | if($$self{_vars}&&$elem->isa('PPI::Statement::Variable')) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
$self->{"_vars"} and $elem->isa("PPI::Statement::Variable")
MC/DC100%2/2
$self->{"_vars"} and $elem->isa("PPI::Statement::Variable")
$self->{"_vars"}
$elem->isa("PPI::Statement::Variable")
Truth table100%3/3
A and B
A= $self->{"_vars"}B= $elem->isa("PPI::Statement::Variable")
| |||||||||||||||||||||||||||||||||
| 115 | 62 | %violations=map {$_=>undef} grep {!happy($_,@$self{qw/_words _length _lpw _smallest _short/})} $elem->variables() | |||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||
| 117 | ▶ | elsif($$self{_subs}&&$elem->isa('PPI::Statement::Sub')) { | |||||||||||||||||||||||||||||||
|
Condition100%3/3
$self->{"_subs"} and $elem->isa("PPI::Statement::Sub")
MC/DC100%2/2
$self->{"_subs"} and $elem->isa("PPI::Statement::Sub")
$self->{"_subs"}
$elem->isa("PPI::Statement::Sub")
Truth table100%3/3
A and B
A= $self->{"_subs"}B= $elem->isa("PPI::Statement::Sub")
| |||||||||||||||||||||||||||||||||
| 118 | 30 | %violations=map {$_=>undef} grep {!happy($_,@$self{qw/_words _length _lpw _smallest _short/})} $elem->name(); | |||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||
| 120 | 100 | ▶ | if(%violations) { return $self->invalid($elem,join(',',sort keys(%violations))) } | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 121 | 48 | return; | |||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||
| 124 | 11 | 1; | |||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||
| 126 | __END__ | ||||||||||||||||||||||||||||||||