« Policy/Community/PackageMatchesFilename.pm
↑ Summary
Policy/Community/ConditionalImplicitReturn.pm »
| 1 | package Perl::Critic::Policy::Community::DollarAB; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Perl::Critic::Utils qw(:severities :classification :ppi); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use parent 'Perl::Critic::Policy'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 1 | our $VERSION = 'v1.0.4'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use constant DESC => 'Using $a or $b outside sort()'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use constant EXPL => '$a and $b are special package variables for use in sort() and related functions. Declaring them as lexicals like "my $a" may break sort(). Use different variable names.'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 9 | ▶ | sub supported_parameters { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
supported_parameters: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 9 | name => 'extra_pair_functions', | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | description => 'Non-standard functions in which to allow $a and $b', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | behavior => 'string list', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 4 | ▶ | sub default_severity { $SEVERITY_HIGH } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
default_severity: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 0 | ▶ | sub default_themes { 'community' } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
default_themes: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 9 | ▶ | sub applies_to { 'PPI::Token::Symbol' } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
applies_to: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 1 | my @sorters = qw(sort reduce pairgrep pairfirst pairmap pairwise); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 37 | ▶ | sub violates { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
violates: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 37 | my ($self, $elem) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 37 | ▶ | return () unless $elem->symbol eq '$a' or $elem->symbol eq '$b'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$elem->symbol eq "\$a" or $elem->symbol eq "\$b"
MC/DC100%2/2
$elem->symbol eq "\$a" or $elem->symbol eq "\$b"
$elem->symbol eq "\$a"
$elem->symbol eq "\$b"
Truth table100%3/3
A or B
A= $elem->symbol eq "\$a"B= $elem->symbol eq "\$b"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 31 | my %sorters_hash = map { ($_ => 1) } @sorters, keys %{$self->{_extra_pair_functions}}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 31 | my $found = $self->_find_sorter($elem, \%sorters_hash); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 31 | ▶ | return $self->violation(DESC, EXPL, $elem) unless $found; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 27 | return (); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 33 | ▶ | sub _find_sorter { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_find_sorter: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 33 | my ($self, $elem, $sorters) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 33 | my $outer = $elem->parent; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 33 | ▶ | $outer = $outer->parent until !$outer or $outer->isa('PPI::Structure::Block'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not $outer or $outer->isa("PPI::Structure::Block")
MC/DC100%2/2
not $outer or $outer->isa("PPI::Structure::Block")
not $outer
$outer->isa("PPI::Structure::Block")
Truth table100%3/3
not(A) or B
A= not $outerB= $outer->isa("PPI::Structure::Block")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 33 | ▶ | return '' unless $outer; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | # Find function or method call (assumes block/sub is first argument) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 29 | my $function = $outer->previous_token; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | $function = $function->previous_token until !$function | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 29 | ▶ | or ($function->isa('PPI::Token::Word') and $function =~ m/([^:]+)\z/ and exists $sorters->{$1}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition88%8/9
not $function or $function->isa("PPI::Token::Word") and $function =~ /([^:]+)\z/ and exists $sorters->{$1}
$function->isa("PPI::Token::Word") and $function =~ /([^:]+)\z/ and exists $sorters->{$1}
$function->isa("PPI::Token::Word") and $function =~ /([^:]+)\z/
MC/DC75%3/4
not $function or $function->isa("PPI::Token::Word") and $function =~ /([^:]+)\z/ and exists $sorters->{$1}
not $function
$function->isa("PPI::Token::Word")
$function =~ /([^:]+)\z/
exists $sorters->{$1}
Truth table80%4/5
not(A) or B and C and D
A= not $functionB= $function->isa("PPI::Token::Word")C= $function =~ /([^:]+)\z/D= exists $sorters->{$1}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 29 | ▶ | return $self->_find_sorter($outer) unless $function; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 27 | return $function; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | Perl::Critic::Policy::Community::DollarAB - Don't use $a or $b as variable | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | names outside sort | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | The special variables C<$a> and C<$b> are reserved for C<sort()> and similar | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | functions which assign to them to iterate over pairs of values. These are | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | global variables, and declaring them as lexical variables with C<my> to use | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | them outside this context can break usage of these functions. Use different | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | names for your variables. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | my $a = 1; # not ok | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | my $abc = 1; # ok | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | sort { $a <=> $b } (3,2,1); # ok | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | =head1 AFFILIATION | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | This policy is part of L<Perl::Critic::Community>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | =head1 CONFIGURATION | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | This policy can be configured to allow C<$a> and C<$b> in additional functions, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | by putting an entry in a C<.perlcriticrc> file like this: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | [Community::DollarAB] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | extra_pair_functions = pairfoo pairbar | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | Dan Book, C<dbook@cpan.org> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | Copyright 2015, Dan Book. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | This library is free software; you may redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | the terms of the Artistic License version 2.0. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | L<Perl::Critic> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||