File Coverage

blib/lib/Perl/Critic/Policy/Reneeb/Capitalization.pm
Criterion Covered Total %
statement 33 33 100.0
branch 6 6 100.0
condition n/a
subroutine 6 6 100.0
pod 2 2 100.0
total 47 47 100.0


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::Reneeb::Capitalization;
2              
3             # ABSTRACT: NamingConventions::Capitalization plus the ability to exempt "Full qualified package names"
4              
5 5     5   3321 use strict;
  5         13  
  5         142  
6 5     5   23 use warnings;
  5         11  
  5         127  
7              
8 5     5   24 use base 'Perl::Critic::Policy::NamingConventions::Capitalization';
  5         11  
  5         3053  
9              
10             our $VERSION = '2.04';
11              
12             sub supported_parameters {
13 15     15 1 131821 my ($self) = @_;
14              
15 15         127 my @params = $self->SUPER::supported_parameters();
16              
17 15         785 push @params, {
18             name => 'full_qualified_package_exemptions',
19             description => 'Package names that are exempt from capitalization rules. The values here are regexes that will be surrounded by \A and \z.',
20             default_string => 'main',
21             behavior => 'string list',
22             };
23              
24 15         77 return @params;
25             }
26              
27             sub initialize_if_enabled {
28 4     4 1 22428 my ($self, $config) = @_;
29              
30 4         31 my $return = $self->SUPER::initialize_if_enabled( $config );
31              
32 4         8273 my $option = $self->{_full_qualified_package_exemptions};
33              
34 4         20 my $configuration_exceptions =
35             Perl::Critic::Exception::AggregateConfiguration->new();
36              
37 4         2830 for my $pattern ( sort keys %{$option} ) {
  4         26  
38 4         10 my $regex;
39 4         166 eval { $regex = qr{ \A $pattern \z }xms; }
40 4 100       12 or do {
41 1         95 $configuration_exceptions->add_exception(
42             Perl::Critic::Exception::Configuration::Option::Policy::ParameterValue->new(
43             policy => $self,
44             option_name => '_full_qualified_package_exemptions',
45             option_value => $pattern,
46             message_suffix =>
47             "is not a valid regular expression: $@",
48             )
49             );
50             };
51             }
52              
53 4 100       1416 if ( $configuration_exceptions->has_exceptions ) {
54 1         31 $configuration_exceptions->throw;
55             }
56              
57 3         140 return $return;
58             }
59              
60             sub _package_capitalization {
61 21     21   54304 my ($self, $elem) = @_;
62            
63 21         72 my $namespace = $elem->namespace();
64 21         559 my $option = $self->{_full_qualified_package_exemptions};
65              
66 21         41 for my $pattern ( sort keys %{$option} ) {
  21         71  
67 21 100       217 return if $namespace =~ m{\A $pattern \z}xms;
68             }
69            
70 16         76 return $self->SUPER::_package_capitalization( $elem );
71             }
72              
73             1;
74              
75             __END__
76              
77             =pod
78              
79             =encoding UTF-8
80              
81             =head1 NAME
82              
83             Perl::Critic::Policy::Reneeb::Capitalization - NamingConventions::Capitalization plus the ability to exempt "Full qualified package names"
84              
85             =head1 VERSION
86              
87             version 2.04
88              
89             =head1 METHODS
90              
91             =head2 supported_parameters
92              
93             Same parameters as for L<Perl::Critic::Policy::NamingConventions::Capitalization> plus
94             C<full_qualified_package_exemptions>.
95              
96             =head2 initialize_if_enabled
97              
98             Checks the parameters
99              
100             =head1 AUTHOR
101              
102             Renee Baecker <reneeb@cpan.org>
103              
104             =head1 COPYRIGHT AND LICENSE
105              
106             This software is Copyright (c) 2015 by Renee Baecker.
107              
108             This is free software, licensed under:
109              
110             The Artistic License 2.0 (GPL Compatible)
111              
112             =cut