| 1 | package Perl::Critic::Policy::Bangs::ProhibitVagueNames; | ||||||||||||||||||
| 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 qw( :booleans :severities ); | ||||||||||||||||
|
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 | #---------------------------------------------------------------------------- | ||||||||||||||||||
| 11 | |||||||||||||||||||
| 12 | 12 | ▶ | sub supported_parameters { | ||||||||||||||||
|
Subroutine
supported_parameters: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||
| 13 | return ( | ||||||||||||||||||
| 14 | { | ||||||||||||||||||
| 15 | 12 | name => 'names', | |||||||||||||||||
| 16 | description => 'Words to prohibit as variable and subroutine names.', | ||||||||||||||||||
| 17 | behavior => 'string list', | ||||||||||||||||||
| 18 | default_string => 'data info var obj object tmp temp', | ||||||||||||||||||
| 19 | }, | ||||||||||||||||||
| 20 | { | ||||||||||||||||||
| 21 | name => 'add_names', | ||||||||||||||||||
| 22 | description => 'Additional words to prohibit as variable and subroutine names.', | ||||||||||||||||||
| 23 | behavior => 'string list', | ||||||||||||||||||
| 24 | }, | ||||||||||||||||||
| 25 | ); | ||||||||||||||||||
| 26 | } | ||||||||||||||||||
| 27 | |||||||||||||||||||
| 28 | 21 | ▶ | sub default_severity { return $SEVERITY_MEDIUM } | ||||||||||||||||
|
Subroutine
default_severity: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||
| 29 | 0 | ▶ | sub default_themes { return qw( bangs readability ) } | ||||||||||||||||
|
Subroutine
default_themes: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||
| 30 | 7 | ▶ | sub applies_to { return 'PPI::Statement::Variable', 'PPI::Statement::Sub' } | ||||||||||||||||
|
Subroutine
applies_to: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||
| 31 | |||||||||||||||||||
| 32 | =for stopwords whitespace | ||||||||||||||||||
| 33 | |||||||||||||||||||
| 34 | =head1 NAME | ||||||||||||||||||
| 35 | |||||||||||||||||||
| 36 | Perl::Critic::Policy::Bangs::ProhibitVagueNames - Don't use generic variable and subroutine names. | ||||||||||||||||||
| 37 | |||||||||||||||||||
| 38 | =head1 AFFILIATION | ||||||||||||||||||
| 39 | |||||||||||||||||||
| 40 | This Policy is part of the L<Perl::Critic::Bangs> distribution. | ||||||||||||||||||
| 41 | |||||||||||||||||||
| 42 | =head1 DESCRIPTION | ||||||||||||||||||
| 43 | |||||||||||||||||||
| 44 | Variables and subroutines should have descriptive names. Names like | ||||||||||||||||||
| 45 | C<$data> and C<$info> are completely vague. | ||||||||||||||||||
| 46 | |||||||||||||||||||
| 47 | my $data = shift; # not OK. | ||||||||||||||||||
| 48 | my $userinfo = shift # OK | ||||||||||||||||||
| 49 | |||||||||||||||||||
| 50 | See | ||||||||||||||||||
| 51 | L<http://www.oreillynet.com/onlamp/blog/2004/03/the_worlds_two_worst_variable.html> | ||||||||||||||||||
| 52 | for more of my ranting on this. | ||||||||||||||||||
| 53 | |||||||||||||||||||
| 54 | By default, the following names are bad: data, info, var, obj, object, tmp, temp | ||||||||||||||||||
| 55 | |||||||||||||||||||
| 56 | The checking of names is case-insensitive. C<$info> and C<$INFO> are equally bad. | ||||||||||||||||||
| 57 | |||||||||||||||||||
| 58 | =head1 CONFIGURATION | ||||||||||||||||||
| 59 | |||||||||||||||||||
| 60 | This policy has two options: C<names> and C<add_names>. | ||||||||||||||||||
| 61 | |||||||||||||||||||
| 62 | =head2 C<names> | ||||||||||||||||||
| 63 | |||||||||||||||||||
| 64 | To replace the list of vague names, specify them as a whitespace | ||||||||||||||||||
| 65 | delimited set of prohibited names. | ||||||||||||||||||
| 66 | |||||||||||||||||||
| 67 | [Bangs::ProhibitVagueNames] | ||||||||||||||||||
| 68 | names = data count line next | ||||||||||||||||||
| 69 | |||||||||||||||||||
| 70 | =head2 C<add_names> | ||||||||||||||||||
| 71 | |||||||||||||||||||
| 72 | To add to the list of vague names, specify them as a whitespace | ||||||||||||||||||
| 73 | delimited set of prohibited names. | ||||||||||||||||||
| 74 | |||||||||||||||||||
| 75 | [Bangs::ProhibitVagueNames] | ||||||||||||||||||
| 76 | add_names = foo bar bat | ||||||||||||||||||
| 77 | |||||||||||||||||||
| 78 | =cut | ||||||||||||||||||
| 79 | |||||||||||||||||||
| 80 | 9 | ▶ | sub initialize_if_enabled { | ||||||||||||||||
|
Subroutine
initialize_if_enabled: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||
| 81 | 9 | my ( $self, $config ) = @_; | |||||||||||||||||
| 82 | |||||||||||||||||||
| 83 | 9 | $self->{_names} = { %{ $self->{_names} }, %{ $self->{_add_names} } }; | |||||||||||||||||
| 84 | |||||||||||||||||||
| 85 | 9 | return $TRUE; | |||||||||||||||||
| 86 | } | ||||||||||||||||||
| 87 | |||||||||||||||||||
| 88 | |||||||||||||||||||
| 89 | 26 | ▶ | sub violates { | ||||||||||||||||
|
Subroutine
violates: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||
| 90 | 26 | my ( $self, $elem, $doc ) = @_; | |||||||||||||||||
| 91 | |||||||||||||||||||
| 92 | 26 | my @violations; | |||||||||||||||||
| 93 | |||||||||||||||||||
| 94 | 26 | my $type = ref($elem); | |||||||||||||||||
| 95 | 26 | ▶ | if ( $type eq 'PPI::Statement::Variable' ) { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 96 | 22 | for my $symbol ( $elem->symbols ) { | |||||||||||||||||
| 97 | # Make $basename be the variable name with no sigils or namespaces. | ||||||||||||||||||
| 98 | 25 | my $fullname = $symbol->canonical; | |||||||||||||||||
| 99 | 25 | my $basename = $fullname; | |||||||||||||||||
| 100 | 25 | $basename =~ s/^[\$@%]//; | |||||||||||||||||
| 101 | 25 | $basename =~ s/.*:://; | |||||||||||||||||
| 102 | |||||||||||||||||||
| 103 | 25 | push( @violations, $self->_potential_violation( $symbol, $fullname, $basename, 'Variable' ) ); | |||||||||||||||||
| 104 | } | ||||||||||||||||||
| 105 | } | ||||||||||||||||||
| 106 | elsif ( $type eq 'PPI::Statement::Sub' ) { | ||||||||||||||||||
| 107 | 3 | my $fullname = $elem->name; | |||||||||||||||||
| 108 | 3 | my $basename = $fullname; | |||||||||||||||||
| 109 | 3 | $basename =~ s/.*:://; | |||||||||||||||||
| 110 | |||||||||||||||||||
| 111 | 3 | push( @violations, $self->_potential_violation( $elem, $fullname, $basename, 'Subroutine' ) ); | |||||||||||||||||
| 112 | } | ||||||||||||||||||
| 113 | elsif ( $type eq 'PPI::Statement::Scheduled' ) { | ||||||||||||||||||
| 114 | # Ignore BEGIN, INIT, etc | ||||||||||||||||||
| 115 | } | ||||||||||||||||||
| 116 | else { | ||||||||||||||||||
| 117 | 0 | die "Unknown type $type"; | |||||||||||||||||
| 118 | } | ||||||||||||||||||
| 119 | |||||||||||||||||||
| 120 | 26 | return @violations; | |||||||||||||||||
| 121 | } | ||||||||||||||||||
| 122 | |||||||||||||||||||
| 123 | 28 | ▶ | sub _potential_violation { | ||||||||||||||||
|
Subroutine
_potential_violation: called CC=3 SCAR=11.0
| |||||||||||||||||||
| 124 | 28 | my $self = shift; | |||||||||||||||||
| 125 | 28 | my $symbol = shift; | |||||||||||||||||
| 126 | 28 | my $fullname = shift; | |||||||||||||||||
| 127 | 28 | my $basename = shift; | |||||||||||||||||
| 128 | 28 | my $what = shift; | |||||||||||||||||
| 129 | |||||||||||||||||||
| 130 | 28 | $basename = lc $basename; | |||||||||||||||||
| 131 | |||||||||||||||||||
| 132 | 28 | foreach my $naughty ( keys %{ $self->{'_names'} } ) { | |||||||||||||||||
| 133 | 139 | ▶ | if ( $basename eq lc $naughty ) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 134 | 21 | my $desc = qq{$what named "$fullname"}; | |||||||||||||||||
| 135 | 21 | my $expl = "$what names should be specific, not vague"; | |||||||||||||||||
| 136 | 21 | return $self->violation( $desc, $expl, $symbol ); | |||||||||||||||||
| 137 | } | ||||||||||||||||||
| 138 | } | ||||||||||||||||||
| 139 | |||||||||||||||||||
| 140 | 7 | return; | |||||||||||||||||
| 141 | } | ||||||||||||||||||
| 142 | |||||||||||||||||||
| 143 | 5 | 1; | |||||||||||||||||
| 144 | |||||||||||||||||||
| 145 | __END__ | ||||||||||||||||||