1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
|
package Perl::Critic::Policy::TooMuchCode::ProhibitExcessiveColons;
use strict;
use warnings;
use Perl::Critic::Utils;
use parent 'Perl::Critic::Policy';
our $VERSION = '0.01';
sub default_themes { return qw( maintenance ) }
sub applies_to { return 'PPI::Statement::Include' }
#---------------------------------------------------------------------------
sub violates {
my ( $self, $elem, $doc ) = @_;
my @violations = $self->gather_violations_generic($elem, $doc);
return @violations;
}
sub gather_violations_generic {
my ( $self, $elem, undef ) = @_;
# PPI::Statement::Include doesn't handle this weird case of `use Data::::Dumper`.
# The `PPI::Statement::Include#module` method does not catch 'Data::::Dumper' as the
# module name, but `Data::` instead.
# So we are just use strings here.
return unless index("$elem", "::::") > 0;
return $self->violation(
"Too many colons in the module name.",
"The statement <$elem> contains so many colons to separate namespaces, while 2 colons is usually enough.",
$elem,
);
}
1;
=encoding utf-8
=head1 NAME
TooMuchCode::ProhibitExcessiveColons - Finds '::::::::' in module names.
=head1 DESCRIPTION
In an include statement, it is possible to have a lot of colons:
use Data::::Dumper;
... or
use Data::::::::Dumper;
As long as the number of colons is a multiple of two.
However, just because it is doable, does not mean it is sensible.
C<use Data::::::Dumper> will make perl look for C<lib/Data///Dumper.pm>,
which is usually the same as C<lib/Data/Dumper.pm>.
This policy restrict you to use only two colons to delimit one layer of namespace.
=cut
|