blib/lib/Perl/Critic/PolicyParameter/Behavior/Enumeration.pm

filter: Statementstmt 100% 59 / 59 Branchbran 93.7% 15 / 16 Conditioncond 100% 12 / 12 MC/DCmcdc 100% 7 / 7 Subroutinesub 100% 10 / 10 Podpod 100% 2 / 2 totaltotal 99.0% 105 / 106 CC 13 SCAR 12.4
CC
13
Cov
99%
initialize_parameter
5.0
__ANON__
5.0
__ANON__
4.0
CRAP 3.5
« blib/lib/Perl/Critic/Policy/InputOutput/ProhibitBacktickOperators.pm ↑ Summary blib/lib/Perl/Critic/Policy/ControlStructures/ProhibitYadaOperator.pm »
1 package Perl::Critic::PolicyParameter::Behavior::Enumeration;
2  
3 40 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 use Perl::Critic::Exception::Fatal::PolicyDefinition
8 40 ▶ qw( throw_policy_definition );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 40 ▶use Perl::Critic::Utils qw( :characters words_from_string hashify );
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 40 ▶use parent qw{ Perl::Critic::PolicyParameter::Behavior };
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 40 our $VERSION = '1.150';
14  
15 #-----------------------------------------------------------------------------
16  
17 sub initialize_parameter {
18 746 ▶ my ($self, $parameter, $specification) = @_;
Subroutine initialize_parameter: called CC=5 SCAR=16.1
Pod documented
19  
20 my $valid_values = $specification->{enumeration_values}
21 746 ▶ or throw_policy_definition
Branch: 1 branch
BranchTrueFalseTotal
my $valid_values = $specification->{"enumeration_values"} or throw_policy_definition('No enumeration_values given for ' . $parameter->get_name . $Perl::Critic::PolicyParameter::Behavior::Enumeration::PERIOD) 1 745 746
22 'No enumeration_values given for '
23 . $parameter->get_name()
24 . $PERIOD;
25 745 ▶ ref $valid_values eq 'ARRAY'
Branch: 1 branch
BranchTrueFalseTotal
ref $valid_values eq "ARRAY" or throw_policy_definition('The value given for enumeration_values for ' . $parameter->get_name . ' is not an array reference.') 1 744 745
26 or throw_policy_definition
27 'The value given for enumeration_values for '
28 . $parameter->get_name()
29 . ' is not an array reference.';
30 744 ▶ scalar @{$valid_values} > 1
Branch: 1 branch
BranchTrueFalseTotal
scalar @{$valid_values;} > 1 or throw_policy_definition('There were not at least two valid values given for enumeration_values for ' . $parameter->get_name . $Perl::Critic::PolicyParameter::Behavior::Enumeration::PERIOD) 2 742 744
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 742 my $value_lookup = { hashify( @{$valid_values} ) };
42 742 $parameter->_get_behavior_values()->{enumeration_values} = $value_lookup;
43  
44 my $allow_multiple_values =
45 742 $specification->{enumeration_allow_multiple_values};
46  
47 742 ▶ if ($allow_multiple_values) {
Branch: 1 branch
BranchTrueFalseTotal
if ($allow_multiple_values) { } 462 280 742
48 $parameter->_set_parser(
49 sub {
50 # Normally bad thing, obscuring a variable in an 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 462 ▶ my ($policy, $parameter, $config_string) = @_; ## no critic(Variables::ProhibitReusedNames)
Subroutine __ANON__: called CC=4 SCAR=13.9
54  
55 462 my @potential_values;
56 462 ▶ my $value_string = $config_string // $parameter->get_default_string();
Condition100%3/3
$config_string // $parameter->get_default_string
l!l&&r!l&&!r
16180266
MC/DC100%2/2
$config_string // $parameter->get_default_string
$config_string $parameter->get_default_string
Truth table100%3/3
A // B
A= $config_stringB= $parameter->get_default_string
ABresult
1X1
011
000
57  
58 462 ▶ if ( defined $value_string ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value_string) 461 1 462
59 461 @potential_values = words_from_string($value_string);
60  
61 my @bad_values =
62 461 grep { not exists $value_lookup->{$_} } @potential_values;
63 461 ▶ if (@bad_values) {
Branch: 1 branch
BranchTrueFalseTotal
if (@bad_values) 1 460 461
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 . join (q{, }, sort keys %{$value_lookup})
72 . qq{.\n},
73 );
74 }
75 }
76  
77 461 my %actual_values = hashify(@potential_values);
78  
79 461 $policy->__set_parameter_value($parameter, \%actual_values);
80  
81 461 return;
82 }
83 462 );
84 } else {
85 $parameter->_set_parser(
86 sub {
87 # Normally bad thing, obscuring a variable in an 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 280 ▶ my ($policy, $parameter, $config_string) = @_; ## no critic(Variables::ProhibitReusedNames)
Subroutine __ANON__: called CC=5 SCAR=16.1
91  
92 280 ▶ my $value_string = $config_string // $parameter->get_default_string();
Condition100%3/3
$config_string // $parameter->get_default_string
l!l&&r!l&&!r
1017991
MC/DC100%2/2
$config_string // $parameter->get_default_string
$config_string $parameter->get_default_string
Truth table100%3/3
A // B
A= $config_stringB= $parameter->get_default_string
ABresult
1X1
011
000
93  
94 280 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value_string and $Perl::Critic::PolicyParameter::Behavior::Enumeration::EMPTY ne $value_string and not defined $value_lookup->{$value_string}) 2 278 280
Condition100%6/6
defined $value_string and $Perl::Critic::PolicyParameter::Behavior::Enumeration::EMPTY ne $value_string and not defined $value_lookup->{$value_string}
!ll&&!rl&&r
931852
defined $value_string and $Perl::Critic::PolicyParameter::Behavior::Enumeration::EMPTY ne $value_string
!ll&&!rl&&r
192187
MC/DC100%3/3
defined $value_string and $Perl::Critic::PolicyParameter::Behavior::Enumeration::EMPTY ne $value_string and not defined $value_lookup->{$value_string}
defined $value_string $Perl::Critic::PolicyParameter::Behavior::Enumeration::EMPTY ne $value_string not defined $value_lookup->{$value_string}
Truth table100%4/4
A and B and not(C)
A= defined $value_stringB= $Perl::Critic::PolicyParameter::Behavior::Enumeration::EMPTY ne $value_stringC= not defined $value_lookup->{$value_string}
ABCresult
0XX0
10X0
1100
1111
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 . join (q{, }, sort keys %{$value_lookup})
105 . qq{.\n},
106 );
107 }
108  
109 278 $policy->__set_parameter_value($parameter, $value_string);
110  
111 278 return;
112 }
113 280 );
114 }
115  
116 742 return;
117 }
118  
119 #-----------------------------------------------------------------------------
120  
121 sub generate_parameter_description {
122 72 ▶ my ($self, $parameter) = @_;
Subroutine generate_parameter_description: called CC=2 SCAR=7.0
Pod documented
123  
124 72 my $description = $parameter->_get_description_with_trailing_period();
125 72 ▶ if ( $description ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($description) 72 0 72
126 72 $description .= qq{\n};
127 }
128  
129 72 my %values = %{$parameter->_get_behavior_values()->{enumeration_values}};
130 return
131 72 $description
132 . 'Valid values: '
133 . join (', ', sort keys %values)
134 . $PERIOD;
135 }
136  
137 #-----------------------------------------------------------------------------
138  
139 40 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-2023 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 :
« blib/lib/Perl/Critic/Policy/InputOutput/ProhibitBacktickOperators.pm ↑ Summary blib/lib/Perl/Critic/Policy/ControlStructures/ProhibitYadaOperator.pm »