blib/lib/Perl/Critic/Policy/NamingConventions/ProhibitAmbiguousNames.pm

filter: Statementstmt 95.5% 43 / 45 Branchbran 58.3% 7 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 11 / 11 Podpod 80.0% 4 / 5 totaltotal 89.0% 65 / 73 CC 9 SCAR 16.5
CC
9
Cov
88%
violates
9.8
BEGIN
1.0
BEGIN
1.0
CRAP 5.2
« blib/lib/Perl/Critic/PolicyFactory.pm ↑ Summary blib/lib/Perl/Critic/Statistics.pm »
1 package Perl::Critic::Policy::NamingConventions::ProhibitAmbiguousNames;
2  
3 40 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 40 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 40 ▶use Perl::Critic::Utils qw{ :severities :data_conversion };
Subroutine BEGIN: called CC=1 SCAR=0.0
9 40 ▶use parent 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 40 our $VERSION = '1.150';
12  
13 #-----------------------------------------------------------------------------
14  
15 40 Readonly::Scalar my $EXPL => [ 48 ];
16  
17 40 Readonly::Scalar my $DEFAULT_FORBID =>
18 'abstract bases close contract last left no record right second set';
19  
20 #-----------------------------------------------------------------------------
21  
22 sub supported_parameters {
23 return (
24 {
25 91 ▶ name => 'forbid',
Subroutine supported_parameters: called CC=1 SCAR=0.0
Pod undocumented
26 description => 'The variable names that are not to be allowed.',
27 default_string => $DEFAULT_FORBID,
28 behavior => 'string list',
29 },
30 );
31 }
32  
33 75 ▶sub default_severity { return $SEVERITY_MEDIUM }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
34 86 ▶sub default_themes { return qw(core pbp maintenance) }
Subroutine default_themes: called CC=1 SCAR=0.0
Pod documented
35 31 ▶sub applies_to { return qw(PPI::Statement::Sub
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
36 PPI::Statement::Variable) }
37  
38 #-----------------------------------------------------------------------------
39  
40 sub violates {
41 90 ▶ my ( $self, $elem, undef ) = @_;
Subroutine violates: called CC=9 SCAR=22.9
Pod documented
42  
43 90 ▶ if ( $elem->isa('PPI::Statement::Sub') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem->isa("PPI::Statement::Sub")) 5 85 90
44 5 my @words = grep { $_->isa('PPI::Token::Word') } $elem->schildren();
45 5 for my $word (@words) {
46  
47 # strip off any leading "Package::"
48 10 my ($name) = $word =~ m/ (\w+) \z /xms;
49 10 ▶ next if not defined $name; # should never happen, right?
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 10 10
50  
51 10 ▶ if ( exists $self->{_forbid}->{$name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"_forbid"}{$name}) 0 10 10
52 0 return $self->violation(
53 qq<Ambiguously named subroutine "$name">,
54 $EXPL,
55 $elem,
56 );
57 }
58 }
59 5 return; # ok
60 }
61  
62 # PPI::Statement::Variable
63  
64 # Accumulate them since there can be more than one violation
65 # per variable statement
66 85 my @violations;
67  
68 # TODO: false positive bug - this can erroneously catch the
69 # assignment half of a variable statement
70  
71 85 my $symbols = $elem->find('PPI::Token::Symbol');
72 85 ▶ if ($symbols) { # this should always be true, right?
Branch: 1 branch
BranchTrueFalseTotal
if ($symbols) 85 0 85
73 85 for my $symbol ( @{$symbols} ) {
74  
75 # Strip off sigil and any leading "Package::"
76 # Beware that punctuation vars may have no
77 # alphanumeric characters.
78  
79 85 my ($name) = $symbol =~ m/ (\w+) \z /xms;
80 85 ▶ next if ! defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 85 85
81  
82 85 ▶ if ( exists $self->{_forbid}->{$name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"_forbid"}{$name}) 0 85 85
83 0 push
84 @violations,
85 $self->violation(
86 qq<Ambiguously named variable "$name">,
87 $EXPL,
88 $elem,
89 );
90 }
91 }
92 }
93  
94 85 return @violations;
95 }
96  
97 40 1;
98  
99 __END__
100  
101 #-----------------------------------------------------------------------------
102  
103 =pod
104  
105 =for stopwords bioinformatics
106  
107 =head1 NAME
108  
109 Perl::Critic::Policy::NamingConventions::ProhibitAmbiguousNames - Don't use vague variable or subroutine names like 'last' or 'record'.
110  
111  
112 =head1 AFFILIATION
113  
114 This Policy is part of the core L<Perl::Critic|Perl::Critic>
115 distribution.
116  
117  
118 =head1 DESCRIPTION
119  
120 Conway lists a collection of English words which are highly ambiguous
121 as variable or subroutine names. For example, C<$last> can mean
122 previous or final.
123  
124 This policy tests against a list of ambiguous words for variable
125 names.
126  
127  
128 =head1 CONFIGURATION
129  
130 The default list of forbidden words is:
131  
132 abstract bases close contract last left no record right second set
133  
134 This list can be changed by giving a value for C<forbid> of a series
135 of forbidden words separated by spaces.
136  
137 For example, if you decide that C<bases> is an OK name for variables
138 (e.g. in bioinformatics), then put something like the following in
139 C<$HOME/.perlcriticrc>:
140  
141 [NamingConventions::ProhibitAmbiguousNames]
142 forbid = last set left right no abstract contract record second close
143  
144  
145 =head1 BUGS
146  
147 Currently this policy checks the entire variable and subroutine name,
148 not parts of the name. For example, it catches C<$last> but not
149 C<$last_record>. Hopefully future versions will catch both cases.
150  
151 Some variable statements will be false positives if they have
152 assignments where the right hand side uses forbidden names. For
153 example, in this case the C<last> incorrectly triggers a violation.
154  
155 my $previous_record = $Foo::last;
156  
157  
158 =head1 AUTHOR
159  
160 Chris Dolan <cdolan@cpan.org>
161  
162  
163 =head1 COPYRIGHT
164  
165 Copyright (c) 2005-2011 Chris Dolan.
166  
167 This program is free software; you can redistribute it and/or modify
168 it under the same terms as Perl itself. The full text of this license
169 can be found in the LICENSE file included with this module.
170  
171 =cut
172  
173 # Local Variables:
174 # mode: cperl
175 # cperl-indent-level: 4
176 # fill-column: 78
177 # indent-tabs-mode: nil
178 # c-indentation-style: bsd
179 # End:
180 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :
« blib/lib/Perl/Critic/PolicyFactory.pm ↑ Summary blib/lib/Perl/Critic/Statistics.pm »