« blib/lib/Perl/Critic/PolicyParameter/Behavior/StringList.pm
↑ Summary
blib/lib/Perl/Critic/Policy/ValuesAndExpressions/ProhibitVersionStrings.pm »
| 1 | package Perl::Critic::Policy::Variables::ProhibitAugmentedAssignmentInDeclaration; | ||||||||||||||||||||||||||||
| 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 List::SomeUtils qw( firstval ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 40 | ▶ | use Readonly; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 40 | ▶ | use Perl::Critic::Utils qw{ :severities :data_conversion }; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 40 | ▶ | use parent 'Perl::Critic::Policy'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 40 | our $VERSION = '1.150'; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 40 | Readonly::Scalar my $DESC => q{Augmented assignment operator '%s' used in declaration}; | |||||||||||||||||||||||||||
| 17 | 40 | Readonly::Scalar my $EXPL => q{Use simple assignment when initializing variables}; | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | sub supported_parameters { | ||||||||||||||||||||||||||||
| 22 | return ( | ||||||||||||||||||||||||||||
| 23 | { | ||||||||||||||||||||||||||||
| 24 | 91 | ▶ | name => 'allow_our', | ||||||||||||||||||||||||||
|
Subroutine
supported_parameters: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 25 | description => | ||||||||||||||||||||||||||||
| 26 | q<Allow augmented assignment for our variables.>, | ||||||||||||||||||||||||||||
| 27 | default_string => '0', | ||||||||||||||||||||||||||||
| 28 | behavior => 'boolean', | ||||||||||||||||||||||||||||
| 29 | }, | ||||||||||||||||||||||||||||
| 30 | ); | ||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | 75 | ▶ | sub default_severity { return $SEVERITY_HIGH } | ||||||||||||||||||||||||||
|
Subroutine
default_severity: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 33 | 74 | ▶ | sub default_themes { return qw( core bugs ) } | ||||||||||||||||||||||||||
|
Subroutine
default_themes: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 34 | 33 | ▶ | sub applies_to { return 'PPI::Statement::Variable' } | ||||||||||||||||||||||||||
|
Subroutine
applies_to: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | sub violates { | ||||||||||||||||||||||||||||
| 39 | 90 | ▶ | my ( $self, $elem, undef ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
violates: called CC=5 SCAR=17.6
Pod
documented
| |||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 90 | state $augmented_assignments = { hashify( qw( **= += -= .= *= /= %= x= &= |= ^= <<= >>= &&= ||= //= ) ) }; | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | # The assignment operator associated with a PPI::Statement::Variable | ||||||||||||||||||||||||||||
| 44 | # element is assumed to be the first immediate child of that element. | ||||||||||||||||||||||||||||
| 45 | # Other operators in the statement, e.g. the ',' in "my ( $a, $b ) = ();", | ||||||||||||||||||||||||||||
| 46 | # as assumed to never be immediate children. | ||||||||||||||||||||||||||||
| 47 | # | ||||||||||||||||||||||||||||
| 48 | return | ||||||||||||||||||||||||||||
| 49 | 90 | ▶ | if $self->{_allow_our} and $elem->type eq 'our'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->{"_allow_our"} and $elem->type eq "our"
MC/DC0%0/2
$self->{"_allow_our"} and $elem->type eq "our"
$self->{"_allow_our"}
$elem->type eq "our"
Truth table33%1/3
A and B
A= $self->{"_allow_our"}B= $elem->type eq "our"
| |||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 90 | ▶ | my $found = firstval { $_->isa('PPI::Token::Operator') } $elem->children(); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 52 | 90 | ▶ | if ( $found ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 90 | my $op = $found->content(); | |||||||||||||||||||||||||||
| 54 | 90 | ▶ | if ( exists $augmented_assignments->{ $op } ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | 0 | return $self->violation( sprintf( $DESC, $op ), $EXPL, $found ); | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 90 | return; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 40 | 1; | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | __END__ | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | =pod | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | =for stopwords O'Regan | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | =head1 NAME | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | Perl::Critic::Policy::Variables::ProhibitAugmentedAssignmentInDeclaration - Do not write C< my $foo .= 'bar'; >. | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | =head1 AFFILIATION | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | This Policy is part of the core L<Perl::Critic|Perl::Critic> | ||||||||||||||||||||||||||||
| 81 | distribution. | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | Variable declarations that also do initialization with '=' are common. | ||||||||||||||||||||||||||||
| 87 | Perl also allows you to use operators like '.=', '+=', etc., but it | ||||||||||||||||||||||||||||
| 88 | it is more clear to not do so. | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | my $foo .= 'bar'; # same as my $foo = 'bar'; | ||||||||||||||||||||||||||||
| 91 | our $foo *= 2; # same as our $foo = 0; | ||||||||||||||||||||||||||||
| 92 | my ( $foo, $bar ) += ( 1, 2 ); # same as my ( $foo, $bar ) = ( undef, 2 ); | ||||||||||||||||||||||||||||
| 93 | local $Carp::CarpLevel += 1; # same as local $Carp::CarpLevel = 1; | ||||||||||||||||||||||||||||
| 94 | state $foo += 2; # adds 2 every time it's encountered | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | Such constructs are usually the result of botched cut-and-paste, and often are | ||||||||||||||||||||||||||||
| 97 | bugs. Some produce warnings. | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | =head1 CONFIGURATION | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | There is an C<allow_our> boolean option for this Policy. If set, augmented | ||||||||||||||||||||||||||||
| 102 | assignments are allowed when declaring C<our> variables. Since C<our> | ||||||||||||||||||||||||||||
| 103 | variables are globally accessible, some modules will want to allow users to | ||||||||||||||||||||||||||||
| 104 | initialize the variable prior to the module using the variable. Modules may | ||||||||||||||||||||||||||||
| 105 | also wish to use the same our variable in different scopes without declaring | ||||||||||||||||||||||||||||
| 106 | it at the outer scope. | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | With this option set, the following are flagged as indicated: | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | our $DEBUG //= 1; # ok | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | This can be enabled in your F<.perlcriticrc>: | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | [Perl::Critic::Policy::Variables::ProhibitAugmentedAssignmentInDeclaration] | ||||||||||||||||||||||||||||
| 115 | allow_our = 1 | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | Mike O'Regan | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | Copyright (c) 2011-2023 Mike O'Regan | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | This program is free software; you can redistribute it and/or modify | ||||||||||||||||||||||||||||
| 127 | it under the same terms as Perl itself. The full text of this license | ||||||||||||||||||||||||||||
| 128 | can be found in the LICENSE file included with this module. | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | =cut | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | # Local Variables: | ||||||||||||||||||||||||||||
| 133 | # mode: cperl | ||||||||||||||||||||||||||||
| 134 | # cperl-indent-level: 4 | ||||||||||||||||||||||||||||
| 135 | # fill-column: 78 | ||||||||||||||||||||||||||||
| 136 | # indent-tabs-mode: nil | ||||||||||||||||||||||||||||
| 137 | # c-indentation-style: bsd | ||||||||||||||||||||||||||||
| 138 | # End: | ||||||||||||||||||||||||||||
| 139 | # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround : | ||||||||||||||||||||||||||||