File Coverage

blib/lib/Perl/Critic/PolicyParameter/Behavior/Enumeration.pm
Criterion Covered Total %
statement 57 57 100.0
branch 15 16 93.7
condition 12 12 100.0
subroutine 10 10 100.0
pod 2 2 100.0
total 96 97 98.9


line stmt bran cond sub pod time code
1             package Perl::Critic::PolicyParameter::Behavior::Enumeration;
2              
3 40     40   710 use 5.010001;
  40         157  
4 40     40   272 use strict;
  40         106  
  40         806  
5 40     40   208 use warnings;
  40         91  
  40         1235  
6              
7             use Perl::Critic::Exception::Fatal::PolicyDefinition
8 40     40   238 qw{ &throw_policy_definition };
  40         164  
  40         4107  
9 40     40   344 use Perl::Critic::Utils qw{ :characters &words_from_string &hashify };
  40         247  
  40         2033  
10              
11 40     40   9676 use parent qw{ Perl::Critic::PolicyParameter::Behavior };
  40         104  
  40         479  
12              
13             our $VERSION = '1.146';
14              
15             #-----------------------------------------------------------------------------
16              
17             sub initialize_parameter {
18 947     947 1 2761 my ($self, $parameter, $specification) = @_;
19              
20             my $valid_values = $specification->{enumeration_values}
21 947 100       3321 or throw_policy_definition
22             'No enumeration_values given for '
23             . $parameter->get_name()
24             . $PERIOD;
25 946 100       3369 ref $valid_values eq 'ARRAY'
26             or throw_policy_definition
27             'The value given for enumeration_values for '
28             . $parameter->get_name()
29             . ' is not an array reference.';
30 945 100       1954 scalar @{$valid_values} > 1
  945         3255  
31             or throw_policy_definition
32             'There were not at least two valid values given for'
33             . ' enumeration_values for '
34             . $parameter->get_name()
35             . $PERIOD;
36              
37             # Unfortunately, this has to be a reference, rather than a regular hash,
38             # due to a problem in Devel::Cycle
39             # (http://rt.cpan.org/Ticket/Display.html?id=25360) which causes
40             # t/92_memory_leaks.t to fall over.
41 943         2035 my $value_lookup = { hashify( @{$valid_values} ) };
  943         3488  
42 943         3985 $parameter->_get_behavior_values()->{enumeration_values} = $value_lookup;
43              
44             my $allow_multiple_values =
45 943         2518 $specification->{enumeration_allow_multiple_values};
46              
47 943 100       2940 if ($allow_multiple_values) {
48             $parameter->_set_parser(
49             sub {
50             # Normally bad thing, obscuring a variable in a outer scope
51             # with a variable with the same name is being done here in
52             # order to remain consistent with the parser function interface.
53 617     617   2150 my ($policy, $parameter, $config_string) = @_; ## no critic(Variables::ProhibitReusedNames)
54              
55 617         1319 my @potential_values;
56 617   100     2717 my $value_string = $config_string // $parameter->get_default_string();
57              
58 617 100       1925 if ( defined $value_string ) {
59 616         2228 @potential_values = words_from_string($value_string);
60              
61             my @bad_values =
62 616         1586 grep { not exists $value_lookup->{$_} } @potential_values;
  645         2010  
63 616 100       2007 if (@bad_values) {
64             $policy->throw_parameter_value_exception(
65             $parameter->get_name(),
66             $value_string,
67             undef,
68             q{contains invalid values: }
69             . join (q{, }, @bad_values)
70             . q{. Allowed values are: }
71 1         6 . join (q{, }, sort keys %{$value_lookup})
  1         11  
72             . qq{.\n},
73             );
74             }
75             }
76              
77 616         1989 my %actual_values = hashify(@potential_values);
78              
79 616         3211 $policy->__set_parameter_value($parameter, \%actual_values);
80              
81 616         1827 return;
82             }
83 617         4649 );
84             } else {
85             $parameter->_set_parser(
86             sub {
87             # Normally bad thing, obscuring a variable in a outer scope
88             # with a variable with the same name is being done here in
89             # order to remain consistent with the parser function interface.
90 326     326   1261 my ($policy, $parameter, $config_string) = @_; ## no critic(Variables::ProhibitReusedNames)
91              
92 326   100     1641 my $value_string = $config_string // $parameter->get_default_string();
93              
94 326 100 100     2665 if (
      100        
95             defined $value_string
96             and $EMPTY ne $value_string
97             and not defined $value_lookup->{$value_string}
98             ) {
99             $policy->throw_parameter_value_exception(
100             $parameter->get_name(),
101             $value_string,
102             undef,
103             q{is not one of the allowed values: }
104 2         10 . join (q{, }, sort keys %{$value_lookup})
  2         50  
105             . qq{.\n},
106             );
107             }
108              
109 324         1408 $policy->__set_parameter_value($parameter, $value_string);
110              
111 324         813 return;
112             }
113 326         2382 );
114             }
115              
116 943         2698 return;
117             }
118              
119             #-----------------------------------------------------------------------------
120              
121             sub generate_parameter_description {
122 72     72 1 173 my ($self, $parameter) = @_;
123              
124 72         180 my $description = $parameter->_get_description_with_trailing_period();
125 72 50       205 if ( $description ) {
126 72         162 $description .= qq{\n};
127             }
128              
129 72         128 my %values = %{$parameter->_get_behavior_values()->{enumeration_values}};
  72         175  
130             return
131 72         725 $description
132             . 'Valid values: '
133             . join (', ', sort keys %values)
134             . $PERIOD;
135             }
136              
137             #-----------------------------------------------------------------------------
138              
139             1;
140              
141             __END__
142              
143             #-----------------------------------------------------------------------------
144              
145             =pod
146              
147             =for stopwords
148              
149             =head1 NAME
150              
151             Perl::Critic::PolicyParameter::Behavior::Enumeration - Actions appropriate for an enumerated value.
152              
153              
154             =head1 DESCRIPTION
155              
156             Provides a standard set of functionality for an enumerated
157             L<Perl::Critic::PolicyParameter|Perl::Critic::PolicyParameter> so that
158             the developer of a policy does not have to provide it her/himself.
159              
160             NOTE: Do not instantiate this class. Use the singleton instance held
161             onto by
162             L<Perl::Critic::PolicyParameter|Perl::Critic::PolicyParameter>.
163              
164              
165             =head1 INTERFACE SUPPORT
166              
167             This is considered to be a non-public class. Its interface is subject
168             to change without notice.
169              
170              
171             =head1 METHODS
172              
173             =over
174              
175             =item C<initialize_parameter( $parameter, $specification )>
176              
177             Plug in the functionality this behavior provides into the parameter,
178             based upon the configuration provided by the specification.
179              
180             This behavior looks for two configuration items:
181              
182             =over
183              
184             =item enumeration_values
185              
186             Mandatory. The set of valid values for the parameter, as an array
187             reference.
188              
189              
190             =item enumeration_allow_multiple_values
191              
192             Optional, defaults to false. Should the parameter support a single
193             value or accept multiple?
194              
195              
196             =back
197              
198              
199             =item C<generate_parameter_description( $parameter )>
200              
201             Create a description of the parameter, based upon the description on
202             the parameter itself, but enhancing it with information from this
203             behavior.
204              
205             In this specific case, the universe of values is added at the end.
206              
207              
208             =back
209              
210              
211             =head1 AUTHOR
212              
213             Elliot Shank <perl@galumph.com>
214              
215              
216             =head1 COPYRIGHT
217              
218             Copyright (c) 2006-2011 Elliot Shank.
219              
220             This program is free software; you can redistribute it and/or modify
221             it under the same terms as Perl itself. The full text of this license
222             can be found in the LICENSE file included with this module.
223              
224             =cut
225              
226             # Local Variables:
227             # mode: cperl
228             # cperl-indent-level: 4
229             # fill-column: 78
230             # indent-tabs-mode: nil
231             # c-indentation-style: bsd
232             # End:
233             # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :