| 1 | package Perl::Critic::Policy::Bangs::ProhibitNumberedNames; | ||||||||||||||||||
| 2 | |||||||||||||||||||
| 3 | 5 | ▶ | use strict; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 4 | 5 | ▶ | use warnings; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 5 | 5 | ▶ | use Perl::Critic::Utils; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 6 | 5 | ▶ | use base 'Perl::Critic::Policy'; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 7 | |||||||||||||||||||
| 8 | 5 | our $VERSION = '1.14'; | |||||||||||||||||
| 9 | |||||||||||||||||||
| 10 | 17 | ▶ | sub supported_parameters { | ||||||||||||||||
|
Subroutine
supported_parameters: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||
| 11 | return ( | ||||||||||||||||||
| 12 | { | ||||||||||||||||||
| 13 | 17 | name => 'exceptions', | |||||||||||||||||
| 14 | description => 'Things to allow in variable and subroutine names.', | ||||||||||||||||||
| 15 | behavior => 'string list', | ||||||||||||||||||
| 16 | default_string => 'base64 md5 rc4 sha0 sha1 sha256 utf8 x11 win32', | ||||||||||||||||||
| 17 | }, | ||||||||||||||||||
| 18 | { | ||||||||||||||||||
| 19 | name => 'add_exceptions', | ||||||||||||||||||
| 20 | description => 'Additional things to allow in variable and subroutine names.', | ||||||||||||||||||
| 21 | behavior => 'string list', | ||||||||||||||||||
| 22 | }, | ||||||||||||||||||
| 23 | ); | ||||||||||||||||||
| 24 | } | ||||||||||||||||||
| 25 | |||||||||||||||||||
| 26 | 8 | ▶ | sub default_severity { return $SEVERITY_MEDIUM } | ||||||||||||||||
|
Subroutine
default_severity: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||
| 27 | 0 | ▶ | sub default_themes { return qw( bangs maintenance ) } | ||||||||||||||||
|
Subroutine
default_themes: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||
| 28 | 12 | ▶ | sub applies_to { return 'PPI::Statement::Variable', 'PPI::Statement::Sub' } | ||||||||||||||||
|
Subroutine
applies_to: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||
| 29 | |||||||||||||||||||
| 30 | =head1 NAME | ||||||||||||||||||
| 31 | |||||||||||||||||||
| 32 | Perl::Critic::Policy::Bangs::ProhibitNumberedNames - Prohibit variables and subroutines with names that end in digits. | ||||||||||||||||||
| 33 | |||||||||||||||||||
| 34 | =head1 AFFILIATION | ||||||||||||||||||
| 35 | |||||||||||||||||||
| 36 | This Policy is part of the L<Perl::Critic::Bangs> distribution. | ||||||||||||||||||
| 37 | |||||||||||||||||||
| 38 | =head1 DESCRIPTION | ||||||||||||||||||
| 39 | |||||||||||||||||||
| 40 | Similar variables should be meaningfully different. A lazy way to | ||||||||||||||||||
| 41 | differentiate similar variables is by tacking a number at the end. | ||||||||||||||||||
| 42 | |||||||||||||||||||
| 43 | my $total = $price * $quantity; | ||||||||||||||||||
| 44 | my $total2 = $total + ($total * $taxrate); | ||||||||||||||||||
| 45 | my $total3 = $total2 + $shipping; | ||||||||||||||||||
| 46 | |||||||||||||||||||
| 47 | The difference between C<$total> and C<$total3> is not described | ||||||||||||||||||
| 48 | by the silly "3" at the end. Instead, it should be: | ||||||||||||||||||
| 49 | |||||||||||||||||||
| 50 | my $merch_total = $price * $quantity; | ||||||||||||||||||
| 51 | my $subtotal = $merch_total + ($merch_total * $taxrate); | ||||||||||||||||||
| 52 | my $grand_total = $subtotal + $shipping; | ||||||||||||||||||
| 53 | |||||||||||||||||||
| 54 | Both variable and subroutine names are checked. | ||||||||||||||||||
| 55 | |||||||||||||||||||
| 56 | See | ||||||||||||||||||
| 57 | L<http://www.oreillynet.com/onlamp/blog/2004/03/the_worlds_two_worst_variable.html> | ||||||||||||||||||
| 58 | for more of my ranting on this. | ||||||||||||||||||
| 59 | |||||||||||||||||||
| 60 | =head1 CONFIGURATION | ||||||||||||||||||
| 61 | |||||||||||||||||||
| 62 | This policy has two options: C<exceptions> and C<add_exceptions>. | ||||||||||||||||||
| 63 | |||||||||||||||||||
| 64 | =head2 C<exceptions> | ||||||||||||||||||
| 65 | |||||||||||||||||||
| 66 | This policy starts with a list of numbered names that are legitimate | ||||||||||||||||||
| 67 | to have ending with a number: | ||||||||||||||||||
| 68 | |||||||||||||||||||
| 69 | base64 md5 rc4 sha0 sha1 sha256 utf8 x11 win32 | ||||||||||||||||||
| 70 | |||||||||||||||||||
| 71 | The exceptions for the policy also apply to names based on the exceptions. | ||||||||||||||||||
| 72 | If C<$base64> is acceptable as an exception, so is C<$calculated_base64>. | ||||||||||||||||||
| 73 | The exception must be separated from the left part of the name by at | ||||||||||||||||||
| 74 | least one underscore to be recognized. | ||||||||||||||||||
| 75 | |||||||||||||||||||
| 76 | The exceptions are case-insensitive. C<$UTF8> and C<$utf8> are both | ||||||||||||||||||
| 77 | seen the same as far as being exceptions. | ||||||||||||||||||
| 78 | |||||||||||||||||||
| 79 | To replace the list of exceptions, specify a value for the | ||||||||||||||||||
| 80 | C<exceptions> option. | ||||||||||||||||||
| 81 | |||||||||||||||||||
| 82 | [Bangs::ProhibitNumberedNames] | ||||||||||||||||||
| 83 | exceptions = logan7 babylon5 | ||||||||||||||||||
| 84 | |||||||||||||||||||
| 85 | =head2 C<add_exceptions> | ||||||||||||||||||
| 86 | |||||||||||||||||||
| 87 | To add exceptions to the list, give a value for C<add_exceptions> in | ||||||||||||||||||
| 88 | your F<.perlcriticrc> file like this: | ||||||||||||||||||
| 89 | |||||||||||||||||||
| 90 | [Bangs::ProhibitNumberedNames] | ||||||||||||||||||
| 91 | add_exceptions = adam12 route66 | ||||||||||||||||||
| 92 | |||||||||||||||||||
| 93 | =cut | ||||||||||||||||||
| 94 | |||||||||||||||||||
| 95 | 14 | ▶ | sub initialize_if_enabled { | ||||||||||||||||
|
Subroutine
initialize_if_enabled: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||
| 96 | 14 | my ( $self, $config ) = @_; | |||||||||||||||||
| 97 | |||||||||||||||||||
| 98 | $self->{_exceptions} = | ||||||||||||||||||
| 99 | 14 | { %{ $self->{_exceptions} }, %{ $self->{_add_exceptions} } }; | |||||||||||||||||
| 100 | |||||||||||||||||||
| 101 | 14 | return $TRUE; | |||||||||||||||||
| 102 | } | ||||||||||||||||||
| 103 | |||||||||||||||||||
| 104 | 6 | ▶ | sub _init_exception_regexes { | ||||||||||||||||
|
Subroutine
_init_exception_regexes: called CC=2 SCAR=6.9
| |||||||||||||||||||
| 105 | 6 | my $self = shift; | |||||||||||||||||
| 106 | |||||||||||||||||||
| 107 | 6 | my @regexes; | |||||||||||||||||
| 108 | 6 | for my $exception ( keys %{$self->{_exceptions}} ) { | |||||||||||||||||
| 109 | 40 | push( @regexes, qr/.*_\Q$exception\E$/ ); | |||||||||||||||||
| 110 | } | ||||||||||||||||||
| 111 | |||||||||||||||||||
| 112 | 6 | $self->{_exception_regexes} = \@regexes; | |||||||||||||||||
| 113 | |||||||||||||||||||
| 114 | 6 | return; | |||||||||||||||||
| 115 | } | ||||||||||||||||||
| 116 | |||||||||||||||||||
| 117 | 25 | ▶ | sub violates { | ||||||||||||||||
|
Subroutine
violates: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||
| 118 | 25 | my ( $self, $elem, $doc ) = @_; | |||||||||||||||||
| 119 | |||||||||||||||||||
| 120 | 25 | my @violations; | |||||||||||||||||
| 121 | |||||||||||||||||||
| 122 | 25 | my $type = ref($elem); | |||||||||||||||||
| 123 | 25 | ▶ | if ( $type eq 'PPI::Statement::Variable' ) { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 124 | 20 | for my $symbol ( $elem->symbols ) { | |||||||||||||||||
| 125 | # make $basename be the variable name with no sigils or namespaces. | ||||||||||||||||||
| 126 | 22 | my $fullname = $symbol->canonical; | |||||||||||||||||
| 127 | 22 | my $basename = $fullname; | |||||||||||||||||
| 128 | 22 | $basename =~ s/.*:://; | |||||||||||||||||
| 129 | 22 | $basename =~ s/^[\$@%]//; | |||||||||||||||||
| 130 | |||||||||||||||||||
| 131 | 22 | push( @violations, $self->_potential_violation( $symbol, $fullname, $basename, 'Variable' ) ); | |||||||||||||||||
| 132 | } | ||||||||||||||||||
| 133 | } | ||||||||||||||||||
| 134 | elsif ( $type eq 'PPI::Statement::Sub' ) { | ||||||||||||||||||
| 135 | 4 | my $fullname = $elem->name; | |||||||||||||||||
| 136 | 4 | my $basename = $fullname; | |||||||||||||||||
| 137 | 4 | $basename =~ s/.*:://; | |||||||||||||||||
| 138 | |||||||||||||||||||
| 139 | 4 | push( @violations, $self->_potential_violation( $elem, $fullname, $basename, 'Subroutine' ) ); | |||||||||||||||||
| 140 | } | ||||||||||||||||||
| 141 | elsif ( $type eq 'PPI::Statement::Scheduled' ) { | ||||||||||||||||||
| 142 | # Ignore BEGIN, INIT, etc | ||||||||||||||||||
| 143 | } | ||||||||||||||||||
| 144 | else { | ||||||||||||||||||
| 145 | 0 | die "Unknown type $type"; | |||||||||||||||||
| 146 | } | ||||||||||||||||||
| 147 | |||||||||||||||||||
| 148 | 25 | return @violations; | |||||||||||||||||
| 149 | } | ||||||||||||||||||
| 150 | |||||||||||||||||||
| 151 | 26 | ▶ | sub _potential_violation { | ||||||||||||||||
|
Subroutine
_potential_violation: called CC=6 SCAR=17.9
| |||||||||||||||||||
| 152 | 26 | my $self = shift; | |||||||||||||||||
| 153 | 26 | my $symbol = shift; | |||||||||||||||||
| 154 | 26 | my $fullname = shift; | |||||||||||||||||
| 155 | 26 | my $basename = shift; | |||||||||||||||||
| 156 | 26 | my $what = shift; | |||||||||||||||||
| 157 | |||||||||||||||||||
| 158 | 26 | ▶ | if ( $basename =~ /\D+\d+$/ ) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 159 | 19 | $basename = lc $basename; | |||||||||||||||||
| 160 | |||||||||||||||||||
| 161 | # Check to see if it's an exact match for an exception. | ||||||||||||||||||
| 162 | # $md5 is excepted by "md5" | ||||||||||||||||||
| 163 | 19 | ▶ | return if $self->{_exceptions}{$basename}; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 164 | |||||||||||||||||||
| 165 | # Check to see if they match the end of the variable regexes. | ||||||||||||||||||
| 166 | # $foo_md5 is excepted by "md5" | ||||||||||||||||||
| 167 | 10 | ▶ | $self->_init_exception_regexes unless $self->{_exception_regexes}; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 168 | 10 | for my $re ( @{$self->{_exception_regexes}} ) { | |||||||||||||||||
| 169 | 58 | ▶ | return if $basename =~ $re; # We're OK via exception | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 170 | } | ||||||||||||||||||
| 171 | |||||||||||||||||||
| 172 | 8 | my $desc = qq{$what named "$fullname"}; | |||||||||||||||||
| 173 | 8 | my $expl = "$what names should not be differentiated only by digits"; | |||||||||||||||||
| 174 | 8 | return $self->violation( $desc, $expl, $symbol ); | |||||||||||||||||
| 175 | } | ||||||||||||||||||
| 176 | |||||||||||||||||||
| 177 | 7 | return; | |||||||||||||||||
| 178 | } | ||||||||||||||||||
| 179 | |||||||||||||||||||
| 180 | 5 | 1; | |||||||||||||||||
| 181 | |||||||||||||||||||
| 182 | __END__ | ||||||||||||||||||