« lib/Perl/Critic/Policy/CodeLayout/ProhibitHardTabs.pm
↑ Summary
lib/Perl/Critic/Policy/Modules/RequireExplicitPackage.pm »
| 1 | package Perl::Critic::Policy::ControlStructures::ProhibitNegativeExpressionsInUnlessAndUntilConditions; | ||||||||||||||||||||||||||||
| 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 Readonly; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 40 | ▶ | use Perl::Critic::Utils qw< :characters :severities :classification hashify >; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 40 | ▶ | use parent 'Perl::Critic::Policy'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 40 | our $VERSION = '1.156'; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 40 | Readonly::Scalar my $EXPL => [99]; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 90 | ▶ | sub supported_parameters { return qw< > } | ||||||||||||||||||||||||||
|
Subroutine
supported_parameters: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 21 | 74 | ▶ | sub default_severity { return $SEVERITY_MEDIUM } | ||||||||||||||||||||||||||
|
Subroutine
default_severity: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 22 | 85 | ▶ | sub default_themes { return qw( core maintenance pbp ) } | ||||||||||||||||||||||||||
|
Subroutine
default_themes: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 23 | 31 | ▶ | sub applies_to { return 'PPI::Token::Word' } | ||||||||||||||||||||||||||
|
Subroutine
applies_to: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 332 | ▶ | sub violates { | ||||||||||||||||||||||||||
|
Subroutine
violates: called CC=6 SCAR=19.0
Pod
documented
| |||||||||||||||||||||||||||||
| 28 | 332 | my ( $self, $token, undef ) = @_; | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 332 | state $until_or_unless = { hashify( qw( until unless ) ) }; | |||||||||||||||||||||||||||
| 31 | 332 | ▶ | return if !exists $until_or_unless->{$token->content}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 18 | ▶ | return if is_hash_key($token); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | 18 | ▶ | return if is_subroutine_name($token); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | 18 | ▶ | return if is_method_call($token); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | 18 | ▶ | return if is_included_module_name($token); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | return | ||||||||||||||||||||||||||||
| 39 | map | ||||||||||||||||||||||||||||
| 40 | 0 | { $self->_violation_for_operator( $_, $token ) } | |||||||||||||||||||||||||||
| 41 | _get_negative_operators( $token ); | ||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 18 | ▶ | sub _get_negative_operators { | ||||||||||||||||||||||||||
|
Subroutine
_get_negative_operators: called CC=5 SCAR=17.3
| |||||||||||||||||||||||||||||
| 47 | 18 | my ($token) = @_; | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 18 | my @operators; | |||||||||||||||||||||||||||
| 50 | 18 | foreach my $element ( _get_condition_elements($token) ) { | |||||||||||||||||||||||||||
| 51 | 48 | ▶ | if ( $element->isa('PPI::Node') ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 2 | my $operators = $element->find( \&_is_negative_operator ); | |||||||||||||||||||||||||||
| 53 | 2 | ▶ | if ($operators) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 0 | push @operators, @{$operators}; | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | else { | ||||||||||||||||||||||||||||
| 58 | 46 | ▶ | if ( _is_negative_operator( undef, $element ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 0 | push @operators, $element; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 18 | return @operators; | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 18 | ▶ | sub _get_condition_elements { | ||||||||||||||||||||||||||
|
Subroutine
_get_condition_elements: called CC=7 SCAR=19.6
| |||||||||||||||||||||||||||||
| 70 | 18 | my ($token) = @_; | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 18 | my $statement = $token->statement(); | |||||||||||||||||||||||||||
| 73 | 18 | ▶ | return if not $statement; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 18 | ▶ | if ($statement->isa('PPI::Statement::Compound')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 2 | my $condition = $token->snext_sibling(); | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 2 | ▶ | return if not $condition; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 2 | ▶ | return if not $condition->isa('PPI::Structure::Condition'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 2 | return ( $condition ); | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 16 | my @condition_elements; | |||||||||||||||||||||||||||
| 85 | 16 | my $element = $token; | |||||||||||||||||||||||||||
| 86 | 16 | ▶ | while ( | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$element = $element->snext_sibling and $element->content ne $Perl::Critic::Policy::ControlStructures::ProhibitNegativeExpressionsInUnlessAndUntilConditions::SCOLON
MC/DC100%2/2
$element = $element->snext_sibling and $element->content ne $Perl::Critic::Policy::ControlStructures::ProhibitNegativeExpressionsInUnlessAndUntilConditions::SCOLON
$element = $element->snext_sibling
$element->content ne $Perl::Critic::Policy::ControlStructures::ProhibitNegativeExpressionsInUnlessAndUntilConditions::SCOLON
Truth table100%3/3
A and B
A= $element = $element->snext_siblingB= $element->content ne $Perl::Critic::Policy::ControlStructures::ProhibitNegativeExpressionsInUnlessAndUntilConditions::SCOLON
| |||||||||||||||||||||||||||||
| 87 | $element = $element->snext_sibling() | ||||||||||||||||||||||||||||
| 88 | and $element->content() ne $SCOLON | ||||||||||||||||||||||||||||
| 89 | ) { | ||||||||||||||||||||||||||||
| 90 | 46 | push @condition_elements, $element; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 16 | return @condition_elements; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 40 | Readonly::Hash my %NEGATIVE_OPERATORS => hashify( | |||||||||||||||||||||||||||
| 99 | qw/ | ||||||||||||||||||||||||||||
| 100 | ! not | ||||||||||||||||||||||||||||
| 101 | !~ ne != | ||||||||||||||||||||||||||||
| 102 | < > <= >= <=> | ||||||||||||||||||||||||||||
| 103 | lt gt le ge cmp | ||||||||||||||||||||||||||||
| 104 | / | ||||||||||||||||||||||||||||
| 105 | ); | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 64 | ▶ | sub _is_negative_operator { | ||||||||||||||||||||||||||
|
Subroutine
_is_negative_operator: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||
| 108 | 64 | my (undef, $element) = @_; | |||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | return | ||||||||||||||||||||||||||||
| 111 | $element->isa('PPI::Token::Operator') | ||||||||||||||||||||||||||||
| 112 | 64 | ▶ | && $NEGATIVE_OPERATORS{$element}; | ||||||||||||||||||||||||||
|
Condition66%2/3
$element->isa('PPI::Token::Operator') && $NEGATIVE_OPERATORS{$element}
MC/DC0%0/2
$element->isa('PPI::Token::Operator') && $NEGATIVE_OPERATORS{$element}
$element->isa('PPI::Token::Operator')
$NEGATIVE_OPERATORS{$element}
Truth table66%2/3
A && B
A= $element->isa('PPI::Token::Operator')B= $NEGATIVE_OPERATORS{$element}
| |||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 0 | ▶ | sub _violation_for_operator { | ||||||||||||||||||||||||||
|
Subroutine
_violation_for_operator: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 118 | 0 | my ($self, $operator, $control_structure) = @_; | |||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | return | ||||||||||||||||||||||||||||
| 121 | 0 | $self->violation( | |||||||||||||||||||||||||||
| 122 | qq<Found "$operator" in condition for an "$control_structure">, | ||||||||||||||||||||||||||||
| 123 | $EXPL, | ||||||||||||||||||||||||||||
| 124 | $control_structure, | ||||||||||||||||||||||||||||
| 125 | ); | ||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 40 | 1; | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | __END__ | ||||||||||||||||||||||||||||