File Coverage

blib/lib/Perl/Critic/Config.pm
Criterion Covered Total %
statement 381 395 96.4
branch 92 104 88.4
condition 40 50 80.0
subroutine 69 70 98.5
pod 26 26 100.0
total 608 645 94.2


line stmt bran cond sub pod time code
1             package Perl::Critic::Config;
2              
3 40     40   2307 use 5.010001;
  40         150  
4 40     40   242 use strict;
  40         101  
  40         835  
5 40     40   213 use warnings;
  40         86  
  40         1221  
6              
7 40     40   259 use English qw(-no_match_vars);
  40         119  
  40         332  
8 40     40   14170 use Readonly;
  40         145  
  40         1933  
9              
10 40     40   253 use List::SomeUtils qw(any none apply);
  40         103  
  40         2246  
11 40     40   286 use Scalar::Util qw(blessed);
  40         81  
  40         2001  
12              
13 40     40   8018 use Perl::Critic::Exception::AggregateConfiguration;
  40         124  
  40         1927  
14 40     40   743 use Perl::Critic::Exception::Configuration;
  40         97  
  40         2056  
15 40     40   20414 use Perl::Critic::Exception::Configuration::Option::Global::ParameterValue;
  40         124  
  40         2230  
16 40     40   381 use Perl::Critic::Exception::Fatal::Internal qw{ throw_internal };
  40         112  
  40         1302  
17 40     40   12647 use Perl::Critic::PolicyFactory;
  40         118  
  40         305  
18 40     40   19852 use Perl::Critic::Theme qw( $RULE_INVALID_CHARACTER_REGEX cook_rule );
  40         175  
  40         878  
19 40     40   20081 use Perl::Critic::UserProfile qw();
  40         148  
  40         1370  
20 40         2168 use Perl::Critic::Utils qw{
21             :booleans :characters :severities :internal_lookup :classification
22             :data_conversion
23 40     40   353 };
  40         169  
24 40         186072 use Perl::Critic::Utils::Constants qw<
25             :profile_strictness
26             $_MODULE_VERSION_TERM_ANSICOLOR
27 40     40   23677 >;
  40         150  
28              
29             #-----------------------------------------------------------------------------
30              
31             our $VERSION = '1.146';
32              
33             #-----------------------------------------------------------------------------
34              
35             Readonly::Scalar my $SINGLE_POLICY_CONFIG_KEY => 'single-policy';
36              
37             #-----------------------------------------------------------------------------
38             # Constructor
39              
40             sub new {
41              
42 2921     2921 1 84058 my ( $class, %args ) = @_;
43 2921         6517 my $self = bless {}, $class;
44 2921         11895 $self->_init( %args );
45 2917         19412 return $self;
46             }
47              
48             #-----------------------------------------------------------------------------
49              
50             sub _init {
51 2921     2921   7253 my ( $self, %args ) = @_;
52              
53             # -top or -theme imply that -severity is 1, unless it is already defined
54 2921 100 100     15490 if ( defined $args{-top} || defined $args{-theme} ) {
55 55   66     251 $args{-severity} ||= $SEVERITY_LOWEST;
56             }
57              
58 2921         11958 my $errors = Perl::Critic::Exception::AggregateConfiguration->new();
59              
60             # Construct the UserProfile to get default options.
61 2921         2190546 my $profile_source = $args{-profile}; # Can be file path or data struct
62 2921         15833 my $profile = Perl::Critic::UserProfile->new( -profile => $profile_source );
63 2921         10008 my $options_processor = $profile->options_processor();
64 2921         7890 $self->{_profile} = $profile;
65              
66             $self->_validate_and_save_profile_strictness(
67 2921         15520 $args{'-profile-strictness'},
68             $errors,
69             );
70              
71             # If given, these options should always have a true value.
72             $self->_validate_and_save_regex(
73 2921         12308 'include', $args{-include}, $options_processor->include(), $errors
74             );
75             $self->_validate_and_save_regex(
76 2921         12959 'exclude', $args{-exclude}, $options_processor->exclude(), $errors
77             );
78             $self->_validate_and_save_regex(
79             $SINGLE_POLICY_CONFIG_KEY,
80 2921         14682 $args{ qq/-$SINGLE_POLICY_CONFIG_KEY/ },
81             $options_processor->single_policy(),
82             $errors,
83             );
84             $self->_validate_and_save_color_severity(
85 2921         13135 'color_severity_highest', $args{'-color-severity-highest'},
86             $options_processor->color_severity_highest(), $errors
87             );
88             $self->_validate_and_save_color_severity(
89 2921         14462 'color_severity_high', $args{'-color-severity-high'},
90             $options_processor->color_severity_high(), $errors
91             );
92             $self->_validate_and_save_color_severity(
93 2921         15392 'color_severity_medium', $args{'-color-severity-medium'},
94             $options_processor->color_severity_medium(), $errors
95             );
96             $self->_validate_and_save_color_severity(
97 2921         13642 'color_severity_low', $args{'-color-severity-low'},
98             $options_processor->color_severity_low(), $errors
99             );
100             $self->_validate_and_save_color_severity(
101 2921         13044 'color_severity_lowest', $args{'-color-severity-lowest'},
102             $options_processor->color_severity_lowest(), $errors
103             );
104              
105 2921         15007 $self->_validate_and_save_verbosity($args{-verbose}, $errors);
106 2921         12942 $self->_validate_and_save_severity($args{-severity}, $errors);
107 2921         12156 $self->_validate_and_save_top($args{-top}, $errors);
108 2921         12295 $self->_validate_and_save_theme($args{-theme}, $errors);
109 2921         15185 $self->_validate_and_save_pager($args{-pager}, $errors);
110             $self->_validate_and_save_program_extensions(
111 2921         12465 $args{'-program-extensions'}, $errors);
112              
113             # If given, these options can be true or false (but defined)
114 2921   100     14785 $self->{_force} = _boolean_to_number( $args{-force} // $options_processor->force() );
115 2921   100     12212 $self->{_only} = _boolean_to_number( $args{-only} // $options_processor->only() );
116 2921   66     12298 $self->{_color} = _boolean_to_number( $args{-color} // $options_processor->color() );
117             $self->{_unsafe_allowed} =
118 2921   66     13021 _boolean_to_number( $args{'-allow-unsafe'} // $options_processor->allow_unsafe() );
119             $self->{_criticism_fatal} =
120             _boolean_to_number(
121 2921   66     10566 $args{'-criticism-fatal'} // $options_processor->criticism_fatal()
122             );
123              
124              
125             # Construct a Factory with the Profile
126 2921         10892 my $factory =
127             Perl::Critic::PolicyFactory->new(
128             -profile => $profile,
129             -errors => $errors,
130             '-profile-strictness' => $self->profile_strictness(),
131             );
132 2921         7882 $self->{_factory} = $factory;
133              
134             # Initialize internal storage for Policies
135 2921         8093 $self->{_all_policies_enabled_or_not} = [];
136 2921         6987 $self->{_policies} = [];
137              
138             # "NONE" means don't load any policies
139 2921 100 100     16419 if ( not defined $profile_source or $profile_source ne 'NONE' ) {
140             # Heavy lifting here...
141 81         396 $self->_load_policies($errors);
142             }
143              
144 2921 100       15565 if ( $errors->has_exceptions() ) {
145 4         64 $errors->rethrow();
146             }
147              
148 2917         84776 return $self;
149             }
150              
151             #-----------------------------------------------------------------------------
152              
153             sub _boolean_to_number { ## no critic (RequireArgUnpacking)
154 14605 100   14605   33047 return $_[0] ? $TRUE : $FALSE;
155             }
156              
157             #-----------------------------------------------------------------------------
158              
159             sub add_policy {
160              
161 10541     10541 1 29531 my ( $self, %args ) = @_;
162              
163 10541 100       39729 if ( not $args{-policy} ) {
164 1         9 throw_internal q{The -policy argument is required};
165             }
166              
167 10540         195090 my $policy = $args{-policy};
168              
169             # If the -policy is already a blessed object, then just add it directly.
170 10540 100       43740 if ( blessed $policy ) {
171 7742         22617 $self->_add_policy_if_enabled($policy);
172 7742         23637 return $self;
173             }
174              
175             # NOTE: The "-config" option is supported for backward compatibility.
176 2798   100     11423 my $params = $args{-params} || $args{-config};
177              
178 2798         4970 my $factory = $self->{_factory};
179 2798         12805 my $policy_object =
180             $factory->create_policy(-name=>$policy, -params=>$params);
181 2795         11939 $self->_add_policy_if_enabled($policy_object);
182              
183 2795         12285 return $self;
184             }
185              
186             #-----------------------------------------------------------------------------
187              
188             sub _add_policy_if_enabled {
189 10537     10537   19868 my ( $self, $policy_object ) = @_;
190              
191 10537 50       36553 my $config = $policy_object->__get_config()
192             or throw_internal
193             q{Policy was not set up properly because it does not have }
194             . q{a value for its config attribute.};
195              
196 10537         17535 push @{ $self->{_all_policies_enabled_or_not} }, $policy_object;
  10537         24711  
197 10537 100       39760 if ( $policy_object->initialize_if_enabled( $config ) ) {
198 10441         38305 $policy_object->__set_enabled($TRUE);
199 10441         16493 push @{ $self->{_policies} }, $policy_object;
  10441         21944  
200             }
201             else {
202 96         521 $policy_object->__set_enabled($FALSE);
203             }
204              
205 10537         18556 return;
206             }
207              
208             #-----------------------------------------------------------------------------
209              
210             sub _load_policies {
211              
212 81     81   289 my ( $self, $errors ) = @_;
213 81         249 my $factory = $self->{_factory};
214 81         364 my @policies = $factory->create_all_policies( $errors );
215              
216 81 100       420 return if $errors->has_exceptions();
217              
218 79         1174 for my $policy ( @policies ) {
219              
220             # If -single-policy is true, only load policies that match it
221 11455 100       28669 if ( $self->single_policy() ) {
222 580 100       935 if ( $self->_policy_is_single_policy( $policy ) ) {
223 147         357 $self->add_policy( -policy => $policy );
224             }
225 580         1640 next;
226             }
227              
228             # Always exclude unsafe policies, unless instructed not to
229 10875 100 100     74177 next if not ( $policy->is_safe() or $self->unsafe_allowed() );
230              
231             # To load, or not to load -- that is the question.
232 10874 100       24959 my $load_me = $self->only() ? $FALSE : $TRUE;
233              
234             ## no critic (ProhibitPostfixControls)
235 10874 100       22671 $load_me = $FALSE if $self->_policy_is_disabled( $policy );
236 10874 100       26928 $load_me = $TRUE if $self->_policy_is_enabled( $policy );
237 10874 100       24524 $load_me = $FALSE if $self->_policy_is_unimportant( $policy );
238 10874 100       22260 $load_me = $FALSE if not $self->_policy_is_thematic( $policy );
239 10874 100       32000 $load_me = $TRUE if $self->_policy_is_included( $policy );
240 10874 100       34590 $load_me = $FALSE if $self->_policy_is_excluded( $policy );
241              
242              
243 10874 100       34657 next if not $load_me;
244 7595         21457 $self->add_policy( -policy => $policy );
245             }
246              
247             # When using -single-policy, only one policy should ever be loaded.
248 79 100 100     426 if ($self->single_policy() && scalar $self->policies() != 1) {
249 2         10 $self->_add_single_policy_exception_to($errors);
250             }
251              
252 79         32548 return;
253             }
254              
255             #-----------------------------------------------------------------------------
256              
257             sub _policy_is_disabled {
258 10874     10874   18022 my ($self, $policy) = @_;
259 10874         24799 my $profile = $self->_profile();
260 10874         32419 return $profile->policy_is_disabled( $policy );
261             }
262              
263             #-----------------------------------------------------------------------------
264              
265             sub _policy_is_enabled {
266 10874     10874   19079 my ($self, $policy) = @_;
267 10874         20440 my $profile = $self->_profile();
268 10874         27052 return $profile->policy_is_enabled( $policy );
269             }
270              
271             #-----------------------------------------------------------------------------
272              
273             sub _policy_is_thematic {
274 10874     10874   18380 my ($self, $policy) = @_;
275 10874         21390 my $theme = $self->theme();
276 10874         37605 return $theme->policy_is_thematic( -policy => $policy );
277             }
278              
279             #-----------------------------------------------------------------------------
280              
281             sub _policy_is_unimportant {
282 10874     10874   18627 my ($self, $policy) = @_;
283 10874         42718 my $policy_severity = $policy->get_severity();
284 10874         19265 my $min_severity = $self->{_severity};
285 10874         28636 return $policy_severity < $min_severity;
286             }
287              
288             #-----------------------------------------------------------------------------
289              
290             sub _policy_is_included {
291 10874     10874   19695 my ($self, $policy) = @_;
292 10874         23833 my $policy_long_name = ref $policy;
293 10874         23058 my @inclusions = $self->include();
294 10874     717   62268 return any { $policy_long_name =~ m/$_/ixms } @inclusions;
  717         5572  
295             }
296              
297             #-----------------------------------------------------------------------------
298              
299             sub _policy_is_excluded {
300 10874     10874   20898 my ($self, $policy) = @_;
301 10874         18575 my $policy_long_name = ref $policy;
302 10874         21693 my @exclusions = $self->exclude();
303 10874     855   41892 return any { $policy_long_name =~ m/$_/ixms } @exclusions;
  855         5402  
304             }
305              
306             #-----------------------------------------------------------------------------
307              
308             sub _policy_is_single_policy {
309 580     580   899 my ($self, $policy) = @_;
310              
311 580         862 my @patterns = $self->single_policy();
312 580 50       1132 return if not @patterns;
313              
314 580         2443 my $policy_long_name = ref $policy;
315 580     580   2007 return any { $policy_long_name =~ m/$_/ixms } @patterns;
  580         2462  
316             }
317              
318             #-----------------------------------------------------------------------------
319              
320             sub _new_global_value_exception {
321 16     16   120 my ($self, @args) = @_;
322              
323             return
324 16         109 Perl::Critic::Exception::Configuration::Option::Global::ParameterValue
325             ->new(@args);
326             }
327              
328             #-----------------------------------------------------------------------------
329              
330             sub _add_single_policy_exception_to {
331 2     2   9 my ($self, $errors) = @_;
332              
333 2         7 my $message_suffix = $EMPTY;
334 2         9 my $patterns = join q{", "}, $self->single_policy();
335              
336 2 100       10 if (scalar $self->policies() == 0) {
337 1         5 $message_suffix =
338             q{did not match any policies (in combination with }
339             . q{other policy restrictions).};
340             }
341             else {
342 1         5 $message_suffix = qq{matched multiple policies:\n\t};
343 1     143   10 $message_suffix .= join qq{,\n\t}, apply { chomp } sort $self->policies();
  143         3496  
344             }
345              
346 2         136 $errors->add_exception(
347             $self->_new_global_value_exception(
348             option_name => $SINGLE_POLICY_CONFIG_KEY,
349             option_value => $patterns,
350             message_suffix => $message_suffix,
351             )
352             );
353              
354 2         11 return;
355             }
356              
357             #-----------------------------------------------------------------------------
358              
359             sub _validate_and_save_regex {
360 8763     8763   23268 my ($self, $option_name, $args_value, $default_value, $errors) = @_;
361              
362 8763         20445 my $full_option_name;
363             my $source;
364 8763         0 my @regexes;
365              
366 8763 100       16830 if ($args_value) {
367 8         30 $full_option_name = "-$option_name";
368              
369 8 100       37 if (ref $args_value) {
370 4         10 @regexes = @{ $args_value };
  4         12  
371             }
372             else {
373 4         16 @regexes = ( $args_value );
374             }
375             }
376              
377 8763 100       18705 if (not @regexes) {
378 8755         12233 $full_option_name = $option_name;
379 8755         14667 $source = $self->_profile()->source();
380              
381 8755 100       23144 if (ref $default_value) {
    100          
382 5838         9065 @regexes = @{ $default_value };
  5838         11123  
383             }
384             elsif ($default_value) {
385 1         3 @regexes = ( $default_value );
386             }
387             }
388              
389 8763         12443 my $found_errors;
390 8763         15991 foreach my $regex (@regexes) {
391 18         252 eval { qr/$regex/ixms }
392 18 100       47 or do {
393 3   50     11 my $cleaned_error = $EVAL_ERROR || '<unknown reason>';
394 3         24 $cleaned_error =~
395             s/ [ ] at [ ] .* Config [.] pm [ ] line [ ] \d+ [.] \n? \z/./xms;
396              
397 3         16 $errors->add_exception(
398             $self->_new_global_value_exception(
399             option_name => $option_name,
400             option_value => $regex,
401             source => $source,
402             message_suffix => qq{is not valid: $cleaned_error},
403             )
404             );
405              
406 3         12 $found_errors = 1;
407             }
408             }
409              
410 8763 100       16454 if (not $found_errors) {
411 8760         13497 my $option_key = $option_name;
412 8760         23705 $option_key =~ s/ - /_/xmsg;
413              
414 8760         22447 $self->{"_$option_key"} = \@regexes;
415             }
416              
417 8763         17568 return;
418             }
419              
420             #-----------------------------------------------------------------------------
421              
422             sub _validate_and_save_profile_strictness {
423 2921     2921   10968 my ($self, $args_value, $errors) = @_;
424              
425 2921         7994 my $option_name;
426             my $source;
427 2921         0 my $profile_strictness;
428              
429 2921 50       8555 if ($args_value) {
430 0         0 $option_name = '-profile-strictness';
431 0         0 $profile_strictness = $args_value;
432             }
433             else {
434 2921         5131 $option_name = 'profile-strictness';
435              
436 2921         8399 my $profile = $self->_profile();
437 2921         9401 $source = $profile->source();
438 2921         7368 $profile_strictness = $profile->options_processor()->profile_strictness();
439             }
440              
441 2921 100       15431 if ( not $PROFILE_STRICTNESSES{$profile_strictness} ) {
442 1         13 $errors->add_exception(
443             $self->_new_global_value_exception(
444             option_name => $option_name,
445             option_value => $profile_strictness,
446             source => $source,
447             message_suffix => q{is not one of "}
448             . join ( q{", "}, (sort keys %PROFILE_STRICTNESSES) )
449             . q{".},
450             )
451             );
452              
453 1         6 $profile_strictness = $PROFILE_STRICTNESS_FATAL;
454             }
455              
456 2921         28617 $self->{_profile_strictness} = $profile_strictness;
457              
458 2921         5612 return;
459             }
460              
461             #-----------------------------------------------------------------------------
462              
463             sub _validate_and_save_verbosity {
464 2921     2921   10142 my ($self, $args_value, $errors) = @_;
465              
466 2921         9335 my $option_name;
467             my $source;
468 2921         0 my $verbosity;
469              
470 2921 50       7487 if ($args_value) {
471 0         0 $option_name = '-verbose';
472 0         0 $verbosity = $args_value;
473             }
474             else {
475 2921         5001 $option_name = 'verbose';
476              
477 2921         6174 my $profile = $self->_profile();
478 2921         8167 $source = $profile->source();
479 2921         8565 $verbosity = $profile->options_processor()->verbose();
480             }
481              
482 2921 100 66     9544 if (
483             is_integer($verbosity)
484             and not is_valid_numeric_verbosity($verbosity)
485             ) {
486 1         18 $errors->add_exception(
487             $self->_new_global_value_exception(
488             option_name => $option_name,
489             option_value => $verbosity,
490             source => $source,
491             message_suffix =>
492             'is not the number of one of the pre-defined verbosity formats.',
493             )
494             );
495             }
496             else {
497 2920         31647 $self->{_verbose} = $verbosity;
498             }
499              
500 2921         7777 return;
501             }
502              
503             #-----------------------------------------------------------------------------
504              
505             sub _validate_and_save_severity {
506 2921     2921   8928 my ($self, $args_value, $errors) = @_;
507              
508 2921         8495 my $option_name;
509             my $source;
510 2921         0 my $severity;
511              
512 2921 100       6252 if ($args_value) {
513 67         165 $option_name = '-severity';
514 67         169 $severity = $args_value;
515             }
516             else {
517 2854         5143 $option_name = 'severity';
518              
519 2854         5807 my $profile = $self->_profile();
520 2854         7421 $source = $profile->source();
521 2854         7461 $severity = $profile->options_processor()->severity();
522             }
523              
524 2921 100       8802 if ( is_integer($severity) ) {
    100          
525 2915 100 66     15757 if (
526             $severity >= $SEVERITY_LOWEST and $severity <= $SEVERITY_HIGHEST
527             ) {
528 2914         7027 $self->{_severity} = $severity;
529             }
530             else {
531 1         12 $errors->add_exception(
532             $self->_new_global_value_exception(
533             option_name => $option_name,
534             option_value => $severity,
535             source => $source,
536             message_suffix =>
537             "is not between $SEVERITY_LOWEST (low) and $SEVERITY_HIGHEST (high).",
538             )
539             );
540             }
541             }
542 20     20   285 elsif ( not any { $_ eq lc $severity } @SEVERITY_NAMES ) {
543 1         5 $errors->add_exception(
544             $self->_new_global_value_exception(
545             option_name => $option_name,
546             option_value => $severity,
547             source => $source,
548             message_suffix =>
549             q{is not one of the valid severity names: "}
550             . join (q{", "}, @SEVERITY_NAMES)
551             . q{".},
552             )
553             );
554             }
555             else {
556 5         27 $self->{_severity} = severity_to_number($severity);
557             }
558              
559 2921         5609 return;
560             }
561              
562             #-----------------------------------------------------------------------------
563              
564             sub _validate_and_save_top {
565 2921     2921   7728 my ($self, $args_value, $errors) = @_;
566              
567 2921         8707 my $option_name;
568             my $source;
569 2921         0 my $top;
570              
571 2921 100 100     8882 if (defined $args_value and $args_value ne q{}) {
572 2         6 $option_name = '-top';
573 2         6 $top = $args_value;
574             }
575             else {
576 2919         4884 $option_name = 'top';
577              
578 2919         5476 my $profile = $self->_profile();
579 2919         7837 $source = $profile->source();
580 2919         6909 $top = $profile->options_processor()->top();
581             }
582              
583 2921 100 66     7936 if ( is_integer($top) and $top >= 0 ) {
584 2920         7966 $self->{_top} = $top;
585             }
586             else {
587 1         6 $errors->add_exception(
588             $self->_new_global_value_exception(
589             option_name => $option_name,
590             option_value => $top,
591             source => $source,
592             message_suffix => q{is not a non-negative integer.},
593             )
594             );
595             }
596              
597 2921         5520 return;
598             }
599              
600             #-----------------------------------------------------------------------------
601              
602             sub _validate_and_save_theme {
603 2921     2921   7272 my ($self, $args_value, $errors) = @_;
604              
605 2921         8137 my $option_name;
606             my $source;
607 2921         0 my $theme_rule;
608              
609 2921 100       7068 if ($args_value) {
610 52         135 $option_name = '-theme';
611 52         123 $theme_rule = $args_value;
612             }
613             else {
614 2869         4713 $option_name = 'theme';
615              
616 2869         5746 my $profile = $self->_profile();
617 2869         6539 $source = $profile->source();
618 2869         7372 $theme_rule = $profile->options_processor()->theme();
619             }
620              
621 2921 50       15502 if ( $theme_rule =~ m/$RULE_INVALID_CHARACTER_REGEX/xms ) {
622 0         0 my $bad_character = $1;
623              
624 0         0 $errors->add_exception(
625             $self->_new_global_value_exception(
626             option_name => $option_name,
627             option_value => $theme_rule,
628             source => $source,
629             message_suffix =>
630             qq{contains an illegal character ("$bad_character").},
631             )
632             );
633             }
634             else {
635 2921         12435 my $rule_as_code = cook_rule($theme_rule);
636 2921         6742 $rule_as_code =~ s/ [\w\d]+ / 1 /gxms;
637              
638             # eval of an empty string does not reset $@ in Perl 5.6.
639 2921         6620 local $EVAL_ERROR = $EMPTY;
640 2921         173293 eval $rule_as_code; ## no critic (ProhibitStringyEval, RequireCheckingReturnValueOfEval)
641              
642 2921 100       14860 if ($EVAL_ERROR) {
643 1         6 $errors->add_exception(
644             $self->_new_global_value_exception(
645             option_name => $option_name,
646             option_value => $theme_rule,
647             source => $source,
648             message_suffix => q{is not syntactically valid.},
649             )
650             );
651             }
652             else {
653             eval {
654             $self->{_theme} =
655 2920         18989 Perl::Critic::Theme->new( -rule => $theme_rule );
656             }
657 2920 50       4992 or do {
658 0         0 $errors->add_exception_or_rethrow( $EVAL_ERROR );
659             };
660             }
661             }
662              
663 2921         6449 return;
664             }
665              
666             #-----------------------------------------------------------------------------
667              
668             sub _validate_and_save_pager {
669 2921     2921   10086 my ($self, $args_value, $errors) = @_;
670              
671 2921         4977 my $pager;
672 2921 50       10914 if ( $args_value ) {
    50          
673 0         0 $pager = $args_value;
674             }
675             elsif ( $ENV{PERLCRITIC_PAGER} ) {
676 0         0 $pager = $ENV{PERLCRITIC_PAGER};
677             }
678             else {
679 2921         6461 my $profile = $self->_profile();
680 2921         9778 $pager = $profile->options_processor()->pager();
681             }
682              
683 2921 50       9468 if ($pager eq '$PAGER') { ## no critic (RequireInterpolationOfMetachars)
684 0         0 $pager = $ENV{PAGER};
685             }
686 2921   33     12455 $pager ||= $EMPTY;
687              
688 2921         6552 $self->{_pager} = $pager;
689              
690 2921         4777 return;
691             }
692              
693             #-----------------------------------------------------------------------------
694              
695             sub _validate_and_save_color_severity {
696 14605     14605   39229 my ($self, $option_name, $args_value, $default_value, $errors) = @_;
697              
698 14605         34697 my $source;
699             my $color_severity;
700 14605         0 my $full_option_name;
701              
702 14605 100       25947 if (defined $args_value) {
703 5         13 $full_option_name = "-$option_name";
704 5         10 $color_severity = lc $args_value;
705             }
706             else {
707 14600         22463 $full_option_name = $option_name;
708 14600         25597 $source = $self->_profile()->source();
709 14600         28076 $color_severity = lc $default_value;
710             }
711 14605         36392 $color_severity =~ s/ \s+ / /xmsg;
712 14605         24118 $color_severity =~ s/ \A\s+ //xms;
713 14605         22363 $color_severity =~ s/ \s+\z //xms;
714 14605         45665 $full_option_name =~ s/ _ /-/xmsg;
715              
716             # Should we really be validating this?
717 14605         22591 my $found_errors;
718 14605 50       21439 if (
719             eval {
720 14605         76480 require Term::ANSIColor;
721 14605         273338 Term::ANSIColor->VERSION( $_MODULE_VERSION_TERM_ANSICOLOR );
722 14605         55444 1;
723             }
724             ) {
725 14605         36314 $found_errors =
726             not Term::ANSIColor::colorvalid( words_from_string($color_severity) );
727             }
728              
729             # If we do not have Term::ANSIColor we can not validate, but we store the
730             # values anyway for the benefit of Perl::Critic::ProfilePrototype.
731              
732 14605 100       166850 if ($found_errors) {
733 5         15 $errors->add_exception(
734             $self->_new_global_value_exception(
735             option_name => $full_option_name,
736             option_value => $color_severity,
737             source => $source,
738             message_suffix => 'is not valid.',
739             )
740             );
741             }
742             else {
743 14600         21423 my $option_key = $option_name;
744 14600         25016 $option_key =~ s/ - /_/xmsg;
745              
746 14600         40870 $self->{"_$option_key"} = $color_severity;
747             }
748              
749 14605         25016 return;
750             }
751              
752             #-----------------------------------------------------------------------------
753              
754             sub _validate_and_save_program_extensions {
755 2921     2921   7880 my ($self, $args_value, $errors) = @_;
756              
757 2921         6934 delete $self->{_program_extensions_as_regexes};
758              
759             my $extension_list = q{ARRAY} eq ref $args_value ?
760 2921 50       9500 [map {words_from_string($_)} @{ $args_value }] :
  0         0  
  0         0  
761             $self->_profile()->options_processor()->program_extensions();
762              
763 2921         5682 my %program_extensions = hashify( @{ $extension_list } );
  2921         10232  
764              
765 2921         13102 $self->{_program_extensions} = [keys %program_extensions];
766              
767 2921         5989 return;
768              
769             }
770              
771             #-----------------------------------------------------------------------------
772             # Begin ACCESSSOR methods
773              
774             sub _profile {
775 65429     65429   100117 my ($self) = @_;
776 65429         130416 return $self->{_profile};
777             }
778              
779             #-----------------------------------------------------------------------------
780              
781             sub all_policies_enabled_or_not {
782 2     2 1 28 my ($self) = @_;
783 2         6 return @{ $self->{_all_policies_enabled_or_not} };
  2         15  
784             }
785              
786             #-----------------------------------------------------------------------------
787              
788             sub policies {
789 5484     5484 1 12606 my ($self) = @_;
790 5484         7997 return @{ $self->{_policies} };
  5484         18375  
791             }
792              
793             #-----------------------------------------------------------------------------
794              
795             sub exclude {
796 10878     10878 1 26466 my ($self) = @_;
797 10878         16322 return @{ $self->{_exclude} };
  10878         21893  
798             }
799              
800             #-----------------------------------------------------------------------------
801              
802             sub force {
803 2732     2732 1 7155 my ($self) = @_;
804 2732         8912 return $self->{_force};
805             }
806              
807             #-----------------------------------------------------------------------------
808              
809             sub include {
810 10878     10878 1 16916 my ($self) = @_;
811 10878         15624 return @{ $self->{_include} };
  10878         26968  
812             }
813              
814             #-----------------------------------------------------------------------------
815              
816             sub only {
817 10882     10882 1 17943 my ($self) = @_;
818 10882         27936 return $self->{_only};
819             }
820              
821             #-----------------------------------------------------------------------------
822              
823             sub profile_strictness {
824 2924     2924 1 5381 my ($self) = @_;
825 2924         15181 return $self->{_profile_strictness};
826             }
827              
828             #-----------------------------------------------------------------------------
829              
830             sub severity {
831 12     12 1 63 my ($self) = @_;
832 12         94 return $self->{_severity};
833             }
834              
835             #-----------------------------------------------------------------------------
836              
837             sub single_policy {
838 12119     12119 1 21809 my ($self) = @_;
839 12119         16257 return @{ $self->{_single_policy} };
  12119         34150  
840             }
841              
842             #-----------------------------------------------------------------------------
843              
844             sub theme {
845 10881     10881 1 16619 my ($self) = @_;
846 10881         19623 return $self->{_theme};
847             }
848              
849             #-----------------------------------------------------------------------------
850              
851             sub top {
852 938     938 1 2361 my ($self) = @_;
853 938         4543 return $self->{_top};
854             }
855              
856             #-----------------------------------------------------------------------------
857              
858             sub verbose {
859 7     7 1 26 my ($self) = @_;
860 7         37 return $self->{_verbose};
861             }
862              
863             #-----------------------------------------------------------------------------
864              
865             sub color {
866 6     6 1 24 my ($self) = @_;
867 6         31 return $self->{_color};
868             }
869              
870             #-----------------------------------------------------------------------------
871              
872             sub pager {
873 4     4 1 16 my ($self) = @_;
874 4         31 return $self->{_pager};
875             }
876              
877             #-----------------------------------------------------------------------------
878              
879             sub unsafe_allowed {
880 7     7 1 51 my ($self) = @_;
881 7         42 return $self->{_unsafe_allowed};
882             }
883              
884             #-----------------------------------------------------------------------------
885              
886             sub criticism_fatal {
887 3     3 1 15 my ($self) = @_;
888 3         16 return $self->{_criticism_fatal};
889             }
890              
891             #-----------------------------------------------------------------------------
892              
893             sub site_policy_names {
894 0     0 1 0 return Perl::Critic::PolicyFactory::site_policy_names();
895             }
896              
897             #-----------------------------------------------------------------------------
898              
899             sub color_severity_highest {
900 6     6 1 24 my ($self) = @_;
901 6         30 return $self->{_color_severity_highest};
902             }
903              
904             #-----------------------------------------------------------------------------
905              
906             sub color_severity_high {
907 6     6 1 23 my ($self) = @_;
908 6         28 return $self->{_color_severity_high};
909             }
910              
911             #-----------------------------------------------------------------------------
912              
913             sub color_severity_medium {
914 6     6 1 21 my ($self) = @_;
915 6         25 return $self->{_color_severity_medium};
916             }
917              
918             #-----------------------------------------------------------------------------
919              
920             sub color_severity_low {
921 6     6 1 28 my ($self) = @_;
922 6         29 return $self->{_color_severity_low};
923             }
924              
925             #-----------------------------------------------------------------------------
926              
927             sub color_severity_lowest {
928 6     6 1 21 my ($self) = @_;
929 6         40 return $self->{_color_severity_lowest};
930             }
931              
932             #-----------------------------------------------------------------------------
933              
934             sub program_extensions {
935 2731     2731 1 5210 my ($self) = @_;
936 2731         4106 return @{ $self->{_program_extensions} };
  2731         10391  
937             }
938              
939             #-----------------------------------------------------------------------------
940              
941             sub program_extensions_as_regexes {
942 2727     2727 1 5829 my ($self) = @_;
943              
944 0         0 return @{ $self->{_program_extensions_as_regexes} }
945 2727 50       7583 if $self->{_program_extensions_as_regexes};
946              
947 2727         7979 my %program_extensions = hashify( $self->program_extensions() );
948 2727         7360 $program_extensions{'.PL'} = 1;
949             return @{
950 2727         5143 $self->{_program_extensions_as_regexes} = [
951 2727         9293 map { qr< @{[quotemeta $_]} \z >smx } sort keys %program_extensions
  2727         4772  
  2727         39216  
952             ]
953             };
954             }
955              
956             1;
957              
958             #-----------------------------------------------------------------------------
959              
960             __END__
961              
962             =pod
963              
964             =for stopwords colour INI-style -params
965              
966             =head1 NAME
967              
968             Perl::Critic::Config - The final derived Perl::Critic configuration, combined from any profile file and command-line parameters.
969              
970              
971             =head1 DESCRIPTION
972              
973             Perl::Critic::Config takes care of finding and processing
974             user-preferences for L<Perl::Critic|Perl::Critic>. The Config object
975             defines which Policy modules will be loaded into the Perl::Critic
976             engine and how they should be configured. You should never really
977             need to instantiate Perl::Critic::Config directly because the
978             Perl::Critic constructor will do it for you.
979              
980              
981             =head1 INTERFACE SUPPORT
982              
983             This is considered to be a non-public class. Its interface is subject
984             to change without notice.
985              
986              
987             =head1 CONSTRUCTOR
988              
989             =over
990              
991             =item C<< new(...) >>
992              
993             Not properly documented because you shouldn't be using this.
994              
995              
996             =back
997              
998             =head1 METHODS
999              
1000             =over
1001              
1002             =item C<< add_policy( -policy => $policy_name, -params => \%param_hash ) >>
1003              
1004             Creates a Policy object and loads it into this Config. If the object
1005             cannot be instantiated, it will throw a fatal exception. Otherwise,
1006             it returns a reference to this Critic.
1007              
1008             B<-policy> is the name of a
1009             L<Perl::Critic::Policy|Perl::Critic::Policy> subclass module. The
1010             C<'Perl::Critic::Policy'> portion of the name can be omitted for
1011             brevity. This argument is required.
1012              
1013             B<-params> is an optional reference to a hash of Policy parameters.
1014             The contents of this hash reference will be passed into to the
1015             constructor of the Policy module. See the documentation in the
1016             relevant Policy module for a description of the arguments it supports.
1017              
1018              
1019             =item C< all_policies_enabled_or_not() >
1020              
1021             Returns a list containing references to all the Policy objects that
1022             have been seen. Note that the state of these objects is not
1023             trustworthy. In particular, it is likely that some of them are not
1024             prepared to examine any documents.
1025              
1026              
1027             =item C< policies() >
1028              
1029             Returns a list containing references to all the Policy objects that
1030             have been enabled and loaded into this Config.
1031              
1032              
1033             =item C< exclude() >
1034              
1035             Returns the value of the C<-exclude> attribute for this Config.
1036              
1037              
1038             =item C< include() >
1039              
1040             Returns the value of the C<-include> attribute for this Config.
1041              
1042              
1043             =item C< force() >
1044              
1045             Returns the value of the C<-force> attribute for this Config.
1046              
1047              
1048             =item C< only() >
1049              
1050             Returns the value of the C<-only> attribute for this Config.
1051              
1052              
1053             =item C< profile_strictness() >
1054              
1055             Returns the value of the C<-profile-strictness> attribute for this
1056             Config.
1057              
1058              
1059             =item C< severity() >
1060              
1061             Returns the value of the C<-severity> attribute for this Config.
1062              
1063              
1064             =item C< single_policy() >
1065              
1066             Returns the value of the C<-single-policy> attribute for this Config.
1067              
1068              
1069             =item C< theme() >
1070              
1071             Returns the L<Perl::Critic::Theme|Perl::Critic::Theme> object that was
1072             created for this Config.
1073              
1074              
1075             =item C< top() >
1076              
1077             Returns the value of the C<-top> attribute for this Config.
1078              
1079              
1080             =item C< verbose() >
1081              
1082             Returns the value of the C<-verbose> attribute for this Config.
1083              
1084              
1085             =item C< color() >
1086              
1087             Returns the value of the C<-color> attribute for this Config.
1088              
1089              
1090             =item C< pager() >
1091              
1092             Returns the value of the C<-pager> attribute for this Config.
1093              
1094              
1095             =item C< unsafe_allowed() >
1096              
1097             Returns the value of the C<-allow-unsafe> attribute for this Config.
1098              
1099              
1100             =item C< criticism_fatal() >
1101              
1102             Returns the value of the C<-criticism-fatal> attribute for this Config.
1103              
1104              
1105             =item C< color_severity_highest() >
1106              
1107             Returns the value of the C<-color-severity-highest> attribute for this
1108             Config.
1109              
1110              
1111             =item C< color_severity_high() >
1112              
1113             Returns the value of the C<-color-severity-high> attribute for this
1114             Config.
1115              
1116              
1117             =item C< color_severity_medium() >
1118              
1119             Returns the value of the C<-color-severity-medium> attribute for this
1120             Config.
1121              
1122              
1123             =item C< color_severity_low() >
1124              
1125             Returns the value of the C<-color-severity-low> attribute for this
1126             Config.
1127              
1128              
1129             =item C< color_severity_lowest() >
1130              
1131             Returns the value of the C<-color-severity-lowest> attribute for this
1132             Config.
1133              
1134             =item C< program_extensions() >
1135              
1136             Returns the value of the C<-program_extensions> attribute for this Config.
1137             This is an array of the file name extensions that represent program files.
1138              
1139             =item C< program_extensions_as_regexes() >
1140              
1141             Returns the value of the C<-program_extensions> attribute for this Config, as
1142             an array of case-sensitive regexes matching the ends of the file names that
1143             represent program files.
1144              
1145             =back
1146              
1147              
1148             =head1 SUBROUTINES
1149              
1150             Perl::Critic::Config has a few static subroutines that are used
1151             internally, but may be useful to you in some way.
1152              
1153              
1154             =over
1155              
1156             =item C<site_policy_names()>
1157              
1158             Returns a list of all the Policy modules that are currently installed
1159             in the Perl::Critic:Policy namespace. These will include modules that
1160             are distributed with Perl::Critic plus any third-party modules that
1161             have been installed.
1162              
1163              
1164             =back
1165              
1166              
1167             =head1 CONFIGURATION
1168              
1169             Most of the settings for Perl::Critic and each of the Policy modules
1170             can be controlled by a configuration file. The default configuration
1171             file is called F<.perlcriticrc>.
1172             L<Perl::Critic::Config|Perl::Critic::Config> will look for this file
1173             in the current directory first, and then in your home directory.
1174             Alternatively, you can set the C<PERLCRITIC> environment variable to
1175             explicitly point to a different file in another location. If none of
1176             these files exist, and the C<-profile> option is not given to the
1177             constructor, then all Policies will be loaded with their default
1178             configuration.
1179              
1180             The format of the configuration file is a series of INI-style blocks
1181             that contain key-value pairs separated by '='. Comments should start
1182             with '#' and can be placed on a separate line or after the name-value
1183             pairs if you desire.
1184              
1185             Default settings for Perl::Critic itself can be set B<before the first
1186             named block.> For example, putting any or all of these at the top of
1187             your configuration file will set the default value for the
1188             corresponding Perl::Critic constructor argument.
1189              
1190             severity = 3 #Integer from 1 to 5
1191             only = 1 #Zero or One
1192             force = 0 #Zero or One
1193             verbose = 4 #Integer or format spec
1194             top = 50 #A positive integer
1195             theme = risky + (pbp * security) - cosmetic #A theme expression
1196             include = NamingConventions ClassHierarchies #Space-delimited list
1197             exclude = Variables Modules::RequirePackage #Space-delimited list
1198             color = 1 #Zero or One
1199             allow_unsafe = 1 #Zero or One
1200             color-severity-highest = bold red #Term::ANSIColor
1201             color-severity-high = magenta #Term::ANSIColor
1202             color-severity-medium = #no coloring
1203             color-severity-low = #no coloring
1204             color-severity-lowest = #no coloring
1205             program-extensions = #Space-delimited list
1206              
1207             The remainder of the configuration file is a series of blocks like
1208             this:
1209              
1210             [Perl::Critic::Policy::Category::PolicyName]
1211             severity = 1
1212             set_themes = foo bar
1213             add_themes = baz
1214             arg1 = value1
1215             arg2 = value2
1216              
1217             C<Perl::Critic::Policy::Category::PolicyName> is the full name of a
1218             module that implements the policy. The Policy modules distributed
1219             with Perl::Critic have been grouped into categories according to the
1220             table of contents in Damian Conway's book B<Perl Best Practices>. For
1221             brevity, you can omit the C<'Perl::Critic::Policy'> part of the module
1222             name.
1223              
1224             C<severity> is the level of importance you wish to assign to the
1225             Policy. All Policy modules are defined with a default severity value
1226             ranging from 1 (least severe) to 5 (most severe). However, you may
1227             disagree with the default severity and choose to give it a higher or
1228             lower severity, based on your own coding philosophy.
1229              
1230             The remaining key-value pairs are configuration parameters that will
1231             be passed into the constructor of that Policy. The constructors for
1232             most Policy modules do not support arguments, and those that do should
1233             have reasonable defaults. See the documentation on the appropriate
1234             Policy module for more details.
1235              
1236             Instead of redefining the severity for a given Policy, you can
1237             completely disable a Policy by prepending a '-' to the name of the
1238             module in your configuration file. In this manner, the Policy will
1239             never be loaded, regardless of the C<-severity> given to the
1240             Perl::Critic::Config constructor.
1241              
1242             A simple configuration might look like this:
1243              
1244             #--------------------------------------------------------------
1245             # I think these are really important, so always load them
1246              
1247             [TestingAndDebugging::RequireUseStrict]
1248             severity = 5
1249              
1250             [TestingAndDebugging::RequireUseWarnings]
1251             severity = 5
1252              
1253             #--------------------------------------------------------------
1254             # I think these are less important, so only load when asked
1255              
1256             [Variables::ProhibitPackageVars]
1257             severity = 2
1258              
1259             [ControlStructures::ProhibitPostfixControls]
1260             allow = if unless #My custom configuration
1261             severity = 2
1262              
1263             #--------------------------------------------------------------
1264             # Give these policies a custom theme. I can activate just
1265             # these policies by saying (-theme => 'larry + curly')
1266              
1267             [Modules::RequireFilenameMatchesPackage]
1268             add_themes = larry
1269              
1270             [TestingAndDebugging::RequireTestLables]
1271             add_themes = curly moe
1272              
1273             #--------------------------------------------------------------
1274             # I do not agree with these at all, so never load them
1275              
1276             [-NamingConventions::Capitalization]
1277             [-ValuesAndExpressions::ProhibitMagicNumbers]
1278              
1279             #--------------------------------------------------------------
1280             # For all other Policies, I accept the default severity, theme
1281             # and other parameters, so no additional configuration is
1282             # required for them.
1283              
1284             For additional configuration examples, see the F<perlcriticrc> file
1285             that is included in this F<t/examples> directory of this distribution.
1286              
1287              
1288             =head1 THE POLICIES
1289              
1290             A large number of Policy modules are distributed with Perl::Critic.
1291             They are described briefly in the companion document
1292             L<Perl::Critic::PolicySummary|Perl::Critic::PolicySummary> and in more
1293             detail in the individual modules themselves.
1294              
1295              
1296             =head1 POLICY THEMES
1297              
1298             Each Policy is defined with one or more "themes". Themes can be used
1299             to create arbitrary groups of Policies. They are intended to provide
1300             an alternative mechanism for selecting your preferred set of Policies.
1301             For example, you may wish disable a certain subset of Policies when
1302             analyzing test programs. Conversely, you may wish to enable only a
1303             specific subset of Policies when analyzing modules.
1304              
1305             The Policies that ship with Perl::Critic are have been broken into the
1306             following themes. This is just our attempt to provide some basic
1307             logical groupings. You are free to invent new themes that suit your
1308             needs.
1309              
1310             THEME DESCRIPTION
1311             --------------------------------------------------------------------------
1312             core All policies that ship with Perl::Critic
1313             pbp Policies that come directly from "Perl Best Practices"
1314             bugs Policies that prevent or reveal bugs
1315             maintenance Policies that affect the long-term health of the code
1316             cosmetic Policies that only have a superficial effect
1317             complexity Policies that specificaly relate to code complexity
1318             security Policies that relate to security issues
1319             tests Policies that are specific to test programs
1320              
1321             Say C<`perlcritic -list`> to get a listing of all available policies
1322             and the themes that are associated with each one. You can also change
1323             the theme for any Policy in your F<.perlcriticrc> file. See the
1324             L<"CONFIGURATION"> section for more information about that.
1325              
1326             Using the C<-theme> option, you can combine theme names with
1327             mathematical and boolean operators to create an arbitrarily complex
1328             expression that represents a custom "set" of Policies. The following
1329             operators are supported
1330              
1331             Operator Alternative Meaning
1332             ----------------------------------------------------------------------------
1333             * and Intersection
1334             - not Difference
1335             + or Union
1336              
1337             Operator precedence is the same as that of normal mathematics. You
1338             can also use parenthesis to enforce precedence. Here are some
1339             examples:
1340              
1341             Expression Meaning
1342             ----------------------------------------------------------------------------
1343             pbp * bugs All policies that are "pbp" AND "bugs"
1344             pbp and bugs Ditto
1345              
1346             bugs + cosmetic All policies that are "bugs" OR "cosmetic"
1347             bugs or cosmetic Ditto
1348              
1349             pbp - cosmetic All policies that are "pbp" BUT NOT "cosmetic"
1350             pbp not cosmetic Ditto
1351              
1352             -maintenance All policies that are NOT "maintenance"
1353             not maintenance Ditto
1354              
1355             (pbp - bugs) * complexity All policies that are "pbp" BUT NOT "bugs",
1356             AND "complexity"
1357             (pbp not bugs) and complexity Ditto
1358              
1359             Theme names are case-insensitive. If C<-theme> is set to an empty
1360             string, then it is equivalent to the set of all Policies. A theme
1361             name that doesn't exist is equivalent to an empty set. Please See
1362             L<http://en.wikipedia.org/wiki/Set> for a discussion on set theory.
1363              
1364              
1365             =head1 SEE ALSO
1366              
1367             L<Perl::Critic::OptionsProcessor|Perl::Critic::OptionsProcessor>,
1368             L<Perl::Critic::UserProfile|Perl::Critic::UserProfile>
1369              
1370              
1371             =head1 AUTHOR
1372              
1373             Jeffrey Ryan Thalhammer <jeff@imaginative-software.com>
1374              
1375              
1376             =head1 COPYRIGHT
1377              
1378             Copyright (c) 2005-2021 Imaginative Software Systems. All rights reserved.
1379              
1380             This program is free software; you can redistribute it and/or modify
1381             it under the same terms as Perl itself. The full text of this license
1382             can be found in the LICENSE file included with this module.
1383              
1384             =cut
1385              
1386             ##############################################################################
1387             # Local Variables:
1388             # mode: cperl
1389             # cperl-indent-level: 4
1390             # fill-column: 78
1391             # indent-tabs-mode: nil
1392             # c-indentation-style: bsd
1393             # End:
1394             # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :