1 ##############################################################################
2 # $URL: http://perlcritic.tigris.org/svn/perlcritic/trunk/Perl-Critic/lib/Perl/Critic/PolicyConfig.pm $
3 # $Date: 2008-07-03 10:19:10 -0500 (Thu, 03 Jul 2008) $
6 ##############################################################################
8 package Perl::Critic::PolicyConfig;
16 our $VERSION = '1.088';
18 use Perl::Critic::Exception::AggregateConfiguration;
19 use Perl::Critic::Exception::Configuration::Option::Policy::ParameterValue;
20 use Perl::Critic::Utils qw< :booleans :characters severity_to_number >;
22 #-----------------------------------------------------------------------------
24 Readonly::Scalar my $NON_PUBLIC_DATA => '_non_public_data';
25 Readonly::Scalar my $NO_LIMIT => 'no_limit';
27 #-----------------------------------------------------------------------------
30 my ($class, $policy_short_name, $specification) = @_;
32 my %self = $specification ? %{ $specification } : ();
35 $non_public_data{_policy_short_name} = $policy_short_name;
37 foreach my $standard_parameter (
38 qw< maximum_violations_per_document severity set_themes add_themes >
40 if ( exists $self{$standard_parameter} ) {
41 $non_public_data{"_$standard_parameter"} =
42 delete $self{$standard_parameter};
46 $self{$NON_PUBLIC_DATA} = \%non_public_data;
49 return bless \%self, $class;
52 #-----------------------------------------------------------------------------
54 sub _validate_maximum_violations_per_document {
55 my ($self, $errors) = @_;
57 my $user_maximum_violations =
58 $self->get_maximum_violations_per_document();
60 if ( defined $user_maximum_violations ) {
62 $user_maximum_violations =~ m/$NO_LIMIT/xmsio
63 or $user_maximum_violations eq $EMPTY
65 $user_maximum_violations = undef;
67 elsif ( not is_integer($user_maximum_violations) ) {
68 $errors->add_exception(
69 new_parameter_value_exception(
70 'maximum_violations_per_document',
71 $user_maximum_violations,
73 "does not look like an integer.\n"
79 elsif ( $user_maximum_violations < 0 ) {
80 $errors->add_exception(
81 new_parameter_value_exception(
82 'maximum_violations_per_document',
83 $user_maximum_violations,
85 "is not greater than or equal to zero.\n"
92 $self->set_maximum_violations_per_document(
93 $user_maximum_violations
100 #-----------------------------------------------------------------------------
102 sub _get_non_public_data {
105 return $self->{$NON_PUBLIC_DATA};
108 #-----------------------------------------------------------------------------
110 sub get_policy_short_name {
113 return $self->_get_non_public_data()->{_policy_short_name};
116 #-----------------------------------------------------------------------------
121 return $self->_get_non_public_data()->{_set_themes};
124 #-----------------------------------------------------------------------------
129 return $self->_get_non_public_data()->{_add_themes};
132 #-----------------------------------------------------------------------------
137 return $self->_get_non_public_data()->{_severity};
140 #-----------------------------------------------------------------------------
142 sub is_maximum_violations_per_document_unlimited {
145 my $maximum_violations = $self->get_maximum_violations_per_document();
147 not defined $maximum_violations
148 or $maximum_violations eq $EMPTY
149 or $maximum_violations =~ m<\A $NO_LIMIT \z>xmsio
157 #-----------------------------------------------------------------------------
159 sub get_maximum_violations_per_document {
162 return $self->_get_non_public_data()->{_maximum_violations_per_document};
165 #-----------------------------------------------------------------------------
168 my ($self, $parameter) = @_;
170 return if $parameter eq $NON_PUBLIC_DATA;
172 return $self->{$parameter};
175 #-----------------------------------------------------------------------------
178 my ($self, $parameter) = @_;
180 return if $parameter eq $NON_PUBLIC_DATA;
182 delete $self->{$parameter};
187 #-----------------------------------------------------------------------------
192 return 1 >= keys %{$self};
195 #-----------------------------------------------------------------------------
197 sub get_parameter_names {
200 return grep { $_ ne $NON_PUBLIC_DATA } keys %{$self};
203 #-----------------------------------------------------------------------------
209 #-----------------------------------------------------------------------------
217 Perl::Critic::PolicyConfig - Configuration data for a Policy.
222 A container for the configuration of a Policy.
229 =item C<get_policy_short_name()>
231 The name of the policy this configuration is for. Primarily here for
232 the sake of debugging.
235 =item C< get_set_themes() >
237 The value of C<set_themes> in the user's F<.perlcriticrc>.
240 =item C< get_add_themes() >
242 The value of C<add_themes> in the user's F<.perlcriticrc>.
245 =item C< get_severity() >
247 The value of C<severity> in the user's F<.perlcriticrc>.
250 =item C< is_maximum_violations_per_document_unlimited() >
252 Answer whether the value of C<maximum_violations_per_document> should
253 be considered to be unlimited.
256 =item C< get_maximum_violations_per_document() >
258 The value of C<maximum_violations_per_document> in the user's
262 =item C< get($parameter) >
264 Retrieve the value of the specified parameter in the user's
268 =item C< remove($parameter) >
270 Delete the value of the specified parameter.
273 =item C< is_empty() >
275 Answer whether there is any non-standard configuration information
279 =item C< get_parameter_names() >
281 Retrieve the names of the parameters in this object.
289 L<Perl::Critic::DEVELOPER/"MAKING YOUR POLICY CONFIGURABLE">
294 Elliot Shank <perl@galumph.com>
299 Copyright (c) 2008 Elliot Shank. All rights reserved.
301 This program is free software; you can redistribute it and/or modify
302 it under the same terms as Perl itself. The full text of this license
303 can be found in the LICENSE file included with this module.
309 # cperl-indent-level: 4
311 # indent-tabs-mode: nil
312 # c-indentation-style: bsd
314 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :