Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'allow' parameter to Subroutines::ProhibitBuiltinHomonyms #932

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions lib/Perl/Critic/Policy/Subroutines/ProhibitBuiltinHomonyms.pm
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use warnings;
use Readonly;

use Perl::Critic::Utils qw{ :severities :data_conversion
:classification :characters };
:classification :characters $EMPTY };

use base 'Perl::Critic::Policy';

Expand All @@ -21,7 +21,16 @@ Readonly::Scalar my $EXPL => [177];

#-----------------------------------------------------------------------------

sub supported_parameters { return () }
sub supported_parameters {
return (
{
name => 'allow',
description => q<Subroutines matching the name regex to allow under this policy.>,
default_string => $EMPTY,
behavior => 'string list',
},
);
}
sub default_severity { return $SEVERITY_HIGH }
sub default_themes { return qw( core bugs pbp certrule ) }
sub applies_to { return 'PPI::Statement::Sub' }
Expand All @@ -32,6 +41,7 @@ sub violates {
my ( $self, $elem, undef ) = @_;
return if $elem->isa('PPI::Statement::Scheduled'); #e.g. BEGIN, INIT, END
return if exists $ALLOW{ $elem->name() };
return if exists $self->{_allow}{ $elem->name() };

my $homonym_type = $EMPTY;
if ( is_perl_builtin( $elem ) ) {
Expand Down Expand Up @@ -89,7 +99,14 @@ as well as C<AUTOLOAD>, C<DESTROY>, and C<import> subroutines.

=head1 CONFIGURATION

This Policy is not configurable except for the standard options.
You can configure additional builtin homonyms to accept by specifying them
in a space-delimited list to the C<allow> option:

[Subroutines::ProhibitUnusedPrivateSubroutines]
allow = default index

These are added to the default list of exemptions from this policy. So the
above allows C<< sub default {} >> and C<< sub index {} >>.


=head1 CAVEATS
Expand Down
12 changes: 12 additions & 0 deletions t/Subroutines/ProhibitBuiltinHomonyms.run
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ INIT { do_something(); }
CHECK { do_something(); }
END { do_something(); }

#-----------------------------------------------------------------------------

## name Allowed homonyms with 'allow' parameter
## failures 1
## parms { allow => 'default delete index' }
## cut

sub default { do_someting(); }
sub delete { do_someting(); }
sub index { do_someting(); }
sub open { do_something(); } # not allowed

#-----------------------------------------------------------------------------
# Local Variables:
# mode: cperl
Expand Down