File Coverage

blib/lib/Perl/Critic/Policy/logicLAB/RequirePackageNamePattern.pm
Criterion Covered Total %
statement 66 74 89.1
branch 15 22 68.1
condition 4 11 36.3
subroutine 15 15 100.0
pod 4 4 100.0
total 104 126 82.5


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