« Policy/CodeLayout/RequireKRParens.pm
↑ Summary
Policy/CodeLayout/RequireSpaceAroundBinaryOperators.pm »
| 1 | package Perl::Critic::Policy::ValuesAndExpressions::ProhibitListsInMultiConstants; | ||||||||||
| 2 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 2 | ▶ | use parent qw[ Perl::Critic::Policy ]; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 2 | ▶ | use Perl::Critic::Utils qw[ :severities :classification ]; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 2 | ▶ | use constant { # PPI search return values | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 2 | MATCH => 1, | |||||||||
| 9 | NO_MATCH => 0, | ||||||||||
| 10 | NO_DESCEND => undef, | ||||||||||
| 11 | 2 | }; | |||||||||
| 12 | |||||||||||
| 13 | 2 | ▶ | sub default_severity { return $SEVERITY_HIGH } | ||||||||
|
Subroutine
default_severity: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 14 | 0 | ▶ | sub default_themes { return qw[ bugs ] } | ||||||||
|
Subroutine
default_themes: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 15 | 4 | ▶ | sub applies_to { return 'PPI::Statement::Include' } | ||||||||
|
Subroutine
applies_to: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 16 | |||||||||||
| 17 | 5 | ▶ | sub violates { | ||||||||
|
Subroutine
violates: called CC=7 SCAR=19.6
Pod
documented
| |||||||||||
| 18 | 5 | my ($self, $elem, $doc) = @_; | |||||||||
| 19 | 5 | ▶ | return if not $elem->pragma; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 20 | 5 | ▶ | return if $elem->module ne 'constant'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 21 | |||||||||||
| 22 | 5 | my ($hash) = $elem->arguments; | |||||||||
| 23 | 5 | ▶ | return if not $hash; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 24 | 5 | ▶ | return if not $hash->isa('PPI::Structure::Constructor'); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 25 | 3 | ▶ | return if $hash->braces ne '{}'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 26 | |||||||||||
| 27 | 66 | ▶ | my $lists_ref = $hash->find(sub { | ||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=17.9
| |||||||||||
| 28 | 66 | my $el = $_[1]; | |||||||||
| 29 | |||||||||||
| 30 | 66 | ▶ | if ($el->isa('PPI::Structure::List')) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 31 | 3 | ▶ | return _is_function_arg($el) ? NO_DESCEND : MATCH; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 32 | } | ||||||||||
| 33 | 63 | ▶ | return MATCH if $el->isa('PPI::Token::QuoteLike::Words'); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 34 | |||||||||||
| 35 | # descend only into top-level expressions | ||||||||||
| 36 | 62 | ▶ | return NO_DESCEND if not $el->isa('PPI::Statement::Expression'); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 37 | 4 | ▶ | return NO_DESCEND if $el->parent->isa('PPI::Statement::Expression'); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 38 | |||||||||||
| 39 | 4 | return NO_MATCH; | |||||||||
| 40 | 3 | }); | |||||||||
| 41 | 3 | ▶ | return if not $lists_ref; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 42 | |||||||||||
| 43 | my @violations = map { | ||||||||||
| 44 | 1 | $self->violation('List inside a multiple constant declaration', | |||||||||
| 45 | 'Use a separate constant declaration to use a list', $_) | ||||||||||
| 46 | } @$lists_ref; | ||||||||||
| 47 | 1 | return @violations; | |||||||||
| 48 | } | ||||||||||
| 49 | |||||||||||
| 50 | 3 | ▶ | sub _is_function_arg { | ||||||||
|
Subroutine
_is_function_arg: called CC=2 SCAR=6.9
| |||||||||||
| 51 | 3 | my ($elem) = @_; | |||||||||
| 52 | 3 | my $prev = $elem->sprevious_sibling; | |||||||||
| 53 | 3 | ▶ | return if not $prev->isa('PPI::Token::Word'); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 54 | 2 | return is_function_call($prev); | |||||||||
| 55 | } | ||||||||||
| 56 | |||||||||||
| 57 | 2 | 1; | |||||||||
| 58 | __END__ | ||||||||||