File Coverage

blib/lib/Perl/Critic/PolicyParameter/Behavior/StringList.pm
Criterion Covered Total %
statement 41 41 100.0
branch 8 8 100.0
condition 5 6 83.3
subroutine 8 8 100.0
pod 2 2 100.0
total 64 65 98.4


line stmt bran cond sub pod time code
1             package Perl::Critic::PolicyParameter::Behavior::StringList;
2              
3 40     40   741 use 5.010001;
  40         220  
4 40     40   239 use strict;
  40         169  
  40         865  
5 40     40   215 use warnings;
  40         98  
  40         1380  
6              
7 40     40   322 use Perl::Critic::Utils qw{ :characters &words_from_string &hashify };
  40         97  
  40         2188  
8              
9 40     40   9768 use parent qw{ Perl::Critic::PolicyParameter::Behavior };
  40         99  
  40         252  
10              
11             our $VERSION = '1.146';
12              
13             #-----------------------------------------------------------------------------
14              
15             sub initialize_parameter {
16 13015     13015 1 27100 my ($self, $parameter, $specification) = @_;
17              
18             # Unfortunately, this has to be kept as a reference, rather than a regular
19             # array, due to a problem in Devel::Cycle
20             # (http://rt.cpan.org/Ticket/Display.html?id=25360) which causes
21             # t/92_memory_leaks.t to fall over.
22 13015         25617 my $always_present_values = $specification->{list_always_present_values};
23             $parameter->_get_behavior_values()->{always_present_values} =
24 13015         31500 $always_present_values;
25              
26 13015 100       33948 if ( not $always_present_values ) {
27 11601         23062 $always_present_values = [];
28             }
29              
30             $parameter->_set_parser(
31             sub {
32             # Normally bad thing, obscuring a variable in a outer scope
33             # with a variable with the same name is being done here in
34             # order to remain consistent with the parser function interface.
35 12983     12983   29697 my ($policy, $parameter, $config_string) = @_; ## no critic(Variables::ProhibitReusedNames)
36              
37 12983         21800 my @values = @{$always_present_values};
  12983         27287  
38 12983   100     46011 my $value_string = $config_string // $parameter->get_default_string();
39              
40 12983 100       32051 if ( defined $value_string ) {
41 12355         34261 push @values, words_from_string($value_string);
42             }
43              
44 12983         35523 my %values = hashify(@values);
45              
46 12983         54463 $policy->__set_parameter_value($parameter, \%values);
47              
48 12983         34661 return;
49             }
50 13015         75357 );
51              
52 13015         27615 return;
53             }
54              
55             #-----------------------------------------------------------------------------
56              
57             sub generate_parameter_description {
58 341     341 1 760 my ($self, $parameter) = @_;
59              
60             my $always_present_values =
61 341         938 $parameter->_get_behavior_values()->{always_present_values};
62              
63 341         926 my $description = $parameter->_get_description_with_trailing_period();
64 341 100 66     1455 if ( $description and $always_present_values ) {
65 108         330 $description .= qq{\n};
66             }
67              
68 341 100       755 if ( $always_present_values ) {
69 108         213 $description .= 'Values that are always included: ';
70 108         207 $description .= join ', ', sort @{ $always_present_values };
  108         1098  
71 108         205 $description .= $PERIOD;
72             }
73              
74 341         934 return $description;
75             }
76              
77             1;
78              
79             __END__
80              
81             #-----------------------------------------------------------------------------
82              
83             =pod
84              
85             =for stopwords
86              
87             =head1 NAME
88              
89             Perl::Critic::PolicyParameter::Behavior::StringList - Actions appropriate for a parameter that is a list of strings.
90              
91              
92             =head1 DESCRIPTION
93              
94             Provides a standard set of functionality for a string list
95             L<Perl::Critic::PolicyParameter|Perl::Critic::PolicyParameter> so that
96             the developer of a policy does not have to provide it her/himself.
97              
98             NOTE: Do not instantiate this class. Use the singleton instance held
99             onto by
100             L<Perl::Critic::PolicyParameter|Perl::Critic::PolicyParameter>.
101              
102              
103             =head1 INTERFACE SUPPORT
104              
105             This is considered to be a non-public class. Its interface is subject
106             to change without notice.
107              
108              
109             =head1 METHODS
110              
111             =over
112              
113             =item C<initialize_parameter( $parameter, $specification )>
114              
115             Plug in the functionality this behavior provides into the parameter,
116             based upon the configuration provided by the specification.
117              
118             This behavior looks for one configuration item:
119              
120             =over
121              
122             =item always_present_values
123              
124             Optional. Values that should always be included, regardless of what
125             the configuration of the parameter specifies, as an array reference.
126              
127             =back
128              
129             =item C<generate_parameter_description( $parameter )>
130              
131             Create a description of the parameter, based upon the description on
132             the parameter itself, but enhancing it with information from this
133             behavior.
134              
135             In this specific case, the always present values are added at the end.
136              
137             =back
138              
139              
140             =head1 AUTHOR
141              
142             Elliot Shank <perl@galumph.com>
143              
144             =head1 COPYRIGHT
145              
146             Copyright (c) 2006-2011 Elliot Shank.
147              
148             This program is free software; you can redistribute it and/or modify
149             it under the same terms as Perl itself. The full text of this license
150             can be found in the LICENSE file included with this module.
151              
152             =cut
153              
154             # Local Variables:
155             # mode: cperl
156             # cperl-indent-level: 4
157             # fill-column: 78
158             # indent-tabs-mode: nil
159             # c-indentation-style: bsd
160             # End:
161             # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :