« lib/Perl/Critic/Policy/Documentation/RequirePackageMatchesPodName.pm
↑ Summary
lib/Perl/Critic/Policy/BuiltinFunctions/ProhibitSleepViaSelect.pm »
| 1 | package Perl::Critic::Policy::ValuesAndExpressions::ProhibitCommaSeparatedStatements; | ||||||||||||||||||||||||||||
| 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 | |||||||||||||||||||||||||||||
| 9 | 40 | ▶ | use Perl::Critic::Utils qw{ :booleans :characters :severities :classification hashify }; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 40 | ▶ | use Perl::Critic::Utils::PPI qw{ is_ppi_statement_subclass }; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 40 | ▶ | use parent 'Perl::Critic::Policy'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 40 | our $VERSION = '1.156'; | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 40 | Readonly::Scalar my $DESC => q{Comma used to separate statements}; | |||||||||||||||||||||||||||
| 19 | 40 | Readonly::Scalar my $EXPL => [ 68, 71 ]; | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 91 | ▶ | sub supported_parameters { | ||||||||||||||||||||||||||
|
Subroutine
supported_parameters: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 24 | return ( | ||||||||||||||||||||||||||||
| 25 | { | ||||||||||||||||||||||||||||
| 26 | 91 | name => 'allow_last_statement_to_be_comma_separated_in_map_and_grep', | |||||||||||||||||||||||||||
| 27 | description => 'Allow map and grep blocks to return lists.', | ||||||||||||||||||||||||||||
| 28 | default_string => $FALSE, | ||||||||||||||||||||||||||||
| 29 | behavior => 'boolean', | ||||||||||||||||||||||||||||
| 30 | }, | ||||||||||||||||||||||||||||
| 31 | ); | ||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 74 | ▶ | sub default_severity { return $SEVERITY_HIGH } | ||||||||||||||||||||||||||
|
Subroutine
default_severity: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 35 | 91 | ▶ | sub default_themes { return qw( core bugs pbp certrule ) } | ||||||||||||||||||||||||||
|
Subroutine
default_themes: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 36 | 33 | ▶ | sub applies_to { return 'PPI::Statement' } | ||||||||||||||||||||||||||
|
Subroutine
applies_to: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 296 | ▶ | sub violates { | ||||||||||||||||||||||||||
|
Subroutine
violates: called CC=13 SCAR=28.1
Pod
documented
| |||||||||||||||||||||||||||||
| 41 | 296 | my ( $self, $elem, undef ) = @_; | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | # Grrr... PPI instantiates non-leaf nodes in its class hierarchy... | ||||||||||||||||||||||||||||
| 44 | 296 | ▶ | return if is_ppi_statement_subclass($elem); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | # Now, if PPI hasn't introduced any new PPI::Statement subclasses, we've | ||||||||||||||||||||||||||||
| 47 | # got an element who's class really is PPI::Statement. | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 91 | ▶ | return if _is_parent_a_constructor_or_list($elem); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 85 | ▶ | return if _is_parent_a_for_loop($elem); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 85 | ▶ | if ( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | $self->{_allow_last_statement_to_be_comma_separated_in_map_and_grep} | ||||||||||||||||||||||||||||
| 54 | ) { | ||||||||||||||||||||||||||||
| 55 | 0 | ▶ | return if not _is_direct_part_of_map_or_grep_block($elem); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 85 | foreach my $child ( $elem->schildren() ) { | |||||||||||||||||||||||||||
| 59 | 249 | ▶ | if ( | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->{"_allow_last_statement_to_be_comma_separated_in_map_and_grep"} or _is_last_statement_in_a_block($child)
MC/DC0%0/2
$self->{"_allow_last_statement_to_be_comma_separated_in_map_and_grep"} or _is_last_statement_in_a_block($child)
$self->{"_allow_last_statement_to_be_comma_separated_in_map_and_grep"}
_is_last_statement_in_a_block($child)
Truth table33%1/3
A or B
A= $self->{"_allow_last_statement_to_be_comma_separated_in_map_and_grep"}B= _is_last_statement_in_a_block($child)
| |||||||||||||||||||||||||||||
| 60 | not $self->{_allow_last_statement_to_be_comma_separated_in_map_and_grep} | ||||||||||||||||||||||||||||
| 61 | and not _is_last_statement_in_a_block($child) | ||||||||||||||||||||||||||||
| 62 | ) { | ||||||||||||||||||||||||||||
| 63 | 249 | ▶ | if ( $child->isa('PPI::Token::Word') ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 64 | 43 | ▶ | return if _succeeding_commas_are_list_element_separators($child); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | elsif ( $child->isa('PPI::Token::Operator') ) { | ||||||||||||||||||||||||||||
| 67 | 32 | ▶ | if ( $child->content() eq $COMMA ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 0 | return $self->violation($DESC, $EXPL, $elem); | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 65 | return; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 91 | ▶ | sub _is_parent_a_constructor_or_list { | ||||||||||||||||||||||||||
|
Subroutine
_is_parent_a_constructor_or_list: called CC=3 SCAR=11.3
| |||||||||||||||||||||||||||||
| 78 | 91 | my ($elem) = @_; | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 91 | my $parent = $elem->parent(); | |||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 91 | ▶ | return if not $parent; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | return ( | ||||||||||||||||||||||||||||
| 85 | 91 | ▶ | $parent->isa('PPI::Structure::Constructor') | ||||||||||||||||||||||||||
|
Condition66%2/3
$parent->isa('PPI::Structure::Constructor') || $parent->isa('PPI::Structure::List')
MC/DC50%1/2
$parent->isa('PPI::Structure::Constructor') || $parent->isa('PPI::Structure::List')
$parent->isa('PPI::Structure::Constructor')
$parent->isa('PPI::Structure::List')
Truth table66%2/3
A || B
A= $parent->isa('PPI::Structure::Constructor')B= $parent->isa('PPI::Structure::List')
| |||||||||||||||||||||||||||||
| 86 | or $parent->isa('PPI::Structure::List') | ||||||||||||||||||||||||||||
| 87 | ); | ||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 85 | ▶ | sub _is_parent_a_for_loop { | ||||||||||||||||||||||||||
|
Subroutine
_is_parent_a_for_loop: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 91 | 85 | my ($elem) = @_; | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 85 | my $parent = $elem->parent(); | |||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 85 | ▶ | return if not $parent; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 85 | ▶ | return if not $parent->isa('PPI::Structure::For'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 6 | return 1 == scalar $parent->schildren(); # Multiple means C-style loop. | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 0 | ▶ | sub _is_direct_part_of_map_or_grep_block { | ||||||||||||||||||||||||||
|
Subroutine
_is_direct_part_of_map_or_grep_block: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 103 | 0 | my ($elem) = @_; | |||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 0 | my $parent = $elem->parent(); | |||||||||||||||||||||||||||
| 106 | 0 | ▶ | return if not $parent; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | 0 | ▶ | return if not $parent->isa('PPI::Structure::Block'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 0 | my $block_prior_sibling = $parent->sprevious_sibling(); | |||||||||||||||||||||||||||
| 110 | 0 | ▶ | return if not $block_prior_sibling; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | 0 | ▶ | return if not $block_prior_sibling->isa('PPI::Token::Word'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 0 | state $is_map_or_grep = { hashify( qw( map grep ) ) }; | |||||||||||||||||||||||||||
| 114 | 0 | return $is_map_or_grep->{$block_prior_sibling}; | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 249 | ▶ | sub _is_last_statement_in_a_block { | ||||||||||||||||||||||||||
|
Subroutine
_is_last_statement_in_a_block: called CC=4 SCAR=18.7
| |||||||||||||||||||||||||||||
| 118 | 249 | my ($elem) = @_; | |||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 249 | my $parent = $elem->parent(); | |||||||||||||||||||||||||||
| 121 | 249 | ▶ | return if not $parent; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | 249 | ▶ | return if not $parent->isa('PPI::Structure::Block'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 0 | my $next_sibling = $elem->snext_sibling(); | |||||||||||||||||||||||||||
| 125 | 0 | ▶ | return if not $next_sibling; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 0 | return 1; | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 43 | ▶ | sub _succeeding_commas_are_list_element_separators { | ||||||||||||||||||||||||||
|
Subroutine
_succeeding_commas_are_list_element_separators: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 131 | 43 | my ($elem) = @_; | |||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 43 | ▶ | if ( | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
is_perl_builtin_with_zero_and_or_one_arguments($elem) and not is_perl_builtin_with_multiple_arguments($elem)
MC/DC100%2/2
is_perl_builtin_with_zero_and_or_one_arguments($elem) and not is_perl_builtin_with_multiple_arguments($elem)
is_perl_builtin_with_zero_and_or_one_arguments($elem)
not is_perl_builtin_with_multiple_arguments($elem)
Truth table100%3/3
A and not(B)
A= is_perl_builtin_with_zero_and_or_one_arguments($elem)B= not is_perl_builtin_with_multiple_arguments($elem)
| |||||||||||||||||||||||||||||
| 134 | is_perl_builtin_with_zero_and_or_one_arguments($elem) | ||||||||||||||||||||||||||||
| 135 | and not is_perl_builtin_with_multiple_arguments($elem) | ||||||||||||||||||||||||||||
| 136 | ) { | ||||||||||||||||||||||||||||
| 137 | 8 | return; | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 35 | my $sibling = $elem->snext_sibling(); | |||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 35 | ▶ | return 1 if not $sibling; # There won't be any succeeding commas. | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 35 | return not $sibling->isa('PPI::Structure::List'); | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 40 | 1; | |||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | __END__ | ||||||||||||||||||||||||||||