File Coverage

blib/lib/Perl/Critic/ProfilePrototype.pm
Criterion Covered Total %
statement 114 114 100.0
branch 3 4 75.0
condition 4 4 100.0
subroutine 15 15 100.0
pod 2 2 100.0
total 138 139 99.2


line stmt bran cond sub pod time code
1             package Perl::Critic::ProfilePrototype;
2              
3 2     2   2065 use 5.010001;
  2         8  
4 2     2   18 use strict;
  2         4  
  2         43  
5 2     2   9 use warnings;
  2         5  
  2         56  
6              
7 2     2   11 use Perl::Critic::Config qw{};
  2         3  
  2         32  
8 2     2   8 use Perl::Critic::Policy qw{};
  2         5  
  2         58  
9 2     2   11 use Perl::Critic::Utils qw{ :characters };
  2         7  
  2         119  
10 2     2   463 use overload ( q{""} => 'to_string' );
  2         5  
  2         16  
11              
12             our $VERSION = '1.150';
13              
14             #-----------------------------------------------------------------------------
15              
16             sub new {
17 2     2 1 2119 my ($class, %args) = @_;
18 2         7 my $self = bless {}, $class;
19              
20 2   100     13 my $policies = $args{-policies} || [];
21 2         4 $self->{_policies} = [ sort _by_type @{ $policies } ];
  2         38  
22              
23 2   100     11 $self->{_comment_out_parameters} = $args{'-comment-out-parameters'} // 1;
24              
25 2         6 my $configuration = $args{'-config'};
26 2 100       7 if (not $configuration) {
27 1         7 $configuration = Perl::Critic::Config->new(-profile => $EMPTY);
28             }
29 2         18 $self->{_configuration} = $configuration;
30              
31              
32 2         10 return $self;
33             }
34              
35             #-----------------------------------------------------------------------------
36              
37             sub _get_policies {
38 1     1   4 my ($self) = @_;
39              
40 1         4 return $self->{_policies};
41             }
42              
43             sub _comment_out_parameters {
44 2     2   6 my ($self) = @_;
45              
46 2         16 return $self->{_comment_out_parameters};
47             }
48              
49             sub _configuration {
50 1     1   3 my ($self) = @_;
51              
52 1         4 return $self->{_configuration};
53             }
54              
55             #-----------------------------------------------------------------------------
56              
57             sub _line_prefix {
58 2     2   6 my ($self) = @_;
59              
60 2 50       6 return $self->_comment_out_parameters() ? q{# } : $EMPTY;
61             }
62              
63             #-----------------------------------------------------------------------------
64              
65             sub to_string {
66 1     1 1 8 my ($self) = @_;
67              
68 1         6 my $prefix = $self->_line_prefix();
69 1         5 my $configuration = $self->_configuration();
70              
71 1         2 my $prototype = "# Globals\n";
72              
73 1         4 $prototype .= $prefix;
74 1         2 $prototype .= q{severity = };
75 1         6 $prototype .= $configuration->severity();
76 1         3 $prototype .= "\n";
77              
78 1         13 $prototype .= $prefix;
79 1         3 $prototype .= q{force = };
80 1         5 $prototype .= $configuration->force();
81 1         3 $prototype .= "\n";
82              
83 1         3 $prototype .= $prefix;
84 1         3 $prototype .= q{only = };
85 1         3 $prototype .= $configuration->only();
86 1         2 $prototype .= "\n";
87              
88 1         3 $prototype .= $prefix;
89 1         3 $prototype .= q{allow-unsafe = };
90 1         5 $prototype .= $configuration->unsafe_allowed();
91 1         3 $prototype .= "\n";
92              
93 1         3 $prototype .= $prefix;
94 1         3 $prototype .= q{profile-strictness = };
95 1         13 $prototype .= $configuration->profile_strictness();
96 1         2 $prototype .= "\n";
97              
98 1         2 $prototype .= $prefix;
99 1         3 $prototype .= q{color = };
100 1         6 $prototype .= $configuration->color();
101 1         2 $prototype .= "\n";
102              
103 1         2 $prototype .= $prefix;
104 1         3 $prototype .= q{pager = };
105 1         4 $prototype .= $configuration->pager();
106 1         3 $prototype .= "\n";
107              
108 1         2 $prototype .= $prefix;
109 1         2 $prototype .= q{top = };
110 1         3 $prototype .= $configuration->top();
111 1         3 $prototype .= "\n";
112              
113 1         3 $prototype .= $prefix;
114 1         3 $prototype .= q{verbose = };
115 1         5 $prototype .= $configuration->verbose();
116 1         3 $prototype .= "\n";
117              
118 1         2 $prototype .= $prefix;
119 1         2 $prototype .= q{include = };
120 1         3 $prototype .= join $SPACE, $configuration->include();
121 1         11 $prototype .= "\n";
122              
123 1         2 $prototype .= $prefix;
124 1         3 $prototype .= q{exclude = };
125 1         4 $prototype .= join $SPACE, $configuration->exclude();
126 1         2 $prototype .= "\n";
127              
128 1         2 $prototype .= $prefix;
129 1         3 $prototype .= q{single-policy = };
130 1         10 $prototype .= join $SPACE, $configuration->single_policy();
131 1         3 $prototype .= "\n";
132              
133 1         3 $prototype .= $prefix;
134 1         2 $prototype .= q{theme = };
135 1         4 $prototype .= $configuration->theme()->rule();
136 1         2 $prototype .= "\n";
137              
138 1         5 foreach my $item (qw<
139             color-severity-highest
140             color-severity-high
141             color-severity-medium
142             color-severity-low
143             color-severity-lowest
144             >) {
145 5         18 ( my $accessor = $item ) =~ s/ - /_/gmsx;
146 5         10 $prototype .= $prefix;
147 5         9 $prototype .= "$item = ";
148 5         26 $prototype .= $configuration->$accessor;
149 5         12 $prototype .= "\n";
150             }
151              
152 1         2 $prototype .= $prefix;
153 1         3 $prototype .= q{program-extensions = };
154 1         5 $prototype .= join $SPACE, $configuration->program_extensions();
155              
156 1         4 Perl::Critic::Policy::set_format( $self->_proto_format() );
157              
158 1         2 my $policy_prototypes = join qq{\n}, map { "$_" } @{ $self->_get_policies() };
  143         10916  
  1         5  
159 1         1068 $policy_prototypes =~ s/\s+ \z//xms; # Trim trailing whitespace
160 1         81 return $prototype . "\n\n" . $policy_prototypes . "\n";
161             }
162              
163             #-----------------------------------------------------------------------------
164              
165             # About "%{\\n%\\x7b# \\x7df\n${prefix}%n = %D\\n}O" below:
166             #
167             # The %0 format for a policy specifies how to format parameters.
168             # For a parameter %f specifies the full description.
169             #
170             # The problem is that both of these need to take options, but String::Format
171             # doesn't allow nesting of {}. So, to get the option to the %f, the braces
172             # are hex encoded. I.e., assuming that comment_out_parameters is in effect,
173             # the parameter sees:
174             #
175             # \n%{# }f\n# %n = %D\n
176              
177             sub _proto_format {
178 1     1   4 my ($self) = @_;
179              
180 1         7 my $prefix = $self->_line_prefix();
181              
182 1         13 return <<"END_OF_FORMAT";
183             # %a
184             [%p]
185             ${prefix}set_themes = %t
186             ${prefix}add_themes =
187             ${prefix}severity = %s
188             ${prefix}maximum_violations_per_document = %v
189             %{\\n%\\x7b# \\x7df\\n${prefix}%n = %D\\n}O%{${prefix}Cannot programmatically discover what parameters this policy takes.\\n}U
190             END_OF_FORMAT
191              
192             }
193              
194             #-----------------------------------------------------------------------------
195              
196 142     142   287 sub _by_type { return ref $a cmp ref $b }
197              
198             1;
199              
200             __END__
201              
202             =pod
203              
204             =head1 NAME
205              
206             Perl::Critic::ProfilePrototype - Generate an initial Perl::Critic profile.
207              
208              
209             =head1 DESCRIPTION
210              
211             This is a helper class that generates a prototype of a
212             L<Perl::Critic|Perl::Critic> profile (e.g. a F<.perlcriticrc> file).
213             There are no user-serviceable parts here.
214              
215              
216             =head1 INTERFACE SUPPORT
217              
218             This is considered to be a non-public class. Its interface is subject
219             to change without notice.
220              
221              
222             =head1 CONSTRUCTOR
223              
224             =over
225              
226             =item C<< new( -policies => \@POLICY_OBJECTS ) >>
227              
228             Returns a reference to a new C<Perl::Critic::ProfilePrototype> object.
229              
230              
231             =back
232              
233              
234             =head1 METHODS
235              
236             =over
237              
238             =item to_string()
239              
240             Returns a string representation of this C<ProfilePrototype>. See
241             L<"OVERLOADS"> for more information.
242              
243              
244             =back
245              
246              
247             =head1 OVERLOADS
248              
249             When a
250             L<Perl::Critic::ProfilePrototype|Perl::Critic::ProfilePrototype> is
251             evaluated in string context, it produces a multi-line summary of the
252             policy name, default themes, and default severity for each
253             L<Perl::Critic::Policy|Perl::Critic::Policy> object that was given to
254             the constructor of this C<ProfilePrototype>. If the Policy supports
255             an additional parameters, they will also be listed (but
256             commented-out). The format is suitable for use as a F<.perlcriticrc>
257             file.
258              
259              
260             =head1 AUTHOR
261              
262             Jeffrey Ryan Thalhammer <jeff@imaginative-software.com>
263              
264              
265             =head1 COPYRIGHT
266              
267             Copyright (c) 2005-2023 Imaginative Software Systems
268              
269             This program is free software; you can redistribute it and/or modify
270             it under the same terms as Perl itself. The full text of this license
271             can be found in the LICENSE file included with this module.
272              
273             =cut
274              
275             # Local Variables:
276             # mode: cperl
277             # cperl-indent-level: 4
278             # fill-column: 78
279             # indent-tabs-mode: nil
280             # c-indentation-style: bsd
281             # End:
282             # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :