Login
RT #75397: ProhibitBuiltinHomonyms should include the sub name in the
[gknop/Perl-Critic.git] / lib / Perl / Critic / Policy / Subroutines / ProhibitBuiltinHomonyms.pm
CommitLineData
6036a254 1##############################################################################
dff08b70
JRT
2# $URL$
3# $Date$
4# $Author$
5# $Revision$
6036a254 6##############################################################################
dff08b70 7
59b05e08
JRT
8package Perl::Critic::Policy::Subroutines::ProhibitBuiltinHomonyms;
9
df6dee2b 10use 5.006001;
59b05e08
JRT
11use strict;
12use warnings;
c680a9c9
ES
13use Readonly;
14
57a6408c
JRT
15use Perl::Critic::Utils qw{ :severities :data_conversion
16 :classification :characters };
17
59b05e08
JRT
18use base 'Perl::Critic::Policy';
19
73c61a84 20our $VERSION = '1.117';
59b05e08 21
6036a254 22#-----------------------------------------------------------------------------
dff08b70 23
c680a9c9
ES
24Readonly::Array my @ALLOW => qw( import AUTOLOAD DESTROY );
25Readonly::Hash my %ALLOW => hashify( @ALLOW );
fea2931c 26Readonly::Scalar my $DESC => q{Subroutine name is a homonym for builtin %s %s};
c680a9c9 27Readonly::Scalar my $EXPL => [177];
59b05e08 28
6036a254 29#-----------------------------------------------------------------------------
59b05e08 30
c680a9c9
ES
31sub supported_parameters { return () }
32sub default_severity { return $SEVERITY_HIGH }
33sub default_themes { return qw( core bugs pbp ) }
34sub applies_to { return 'PPI::Statement::Sub' }
dff08b70 35
6036a254 36#-----------------------------------------------------------------------------
bf159007 37
59b05e08 38sub violates {
e5f6c18d 39 my ( $self, $elem, undef ) = @_;
f31e304f 40 return if $elem->isa('PPI::Statement::Scheduled'); #e.g. BEGIN, INIT, END
c680a9c9 41 return if exists $ALLOW{ $elem->name() };
57a6408c
JRT
42
43 my $homonym_type = $EMPTY;
cf6f1e1f 44 if ( is_perl_builtin( $elem ) ) {
57a6408c
JRT
45 $homonym_type = 'function';
46 }
47 elsif ( is_perl_bareword( $elem ) ) {
48 $homonym_type = 'keyword';
59b05e08 49 }
57a6408c
JRT
50 else {
51 return; #ok!
52 }
53
fea2931c 54 my $desc = sprintf $DESC, $homonym_type, $elem->name();
57a6408c 55 return $self->violation($desc, $EXPL, $elem);
59b05e08
JRT
56}
57
581;
59
60__END__
61
6036a254 62#-----------------------------------------------------------------------------
dff08b70
JRT
63
64=pod
65
57a6408c
JRT
66=for stopwords perlfunc perlsyn
67
59b05e08
JRT
68=head1 NAME
69
30993879 70Perl::Critic::Policy::Subroutines::ProhibitBuiltinHomonyms - Don't declare your own C<open> function.
59b05e08 71
af93c316
ES
72=head1 AFFILIATION
73
11f53956
ES
74This Policy is part of the core L<Perl::Critic|Perl::Critic>
75distribution.
af93c316
ES
76
77
59b05e08
JRT
78=head1 DESCRIPTION
79
57a6408c
JRT
80Common sense dictates that you shouldn't declare subroutines with the same
81name as one of Perl's built-in functions or keywords. See
82L<perlfunc|perlfunc> for a list of built-in functions; see L<perlsyn|perlsyn>
83for keywords.
59b05e08 84
57a6408c
JRT
85 sub open {} #not ok
86 sub exit {} #not ok
87 sub print {} #not ok
88 sub foreach {} #not ok
89 sub if {} #not ok
59b05e08
JRT
90
91 #You get the idea...
92
f31e304f
JRT
93Exceptions are made for C<BEGIN>, C<END>, C<INIT> and C<CHECK> blocks,
94as well as C<AUTOLOAD>, C<DESTROY>, and C<import> subroutines.
95
0cb729f0
ES
96
97=head1 CONFIGURATION
98
49860482 99This Policy is not configurable except for the standard options.
0cb729f0
ES
100
101
f31e304f
JRT
102=head1 CAVEATS
103
104It is reasonable to declare an B<object> method with the same name as
105a Perl built-in function, since they are easily distinguished from
106each other. However, at this time, Perl::Critic cannot tell whether a
107subroutine is static or an object method.
5be567de 108
59b05e08
JRT
109=head1 AUTHOR
110
03887e5e 111Jeffrey Ryan Thalhammer <jeff@imaginative-software.com>
59b05e08 112
dff08b70
JRT
113=head1 COPYRIGHT
114
53b8903f 115Copyright (c) 2005-2011 Imaginative Software Systems. All rights reserved.
59b05e08
JRT
116
117This program is free software; you can redistribute it and/or modify
118it under the same terms as Perl itself. The full text of this license
119can be found in the LICENSE file included with this module.
dff08b70
JRT
120
121=cut
737d3b65
CD
122
123# Local Variables:
124# mode: cperl
125# cperl-indent-level: 4
126# fill-column: 78
127# indent-tabs-mode: nil
128# c-indentation-style: bsd
129# End:
96fed375 130# ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :