File Coverage

blib/lib/Perl/Critic/Policy/logicLAB/RequirePackageNamePattern.pm
Criterion Covered Total %
statement 65 73 89.0
branch 15 22 68.1
condition 4 11 36.3
subroutine 15 15 100.0
pod 4 4 100.0
total 103 125 82.4


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::logicLAB::RequirePackageNamePattern;
2              
3 2     2   122620 use strict;
  2         3  
  2         66  
4 2     2   9 use warnings;
  2         3  
  2         58  
5 2     2   40 use 5.006;
  2         6  
6              
7 2     2   9 use base 'Perl::Critic::Policy';
  2         3  
  2         1458  
8 2     2   418070 use Perl::Critic::Utils qw{ $SEVERITY_MEDIUM :booleans};
  2         4  
  2         113  
9 2     2   173 use Carp qw(carp);
  2         4  
  2         82  
10 2     2   678 use Data::Dumper;
  2         6829  
  2         420  
11              
12             our $VERSION = '0.05';
13              
14 2     2   18 use constant supported_parameters => qw(names debug exempt_programs);
  2         5  
  2         365  
15 2     2   11 use constant default_severity => $SEVERITY_MEDIUM;
  2         3  
  2         132  
16 2     2   18 use constant default_themes => qw(logiclab);
  2         5  
  2         1078  
17              
18             sub prepare_to_scan_document {
19 13     13 1 54305 my ( $self, $document ) = @_;
20 13 50 33     84 if ( $self->{exempt_programs} && $document->is_program() ) {
21 0         0 return $FALSE;
22             }
23              
24 13         143 return $document->is_module();
25             }
26              
27             sub applies_to {
28             return (
29 13     13 1 230 qw(
30             PPI::Statement::Package
31             )
32             );
33             }
34              
35             sub violates {
36 13     13 1 244 my ( $self, $elem ) = @_;
37              
38 13 100       45 if ( not $self->{_names} ) {
39 1         1 return;
40             }
41              
42 12         57 my @children = $elem->children;
43              
44 12 50       95 if ( $children[0]->content eq 'package' ) {
45             #TODO we might have to look for words here instead of using an array index
46             #TODO and we should add exception in the case an actual package is not located
47 12         70 my $package = $children[2]->content;
48              
49 12 50       57 if ($self->{debug}) {
50 0         0 print STDERR "located package: $package\n";
51             }
52              
53 12         17 my $no_of_patterns = scalar @{$self->{_names}};
  12         68  
54 12         20 my $no_of_violations = 0;
55              
56 12         41 foreach my $name (@{$self->{_names}}) {
  12         29  
57             #TODO investigate wht this is a regular expression and so are
58             #actual evaluation in line 67, at least according to Perl::Critic
59             #[RegularExpressions::RequireExtendedFormatting]
60 14         144 my $regex = qr/$name/x;
61              
62 14 50       53 if ($self->{debug}) {
63 0         0 print STDERR "Regex: $regex\n";
64             }
65              
66 14 100       102 if ( $package !~ m/$regex/xs ) {
67 6 100       15 if ($no_of_patterns > 1) {
68 1         2 $no_of_violations++;
69              
70 1 50       10 if ($no_of_patterns == $no_of_violations) {
71 0         0 return $self->violation(
72             "Package name: $package is not complying with required standard",
73             "Use specified requirement for package naming for $package",
74             $elem
75             );
76             }
77              
78             } else {
79              
80 5         41 return $self->violation(
81             "Package name: $package is not complying with required standard",
82             "Use specified requirement for package naming for $package",
83             $elem
84             );
85             }
86             }
87             }
88              
89             } else {
90 0         0 carp 'Unable to locate package keyword';
91             }
92              
93 7         20 return;
94             }
95              
96             sub initialize_if_enabled {
97 13     13 1 6219620 my ( $self, $config ) = @_;
98              
99             #debug - order is significant, since we might need debugging
100 13   33     55 $self->{debug} = $config->get('debug') || $FALSE;
101              
102             #Names:
103             #fetching configured names
104 13         158 my $names = $config->get('names');
105              
106 13 50       74 if ($self->{debug}) {
107 0         0 warn "Requirements for package names are: $names\n";
108             }
109              
110             #parsing configured names, see also _parse_names
111 13 100       35 if ($names) {
112 12   50     34 $self->{_names} = $self->_parse_names($names) || q{};
113             }
114              
115             #exempt_programs
116 13   33     70 $self->{exempt_programs} = $config->get('exempt_programs') || $TRUE;
117              
118 13         99 return $TRUE;
119             }
120              
121             sub _parse_names {
122 12     12   23 my ( $self, $config_string ) = @_;
123              
124 12         57 my @names = split /\s*\|\|\s*/x, $config_string;
125              
126 12 50       36 if ($self->{debug}) {
127 0         0 print STDERR "our split line:\n";
128 0         0 print STDERR Dumper \@names;
129             }
130              
131 12         45 return \@names;
132             }
133              
134             1;
135              
136             __END__
137              
138             =pod
139              
140             =begin markdown
141              
142             [![CPAN version](https://badge.fury.io/pl/Perl-Critic-Policy-logicLAB-RequirePackageNamePattern.svg)](http://badge.fury.io/pl/Perl-Critic-Policy-logicLAB-RequirePackageNamePattern)
143             [![Build Status](https://travis-ci.org/jonasbn/pcplrpnp.svg?branch=master)](https://travis-ci.org/jonasbn/pcplrpnp)
144             [![Coverage Status](https://coveralls.io/repos/jonasbn/pcplrpnp/badge.png)](https://coveralls.io/r/jonasbn/pcplrpnp)
145              
146             =end markdown
147              
148             =head1 NAME
149              
150             Perl::Critic::Policy::logicLAB::RequirePackageNamePattern - simple policy for enforcing a package naming policy
151              
152             =head1 AFFILIATION
153              
154             This policy is a policy in the Perl::Critic::logicLAB distribution. The policy
155             is themed: logiclab.
156              
157             =head1 VERSION
158              
159             This documentation describes version 0.05.
160              
161             =head1 DESCRIPTION
162              
163             The policy can be used to enforced naming conventions for packages.
164              
165             =head1 SYNOPSIS
166              
167             Policy configuration:
168              
169             [logicLAB::RequirePackageNamePattern]
170             names = Acme
171              
172             Your package:
173              
174             package This::Is::A::Test;
175              
176             # code goes here
177              
178             1;
179              
180             Invocation of policy:
181              
182             $ perlcritic --single-policy logicLAB::RequirePackageNamePattern lib
183              
184             Explanation:
185              
186             Use specified requirement for package naming for This::Is::A::Test
187              
188             Description:
189              
190             Package name: This::Is::A::Test is not complying with required standard
191              
192             =head1 CONFIGURATION AND ENVIRONMENT
193              
194             This policy allow you to configure the contents of the shebang lines you
195             want to allow using L</names>.
196              
197             =head2 names
198              
199             C<names>, is the configuration parameter used to specify the patterns you
200             want to enforce.
201              
202             The different usage scenarios are documented below
203              
204             =head3 Toplevel namespace
205              
206             [logicLAB::RequirePackageNamePattern]
207             names = ^App::
208              
209             =head3 Subclass
210              
211             [logicLAB::RequirePackageNamePattern]
212             names = ::JONASBN$
213              
214             =head3 Postfix
215              
216             [logicLAB::RequirePackageNamePattern]
217             names = Utils$
218              
219             =head3 Prefix
220              
221             [logicLAB::RequirePackageNamePattern]
222             names = ^Acme
223              
224             =head3 Contains
225              
226             [logicLAB::RequirePackageNamePattern]
227             names = Tiny
228              
229             =head3 Or
230              
231             [logicLAB::RequirePackageNamePattern]
232             names = Acme || logicLAB
233              
234             =head2 debug
235              
236             Optionally and for development purposes I have added a debug flag. This can be set in
237             your L<Perl::Critic> configuration file as follows:
238              
239             [logicLAB::RequirePackageNamePattern]
240             debug = 1
241              
242             This enables more explicit output on what is going on during the actual processing of
243             the policy.
244              
245             =head1 DEPENDENCIES AND REQUIREMENTS
246              
247             =over
248              
249             =item * L<Module::Build>
250              
251             =item * L<Perl::Critic>
252              
253             =item * L<Perl::Critic::Utils>
254              
255             =item * L<Perl::Critic::Policy>
256              
257             =item * L<Test::More>
258              
259             =item * L<Test::Class>
260              
261             =item * L<Test::Perl::Critic>
262              
263             =item * L<Data::Dumper>
264              
265             =back
266              
267             =head1 INCOMPATIBILITIES
268              
269             This distribution has no known incompatibilities.
270              
271             =head1 BUGS AND LIMITATIONS
272              
273             There are no known bugs or limitations
274              
275             =head1 TEST AND QUALITY
276              
277             The following policies have been disabled for this distribution
278              
279             =over
280              
281             =item * L<Perl::Critic::Policy::ValuesAndExpressions::ProhibitConstantPragma>
282              
283             Constants are good, - see the link below.
284              
285             =over
286              
287             =item * L<https://logiclab.jira.com/wiki/display/OPEN/Perl-Critic-Policy-ValuesAndExpressions-ProhibitConstantPragma>
288              
289             =back
290              
291             =item * L<Perl::Critic::Policy::NamingConventions::Capitalization>
292              
293             =back
294              
295             See also F<t/perlcriticrc>
296              
297             =head2 TEST COVERAGE
298              
299             Coverage test executed the following way, the coverage report is based on the
300             version described in this documentation (see L</VERSION>).
301              
302             ./Build testcover
303              
304             ---------------------------- ------ ------ ------ ------ ------ ------ ------
305             File stmt bran cond sub pod time total
306             ---------------------------- ------ ------ ------ ------ ------ ------ ------
307             ...uirePackageNamePattern.pm 89.2 68.2 36.4 100.0 100.0 100.0 82.5
308             Total 89.2 68.2 36.4 100.0 100.0 100.0 82.5
309             ---------------------------- ------ ------ ------ ------ ------ ------ ------
310              
311             =head1 SEE ALSO
312              
313             =over
314              
315             =item * L<Perl::Critic>
316              
317             =item * L<perlmod manpage|http://perldoc.perl.org/perlmod.html>
318              
319             =item * L<http://logiclab.jira.com/wiki/display/PCPLRPNP/Home>
320              
321             =item * L<http://logiclab.jira.com/wiki/display/PCLL/Home>
322              
323             =back
324              
325             =head1 AUTHOR
326              
327             =over
328              
329             =item * Jonas B. Nielsen, jonasbn C<< <jonasbn@cpan.org> >>
330              
331             =back
332              
333             =head1 ACKNOWLEDGEMENT
334              
335             =over
336              
337             =item * Jeffrey Ryan Thalhammer (THALJEF) and the Perl::Critic contributors for
338             Perl::Critic
339              
340             =back
341              
342             =head1 LICENSE AND COPYRIGHT
343              
344             Copyright (c) 2013-2015 Jonas B. Nielsen, jonasbn. All rights reserved.
345              
346             Perl::Critic::Policy::logicLAB::RequirePackageNamePattern; is released under
347             the Artistic License 2.0
348              
349             The distribution is licensed under the Artistic License 2.0, as specified by
350             the license file included in this distribution.
351              
352             =cut