« lib/Perl/Critic/Policy/Subroutines/ProhibitExplicitReturnUndef.pm
↑ Summary
lib/Perl/Critic/Policy/Variables/RequireInitializationForLocalVars.pm »
| 1 | package Perl::Critic::Theme; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 40 | ▶ | use 5.010001; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 40 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 40 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 40 | ▶ | use English qw(-no_match_vars); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 40 | ▶ | use Readonly; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 40 | ▶ | use Exporter 'import'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 40 | ▶ | use Perl::Critic::Utils qw( :characters :data_conversion ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | 40 | ▶ | use Perl::Critic::Exception::Fatal::Internal qw( throw_internal ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | use Perl::Critic::Exception::Configuration::Option::Global::ParameterValue | ||||||||||||||||||||
| 14 | 40 | ▶ | qw( throw_global_value ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | #----------------------------------------------------------------------------- | ||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | 40 | our $VERSION = '1.156'; | |||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | #----------------------------------------------------------------------------- | ||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | 40 | Readonly::Array our @EXPORT_OK => qw{ | |||||||||||||||||||
| 23 | $RULE_INVALID_CHARACTER_REGEX | ||||||||||||||||||||
| 24 | cook_rule | ||||||||||||||||||||
| 25 | }; | ||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | #----------------------------------------------------------------------------- | ||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | 40 | Readonly::Scalar our $RULE_INVALID_CHARACTER_REGEX => | |||||||||||||||||||
| 30 | qr/ ( [^()\s\w\d+\-*&|!] ) /xms; | ||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | #----------------------------------------------------------------------------- | ||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | 40 | Readonly::Scalar my $CONFIG_KEY => 'theme'; | |||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | #----------------------------------------------------------------------------- | ||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | 321 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | 321 | my ( $class, %args ) = @_; | |||||||||||||||||||
| 41 | 321 | my $self = bless {}, $class; | |||||||||||||||||||
| 42 | 321 | $self->_init( %args ); | |||||||||||||||||||
| 43 | 313 | return $self; | |||||||||||||||||||
| 44 | } | ||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | #----------------------------------------------------------------------------- | ||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | 321 | ▶ | sub _init { | ||||||||||||||||||
|
Subroutine
_init: called CC=3 SCAR=11.0
| |||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | 321 | my ($self, %args) = @_; | |||||||||||||||||||
| 51 | 321 | ▶ | my $rule = $args{-rule} || $EMPTY; | ||||||||||||||||||
|
Condition66%2/3
$args{'-rule'} || $Perl::Critic::Theme::EMPTY
MC/DC50%1/2
$args{'-rule'} || $Perl::Critic::Theme::EMPTY
$args{'-rule'}
$Perl::Critic::Theme::EMPTY
Truth table66%2/3
A || B
A= $args{'-rule'}B= $Perl::Critic::Theme::EMPTY
| |||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | 321 | ▶ | if ( $rule =~ m/$RULE_INVALID_CHARACTER_REGEX/xms ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 54 | 8 | throw_global_value | |||||||||||||||||||
| 55 | option_name => $CONFIG_KEY, | ||||||||||||||||||||
| 56 | option_value => $rule, | ||||||||||||||||||||
| 57 | message_suffix => qq{contains an invalid character: "$1".}; | ||||||||||||||||||||
| 58 | } | ||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | 313 | $self->{_rule} = cook_rule( $rule ); | |||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | 313 | return $self; | |||||||||||||||||||
| 63 | } | ||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | #----------------------------------------------------------------------------- | ||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | 5 | ▶ | sub rule { | ||||||||||||||||||
|
Subroutine
rule: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 68 | 5 | my $self = shift; | |||||||||||||||||||
| 69 | 5 | return $self->{_rule}; | |||||||||||||||||||
| 70 | } | ||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | #----------------------------------------------------------------------------- | ||||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | 13485 | ▶ | sub policy_is_thematic { | ||||||||||||||||||
|
Subroutine
policy_is_thematic: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | 13485 | my ($self, %args) = @_; | |||||||||||||||||||
| 77 | my $policy = $args{-policy} | ||||||||||||||||||||
| 78 | 13485 | ▶ | || throw_internal 'The -policy argument is required'; | ||||||||||||||||||
|
Condition33%1/3
$args{'-policy'} || throw_internal('The -policy argument is required')
MC/DC0%0/2
$args{'-policy'} || throw_internal('The -policy argument is required')
$args{'-policy'}
throw_internal('The -policy argument is required')
Truth table33%1/3
A || B
A= $args{'-policy'}B= throw_internal('The -policy argument is required')
| |||||||||||||||||||||
| 79 | 13485 | ▶ | ref $policy | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 80 | || throw_internal 'The -policy must be an object'; | ||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | 13485 | ▶ | my $rule = $self->{_rule} or return 1; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 83 | 10006 | my %themes = hashify( $policy->get_themes() ); | |||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | # This bit of magic turns the rule into a perl expression that can be | ||||||||||||||||||||
| 86 | # eval-ed for truth. Each theme name in the rule is translated to 1 or 0 | ||||||||||||||||||||
| 87 | # if the $policy belongs in that theme. For example: | ||||||||||||||||||||
| 88 | # | ||||||||||||||||||||
| 89 | # 'bugs && (pbp || core)' ...could become... '1 && (0 || 1)' | ||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | 10006 | my $as_code = $rule; #Making a copy, so $rule is preserved | |||||||||||||||||||
| 92 | 10006 | ▶ | $as_code =~ s/ ( [\w\d]+ ) /exists $themes{$1} || 0/gexms; | ||||||||||||||||||
|
Condition100%2/2
exists $themes{$1} || 0
MC/DC100%1/1
exists $themes{$1} || 0
exists $themes{$1}
Truth table100%2/2
A
A= exists $themes{$1}
| |||||||||||||||||||||
| 93 | 10006 | my $is_thematic = eval $as_code; ## no critic (ProhibitStringyEval) | |||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | 10006 | ▶ | if ($EVAL_ERROR) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 96 | 1 | throw_global_value | |||||||||||||||||||
| 97 | option_name => $CONFIG_KEY, | ||||||||||||||||||||
| 98 | option_value => $rule, | ||||||||||||||||||||
| 99 | message_suffix => q{contains a syntax error.}; | ||||||||||||||||||||
| 100 | } | ||||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | 10005 | return $is_thematic; | |||||||||||||||||||
| 103 | } | ||||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | #----------------------------------------------------------------------------- | ||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | 609 | ▶ | sub cook_rule { | ||||||||||||||||||
|
Subroutine
cook_rule: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 108 | 609 | my ($raw_rule) = @_; | |||||||||||||||||||
| 109 | 609 | ▶ | return if not defined $raw_rule; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | #Translate logical operators | ||||||||||||||||||||
| 112 | 609 | $raw_rule =~ s{\b not \b}{!}ixmsg; # "not" -> "!" | |||||||||||||||||||
| 113 | 609 | $raw_rule =~ s{\b and \b}{&&}ixmsg; # "and" -> "&&" | |||||||||||||||||||
| 114 | 609 | $raw_rule =~ s{\b or \b}{||}ixmsg; # "or" -> "||" | |||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | #Translate algebra operators (for backward compatibility) | ||||||||||||||||||||
| 117 | 609 | $raw_rule =~ s{\A [-] }{!}ixmsg; # "-" -> "!" e.g. difference | |||||||||||||||||||
| 118 | 609 | $raw_rule =~ s{ [-] }{&& !}ixmsg; # "-" -> "&& !" e.g. difference | |||||||||||||||||||
| 119 | 609 | $raw_rule =~ s{ [*] }{&&}ixmsg; # "*" -> "&&" e.g. intersection | |||||||||||||||||||
| 120 | 609 | $raw_rule =~ s{ [+] }{||}ixmsg; # "+" -> "||" e.g. union | |||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | 609 | my $cooked_rule = lc $raw_rule; #Is now cooked! | |||||||||||||||||||
| 123 | 609 | return $cooked_rule; | |||||||||||||||||||
| 124 | } | ||||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | 40 | 1; | |||||||||||||||||||
| 128 | |||||||||||||||||||||
| 129 | __END__ | ||||||||||||||||||||