File Coverage

blib/lib/Perl/Critic/PolicyConfig.pm
Criterion Covered Total %
statement 79 79 100.0
branch 13 16 81.2
condition 9 12 75.0
subroutine 23 23 100.0
pod 12 13 92.3
total 136 143 95.1


line stmt bran cond sub pod time code
1             package Perl::Critic::PolicyConfig;
2              
3 40     40   1375 use 5.010001;
  40         166  
4 40     40   235 use strict;
  40         282  
  40         864  
5 40     40   192 use warnings;
  40         92  
  40         1176  
6              
7 40     40   1273 use Readonly;
  40         4110  
  40         3325  
8              
9             our $VERSION = '1.146';
10              
11 40     40   4123 use Perl::Critic::Exception::AggregateConfiguration;
  40         110  
  40         2093  
12 40     40   16649 use Perl::Critic::Exception::Configuration::Option::Policy::ParameterValue;
  40         114  
  40         2406  
13 40     40   15790 use Perl::Critic::Exception::Configuration::Option::Policy::ExtraParameter;
  40         125  
  40         2120  
14 40     40   329 use Perl::Critic::Utils qw< :booleans :characters severity_to_number >;
  40         105  
  40         1989  
15 40     40   23756 use Perl::Critic::Utils::Constants qw< :profile_strictness >;
  40         139  
  40         40210  
16              
17             #-----------------------------------------------------------------------------
18              
19             Readonly::Scalar my $NON_PUBLIC_DATA => '_non_public_data';
20             Readonly::Scalar my $NO_LIMIT => 'no_limit';
21              
22             #-----------------------------------------------------------------------------
23              
24             sub new {
25 30750     30750 0 69373 my ($class, $policy_short_name, $specification) = @_;
26              
27 30750 100       60510 my %self = $specification ? %{ $specification } : ();
  30749         86081  
28 30750         52111 my %non_public_data;
29              
30 30750         59960 $non_public_data{_policy_short_name} = $policy_short_name;
31             $non_public_data{_profile_strictness} =
32 30750         76763 $self{$NON_PUBLIC_DATA}{_profile_strictness};
33              
34 30750         59412 foreach my $standard_parameter (
35             qw< maximum_violations_per_document severity set_themes add_themes >
36             ) {
37 123000 100       242854 if ( exists $self{$standard_parameter} ) {
38             $non_public_data{"_$standard_parameter"} =
39 1307         4177 delete $self{$standard_parameter};
40             }
41             }
42              
43 30750         61936 $self{$NON_PUBLIC_DATA} = \%non_public_data;
44              
45              
46 30750         96678 return bless \%self, $class;
47             }
48              
49             #-----------------------------------------------------------------------------
50              
51             sub _get_non_public_data {
52 60504     60504   83669 my $self = shift;
53              
54 60504         137821 return $self->{$NON_PUBLIC_DATA};
55             }
56              
57             #-----------------------------------------------------------------------------
58              
59             sub get_policy_short_name {
60 4     4 1 1721 my $self = shift;
61              
62 4         13 return $self->_get_non_public_data()->{_policy_short_name};
63             }
64              
65             #-----------------------------------------------------------------------------
66              
67             sub get_set_themes {
68 15123     15123 1 25930 my ($self) = @_;
69              
70 15123         26030 return $self->_get_non_public_data()->{_set_themes};
71             }
72              
73             #-----------------------------------------------------------------------------
74              
75             sub get_add_themes {
76 15123     15123 1 25160 my ($self) = @_;
77              
78 15123         25690 return $self->_get_non_public_data()->{_add_themes};
79             }
80              
81             #-----------------------------------------------------------------------------
82              
83             sub get_severity {
84 15124     15124 1 26340 my ($self) = @_;
85              
86 15124         29055 return $self->_get_non_public_data()->{_severity};
87             }
88              
89             #-----------------------------------------------------------------------------
90              
91             sub is_maximum_violations_per_document_unlimited {
92 15122     15122 1 29199 my ($self) = @_;
93              
94 15122         32838 my $maximum_violations = $self->get_maximum_violations_per_document();
95 15122 100 66     51960 if (
      100        
96             not defined $maximum_violations
97             or $maximum_violations eq $EMPTY
98             or $maximum_violations =~ m<\A $NO_LIMIT \z>xmsio
99             ) {
100 15116         50838 return $TRUE;
101             }
102              
103 6         33 return $FALSE;
104             }
105              
106             #-----------------------------------------------------------------------------
107              
108             sub get_maximum_violations_per_document {
109 15130     15130 1 25935 my ($self) = @_;
110              
111 15130         35005 return $self->_get_non_public_data()->{_maximum_violations_per_document};
112             }
113              
114             #-----------------------------------------------------------------------------
115              
116             sub get {
117 11     11 1 2993 my ($self, $parameter) = @_;
118              
119 11 100       39 return if $parameter eq $NON_PUBLIC_DATA;
120              
121 9         43 return $self->{$parameter};
122             }
123              
124             #-----------------------------------------------------------------------------
125              
126             sub remove {
127 28901     28901 1 53444 my ($self, $parameter) = @_;
128              
129 28901 50       61478 return if $parameter eq $NON_PUBLIC_DATA;
130              
131 28901         47677 delete $self->{$parameter};
132              
133 28901         61971 return;
134             }
135              
136             #-----------------------------------------------------------------------------
137              
138             sub is_empty {
139 3     3 1 9 my ($self) = @_;
140              
141 3         7 return 1 >= keys %{$self};
  3         21  
142             }
143              
144             #-----------------------------------------------------------------------------
145              
146             sub get_parameter_names {
147 15419     15419 1 32144 my ($self) = @_;
148              
149 15419         23656 return grep { $_ ne $NON_PUBLIC_DATA } keys %{$self};
  15565         66263  
  15419         46934  
150             }
151              
152             #-----------------------------------------------------------------------------
153              
154             sub handle_extra_parameters {
155 15416     15416 1 34092 my ($self, $policy, $errors) = @_;
156              
157             my $profile_strictness = $self->{$NON_PUBLIC_DATA}{_profile_strictness}
158 15416   66     52999 // $PROFILE_STRICTNESS_DEFAULT;
159              
160 15416 50       36677 return if $profile_strictness eq $PROFILE_STRICTNESS_QUIET;
161              
162 15416 100       58049 my $parameter_errors = $profile_strictness eq $PROFILE_STRICTNESS_WARN ?
163             Perl::Critic::Exception::AggregateConfiguration->new() : $errors;
164              
165 15416         16177264 foreach my $offered_param ( $self->get_parameter_names() ) {
166 145         490 $parameter_errors->add_exception(
167             Perl::Critic::Exception::Configuration::Option::Policy::ExtraParameter->new(
168             policy => $policy->get_short_name(),
169             option_name => $offered_param,
170             source => undef,
171             )
172             );
173             }
174              
175 15416 50 66     71365 warn qq<$parameter_errors\n>
176             if ($profile_strictness eq $PROFILE_STRICTNESS_WARN
177             && $parameter_errors->has_exceptions());
178              
179 15416         496163 return;
180             }
181              
182             #-----------------------------------------------------------------------------
183              
184             sub set_profile_strictness {
185 15272     15272 1 31922 my ($self, $profile_strictness) = @_;
186              
187 15272         33501 $self->{$NON_PUBLIC_DATA}{_profile_strictness} = $profile_strictness;
188              
189 15272         31243 return;
190             }
191              
192             #-----------------------------------------------------------------------------
193              
194             1;
195              
196             __END__
197              
198             #-----------------------------------------------------------------------------
199              
200             =pod
201              
202             =for stopwords
203              
204             =head1 NAME
205              
206             Perl::Critic::PolicyConfig - Configuration data for a Policy.
207              
208              
209              
210             =head1 DESCRIPTION
211              
212             A container for the configuration of a Policy.
213              
214              
215             =head1 INTERFACE SUPPORT
216              
217             This is considered to be a non-public class. Its interface is subject
218             to change without notice.
219              
220              
221             =head1 METHODS
222              
223             =over
224              
225             =item C<get_policy_short_name()>
226              
227             The name of the policy this configuration is for. Primarily here for
228             the sake of debugging.
229              
230              
231             =item C< get_set_themes() >
232              
233             The value of C<set_themes> in the user's F<.perlcriticrc>.
234              
235              
236             =item C< get_add_themes() >
237              
238             The value of C<add_themes> in the user's F<.perlcriticrc>.
239              
240              
241             =item C< get_severity() >
242              
243             The value of C<severity> in the user's F<.perlcriticrc>.
244              
245              
246             =item C< is_maximum_violations_per_document_unlimited() >
247              
248             Answer whether the value of C<maximum_violations_per_document> should
249             be considered to be unlimited.
250              
251              
252             =item C< get_maximum_violations_per_document() >
253              
254             The value of C<maximum_violations_per_document> in the user's
255             F<.perlcriticrc>.
256              
257              
258             =item C< get($parameter) >
259              
260             Retrieve the value of the specified parameter in the user's
261             F<.perlcriticrc>.
262              
263              
264             =item C< remove($parameter) >
265              
266             Delete the value of the specified parameter.
267              
268              
269             =item C< is_empty() >
270              
271             Answer whether there is any non-standard configuration information
272             left.
273              
274              
275             =item C< get_parameter_names() >
276              
277             Retrieve the names of the parameters in this object.
278              
279              
280             =item C< set_profile_strictness($profile_strictness) >
281              
282             Sets the profile strictness associated with the configuration.
283              
284              
285             =item C< handle_extra_parameters($policy,$errors) >
286              
287             Deals with any extra parameters according to the profile_strictness
288             setting. To be called by Perl::Critic::Policy->new() once all valid
289             policies have been processed and removed from the configuration.
290              
291             If profile_strictness is $PROFILE_STRICTNESS_QUIET, extra policy
292             parameters are ignored.
293              
294             If profile_strictness is $PROFILE_STRICTNESS_WARN, extra policy
295             parameters generate a warning.
296              
297             If profile_strictness is $PROFILE_STRICTNESS_FATAL, extra policy
298             parameters generate a fatal error.
299              
300             If no profile_strictness was set, the behavior is that specified by
301             $PROFILE_STRICTNESS_DEFAULT.
302              
303              
304             =back
305              
306              
307             =head1 SEE ALSO
308              
309             L<Perl::Critic::DEVELOPER/"MAKING YOUR POLICY CONFIGURABLE">
310              
311              
312             =head1 AUTHOR
313              
314             Elliot Shank <perl@galumph.com>
315              
316              
317             =head1 COPYRIGHT
318              
319             Copyright (c) 2008-2011 Elliot Shank.
320              
321             This program is free software; you can redistribute it and/or modify
322             it under the same terms as Perl itself. The full text of this license
323             can be found in the LICENSE file included with this module.
324              
325             =cut
326              
327             # Local Variables:
328             # mode: cperl
329             # cperl-indent-level: 4
330             # fill-column: 78
331             # indent-tabs-mode: nil
332             # c-indentation-style: bsd
333             # End:
334             # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :