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   2061 use 5.010001;
  40         155  
4 40     40   207 use strict;
  40         161  
  40         885  
5 40     40   259 use warnings;
  40         104  
  40         1180  
6              
7 40     40   235 use English qw(-no_match_vars);
  40         126  
  40         302  
8 40     40   14076 use Readonly;
  40         132  
  40         1979  
9              
10 40     40   287 use List::SomeUtils qw(any none apply);
  40         104  
  40         2337  
11 40     40   268 use Scalar::Util qw(blessed);
  40         101  
  40         2063  
12              
13 40     40   8118 use Perl::Critic::Exception::AggregateConfiguration;
  40         115  
  40         1909  
14 40     40   720 use Perl::Critic::Exception::Configuration;
  40         102  
  40         1710  
15 40     40   20746 use Perl::Critic::Exception::Configuration::Option::Global::ParameterValue;
  40         146  
  40         2335  
16 40     40   313 use Perl::Critic::Exception::Fatal::Internal qw{ throw_internal };
  40         104  
  40         1640  
17 40     40   12758 use Perl::Critic::PolicyFactory;
  40         121  
  40         318  
18 40     40   19861 use Perl::Critic::Theme qw( $RULE_INVALID_CHARACTER_REGEX cook_rule );
  40         161  
  40         969  
19 40     40   19974 use Perl::Critic::UserProfile qw();
  40         144  
  40         1340  
20 40         2140 use Perl::Critic::Utils qw{
21             :booleans :characters :severities :internal_lookup :classification
22             :data_conversion
23 40     40   296 };
  40         161  
24 40         187733 use Perl::Critic::Utils::Constants qw<
25             :profile_strictness
26             $_MODULE_VERSION_TERM_ANSICOLOR
27 40     40   23605 >;
  40         146  
28              
29             #-----------------------------------------------------------------------------
30              
31             our $VERSION = '1.148';
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 73305 my ( $class, %args ) = @_;
43 2921         6998 my $self = bless {}, $class;
44 2921         11888 $self->_init( %args );
45 2917         17709 return $self;
46             }
47              
48             #-----------------------------------------------------------------------------
49              
50             sub _init {
51 2921     2921   6994 my ( $self, %args ) = @_;
52              
53             # -top or -theme imply that -severity is 1, unless it is already defined
54 2921 100 100     15739 if ( defined $args{-top} || defined $args{-theme} ) {
55 55   66     250 $args{-severity} ||= $SEVERITY_LOWEST;
56             }
57              
58 2921         10715 my $errors = Perl::Critic::Exception::AggregateConfiguration->new();
59              
60             # Construct the UserProfile to get default options.
61 2921         2167260 my $profile_source = $args{-profile}; # Can be file path or data struct
62 2921         16082 my $profile = Perl::Critic::UserProfile->new( -profile => $profile_source );
63 2921         10237 my $options_processor = $profile->options_processor();
64 2921         6977 $self->{_profile} = $profile;
65              
66             $self->_validate_and_save_profile_strictness(
67 2921         15381 $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         12218 'include', $args{-include}, $options_processor->include(), $errors
74             );
75             $self->_validate_and_save_regex(
76 2921         12367 'exclude', $args{-exclude}, $options_processor->exclude(), $errors
77             );
78             $self->_validate_and_save_regex(
79             $SINGLE_POLICY_CONFIG_KEY,
80 2921         14679 $args{ qq/-$SINGLE_POLICY_CONFIG_KEY/ },
81             $options_processor->single_policy(),
82             $errors,
83             );
84             $self->_validate_and_save_color_severity(
85 2921         15161 'color_severity_highest', $args{'-color-severity-highest'},
86             $options_processor->color_severity_highest(), $errors
87             );
88             $self->_validate_and_save_color_severity(
89 2921         13776 'color_severity_high', $args{'-color-severity-high'},
90             $options_processor->color_severity_high(), $errors
91             );
92             $self->_validate_and_save_color_severity(
93 2921         15729 'color_severity_medium', $args{'-color-severity-medium'},
94             $options_processor->color_severity_medium(), $errors
95             );
96             $self->_validate_and_save_color_severity(
97 2921         14522 'color_severity_low', $args{'-color-severity-low'},
98             $options_processor->color_severity_low(), $errors
99             );
100             $self->_validate_and_save_color_severity(
101 2921         14698 'color_severity_lowest', $args{'-color-severity-lowest'},
102             $options_processor->color_severity_lowest(), $errors
103             );
104              
105 2921         15617 $self->_validate_and_save_verbosity($args{-verbose}, $errors);
106 2921         13117 $self->_validate_and_save_severity($args{-severity}, $errors);
107 2921         13655 $self->_validate_and_save_top($args{-top}, $errors);
108 2921         13292 $self->_validate_and_save_theme($args{-theme}, $errors);
109 2921         14369 $self->_validate_and_save_pager($args{-pager}, $errors);
110             $self->_validate_and_save_program_extensions(
111 2921         11578 $args{'-program-extensions'}, $errors);
112              
113             # If given, these options can be true or false (but defined)
114 2921   100     13794 $self->{_force} = _boolean_to_number( $args{-force} // $options_processor->force() );
115 2921   100     13707 $self->{_only} = _boolean_to_number( $args{-only} // $options_processor->only() );
116 2921   66     11865 $self->{_color} = _boolean_to_number( $args{-color} // $options_processor->color() );
117             $self->{_unsafe_allowed} =
118 2921   66     11500 _boolean_to_number( $args{'-allow-unsafe'} // $options_processor->allow_unsafe() );
119             $self->{_criticism_fatal} =
120             _boolean_to_number(
121 2921   66     11003 $args{'-criticism-fatal'} // $options_processor->criticism_fatal()
122             );
123              
124              
125             # Construct a Factory with the Profile
126 2921         9198 my $factory =
127             Perl::Critic::PolicyFactory->new(
128             -profile => $profile,
129             -errors => $errors,
130             '-profile-strictness' => $self->profile_strictness(),
131             );
132 2921         7749 $self->{_factory} = $factory;
133              
134             # Initialize internal storage for Policies
135 2921         7458 $self->{_all_policies_enabled_or_not} = [];
136 2921         6369 $self->{_policies} = [];
137              
138             # "NONE" means don't load any policies
139 2921 100 100     15890 if ( not defined $profile_source or $profile_source ne 'NONE' ) {
140             # Heavy lifting here...
141 81         349 $self->_load_policies($errors);
142             }
143              
144 2921 100       12290 if ( $errors->has_exceptions() ) {
145 4         70 $errors->rethrow();
146             }
147              
148 2917         79881 return $self;
149             }
150              
151             #-----------------------------------------------------------------------------
152              
153             sub _boolean_to_number { ## no critic (RequireArgUnpacking)
154 14605 100   14605   33602 return $_[0] ? $TRUE : $FALSE;
155             }
156              
157             #-----------------------------------------------------------------------------
158              
159             sub add_policy {
160              
161 10541     10541 1 28590 my ( $self, %args ) = @_;
162              
163 10541 100       38267 if ( not $args{-policy} ) {
164 1         6 throw_internal q{The -policy argument is required};
165             }
166              
167 10540         193980 my $policy = $args{-policy};
168              
169             # If the -policy is already a blessed object, then just add it directly.
170 10540 100       43337 if ( blessed $policy ) {
171 7742         21977 $self->_add_policy_if_enabled($policy);
172 7742         23988 return $self;
173             }
174              
175             # NOTE: The "-config" option is supported for backward compatibility.
176 2798   100     11121 my $params = $args{-params} || $args{-config};
177              
178 2798         4694 my $factory = $self->{_factory};
179 2798         10613 my $policy_object =
180             $factory->create_policy(-name=>$policy, -params=>$params);
181 2795         10998 $self->_add_policy_if_enabled($policy_object);
182              
183 2795         11895 return $self;
184             }
185              
186             #-----------------------------------------------------------------------------
187              
188             sub _add_policy_if_enabled {
189 10537     10537   20282 my ( $self, $policy_object ) = @_;
190              
191 10537 50       39133 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         18232 push @{ $self->{_all_policies_enabled_or_not} }, $policy_object;
  10537         25188  
197 10537 100       38845 if ( $policy_object->initialize_if_enabled( $config ) ) {
198 10441         42750 $policy_object->__set_enabled($TRUE);
199 10441         15329 push @{ $self->{_policies} }, $policy_object;
  10441         23568  
200             }
201             else {
202 96         559 $policy_object->__set_enabled($FALSE);
203             }
204              
205 10537         20132 return;
206             }
207              
208             #-----------------------------------------------------------------------------
209              
210             sub _load_policies {
211              
212 81     81   278 my ( $self, $errors ) = @_;
213 81         216 my $factory = $self->{_factory};
214 81         421 my @policies = $factory->create_all_policies( $errors );
215              
216 81 100       447 return if $errors->has_exceptions();
217              
218 79         1332 for my $policy ( @policies ) {
219              
220             # If -single-policy is true, only load policies that match it
221 11455 100       25653 if ( $self->single_policy() ) {
222 580 100       946 if ( $self->_policy_is_single_policy( $policy ) ) {
223 147         360 $self->add_policy( -policy => $policy );
224             }
225 580         1555 next;
226             }
227              
228             # Always exclude unsafe policies, unless instructed not to
229 10875 100 100     78693 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       22883 my $load_me = $self->only() ? $FALSE : $TRUE;
233              
234             ## no critic (ProhibitPostfixControls)
235 10874 100       22599 $load_me = $FALSE if $self->_policy_is_disabled( $policy );
236 10874 100       26141 $load_me = $TRUE if $self->_policy_is_enabled( $policy );
237 10874 100       25655 $load_me = $FALSE if $self->_policy_is_unimportant( $policy );
238 10874 100       23361 $load_me = $FALSE if not $self->_policy_is_thematic( $policy );
239 10874 100       31234 $load_me = $TRUE if $self->_policy_is_included( $policy );
240 10874 100       35343 $load_me = $FALSE if $self->_policy_is_excluded( $policy );
241              
242              
243 10874 100       36136 next if not $load_me;
244 7595         20182 $self->add_policy( -policy => $policy );
245             }
246              
247             # When using -single-policy, only one policy should ever be loaded.
248 79 100 100     463 if ($self->single_policy() && scalar $self->policies() != 1) {
249 2         10 $self->_add_single_policy_exception_to($errors);
250             }
251              
252 79         30864 return;
253             }
254              
255             #-----------------------------------------------------------------------------
256              
257             sub _policy_is_disabled {
258 10874     10874   18614 my ($self, $policy) = @_;
259 10874         23614 my $profile = $self->_profile();
260 10874         30721 return $profile->policy_is_disabled( $policy );
261             }
262              
263             #-----------------------------------------------------------------------------
264              
265             sub _policy_is_enabled {
266 10874     10874   19420 my ($self, $policy) = @_;
267 10874         18871 my $profile = $self->_profile();
268 10874         25749 return $profile->policy_is_enabled( $policy );
269             }
270              
271             #-----------------------------------------------------------------------------
272              
273             sub _policy_is_thematic {
274 10874     10874   18491 my ($self, $policy) = @_;
275 10874         20265 my $theme = $self->theme();
276 10874         35691 return $theme->policy_is_thematic( -policy => $policy );
277             }
278              
279             #-----------------------------------------------------------------------------
280              
281             sub _policy_is_unimportant {
282 10874     10874   19209 my ($self, $policy) = @_;
283 10874         43789 my $policy_severity = $policy->get_severity();
284 10874         20156 my $min_severity = $self->{_severity};
285 10874         27848 return $policy_severity < $min_severity;
286             }
287              
288             #-----------------------------------------------------------------------------
289              
290             sub _policy_is_included {
291 10874     10874   20479 my ($self, $policy) = @_;
292 10874         20923 my $policy_long_name = ref $policy;
293 10874         23712 my @inclusions = $self->include();
294 10874     717   62003 return any { $policy_long_name =~ m/$_/ixms } @inclusions;
  717         5344  
295             }
296              
297             #-----------------------------------------------------------------------------
298              
299             sub _policy_is_excluded {
300 10874     10874   19933 my ($self, $policy) = @_;
301 10874         18469 my $policy_long_name = ref $policy;
302 10874         21548 my @exclusions = $self->exclude();
303 10874     855   43900 return any { $policy_long_name =~ m/$_/ixms } @exclusions;
  855         5114  
304             }
305              
306             #-----------------------------------------------------------------------------
307              
308             sub _policy_is_single_policy {
309 580     580   866 my ($self, $policy) = @_;
310              
311 580         902 my @patterns = $self->single_policy();
312 580 50       1119 return if not @patterns;
313              
314 580         2292 my $policy_long_name = ref $policy;
315 580     580   1819 return any { $policy_long_name =~ m/$_/ixms } @patterns;
  580         2382  
316             }
317              
318             #-----------------------------------------------------------------------------
319              
320             sub _new_global_value_exception {
321 16     16   117 my ($self, @args) = @_;
322              
323             return
324 16         95 Perl::Critic::Exception::Configuration::Option::Global::ParameterValue
325             ->new(@args);
326             }
327              
328             #-----------------------------------------------------------------------------
329              
330             sub _add_single_policy_exception_to {
331 2     2   7 my ($self, $errors) = @_;
332              
333 2         7 my $message_suffix = $EMPTY;
334 2         7 my $patterns = join q{", "}, $self->single_policy();
335              
336 2 100       10 if (scalar $self->policies() == 0) {
337 1         9 $message_suffix =
338             q{did not match any policies (in combination with }
339             . q{other policy restrictions).};
340             }
341             else {
342 1         3 $message_suffix = qq{matched multiple policies:\n\t};
343 1     143   8 $message_suffix .= join qq{,\n\t}, apply { chomp } sort $self->policies();
  143         3407  
344             }
345              
346 2         143 $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         7 return;
355             }
356              
357             #-----------------------------------------------------------------------------
358              
359             sub _validate_and_save_regex {
360 8763     8763   23756 my ($self, $option_name, $args_value, $default_value, $errors) = @_;
361              
362 8763         19573 my $full_option_name;
363             my $source;
364 8763         0 my @regexes;
365              
366 8763 100       17944 if ($args_value) {
367 8         30 $full_option_name = "-$option_name";
368              
369 8 100       29 if (ref $args_value) {
370 4         8 @regexes = @{ $args_value };
  4         14  
371             }
372             else {
373 4         12 @regexes = ( $args_value );
374             }
375             }
376              
377 8763 100       17633 if (not @regexes) {
378 8755         14303 $full_option_name = $option_name;
379 8755         15966 $source = $self->_profile()->source();
380              
381 8755 100       22171 if (ref $default_value) {
    100          
382 5838         8737 @regexes = @{ $default_value };
  5838         11948  
383             }
384             elsif ($default_value) {
385 1         3 @regexes = ( $default_value );
386             }
387             }
388              
389 8763         12631 my $found_errors;
390 8763         15597 foreach my $regex (@regexes) {
391 18         231 eval { qr/$regex/ixms }
392 18 100       41 or do {
393 3   50     11 my $cleaned_error = $EVAL_ERROR || '<unknown reason>';
394 3         23 $cleaned_error =~
395             s/ [ ] at [ ] .* Config [.] pm [ ] line [ ] \d+ [.] \n? \z/./xms;
396              
397 3         14 $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         14 $found_errors = 1;
407             }
408             }
409              
410 8763 100       16763 if (not $found_errors) {
411 8760         12897 my $option_key = $option_name;
412 8760         23695 $option_key =~ s/ - /_/xmsg;
413              
414 8760         24178 $self->{"_$option_key"} = \@regexes;
415             }
416              
417 8763         17990 return;
418             }
419              
420             #-----------------------------------------------------------------------------
421              
422             sub _validate_and_save_profile_strictness {
423 2921     2921   10627 my ($self, $args_value, $errors) = @_;
424              
425 2921         7640 my $option_name;
426             my $source;
427 2921         0 my $profile_strictness;
428              
429 2921 50       6335 if ($args_value) {
430 0         0 $option_name = '-profile-strictness';
431 0         0 $profile_strictness = $args_value;
432             }
433             else {
434 2921         5579 $option_name = 'profile-strictness';
435              
436 2921         7831 my $profile = $self->_profile();
437 2921         11373 $source = $profile->source();
438 2921         7451 $profile_strictness = $profile->options_processor()->profile_strictness();
439             }
440              
441 2921 100       15323 if ( not $PROFILE_STRICTNESSES{$profile_strictness} ) {
442 1         16 $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         5 $profile_strictness = $PROFILE_STRICTNESS_FATAL;
454             }
455              
456 2921         27325 $self->{_profile_strictness} = $profile_strictness;
457              
458 2921         6413 return;
459             }
460              
461             #-----------------------------------------------------------------------------
462              
463             sub _validate_and_save_verbosity {
464 2921     2921   9117 my ($self, $args_value, $errors) = @_;
465              
466 2921         8102 my $option_name;
467             my $source;
468 2921         0 my $verbosity;
469              
470 2921 50       6812 if ($args_value) {
471 0         0 $option_name = '-verbose';
472 0         0 $verbosity = $args_value;
473             }
474             else {
475 2921         4622 $option_name = 'verbose';
476              
477 2921         6500 my $profile = $self->_profile();
478 2921         7523 $source = $profile->source();
479 2921         7629 $verbosity = $profile->options_processor()->verbose();
480             }
481              
482 2921 100 66     10253 if (
483             is_integer($verbosity)
484             and not is_valid_numeric_verbosity($verbosity)
485             ) {
486 1         27 $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         31421 $self->{_verbose} = $verbosity;
498             }
499              
500 2921         7342 return;
501             }
502              
503             #-----------------------------------------------------------------------------
504              
505             sub _validate_and_save_severity {
506 2921     2921   8575 my ($self, $args_value, $errors) = @_;
507              
508 2921         7245 my $option_name;
509             my $source;
510 2921         0 my $severity;
511              
512 2921 100       6243 if ($args_value) {
513 67         164 $option_name = '-severity';
514 67         156 $severity = $args_value;
515             }
516             else {
517 2854         5125 $option_name = 'severity';
518              
519 2854         6000 my $profile = $self->_profile();
520 2854         8429 $source = $profile->source();
521 2854         6922 $severity = $profile->options_processor()->severity();
522             }
523              
524 2921 100       8317 if ( is_integer($severity) ) {
    100          
525 2915 100 66     14976 if (
526             $severity >= $SEVERITY_LOWEST and $severity <= $SEVERITY_HIGHEST
527             ) {
528 2914         6921 $self->{_severity} = $severity;
529             }
530             else {
531 1         10 $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   242 elsif ( not any { $_ eq lc $severity } @SEVERITY_NAMES ) {
543 1         7 $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         5878 return;
560             }
561              
562             #-----------------------------------------------------------------------------
563              
564             sub _validate_and_save_top {
565 2921     2921   8347 my ($self, $args_value, $errors) = @_;
566              
567 2921         9624 my $option_name;
568             my $source;
569 2921         0 my $top;
570              
571 2921 100 100     9859 if (defined $args_value and $args_value ne q{}) {
572 2         6 $option_name = '-top';
573 2         5 $top = $args_value;
574             }
575             else {
576 2919         4441 $option_name = 'top';
577              
578 2919         7799 my $profile = $self->_profile();
579 2919         8552 $source = $profile->source();
580 2919         7095 $top = $profile->options_processor()->top();
581             }
582              
583 2921 100 66     8412 if ( is_integer($top) and $top >= 0 ) {
584 2920         6661 $self->{_top} = $top;
585             }
586             else {
587 1         12 $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         5703 return;
598             }
599              
600             #-----------------------------------------------------------------------------
601              
602             sub _validate_and_save_theme {
603 2921     2921   7123 my ($self, $args_value, $errors) = @_;
604              
605 2921         7182 my $option_name;
606             my $source;
607 2921         0 my $theme_rule;
608              
609 2921 100       5775 if ($args_value) {
610 52         114 $option_name = '-theme';
611 52         121 $theme_rule = $args_value;
612             }
613             else {
614 2869         4456 $option_name = 'theme';
615              
616 2869         5468 my $profile = $self->_profile();
617 2869         7111 $source = $profile->source();
618 2869         7042 $theme_rule = $profile->options_processor()->theme();
619             }
620              
621 2921 50       14804 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         10819 my $rule_as_code = cook_rule($theme_rule);
636 2921         5886 $rule_as_code =~ s/ [\w\d]+ / 1 /gxms;
637              
638             # eval of an empty string does not reset $@ in Perl 5.6.
639 2921         6159 local $EVAL_ERROR = $EMPTY;
640 2921         170024 eval $rule_as_code; ## no critic (ProhibitStringyEval, RequireCheckingReturnValueOfEval)
641              
642 2921 100       14259 if ($EVAL_ERROR) {
643 1         8 $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         16410 Perl::Critic::Theme->new( -rule => $theme_rule );
656             }
657 2920 50       5684 or do {
658 0         0 $errors->add_exception_or_rethrow( $EVAL_ERROR );
659             };
660             }
661             }
662              
663 2921         7000 return;
664             }
665              
666             #-----------------------------------------------------------------------------
667              
668             sub _validate_and_save_pager {
669 2921     2921   9283 my ($self, $args_value, $errors) = @_;
670              
671 2921         5768 my $pager;
672 2921 50       10747 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         6933 my $profile = $self->_profile();
680 2921         8156 $pager = $profile->options_processor()->pager();
681             }
682              
683 2921 50       11786 if ($pager eq '$PAGER') { ## no critic (RequireInterpolationOfMetachars)
684 0         0 $pager = $ENV{PAGER};
685             }
686 2921   33     12677 $pager ||= $EMPTY;
687              
688 2921         5281 $self->{_pager} = $pager;
689              
690 2921         4889 return;
691             }
692              
693             #-----------------------------------------------------------------------------
694              
695             sub _validate_and_save_color_severity {
696 14605     14605   39233 my ($self, $option_name, $args_value, $default_value, $errors) = @_;
697              
698 14605         31460 my $source;
699             my $color_severity;
700 14605         0 my $full_option_name;
701              
702 14605 100       26795 if (defined $args_value) {
703 5         10 $full_option_name = "-$option_name";
704 5         10 $color_severity = lc $args_value;
705             }
706             else {
707 14600         21517 $full_option_name = $option_name;
708 14600         28598 $source = $self->_profile()->source();
709 14600         28307 $color_severity = lc $default_value;
710             }
711 14605         34608 $color_severity =~ s/ \s+ / /xmsg;
712 14605         24031 $color_severity =~ s/ \A\s+ //xms;
713 14605         22924 $color_severity =~ s/ \s+\z //xms;
714 14605         44643 $full_option_name =~ s/ _ /-/xmsg;
715              
716             # Should we really be validating this?
717 14605         23271 my $found_errors;
718 14605 50       22462 if (
719             eval {
720 14605         80943 require Term::ANSIColor;
721 14605         268851 Term::ANSIColor->VERSION( $_MODULE_VERSION_TERM_ANSICOLOR );
722 14605         55481 1;
723             }
724             ) {
725 14605         39134 $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       167996 if ($found_errors) {
733 5         17 $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         23124 my $option_key = $option_name;
744 14600         27238 $option_key =~ s/ - /_/xmsg;
745              
746 14600         43306 $self->{"_$option_key"} = $color_severity;
747             }
748              
749 14605         26634 return;
750             }
751              
752             #-----------------------------------------------------------------------------
753              
754             sub _validate_and_save_program_extensions {
755 2921     2921   7996 my ($self, $args_value, $errors) = @_;
756              
757 2921         5944 delete $self->{_program_extensions_as_regexes};
758              
759             my $extension_list = q{ARRAY} eq ref $args_value ?
760 2921 50       9620 [map {words_from_string($_)} @{ $args_value }] :
  0         0  
  0         0  
761             $self->_profile()->options_processor()->program_extensions();
762              
763 2921         5098 my %program_extensions = hashify( @{ $extension_list } );
  2921         11226  
764              
765 2921         13322 $self->{_program_extensions} = [keys %program_extensions];
766              
767 2921         6015 return;
768              
769             }
770              
771             #-----------------------------------------------------------------------------
772             # Begin ACCESSOR methods
773              
774             sub _profile {
775 65429     65429   97832 my ($self) = @_;
776 65429         132705 return $self->{_profile};
777             }
778              
779             #-----------------------------------------------------------------------------
780              
781             sub all_policies_enabled_or_not {
782 2     2 1 35 my ($self) = @_;
783 2         5 return @{ $self->{_all_policies_enabled_or_not} };
  2         14  
784             }
785              
786             #-----------------------------------------------------------------------------
787              
788             sub policies {
789 5484     5484 1 11262 my ($self) = @_;
790 5484         8420 return @{ $self->{_policies} };
  5484         19027  
791             }
792              
793             #-----------------------------------------------------------------------------
794              
795             sub exclude {
796 10878     10878 1 18408 my ($self) = @_;
797 10878         14451 return @{ $self->{_exclude} };
  10878         22189  
798             }
799              
800             #-----------------------------------------------------------------------------
801              
802             sub force {
803 2732     2732 1 6331 my ($self) = @_;
804 2732         8916 return $self->{_force};
805             }
806              
807             #-----------------------------------------------------------------------------
808              
809             sub include {
810 10878     10878 1 17723 my ($self) = @_;
811 10878         16052 return @{ $self->{_include} };
  10878         27679  
812             }
813              
814             #-----------------------------------------------------------------------------
815              
816             sub only {
817 10882     10882 1 17874 my ($self) = @_;
818 10882         27584 return $self->{_only};
819             }
820              
821             #-----------------------------------------------------------------------------
822              
823             sub profile_strictness {
824 2924     2924 1 6301 my ($self) = @_;
825 2924         15602 return $self->{_profile_strictness};
826             }
827              
828             #-----------------------------------------------------------------------------
829              
830             sub severity {
831 12     12 1 64 my ($self) = @_;
832 12         87 return $self->{_severity};
833             }
834              
835             #-----------------------------------------------------------------------------
836              
837             sub single_policy {
838 12119     12119 1 20917 my ($self) = @_;
839 12119         16234 return @{ $self->{_single_policy} };
  12119         33197  
840             }
841              
842             #-----------------------------------------------------------------------------
843              
844             sub theme {
845 10881     10881 1 16791 my ($self) = @_;
846 10881         19439 return $self->{_theme};
847             }
848              
849             #-----------------------------------------------------------------------------
850              
851             sub top {
852 938     938 1 2676 my ($self) = @_;
853 938         4368 return $self->{_top};
854             }
855              
856             #-----------------------------------------------------------------------------
857              
858             sub verbose {
859 7     7 1 29 my ($self) = @_;
860 7         35 return $self->{_verbose};
861             }
862              
863             #-----------------------------------------------------------------------------
864              
865             sub color {
866 6     6 1 23 my ($self) = @_;
867 6         28 return $self->{_color};
868             }
869              
870             #-----------------------------------------------------------------------------
871              
872             sub pager {
873 4     4 1 17 my ($self) = @_;
874 4         18 return $self->{_pager};
875             }
876              
877             #-----------------------------------------------------------------------------
878              
879             sub unsafe_allowed {
880 7     7 1 40 my ($self) = @_;
881 7         34 return $self->{_unsafe_allowed};
882             }
883              
884             #-----------------------------------------------------------------------------
885              
886             sub criticism_fatal {
887 3     3 1 9 my ($self) = @_;
888 3         17 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 18 my ($self) = @_;
901 6         29 return $self->{_color_severity_highest};
902             }
903              
904             #-----------------------------------------------------------------------------
905              
906             sub color_severity_high {
907 6     6 1 19 my ($self) = @_;
908 6         27 return $self->{_color_severity_high};
909             }
910              
911             #-----------------------------------------------------------------------------
912              
913             sub color_severity_medium {
914 6     6 1 22 my ($self) = @_;
915 6         43 return $self->{_color_severity_medium};
916             }
917              
918             #-----------------------------------------------------------------------------
919              
920             sub color_severity_low {
921 6     6 1 26 my ($self) = @_;
922 6         25 return $self->{_color_severity_low};
923             }
924              
925             #-----------------------------------------------------------------------------
926              
927             sub color_severity_lowest {
928 6     6 1 23 my ($self) = @_;
929 6         29 return $self->{_color_severity_lowest};
930             }
931              
932             #-----------------------------------------------------------------------------
933              
934             sub program_extensions {
935 2731     2731 1 6417 my ($self) = @_;
936 2731         4961 return @{ $self->{_program_extensions} };
  2731         10725  
937             }
938              
939             #-----------------------------------------------------------------------------
940              
941             sub program_extensions_as_regexes {
942 2727     2727 1 5457 my ($self) = @_;
943              
944 0         0 return @{ $self->{_program_extensions_as_regexes} }
945 2727 50       7168 if $self->{_program_extensions_as_regexes};
946              
947 2727         7591 my %program_extensions = hashify( $self->program_extensions() );
948 2727         6186 $program_extensions{'.PL'} = 1;
949             return @{
950 2727         3868 $self->{_program_extensions_as_regexes} = [
951 2727         8356 map { qr< @{[quotemeta $_]} \z >smx } sort keys %program_extensions
  2727         4258  
  2727         36501  
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::RequireTestLabels]
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 specifically 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 :