| 1 | package Perl::Critic::Policy::ValuesAndExpressions::ProhibitDefinedBeforeLength; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 2 | $Perl::Critic::Policy::ValuesAndExpressions::ProhibitDefinedBeforeLength::VERSION = '0.002'; | |||||||||||||||||||||||||||||||||||||||||
| 3 | # ABSTRACT: Test the value, not its length, and shape it before measuring it. | ||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use warnings FATAL => 'all'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use 5.014; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use re '/aa'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use Readonly; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | 2 | ▶ | use Perl::Critic::Utils qw{ :severities is_hash_key is_method_call }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | 2 | ▶ | use parent qw{Perl::Critic::Policy}; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | 2 | Readonly::Scalar my $PAIR_DESC => 'defined before length on the same value'; | |||||||||||||||||||||||||||||||||||||||||
| 20 | 2 | Readonly::Scalar my $PAIR_EXPL => 'In a boolean context use the value alone; where the length matters, shape the value first ($x //= q{})'; | |||||||||||||||||||||||||||||||||||||||||
| 21 | 2 | Readonly::Scalar my $BARE_DESC => 'length in a boolean context'; | |||||||||||||||||||||||||||||||||||||||||
| 22 | 2 | Readonly::Scalar my $BARE_EXPL => 'The value alone is false for undef and the empty string, and needs no call'; | |||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | # The operators that join the two halves of a pair, positive and negated. | ||||||||||||||||||||||||||||||||||||||||||
| 25 | 2 | Readonly::Hash my %AND => map { $_ => 1 } qw{&& and}; | |||||||||||||||||||||||||||||||||||||||||
| 26 | 2 | Readonly::Hash my %OR => map { $_ => 1 } qw{|| or}; | |||||||||||||||||||||||||||||||||||||||||
| 27 | 2 | Readonly::Hash my %NOT => map { $_ => 1 } ( q{!}, 'not' ); | |||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||
| 29 | # The operators a condition is built from. A length joined to others by these | ||||||||||||||||||||||||||||||||||||||||||
| 30 | # is still a condition if the whole chain is one. | ||||||||||||||||||||||||||||||||||||||||||
| 31 | 2 | Readonly::Hash my %LOGICAL => map { $_ => 1 } ( qw{&& || // and or xor not}, q{!} ); | |||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | # The operators before which a chain is a value rather than a condition: an | ||||||||||||||||||||||||||||||||||||||||||
| 34 | # assignment takes it, a comma passes it, and ?: picks it as a branch. Any | ||||||||||||||||||||||||||||||||||||||||||
| 35 | # other operator to the left belongs to another operand of the chain. | ||||||||||||||||||||||||||||||||||||||||||
| 36 | 2 | Readonly::Hash my %VALUE_STARTS => map { $_ => 1 } ( qw{= += -= *= /= .= %= x= **= //= ||= &&= |= &= ^= <<= >>= => ? :}, q{,} ); | |||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | # What makes the rest of a statement its condition. | ||||||||||||||||||||||||||||||||||||||||||
| 39 | 2 | Readonly::Hash my %MODIFIER => map { $_ => 1 } qw{if unless while until}; | |||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | # The list functions whose block is a test of each element. | ||||||||||||||||||||||||||||||||||||||||||
| 42 | 2 | Readonly::Hash my %TESTS_EACH => map { $_ => 1 } qw{grep first any all none notall}; | |||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | 2 | ▶ | sub supported_parameters { return () } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
supported_parameters: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | 51 | ▶ | sub default_severity { return $SEVERITY_LOW } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
default_severity: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | 0 | ▶ | sub default_themes { return qw{troglodyne performance maintenance} } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
default_themes: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | 73 | ▶ | sub applies_to { return 'PPI::Token::Word' } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
applies_to: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||
| 49 | 247 | ▶ | sub violates { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
violates: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | 247 | my ( $self, $elem, undef ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||
| 52 | 247 | ▶ | return () if is_hash_key($elem) || is_method_call($elem); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
is_hash_key($elem) or is_method_call($elem)
MC/DC100%2/2
is_hash_key($elem) or is_method_call($elem)
is_hash_key($elem)
is_method_call($elem)
Truth table100%3/3
A or B
A= is_hash_key($elem)B= is_method_call($elem)
| |||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | 230 | ▶ | if ( $elem->content eq 'defined' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | 25 | ▶ | return () unless _pair($elem); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | 21 | return $self->violation( $PAIR_DESC, $PAIR_EXPL, $elem ); | |||||||||||||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | 205 | ▶ | return () unless $elem->content eq 'length'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | 70 | ▶ | return () if _second_half_of_a_pair($elem); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | 49 | my ( undef, $after ) = _operand($elem); | |||||||||||||||||||||||||||||||||||||||||
| 63 | 49 | ▶ | return () unless _is_boolean( $elem, $after ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | 30 | return $self->violation( $BARE_DESC, $BARE_EXPL, $elem ); | |||||||||||||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | # The length a defined is paired with, or nothing: `defined $x && length $x`, | ||||||||||||||||||||||||||||||||||||||||||
| 68 | # or its negation `!defined $x || !length $x`, on the same operand. | ||||||||||||||||||||||||||||||||||||||||||
| 69 | 49 | ▶ | sub _pair { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_pair: called CC=11 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | 49 | my ($defined) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | 49 | my $negated = _is_not( $defined->sprevious_sibling ); | |||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | 49 | my ( $operand, $after ) = _operand($defined); | |||||||||||||||||||||||||||||||||||||||||
| 75 | 49 | ▶ | return unless $after && $after->isa('PPI::Token::Operator'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$after and $after->isa("PPI::Token::Operator")
MC/DC0%0/2
$after and $after->isa("PPI::Token::Operator")
$after
$after->isa("PPI::Token::Operator")
Truth table33%1/3
A and B
A= $afterB= $after->isa("PPI::Token::Operator")
| |||||||||||||||||||||||||||||||||||||||||||
| 76 | 49 | ▶ | return unless $negated ? $OR{ $after->content } : $AND{ $after->content }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | 49 | my $length = $after->snext_sibling; | |||||||||||||||||||||||||||||||||||||||||
| 79 | 49 | ▶ | if ($negated) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | 4 | ▶ | return unless _is_not($length); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 81 | 4 | $length = $length->snext_sibling; | |||||||||||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||||||||||||
| 83 | 49 | ▶ | return unless $length && $length->isa('PPI::Token::Word') && $length->content eq 'length'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
$length and $length->isa("PPI::Token::Word") and $length->content eq "length"
$length and $length->isa("PPI::Token::Word")
MC/DC33%1/3
$length and $length->isa("PPI::Token::Word") and $length->content eq "length"
$length
$length->isa("PPI::Token::Word")
$length->content eq "length"
Truth table50%2/4
A and B and C
A= $lengthB= $length->isa("PPI::Token::Word")C= $length->content eq "length"
| |||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | 48 | my ($measured) = _operand($length); | |||||||||||||||||||||||||||||||||||||||||
| 86 | 48 | ▶ | return unless $measured eq $operand; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 87 | 42 | return $length; | |||||||||||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||
| 90 | # Reported already, as the pair. | ||||||||||||||||||||||||||||||||||||||||||
| 91 | 70 | ▶ | sub _second_half_of_a_pair { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_second_half_of_a_pair: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 92 | 70 | my ($length) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | 70 | my $sibling = $length->sprevious_sibling; | |||||||||||||||||||||||||||||||||||||||||
| 95 | 70 | ▶ | while ($sibling) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | 203 | ▶ | if ( $sibling->isa('PPI::Token::Word') && $sibling->content eq 'defined' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$sibling->isa("PPI::Token::Word") and $sibling->content eq "defined"
MC/DC100%2/2
$sibling->isa("PPI::Token::Word") and $sibling->content eq "defined"
$sibling->isa("PPI::Token::Word")
$sibling->content eq "defined"
Truth table100%3/3
A and B
A= $sibling->isa("PPI::Token::Word")B= $sibling->content eq "defined"
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | 24 | my $paired = _pair($sibling); | |||||||||||||||||||||||||||||||||||||||||
| 98 | 24 | ▶ | return 1 if $paired && $paired == $length; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$paired and $paired == $length
MC/DC50%1/2
$paired and $paired == $length
$paired
$paired == $length
Truth table66%2/3
A and B
A= $pairedB= $paired == $length
| |||||||||||||||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||||||||||||
| 100 | 182 | $sibling = $sibling->sprevious_sibling; | |||||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||||
| 102 | 49 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | # Whether the value of a term is only tested for truth. $start is the term's | ||||||||||||||||||||||||||||||||||||||||||
| 106 | # first element and $after the first sibling past it. | ||||||||||||||||||||||||||||||||||||||||||
| 107 | 52 | ▶ | sub _is_boolean { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_is_boolean: called CC=23 SCAR=31.9
| |||||||||||||||||||||||||||||||||||||||||||
| 108 | 52 | my ( $start, $after ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | 52 | my $before = $start->sprevious_sibling; | |||||||||||||||||||||||||||||||||||||||||
| 111 | 52 | ▶ | return 1 if _is_not($before); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||
| 113 | # What the term is used for, from what is right beside it: `?` tests it, | ||||||||||||||||||||||||||||||||||||||||||
| 114 | # and any other operator that is not logical uses its value. | ||||||||||||||||||||||||||||||||||||||||||
| 115 | 49 | ▶ | if ( $after && $after->isa('PPI::Token::Operator') ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$after and $after->isa("PPI::Token::Operator")
MC/DC100%2/2
$after and $after->isa("PPI::Token::Operator")
$after
$after->isa("PPI::Token::Operator")
Truth table100%3/3
A and B
A= $afterB= $after->isa("PPI::Token::Operator")
| |||||||||||||||||||||||||||||||||||||||||||
| 116 | 13 | ▶ | return 1 if $after->content eq q{?}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 117 | 9 | ▶ | return 0 unless $LOGICAL{ $after->content }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||||||||||||
| 119 | 40 | ▶ | return 0 if $before && $before->isa('PPI::Token::Operator') && !$LOGICAL{ $before->content }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$before and $before->isa("PPI::Token::Operator") and not $LOGICAL{$before->content}
$before and $before->isa("PPI::Token::Operator")
MC/DC100%3/3
$before and $before->isa("PPI::Token::Operator") and not $LOGICAL{$before->content}
$before
$before->isa("PPI::Token::Operator")
not $LOGICAL{$before->content}
Truth table100%4/4
A and B and not(C)
A= $beforeB= $before->isa("PPI::Token::Operator")C= not $LOGICAL{$before->content}
| |||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | # Then along the chain of logical operators it is part of. | ||||||||||||||||||||||||||||||||||||||||||
| 122 | 37 | my $left = $before; | |||||||||||||||||||||||||||||||||||||||||
| 123 | 37 | ▶ | while ($left) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 124 | 59 | ▶ | return 1 if $left->isa('PPI::Token::Word') && $MODIFIER{ $left->content }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$left->isa("PPI::Token::Word") and $MODIFIER{$left->content}
MC/DC100%2/2
$left->isa("PPI::Token::Word") and $MODIFIER{$left->content}
$left->isa("PPI::Token::Word")
$MODIFIER{$left->content}
Truth table100%3/3
A and B
A= $left->isa("PPI::Token::Word")B= $MODIFIER{$left->content}
| |||||||||||||||||||||||||||||||||||||||||||
| 125 | 46 | ▶ | return 0 if $left->isa('PPI::Token::Word') && $left->content eq 'return'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$left->isa("PPI::Token::Word") and $left->content eq "return"
MC/DC100%2/2
$left->isa("PPI::Token::Word") and $left->content eq "return"
$left->isa("PPI::Token::Word")
$left->content eq "return"
Truth table100%3/3
A and B
A= $left->isa("PPI::Token::Word")B= $left->content eq "return"
| |||||||||||||||||||||||||||||||||||||||||||
| 126 | 43 | ▶ | return 0 if $left->isa('PPI::Token::Operator') && $VALUE_STARTS{ $left->content }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$left->isa("PPI::Token::Operator") and $VALUE_STARTS{$left->content}
MC/DC100%2/2
$left->isa("PPI::Token::Operator") and $VALUE_STARTS{$left->content}
$left->isa("PPI::Token::Operator")
$VALUE_STARTS{$left->content}
Truth table100%3/3
A and B
A= $left->isa("PPI::Token::Operator")B= $VALUE_STARTS{$left->content}
| |||||||||||||||||||||||||||||||||||||||||||
| 127 | 39 | $left = $left->sprevious_sibling; | |||||||||||||||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||
| 130 | 17 | my $right = $after; | |||||||||||||||||||||||||||||||||||||||||
| 131 | 17 | ▶ | while ($right) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 132 | 1 | ▶ | return 1 if $right->isa('PPI::Token::Operator') && $right->content eq q{?}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$right->isa("PPI::Token::Operator") and $right->content eq "?"
MC/DC0%0/2
$right->isa("PPI::Token::Operator") and $right->content eq "?"
$right->isa("PPI::Token::Operator")
$right->content eq "?"
Truth table33%1/3
A and B
A= $right->isa("PPI::Token::Operator")B= $right->content eq "?"
| |||||||||||||||||||||||||||||||||||||||||||
| 133 | 1 | ▶ | last if $right->isa('PPI::Token::Operator') && $right->content =~ m/\A(?:,|=>|:)\z/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$right->isa("PPI::Token::Operator") and $right->content =~ /\A(?:,|=>|:)\z/aa
MC/DC0%0/2
$right->isa("PPI::Token::Operator") and $right->content =~ /\A(?:,|=>|:)\z/aa
$right->isa("PPI::Token::Operator")
$right->content =~ /\A(?:,|=>|:)\z/aa
Truth table33%1/3
A and B
A= $right->isa("PPI::Token::Operator")B= $right->content =~ /\A(?:,|=>|:)\z/aa
| |||||||||||||||||||||||||||||||||||||||||||
| 134 | 1 | ▶ | last if $right->isa('PPI::Token::Word') && $MODIFIER{ $right->content }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$right->isa("PPI::Token::Word") and $MODIFIER{$right->content}
MC/DC0%0/2
$right->isa("PPI::Token::Word") and $MODIFIER{$right->content}
$right->isa("PPI::Token::Word")
$MODIFIER{$right->content}
Truth table33%1/3
A and B
A= $right->isa("PPI::Token::Word")B= $MODIFIER{$right->content}
| |||||||||||||||||||||||||||||||||||||||||||
| 135 | 0 | $right = $right->snext_sibling; | |||||||||||||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||
| 138 | 17 | return _is_boolean_place($start); | |||||||||||||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||
| 141 | # The chain reached the edge of its statement, so the statement's place decides. | ||||||||||||||||||||||||||||||||||||||||||
| 142 | 17 | ▶ | sub _is_boolean_place { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_is_boolean_place: called CC=13 SCAR=26.2
| |||||||||||||||||||||||||||||||||||||||||||
| 143 | 17 | my ($start) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||
| 145 | 17 | ▶ | my $statement = $start->parent or return 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 146 | 17 | ▶ | my $holder = $statement->parent or return 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||
| 148 | 17 | ▶ | return 1 if $holder->isa('PPI::Structure::Condition'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||
| 150 | # Parenthesised: the list is the term, one level out. After return it is a | ||||||||||||||||||||||||||||||||||||||||||
| 151 | # value like any other term. After any other word, it is the argument list | ||||||||||||||||||||||||||||||||||||||||||
| 152 | # of a call. | ||||||||||||||||||||||||||||||||||||||||||
| 153 | 11 | ▶ | if ( $holder->isa('PPI::Structure::List') ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 154 | 5 | my $before = $holder->sprevious_sibling; | |||||||||||||||||||||||||||||||||||||||||
| 155 | 5 | ▶ | return 0 if $before && $before->isa('PPI::Token::Word') && $before->content ne 'return'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$before and $before->isa("PPI::Token::Word") and $before->content ne "return"
$before and $before->isa("PPI::Token::Word")
MC/DC66%2/3
$before and $before->isa("PPI::Token::Word") and $before->content ne "return"
$before
$before->isa("PPI::Token::Word")
$before->content ne "return"
Truth table75%3/4
A and B and C
A= $beforeB= $before->isa("PPI::Token::Word")C= $before->content ne "return"
| |||||||||||||||||||||||||||||||||||||||||||
| 156 | 3 | return _is_boolean( $holder, $holder->snext_sibling ); | |||||||||||||||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||
| 159 | # The last statement of a grep block is its test. | ||||||||||||||||||||||||||||||||||||||||||
| 160 | 6 | ▶ | if ( $holder->isa('PPI::Structure::Block') ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 161 | 5 | my $call = $holder->sprevious_sibling; | |||||||||||||||||||||||||||||||||||||||||
| 162 | 5 | ▶ | return 0 unless $call && $call->isa('PPI::Token::Word') && $TESTS_EACH{ $call->content }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
$call and $call->isa("PPI::Token::Word") and $TESTS_EACH{$call->content}
$call and $call->isa("PPI::Token::Word")
MC/DC33%1/3
$call and $call->isa("PPI::Token::Word") and $TESTS_EACH{$call->content}
$call
$call->isa("PPI::Token::Word")
$TESTS_EACH{$call->content}
Truth table50%2/4
A and B and C
A= $callB= $call->isa("PPI::Token::Word")C= $TESTS_EACH{$call->content}
| |||||||||||||||||||||||||||||||||||||||||||
| 163 | 4 | my @statements = $holder->schildren; | |||||||||||||||||||||||||||||||||||||||||
| 164 | 4 | ▶ | return $statements[-1] == $statement ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||||||
| 167 | 1 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||||||||||
| 170 | # The operand of a named unary op, as text with the whitespace taken out, and | ||||||||||||||||||||||||||||||||||||||||||
| 171 | # the first sibling after it. defined($x) is the list's contents; defined $x | ||||||||||||||||||||||||||||||||||||||||||
| 172 | # runs until an operator other than -> or anything that cannot be part of a | ||||||||||||||||||||||||||||||||||||||||||
| 173 | # term. No operand at all is $_, as perl reads it. | ||||||||||||||||||||||||||||||||||||||||||
| 174 | 146 | ▶ | sub _operand { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_operand: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||||||
| 175 | 146 | my ($op) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||||||||
| 177 | 146 | my $next = $op->snext_sibling; | |||||||||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||||||||
| 179 | 146 | ▶ | if ( $next && $next->isa('PPI::Structure::List') ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$next and $next->isa("PPI::Structure::List")
MC/DC100%2/2
$next and $next->isa("PPI::Structure::List")
$next
$next->isa("PPI::Structure::List")
Truth table100%3/3
A and B
A= $nextB= $next->isa("PPI::Structure::List")
| |||||||||||||||||||||||||||||||||||||||||||
| 180 | 9 | my $inside = join( q{}, map { $_->content } $next->schildren ); | |||||||||||||||||||||||||||||||||||||||||
| 181 | 9 | ▶ | return ( _squash( length $inside ? $inside : '$_' ), $next->snext_sibling ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||||||||
| 184 | 137 | my @term; | |||||||||||||||||||||||||||||||||||||||||
| 185 | 137 | ▶ | while ( $next && _continues_term( $next, \@term ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$next and _continues_term($next, \@term)
MC/DC100%2/2
$next and _continues_term($next, \@term)
$next
_continues_term($next, \@term)
Truth table100%3/3
A and B
A= $nextB= _continues_term($next, \@term)
| |||||||||||||||||||||||||||||||||||||||||||
| 186 | 197 | push( @term, $next ); | |||||||||||||||||||||||||||||||||||||||||
| 187 | 197 | $next = $next->snext_sibling; | |||||||||||||||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||
| 190 | 137 | my $text = join( q{}, map { $_->content } @term ); | |||||||||||||||||||||||||||||||||||||||||
| 191 | 137 | ▶ | return ( _squash( length $text ? $text : '$_' ), $next ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||||||||||||
| 194 | # A term is a variable, a quoted string or a call, then any run of subscripts, | ||||||||||||||||||||||||||||||||||||||||||
| 195 | # derefs, and method calls with their arguments. A word is part of it only as | ||||||||||||||||||||||||||||||||||||||||||
| 196 | # a function or method name. | ||||||||||||||||||||||||||||||||||||||||||
| 197 | 312 | ▶ | sub _continues_term { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_continues_term: called CC=14 SCAR=27.0
| |||||||||||||||||||||||||||||||||||||||||||
| 198 | 312 | my ( $elem, $term ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||||||||
| 200 | 312 | my $last = $term->[-1]; | |||||||||||||||||||||||||||||||||||||||||
| 201 | 312 | ▶ | return _starts_term($elem) unless $last; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||||||||||||
| 203 | 179 | ▶ | return 1 if $elem->isa('PPI::Structure::Subscript'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 204 | 152 | ▶ | return 1 if $elem->isa('PPI::Token::Operator') && $elem->content eq '->'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$elem->isa("PPI::Token::Operator") and $elem->content eq "->"
MC/DC100%2/2
$elem->isa("PPI::Token::Operator") and $elem->content eq "->"
$elem->isa("PPI::Token::Operator")
$elem->content eq "->"
Truth table100%3/3
A and B
A= $elem->isa("PPI::Token::Operator")B= $elem->content eq "->"
| |||||||||||||||||||||||||||||||||||||||||||
| 205 | 128 | ▶ | return 1 if $last->isa('PPI::Token::Cast') && ( $elem->isa('PPI::Token::Symbol') || $elem->isa('PPI::Structure::Block') ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
$last->isa("PPI::Token::Cast") and $elem->isa("PPI::Token::Symbol") || $elem->isa("PPI::Structure::Block")
$elem->isa("PPI::Token::Symbol") or $elem->isa("PPI::Structure::Block")
MC/DC33%1/3
$last->isa("PPI::Token::Cast") and $elem->isa("PPI::Token::Symbol") || $elem->isa("PPI::Structure::Block")
$last->isa("PPI::Token::Cast")
$elem->isa("PPI::Token::Symbol")
$elem->isa("PPI::Structure::Block")
Truth table50%2/4
A and B or C
A= $last->isa("PPI::Token::Cast")B= $elem->isa("PPI::Token::Symbol")C= $elem->isa("PPI::Structure::Block")
| |||||||||||||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||||||||||||
| 207 | 124 | ▶ | my $after_arrow = $last->isa('PPI::Token::Operator') && $last->content eq '->'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$last->isa('PPI::Token::Operator') && $last->content eq '->'
MC/DC50%1/2
$last->isa('PPI::Token::Operator') && $last->content eq '->'
$last->isa('PPI::Token::Operator')
$last->content eq '->'
Truth table66%2/3
A && B
A= $last->isa('PPI::Token::Operator')B= $last->content eq '->'
| |||||||||||||||||||||||||||||||||||||||||||
| 208 | 124 | ▶ | return 1 if $after_arrow && ( $elem->isa('PPI::Token::Word') || $elem->isa('PPI::Structure::List') ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
$after_arrow and $elem->isa("PPI::Token::Word") || $elem->isa("PPI::Structure::List")
$elem->isa("PPI::Token::Word") or $elem->isa("PPI::Structure::List")
MC/DC33%1/3
$after_arrow and $elem->isa("PPI::Token::Word") || $elem->isa("PPI::Structure::List")
$after_arrow
$elem->isa("PPI::Token::Word")
$elem->isa("PPI::Structure::List")
Truth table50%2/4
A and B or C
A= $after_arrowB= $elem->isa("PPI::Token::Word")C= $elem->isa("PPI::Structure::List")
| |||||||||||||||||||||||||||||||||||||||||||
| 209 | 119 | ▶ | return 1 if $last->isa('PPI::Token::Word') && $elem->isa('PPI::Structure::List'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$last->isa("PPI::Token::Word") and $elem->isa("PPI::Structure::List")
MC/DC100%2/2
$last->isa("PPI::Token::Word") and $elem->isa("PPI::Structure::List")
$last->isa("PPI::Token::Word")
$elem->isa("PPI::Structure::List")
Truth table100%3/3
A and B
A= $last->isa("PPI::Token::Word")B= $elem->isa("PPI::Structure::List")
| |||||||||||||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||||||||||||
| 211 | 113 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||||||||||||||||
| 214 | 133 | ▶ | sub _starts_term { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_starts_term: called CC=8 SCAR=21.2
| |||||||||||||||||||||||||||||||||||||||||||
| 215 | 133 | my ($elem) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||||||||||||
| 217 | 133 | ▶ | return 1 if $elem->isa('PPI::Token::Symbol') || $elem->isa('PPI::Token::Magic') || $elem->isa('PPI::Token::Cast'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$elem->isa("PPI::Token::Symbol") or $elem->isa("PPI::Token::Magic") or $elem->isa("PPI::Token::Cast")
$elem->isa("PPI::Token::Symbol") or $elem->isa("PPI::Token::Magic")
MC/DC66%2/3
$elem->isa("PPI::Token::Symbol") or $elem->isa("PPI::Token::Magic") or $elem->isa("PPI::Token::Cast")
$elem->isa("PPI::Token::Symbol")
$elem->isa("PPI::Token::Magic")
$elem->isa("PPI::Token::Cast")
Truth table75%3/4
A or B or C
A= $elem->isa("PPI::Token::Symbol")B= $elem->isa("PPI::Token::Magic")C= $elem->isa("PPI::Token::Cast")
| |||||||||||||||||||||||||||||||||||||||||||
| 218 | 8 | ▶ | return 1 if $elem->isa('PPI::Token::Quote'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 219 | 8 | ▶ | return 1 if $elem->isa('PPI::Token::Word') && !$MODIFIER{ $elem->content } && $elem->content !~ m/\A(?:for|foreach|x)\z/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
$elem->isa("PPI::Token::Word") and not $MODIFIER{$elem->content} and not $elem->content =~ /\A(?:for|foreach|x)\z/aa
$elem->isa("PPI::Token::Word") and not $MODIFIER{$elem->content}
MC/DC33%1/3
$elem->isa("PPI::Token::Word") and not $MODIFIER{$elem->content} and not $elem->content =~ /\A(?:for|foreach|x)\z/aa
$elem->isa("PPI::Token::Word")
not $MODIFIER{$elem->content}
not $elem->content =~ /\A(?:for|foreach|x)\z/aa
Truth table50%2/4
A and not(B) and not(C)
A= $elem->isa("PPI::Token::Word")B= not $MODIFIER{$elem->content}C= not $elem->content =~ /\A(?:for|foreach|x)\z/aa
| |||||||||||||||||||||||||||||||||||||||||||
| 220 | 2 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||||||||||||
| 223 | 105 | ▶ | sub _is_not { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_is_not: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 224 | 105 | my ($elem) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 225 | 105 | ▶ | return $elem && $elem->isa('PPI::Token::Operator') && $NOT{ $elem->content }; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%6/6
$elem && $elem->isa('PPI::Token::Operator') && $NOT{$elem->content}
$elem and $elem->isa("PPI::Token::Operator")
MC/DC100%3/3
$elem && $elem->isa('PPI::Token::Operator') && $NOT{$elem->content}
$elem
$elem->isa("PPI::Token::Operator")
$NOT{$elem->content}
Truth table100%4/4
A and B && C
A= $elemB= $elem->isa("PPI::Token::Operator")C= $NOT{$elem->content}
| |||||||||||||||||||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||||||||||||
| 228 | 146 | ▶ | sub _squash { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_squash: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 229 | 146 | my ($text) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 230 | 146 | $text =~ s/\s+//g; | |||||||||||||||||||||||||||||||||||||||||
| 231 | 146 | return $text; | |||||||||||||||||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||||||||||||
| 234 | 2 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||||||||||||
| 236 | __END__ | ||||||||||||||||||||||||||||||||||||||||||