File Coverage

blib/lib/Perl/Critic/Policy/TooMuchCode/ProhibitExcessiveColons.pm
Criterion Covered Total %
statement 19 20 95.0
branch 2 2 100.0
condition n/a
subroutine 7 8 87.5
pod 3 4 75.0
total 31 34 91.1


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