blib/lib/Perl/Critic/Policy/Subroutines/ProhibitBuiltinHomonyms.pm

filter: Statementstmt 97.3% 37 / 38 Branchbran 60.0% 6 / 10 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 11 / 11 Podpod 80.0% 4 / 5 totaltotal 85.5% 59 / 69 CC 7 SCAR 13.8
CC
7
Cov
86%
violates
8.2
BEGIN
1.0
BEGIN
1.0
CRAP 4.0
« blib/lib/Perl/Critic/Policy/Variables/ProhibitMatchVars.pm ↑ Summary blib/lib/Perl/Critic/Policy/InputOutput/ProhibitOneArgSelect.pm »
1 package Perl::Critic::Policy::Subroutines::ProhibitBuiltinHomonyms;
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
9 40 ▶ :classification :characters $EMPTY };
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 40 ▶use parent 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 40 our $VERSION = '1.150';
14  
15 #-----------------------------------------------------------------------------
16  
17 40 Readonly::Array my @ALLOW => qw( import unimport AUTOLOAD DESTROY );
18 40 Readonly::Hash my %ALLOW => hashify( @ALLOW );
19 40 Readonly::Scalar my $DESC => q{Subroutine name is a homonym for builtin %s %s};
20 40 Readonly::Scalar my $EXPL => [177];
21  
22 #-----------------------------------------------------------------------------
23  
24 sub supported_parameters {
25 return (
26 {
27 91 ▶ name => 'allow',
Subroutine supported_parameters: called CC=1 SCAR=0.0
Pod undocumented
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 78 ▶sub default_severity { return $SEVERITY_HIGH }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
35 92 ▶sub default_themes { return qw( core bugs pbp certrule ) }
Subroutine default_themes: called CC=1 SCAR=0.0
Pod documented
36 33 ▶sub applies_to { return 'PPI::Statement::Sub' }
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
37  
38 #-----------------------------------------------------------------------------
39  
40 sub violates {
41 5 ▶ my ( $self, $elem, undef ) = @_;
Subroutine violates: called CC=7 SCAR=21.1
Pod documented
42 5 ▶ return if $elem->isa('PPI::Statement::Scheduled'); #e.g. BEGIN, INIT, END
Branch: 1 branch
BranchTrueFalseTotal
if $elem->isa("PPI::Statement::Scheduled") 0 5 5
43 5 ▶ return if exists $ALLOW{ $elem->name() } and not defined $elem->type();
Branch: 1 branch
BranchTrueFalseTotal
if exists $ALLOW{$elem->name} and not defined $elem->type 0 5 5
Condition33%1/3
exists $ALLOW{$elem->name} and not defined $elem->type
!ll&&!rl&&r
500
MC/DC0%0/2
exists $ALLOW{$elem->name} and not defined $elem->type
exists $ALLOW{$elem->name} not defined $elem->type
Truth table33%1/3
A and not(B)
A= exists $ALLOW{$elem->name}B= not defined $elem->type
ABresult
0X0
100
111
44 5 ▶ return if exists $self->{_allow}{ $elem->name() };
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"_allow"}{$elem->name} 0 5 5
45  
46 5 my $homonym_type = $EMPTY;
47 5 ▶ if ( is_perl_builtin( $elem ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (is_perl_builtin($elem)) { } 3 2 5
elsif (is_perl_bareword($elem)) { } 0 2 2
48 3 $homonym_type = 'function';
49 }
50 elsif ( is_perl_bareword( $elem ) ) {
51 0 $homonym_type = 'keyword';
52 }
53 else {
54 2 return; #ok!
55 }
56  
57 3 my $desc = sprintf $DESC, $homonym_type, $elem->name();
58 3 return $self->violation($desc, $EXPL, $elem);
59 }
60  
61 40 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 :
« blib/lib/Perl/Critic/Policy/Variables/ProhibitMatchVars.pm ↑ Summary blib/lib/Perl/Critic/Policy/InputOutput/ProhibitOneArgSelect.pm »