Skip to content

Commit

Permalink
ProhibitBuiltinHomonyms adds an "allows" parm
Browse files Browse the repository at this point in the history
  • Loading branch information
petdance committed Dec 12, 2022
1 parent c213e19 commit 795a627
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
4 changes: 4 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ NEXT
Added a Dockerfile in the extras/ directory for those who want to run P::C
in a container. Thanks, Isaac Gittins. (GH #832)

Subroutines::ProhibitBuiltinHomonyms now can take an "allows" parameter to
specify subroutines that won't violate the policy. Thanks, UTAGAWA Kiki.
(GH #14, #932)

[Bug Fixes]
Fixed some problems with how Perl::Critic determined scope. Thanks, Tom
Wyant. (GH #793)
Expand Down
25 changes: 21 additions & 4 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 parent '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() } and not defined $elem->type();
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 All @@ -105,7 +122,7 @@ Jeffrey Ryan Thalhammer <jeff@imaginative-software.com>
=head1 COPYRIGHT
Copyright (c) 2005-2011 Imaginative Software Systems. All rights reserved.
Copyright (c) 2005-2022 Imaginative Software Systems. All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the same terms as Perl itself. The full text of this license
Expand Down
12 changes: 12 additions & 0 deletions t/Subroutines/ProhibitBuiltinHomonyms.run
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,18 @@ my sub INIT { do_something(); }
my sub CHECK { do_something(); }
my sub END { do_something(); }

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

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

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

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

0 comments on commit 795a627

Please sign in to comment.