File Coverage

blib/lib/Perl/Critic/Policy/Subroutines/ProhibitBuiltinHomonyms.pm
Criterion Covered Total %
statement 31 32 96.8
branch 6 10 60.0
condition 1 3 33.3
subroutine 11 11 100.0
pod 4 5 80.0
total 53 61 86.8


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::Subroutines::ProhibitBuiltinHomonyms;
2              
3 40     40   27291 use 5.010001;
  40         174  
4 40     40   272 use strict;
  40         92  
  40         825  
5 40     40   245 use warnings;
  40         103  
  40         1007  
6 40     40   230 use Readonly;
  40         164  
  40         2274  
7              
8 40         2330 use Perl::Critic::Utils qw{ :severities :data_conversion
9 40     40   260 :classification :characters $EMPTY };
  40         92  
10              
11 40     40   22040 use parent 'Perl::Critic::Policy';
  40         111  
  40         300  
12              
13             our $VERSION = '1.150';
14              
15             #-----------------------------------------------------------------------------
16              
17             Readonly::Array my @ALLOW => qw( import unimport AUTOLOAD DESTROY );
18             Readonly::Hash my %ALLOW => hashify( @ALLOW );
19             Readonly::Scalar my $DESC => q{Subroutine name is a homonym for builtin %s %s};
20             Readonly::Scalar my $EXPL => [177];
21              
22             #-----------------------------------------------------------------------------
23              
24             sub supported_parameters {
25             return (
26             {
27 90     90 0 2022 name => 'allow',
28             description => q<Subroutines matching the name regex to allow under this policy.>,
29             default_string => $EMPTY,
30             behavior => 'string list',
31             },
32             );
33             }
34 77     77 1 327 sub default_severity { return $SEVERITY_HIGH }
35 92     92 1 373 sub default_themes { return qw( core bugs pbp certrule ) }
36 32     32 1 110 sub applies_to { return 'PPI::Statement::Sub' }
37              
38             #-----------------------------------------------------------------------------
39              
40             sub violates {
41 5     5 1 23 my ( $self, $elem, undef ) = @_;
42 5 50       36 return if $elem->isa('PPI::Statement::Scheduled'); #e.g. BEGIN, INIT, END
43 5 50 33     22 return if exists $ALLOW{ $elem->name() } and not defined $elem->type();
44 5 50       332 return if exists $self->{_allow}{ $elem->name() };
45              
46 5         234 my $homonym_type = $EMPTY;
47 5 100       19 if ( is_perl_builtin( $elem ) ) {
    50          
48 3         146 $homonym_type = 'function';
49             }
50             elsif ( is_perl_bareword( $elem ) ) {
51 0         0 $homonym_type = 'keyword';
52             }
53             else {
54 2         131 return; #ok!
55             }
56              
57 3         12 my $desc = sprintf $DESC, $homonym_type, $elem->name();
58 3         157 return $self->violation($desc, $EXPL, $elem);
59             }
60              
61             1;
62              
63             __END__
64              
65             #-----------------------------------------------------------------------------
66              
67             =pod
68              
69             =for stopwords perlfunc perlsyn
70              
71             =head1 NAME
72              
73             Perl::Critic::Policy::Subroutines::ProhibitBuiltinHomonyms - Don't declare your own C<open> function.
74              
75             =head1 AFFILIATION
76              
77             This Policy is part of the core L<Perl::Critic|Perl::Critic>
78             distribution.
79              
80              
81             =head1 DESCRIPTION
82              
83             Common sense dictates that you shouldn't declare subroutines with the same
84             name as one of Perl's built-in functions or keywords. See
85             L<perlfunc|perlfunc> for a list of built-in functions; see L<perlsyn|perlsyn>
86             for keywords.
87              
88             sub open {} #not ok
89             sub exit {} #not ok
90             sub print {} #not ok
91             sub foreach {} #not ok
92             sub if {} #not ok
93              
94             #You get the idea...
95              
96             Exceptions are made for C<BEGIN>, C<END>, C<INIT> and C<CHECK> blocks,
97             as well as C<AUTOLOAD>, C<DESTROY>, and C<import> subroutines.
98              
99              
100             =head1 CONFIGURATION
101              
102             You can configure additional builtin homonyms to accept by specifying them
103             in a space-delimited list to the C<allow> option:
104              
105             [Subroutines::ProhibitUnusedPrivateSubroutines]
106             allow = default index
107              
108             These are added to the default list of exemptions from this policy. So the
109             above allows C<< sub default {} >> and C<< sub index {} >>.
110              
111              
112             =head1 CAVEATS
113              
114             It is reasonable to declare an B<object> method with the same name as
115             a Perl built-in function, since they are easily distinguished from
116             each other. However, at this time, Perl::Critic cannot tell whether a
117             subroutine is static or an object method.
118              
119             =head1 AUTHOR
120              
121             Jeffrey Ryan Thalhammer <jeff@imaginative-software.com>
122              
123             =head1 COPYRIGHT
124              
125             Copyright (c) 2005-2022 Imaginative Software Systems. All rights reserved.
126              
127             This program is free software; you can redistribute it and/or modify
128             it under the same terms as Perl itself. The full text of this license
129             can be found in the LICENSE file included with this module.
130              
131             =cut
132              
133             # Local Variables:
134             # mode: cperl
135             # cperl-indent-level: 4
136             # fill-column: 78
137             # indent-tabs-mode: nil
138             # c-indentation-style: bsd
139             # End:
140             # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :