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   2079 use 5.010001;
  40         166  
4 40     40   237 use strict;
  40         135  
  40         857  
5 40     40   200 use warnings;
  40         121  
  40         1231  
6              
7 40     40   277 use English qw(-no_match_vars);
  40         110  
  40         382  
8 40     40   14860 use Readonly;
  40         155  
  40         1914  
9              
10 40     40   263 use List::SomeUtils qw( any apply );
  40         125  
  40         2149  
11 40     40   282 use Scalar::Util qw(blessed);
  40         167  
  40         2011  
12              
13 40     40   8103 use Perl::Critic::Exception::AggregateConfiguration;
  40         117  
  40         1849  
14 40     40   738 use Perl::Critic::Exception::Configuration;
  40         98  
  40         1627  
15 40     40   19903 use Perl::Critic::Exception::Configuration::Option::Global::ParameterValue;
  40         139  
  40         2161  
16 40     40   330 use Perl::Critic::Exception::Fatal::Internal qw{ throw_internal };
  40         97  
  40         1229  
17 40     40   12575 use Perl::Critic::PolicyFactory;
  40         112  
  40         1622  
18 40     40   19099 use Perl::Critic::Theme qw( $RULE_INVALID_CHARACTER_REGEX cook_rule );
  40         166  
  40         852  
19 40     40   19595 use Perl::Critic::UserProfile qw();
  40         175  
  40         1301  
20 40         2125 use Perl::Critic::Utils qw{
21             :booleans :characters :severities :internal_lookup :classification
22             :data_conversion
23 40     40   299 };
  40         171  
24 40         179051 use Perl::Critic::Utils::Constants qw<
25             :profile_strictness
26             $_MODULE_VERSION_TERM_ANSICOLOR
27 40     40   24581 >;
  40         120  
28              
29             #-----------------------------------------------------------------------------
30              
31             our $VERSION = '1.150';
32              
33             #-----------------------------------------------------------------------------
34              
35             Readonly::Scalar my $SINGLE_POLICY_CONFIG_KEY => 'single-policy';
36              
37             #-----------------------------------------------------------------------------
38             # Constructor
39              
40             sub new {
41              
42 279     279 1 67281 my ( $class, %args ) = @_;
43 279         800 my $self = bless {}, $class;
44 279         1277 $self->_init( %args );
45 275         3890 return $self;
46             }
47              
48             #-----------------------------------------------------------------------------
49              
50             sub _init {
51 279     279   833 my ( $self, %args ) = @_;
52              
53             # -top or -theme imply that -severity is 1, unless it is already defined
54 279 100 100     1853 if ( defined $args{-top} || defined $args{-theme} ) {
55 55   66     246 $args{-severity} ||= $SEVERITY_LOWEST;
56             }
57              
58 279         1533 my $errors = Perl::Critic::Exception::AggregateConfiguration->new();
59              
60             # Construct the UserProfile to get default options.
61 279         185004 my $profile_source = $args{-profile}; # Can be file path or data struct
62 279         1852 my $profile = Perl::Critic::UserProfile->new( -profile => $profile_source );
63 279         1064 my $options_processor = $profile->options_processor();
64 279         766 $self->{_profile} = $profile;
65              
66             $self->_validate_and_save_profile_strictness(
67 279         1634 $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 279         1290 'include', $args{-include}, $options_processor->include(), $errors
74             );
75             $self->_validate_and_save_regex(
76 279         1365 'exclude', $args{-exclude}, $options_processor->exclude(), $errors
77             );
78             $self->_validate_and_save_regex(
79             $SINGLE_POLICY_CONFIG_KEY,
80 279         1887 $args{ qq/-$SINGLE_POLICY_CONFIG_KEY/ },
81             $options_processor->single_policy(),
82             $errors,
83             );
84             $self->_validate_and_save_color_severity(
85 279         1555 'color_severity_highest', $args{'-color-severity-highest'},
86             $options_processor->color_severity_highest(), $errors
87             );
88             $self->_validate_and_save_color_severity(
89 279         1552 'color_severity_high', $args{'-color-severity-high'},
90             $options_processor->color_severity_high(), $errors
91             );
92             $self->_validate_and_save_color_severity(
93 279         1564 'color_severity_medium', $args{'-color-severity-medium'},
94             $options_processor->color_severity_medium(), $errors
95             );
96             $self->_validate_and_save_color_severity(
97 279         1598 'color_severity_low', $args{'-color-severity-low'},
98             $options_processor->color_severity_low(), $errors
99             );
100             $self->_validate_and_save_color_severity(
101 279         1531 'color_severity_lowest', $args{'-color-severity-lowest'},
102             $options_processor->color_severity_lowest(), $errors
103             );
104              
105 279         1766 $self->_validate_and_save_verbosity($args{-verbose}, $errors);
106 279         1219 $self->_validate_and_save_severity($args{-severity}, $errors);
107 279         1158 $self->_validate_and_save_top($args{-top}, $errors);
108 279         1270 $self->_validate_and_save_theme($args{-theme}, $errors);
109 279         1716 $self->_validate_and_save_pager($args{-pager}, $errors);
110             $self->_validate_and_save_program_extensions(
111 279         1323 $args{'-program-extensions'}, $errors);
112              
113             # If given, these options can be true or false (but defined)
114 279   100     1556 $self->{_force} = _boolean_to_number( $args{-force} // $options_processor->force() );
115 279   100     1191 $self->{_only} = _boolean_to_number( $args{-only} // $options_processor->only() );
116 279   66     1193 $self->{_color} = _boolean_to_number( $args{-color} // $options_processor->color() );
117             $self->{_unsafe_allowed} =
118 279   66     1188 _boolean_to_number( $args{'-allow-unsafe'} // $options_processor->allow_unsafe() );
119             $self->{_criticism_fatal} =
120             _boolean_to_number(
121 279   66     1097 $args{'-criticism-fatal'} // $options_processor->criticism_fatal()
122             );
123              
124              
125             # Construct a Factory with the Profile
126 279         1052 my $factory =
127             Perl::Critic::PolicyFactory->new(
128             -profile => $profile,
129             -errors => $errors,
130             '-profile-strictness' => $self->profile_strictness(),
131             );
132 279         799 $self->{_factory} = $factory;
133              
134             # Initialize internal storage for Policies
135 279         701 $self->{_all_policies_enabled_or_not} = [];
136 279         638 $self->{_policies} = [];
137              
138             # "NONE" means don't load any policies
139 279 100 100     1499 if ( not defined $profile_source or $profile_source ne 'NONE' ) {
140             # Heavy lifting here...
141 81         373 $self->_load_policies($errors);
142             }
143              
144 279 100       3732 if ( $errors->has_exceptions() ) {
145 4         98 $errors->rethrow();
146             }
147              
148 275         9016 return $self;
149             }
150              
151             #-----------------------------------------------------------------------------
152              
153             sub _boolean_to_number { ## no critic (RequireArgUnpacking)
154 1395 100   1395   3225 return $_[0] ? $TRUE : $FALSE;
155             }
156              
157             #-----------------------------------------------------------------------------
158              
159             sub add_policy {
160              
161 7899     7899 1 18247 my ( $self, %args ) = @_;
162              
163 7899 100       28907 if ( not $args{-policy} ) {
164 1         8 throw_internal q{The -policy argument is required};
165             }
166              
167 7898         172042 my $policy = $args{-policy};
168              
169             # If the -policy is already a blessed object, then just add it directly.
170 7898 100       28557 if ( blessed $policy ) {
171 7742         20139 $self->_add_policy_if_enabled($policy);
172 7742         21848 return $self;
173             }
174              
175             # NOTE: The "-config" option is supported for backward compatibility.
176 156   100     515 my $params = $args{-params} || $args{-config};
177              
178 156         264 my $factory = $self->{_factory};
179 156         623 my $policy_object =
180             $factory->create_policy(-name=>$policy, -params=>$params);
181 155         578 $self->_add_policy_if_enabled($policy_object);
182              
183 155         515 return $self;
184             }
185              
186             #-----------------------------------------------------------------------------
187              
188             sub _add_policy_if_enabled {
189 7897     7897   13273 my ( $self, $policy_object ) = @_;
190              
191 7897 50       27850 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 7897         11322 push @{ $self->{_all_policies_enabled_or_not} }, $policy_object;
  7897         15832  
197 7897 100       26015 if ( $policy_object->initialize_if_enabled( $config ) ) {
198 7801         25065 $policy_object->__set_enabled($TRUE);
199 7801         9832 push @{ $self->{_policies} }, $policy_object;
  7801         14074  
200             }
201             else {
202 96         497 $policy_object->__set_enabled($FALSE);
203             }
204              
205 7897         11821 return;
206             }
207              
208             #-----------------------------------------------------------------------------
209              
210             sub _load_policies {
211              
212 81     81   290 my ( $self, $errors ) = @_;
213 81         228 my $factory = $self->{_factory};
214 81         400 my @policies = $factory->create_all_policies( $errors );
215              
216 81 100       414 return if $errors->has_exceptions();
217              
218 79         1032 for my $policy ( @policies ) {
219              
220             # If -single-policy is true, only load policies that match it
221 11455 100       25273 if ( $self->single_policy() ) {
222 580 100       969 if ( $self->_policy_is_single_policy( $policy ) ) {
223 147         384 $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     63663 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       22117 my $load_me = $self->only() ? $FALSE : $TRUE;
233              
234             ## no critic (ProhibitPostfixControls)
235 10874 100       21773 $load_me = $FALSE if $self->_policy_is_disabled( $policy );
236 10874 100       23482 $load_me = $TRUE if $self->_policy_is_enabled( $policy );
237 10874 100       23000 $load_me = $FALSE if $self->_policy_is_unimportant( $policy );
238 10874 100       22278 $load_me = $FALSE if not $self->_policy_is_thematic( $policy );
239 10874 100       27600 $load_me = $TRUE if $self->_policy_is_included( $policy );
240 10874 100       32728 $load_me = $FALSE if $self->_policy_is_excluded( $policy );
241              
242              
243 10874 100       34044 next if not $load_me;
244 7595         18630 $self->add_policy( -policy => $policy );
245             }
246              
247             # When using -single-policy, only one policy should ever be loaded.
248 79 100 100     304 if ($self->single_policy() && scalar $self->policies() != 1) {
249 2         11 $self->_add_single_policy_exception_to($errors);
250             }
251              
252 79         30184 return;
253             }
254              
255             #-----------------------------------------------------------------------------
256              
257             sub _policy_is_disabled {
258 10874     10874   16299 my ($self, $policy) = @_;
259 10874         20089 my $profile = $self->_profile();
260 10874         28519 return $profile->policy_is_disabled( $policy );
261             }
262              
263             #-----------------------------------------------------------------------------
264              
265             sub _policy_is_enabled {
266 10874     10874   17795 my ($self, $policy) = @_;
267 10874         18184 my $profile = $self->_profile();
268 10874         23513 return $profile->policy_is_enabled( $policy );
269             }
270              
271             #-----------------------------------------------------------------------------
272              
273             sub _policy_is_thematic {
274 10874     10874   16485 my ($self, $policy) = @_;
275 10874         18851 my $theme = $self->theme();
276 10874         31445 return $theme->policy_is_thematic( -policy => $policy );
277             }
278              
279             #-----------------------------------------------------------------------------
280              
281             sub _policy_is_unimportant {
282 10874     10874   17948 my ($self, $policy) = @_;
283 10874         37193 my $policy_severity = $policy->get_severity();
284 10874         17936 my $min_severity = $self->{_severity};
285 10874         25495 return $policy_severity < $min_severity;
286             }
287              
288             #-----------------------------------------------------------------------------
289              
290             sub _policy_is_included {
291 10874     10874   19623 my ($self, $policy) = @_;
292 10874         20227 my $policy_long_name = ref $policy;
293 10874         22213 my @inclusions = $self->include();
294 10874     717   55542 return any { $policy_long_name =~ m/$_/ixms } @inclusions;
  717         5352  
295             }
296              
297             #-----------------------------------------------------------------------------
298              
299             sub _policy_is_excluded {
300 10874     10874   17639 my ($self, $policy) = @_;
301 10874         16645 my $policy_long_name = ref $policy;
302 10874         20536 my @exclusions = $self->exclude();
303 10874     855   40461 return any { $policy_long_name =~ m/$_/ixms } @exclusions;
  855         5018  
304             }
305              
306             #-----------------------------------------------------------------------------
307              
308             sub _policy_is_single_policy {
309 580     580   836 my ($self, $policy) = @_;
310              
311 580         879 my @patterns = $self->single_policy();
312 580 50       1182 return if not @patterns;
313              
314 580         2307 my $policy_long_name = ref $policy;
315 580     580   1877 return any { $policy_long_name =~ m/$_/ixms } @patterns;
  580         2433  
316             }
317              
318             #-----------------------------------------------------------------------------
319              
320             sub _new_global_value_exception {
321 16     16   118 my ($self, @args) = @_;
322              
323             return
324 16         103 Perl::Critic::Exception::Configuration::Option::Global::ParameterValue
325             ->new(@args);
326             }
327              
328             #-----------------------------------------------------------------------------
329              
330             sub _add_single_policy_exception_to {
331 2     2   6 my ($self, $errors) = @_;
332              
333 2         9 my $message_suffix = $EMPTY;
334 2         7 my $patterns = join q{", "}, $self->single_policy();
335              
336 2 100       9 if (scalar $self->policies() == 0) {
337 1         4 $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   6 $message_suffix .= join qq{,\n\t}, apply { chomp } sort $self->policies();
  143         3561  
344             }
345              
346 2         145 $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         8 return;
355             }
356              
357             #-----------------------------------------------------------------------------
358              
359             sub _validate_and_save_regex {
360 837     837   2241 my ($self, $option_name, $args_value, $default_value, $errors) = @_;
361              
362 837         1991 my $full_option_name;
363             my $source;
364 837         0 my @regexes;
365              
366 837 100       1715 if ($args_value) {
367 8         30 $full_option_name = "-$option_name";
368              
369 8 100       38 if (ref $args_value) {
370 4         9 @regexes = @{ $args_value };
  4         16  
371             }
372             else {
373 4         14 @regexes = ( $args_value );
374             }
375             }
376              
377 837 100       1801 if (not @regexes) {
378 829         1217 $full_option_name = $option_name;
379 829         1543 $source = $self->_profile()->source();
380              
381 829 100       2348 if (ref $default_value) {
    100          
382 554         842 @regexes = @{ $default_value };
  554         1136  
383             }
384             elsif ($default_value) {
385 1         3 @regexes = ( $default_value );
386             }
387             }
388              
389 837         1233 my $found_errors;
390 837         1609 foreach my $regex (@regexes) {
391 18         241 eval { qr/$regex/ixms }
392 18 100       59 or do {
393 3   50     9 my $cleaned_error = $EVAL_ERROR || '<unknown reason>';
394 3         22 $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         11 $found_errors = 1;
407             }
408             }
409              
410 837 100       1578 if (not $found_errors) {
411 834         1323 my $option_key = $option_name;
412 834         2331 $option_key =~ s/ - /_/xmsg;
413              
414 834         2155 $self->{"_$option_key"} = \@regexes;
415             }
416              
417 837         1619 return;
418             }
419              
420             #-----------------------------------------------------------------------------
421              
422             sub _validate_and_save_profile_strictness {
423 279     279   1056 my ($self, $args_value, $errors) = @_;
424              
425 279         821 my $option_name;
426             my $source;
427 279         0 my $profile_strictness;
428              
429 279 50       705 if ($args_value) {
430 0         0 $option_name = '-profile-strictness';
431 0         0 $profile_strictness = $args_value;
432             }
433             else {
434 279         570 $option_name = 'profile-strictness';
435              
436 279         799 my $profile = $self->_profile();
437 279         942 $source = $profile->source();
438 279         740 $profile_strictness = $profile->options_processor()->profile_strictness();
439             }
440              
441 279 100       1524 if ( not $PROFILE_STRICTNESSES{$profile_strictness} ) {
442 1         14 $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         4 $profile_strictness = $PROFILE_STRICTNESS_FATAL;
454             }
455              
456 279         3028 $self->{_profile_strictness} = $profile_strictness;
457              
458 279         528 return;
459             }
460              
461             #-----------------------------------------------------------------------------
462              
463             sub _validate_and_save_verbosity {
464 279     279   877 my ($self, $args_value, $errors) = @_;
465              
466 279         758 my $option_name;
467             my $source;
468 279         0 my $verbosity;
469              
470 279 50       744 if ($args_value) {
471 0         0 $option_name = '-verbose';
472 0         0 $verbosity = $args_value;
473             }
474             else {
475 279         526 $option_name = 'verbose';
476              
477 279         629 my $profile = $self->_profile();
478 279         780 $source = $profile->source();
479 279         907 $verbosity = $profile->options_processor()->verbose();
480             }
481              
482 279 100 66     1062 if (
483             is_integer($verbosity)
484             and not is_valid_numeric_verbosity($verbosity)
485             ) {
486 1         16 $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 278         3033 $self->{_verbose} = $verbosity;
498             }
499              
500 279         644 return;
501             }
502              
503             #-----------------------------------------------------------------------------
504              
505             sub _validate_and_save_severity {
506 279     279   848 my ($self, $args_value, $errors) = @_;
507              
508 279         886 my $option_name;
509             my $source;
510 279         0 my $severity;
511              
512 279 100       700 if ($args_value) {
513 67         167 $option_name = '-severity';
514 67         144 $severity = $args_value;
515             }
516             else {
517 212         381 $option_name = 'severity';
518              
519 212         457 my $profile = $self->_profile();
520 212         563 $source = $profile->source();
521 212         617 $severity = $profile->options_processor()->severity();
522             }
523              
524 279 100       770 if ( is_integer($severity) ) {
    100          
525 273 100 66     1471 if (
526             $severity >= $SEVERITY_LOWEST and $severity <= $SEVERITY_HIGHEST
527             ) {
528 272         690 $self->{_severity} = $severity;
529             }
530             else {
531 1         8 $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   252 elsif ( not any { $_ eq lc $severity } @SEVERITY_NAMES ) {
543 1         4 $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         28 $self->{_severity} = severity_to_number($severity);
557             }
558              
559 279         600 return;
560             }
561              
562             #-----------------------------------------------------------------------------
563              
564             sub _validate_and_save_top {
565 279     279   808 my ($self, $args_value, $errors) = @_;
566              
567 279         774 my $option_name;
568             my $source;
569 279         0 my $top;
570              
571 279 100 100     926 if (defined $args_value and $args_value ne q{}) {
572 2         8 $option_name = '-top';
573 2         6 $top = $args_value;
574             }
575             else {
576 277         506 $option_name = 'top';
577              
578 277         608 my $profile = $self->_profile();
579 277         812 $source = $profile->source();
580 277         764 $top = $profile->options_processor()->top();
581             }
582              
583 279 100 66     752 if ( is_integer($top) and $top >= 0 ) {
584 278         724 $self->{_top} = $top;
585             }
586             else {
587 1         4 $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 279         582 return;
598             }
599              
600             #-----------------------------------------------------------------------------
601              
602             sub _validate_and_save_theme {
603 279     279   796 my ($self, $args_value, $errors) = @_;
604              
605 279         722 my $option_name;
606             my $source;
607 279         0 my $theme_rule;
608              
609 279 100       582 if ($args_value) {
610 52         111 $option_name = '-theme';
611 52         108 $theme_rule = $args_value;
612             }
613             else {
614 227         380 $option_name = 'theme';
615              
616 227         530 my $profile = $self->_profile();
617 227         574 $source = $profile->source();
618 227         607 $theme_rule = $profile->options_processor()->theme();
619             }
620              
621 279 50       1581 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 279         1043 my $rule_as_code = cook_rule($theme_rule);
636 279         746 $rule_as_code =~ s/ [\w\d]+ / 1 /gxms;
637              
638             # eval of an empty string does not reset $@ in Perl 5.6.
639 279         620 local $EVAL_ERROR = $EMPTY;
640 279         11439 eval $rule_as_code; ## no critic (ProhibitStringyEval, RequireCheckingReturnValueOfEval)
641              
642 279 100       1458 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 278         2004 Perl::Critic::Theme->new( -rule => $theme_rule );
656             }
657 278 50       523 or do {
658 0         0 $errors->add_exception_or_rethrow( $EVAL_ERROR );
659             };
660             }
661             }
662              
663 279         666 return;
664             }
665              
666             #-----------------------------------------------------------------------------
667              
668             sub _validate_and_save_pager {
669 279     279   924 my ($self, $args_value, $errors) = @_;
670              
671 279         455 my $pager;
672 279 50       1034 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 279         621 my $profile = $self->_profile();
680 279         969 $pager = $profile->options_processor()->pager();
681             }
682              
683 279 50       943 if ($pager eq '$PAGER') { ## no critic (RequireInterpolationOfMetachars)
684 0         0 $pager = $ENV{PAGER};
685             }
686 279   33     1244 $pager ||= $EMPTY;
687              
688 279         614 $self->{_pager} = $pager;
689              
690 279         471 return;
691             }
692              
693             #-----------------------------------------------------------------------------
694              
695             sub _validate_and_save_color_severity {
696 1395     1395   3743 my ($self, $option_name, $args_value, $default_value, $errors) = @_;
697              
698 1395         3163 my $source;
699             my $color_severity;
700 1395         0 my $full_option_name;
701              
702 1395 100       2601 if (defined $args_value) {
703 5         11 $full_option_name = "-$option_name";
704 5         12 $color_severity = lc $args_value;
705             }
706             else {
707 1390         2085 $full_option_name = $option_name;
708 1390         2558 $source = $self->_profile()->source();
709 1390         2899 $color_severity = lc $default_value;
710             }
711 1395         3348 $color_severity =~ s/ \s+ / /xmsg;
712 1395         2424 $color_severity =~ s/ \A\s+ //xms;
713 1395         2429 $color_severity =~ s/ \s+\z //xms;
714 1395         4333 $full_option_name =~ s/ _ /-/xmsg;
715              
716             # Should we really be validating this?
717 1395         2200 my $found_errors;
718 1395 50       2244 if (
719             eval {
720 1395         18714 require Term::ANSIColor;
721 1395         116232 Term::ANSIColor->VERSION( $_MODULE_VERSION_TERM_ANSICOLOR );
722 1395         5407 1;
723             }
724             ) {
725 1395         3740 $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 1395 100       15943 if ($found_errors) {
733 5         16 $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 1390         2077 my $option_key = $option_name;
744 1390         2539 $option_key =~ s/ - /_/xmsg;
745              
746 1390         4089 $self->{"_$option_key"} = $color_severity;
747             }
748              
749 1395         2531 return;
750             }
751              
752             #-----------------------------------------------------------------------------
753              
754             sub _validate_and_save_program_extensions {
755 279     279   747 my ($self, $args_value, $errors) = @_;
756              
757 279         585 delete $self->{_program_extensions_as_regexes};
758              
759             my $extension_list = q{ARRAY} eq ref $args_value ?
760 279 50       893 [map {words_from_string($_)} @{ $args_value }] :
  0         0  
  0         0  
761             $self->_profile()->options_processor()->program_extensions();
762              
763 279         519 my %program_extensions = hashify( @{ $extension_list } );
  279         953  
764              
765 279         1300 $self->{_program_extensions} = [keys %program_extensions];
766              
767 279         668 return;
768              
769             }
770              
771             #-----------------------------------------------------------------------------
772             # Begin ACCESSOR methods
773              
774             sub _profile {
775 25799     25799   36514 my ($self) = @_;
776 25799         45249 return $self->{_profile};
777             }
778              
779             #-----------------------------------------------------------------------------
780              
781             sub all_policies_enabled_or_not {
782 2     2 1 26 my ($self) = @_;
783 2         5 return @{ $self->{_all_policies_enabled_or_not} };
  2         19  
784             }
785              
786             #-----------------------------------------------------------------------------
787              
788             sub policies {
789 204     204 1 1075 my ($self) = @_;
790 204         408 return @{ $self->{_policies} };
  204         1670  
791             }
792              
793             #-----------------------------------------------------------------------------
794              
795             sub exclude {
796 10878     10878 1 16413 my ($self) = @_;
797 10878         14167 return @{ $self->{_exclude} };
  10878         20941  
798             }
799              
800             #-----------------------------------------------------------------------------
801              
802             sub force {
803 92     92 1 884 my ($self) = @_;
804 92         370 return $self->{_force};
805             }
806              
807             #-----------------------------------------------------------------------------
808              
809             sub include {
810 10878     10878 1 17365 my ($self) = @_;
811 10878         13197 return @{ $self->{_include} };
  10878         24935  
812             }
813              
814             #-----------------------------------------------------------------------------
815              
816             sub only {
817 10882     10882 1 17659 my ($self) = @_;
818 10882         24022 return $self->{_only};
819             }
820              
821             #-----------------------------------------------------------------------------
822              
823             sub profile_strictness {
824 282     282 1 1085 my ($self) = @_;
825 282         1641 return $self->{_profile_strictness};
826             }
827              
828             #-----------------------------------------------------------------------------
829              
830             sub severity {
831 12     12 1 524 my ($self) = @_;
832 12         86 return $self->{_severity};
833             }
834              
835             #-----------------------------------------------------------------------------
836              
837             sub single_policy {
838 12119     12119 1 20069 my ($self) = @_;
839 12119         15027 return @{ $self->{_single_policy} };
  12119         29971  
840             }
841              
842             #-----------------------------------------------------------------------------
843              
844             sub theme {
845 10881     10881 1 16428 my ($self) = @_;
846 10881         17752 return $self->{_theme};
847             }
848              
849             #-----------------------------------------------------------------------------
850              
851             sub top {
852 70     70 1 1076 my ($self) = @_;
853 70         324 return $self->{_top};
854             }
855              
856             #-----------------------------------------------------------------------------
857              
858             sub verbose {
859 7     7 1 46 my ($self) = @_;
860 7         39 return $self->{_verbose};
861             }
862              
863             #-----------------------------------------------------------------------------
864              
865             sub color {
866 6     6 1 562 my ($self) = @_;
867 6         33 return $self->{_color};
868             }
869              
870             #-----------------------------------------------------------------------------
871              
872             sub pager {
873 4     4 1 16 my ($self) = @_;
874 4         21 return $self->{_pager};
875             }
876              
877             #-----------------------------------------------------------------------------
878              
879             sub unsafe_allowed {
880 7     7 1 39 my ($self) = @_;
881 7         37 return $self->{_unsafe_allowed};
882             }
883              
884             #-----------------------------------------------------------------------------
885              
886             sub criticism_fatal {
887 3     3 1 15 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 549 my ($self) = @_;
901 6         27 return $self->{_color_severity_highest};
902             }
903              
904             #-----------------------------------------------------------------------------
905              
906             sub color_severity_high {
907 6     6 1 591 my ($self) = @_;
908 6         29 return $self->{_color_severity_high};
909             }
910              
911             #-----------------------------------------------------------------------------
912              
913             sub color_severity_medium {
914 6     6 1 531 my ($self) = @_;
915 6         25 return $self->{_color_severity_medium};
916             }
917              
918             #-----------------------------------------------------------------------------
919              
920             sub color_severity_low {
921 6     6 1 533 my ($self) = @_;
922 6         28 return $self->{_color_severity_low};
923             }
924              
925             #-----------------------------------------------------------------------------
926              
927             sub color_severity_lowest {
928 6     6 1 546 my ($self) = @_;
929 6         28 return $self->{_color_severity_lowest};
930             }
931              
932             #-----------------------------------------------------------------------------
933              
934             sub program_extensions {
935 91     91 1 705 my ($self) = @_;
936 91         175 return @{ $self->{_program_extensions} };
  91         423  
937             }
938              
939             #-----------------------------------------------------------------------------
940              
941             sub program_extensions_as_regexes {
942 87     87 1 284 my ($self) = @_;
943              
944 0         0 return @{ $self->{_program_extensions_as_regexes} }
945 87 50       292 if $self->{_program_extensions_as_regexes};
946              
947 87         359 my %program_extensions = hashify( $self->program_extensions() );
948 87         263 $program_extensions{'.PL'} = 1;
949             return @{
950 87         168 $self->{_program_extensions_as_regexes} = [
951 87         332 map { qr< @{[quotemeta $_]} \z >smx } sort keys %program_extensions
  87         205  
  87         1678  
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-2023 Imaginative Software Systems
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 :