File Coverage

blib/lib/Perl/Critic/Policy/Subroutines/ProhibitBuiltinHomonyms.pm
Criterion Covered Total %
statement 32 32 100.0
branch 10 10 100.0
condition 3 3 100.0
subroutine 11 11 100.0
pod 4 5 80.0
total 60 61 98.3


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::Subroutines::ProhibitBuiltinHomonyms;
2              
3 40     40   27079 use 5.010001;
  40         166  
4 40     40   261 use strict;
  40         107  
  40         858  
5 40     40   226 use warnings;
  40         103  
  40         1100  
6 40     40   240 use Readonly;
  40         99  
  40         2481  
7              
8 40         2366 use Perl::Critic::Utils qw{ :severities :data_conversion
9 40     40   298 :classification :characters $EMPTY };
  40         107  
10              
11 40     40   22414 use parent 'Perl::Critic::Policy';
  40         138  
  40         269  
12              
13             our $VERSION = '1.146';
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 97     97 0 2060 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 100     100 1 437 sub default_severity { return $SEVERITY_HIGH }
35 92     92 1 393 sub default_themes { return qw( core bugs pbp certrule ) }
36 39     39 1 163 sub applies_to { return 'PPI::Statement::Sub' }
37              
38             #-----------------------------------------------------------------------------
39              
40             sub violates {
41 45     45 1 109 my ( $self, $elem, undef ) = @_;
42 45 100       216 return if $elem->isa('PPI::Statement::Scheduled'); #e.g. BEGIN, INIT, END
43 41 100 100     140 return if exists $ALLOW{ $elem->name() } and not defined $elem->type();
44 37 100       2671 return if exists $self->{_allow}{ $elem->name() };
45              
46 34         1731 my $homonym_type = $EMPTY;
47 34 100       118 if ( is_perl_builtin( $elem ) ) {
    100          
48 16         950 $homonym_type = 'function';
49             }
50             elsif ( is_perl_bareword( $elem ) ) {
51 10         653 $homonym_type = 'keyword';
52             }
53             else {
54 8         489 return; #ok!
55             }
56              
57 26         82 my $desc = sprintf $DESC, $homonym_type, $elem->name();
58 26         1445 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 :