« lib/Perl/Critic/Policy/ValuesAndExpressions/ProhibitMismatchedOperators.pm
↑ Summary
lib/Perl/Critic/Policy/Modules/ProhibitConditionalUseStatements.pm »
| 1 | package Perl::Critic::Policy::Variables::ProhibitPunctuationVars; | ||||||||||||||||||||||||||||
| 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 | 40 | ▶ | use English qw< -no_match_vars >; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 40 | ▶ | use PPI::Token::Magic; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 40 | ▶ | use Perl::Critic::Utils qw( :characters :severities :data_conversion ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 40 | ▶ | use PPIx::Regexp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 40 | ▶ | use PPIx::Regexp::Util 0.068 qw< | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | is_ppi_regexp_element | ||||||||||||||||||||||||||||
| 16 | 40 | >; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 40 | ▶ | use parent 'Perl::Critic::Policy'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 40 | our $VERSION = '1.156'; | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 40 | Readonly::Scalar my $DESC => q<Magic punctuation variable %s used>; | |||||||||||||||||||||||||||
| 25 | 40 | Readonly::Scalar my $EXPL => [79]; | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | # There is no English.pm equivalent for $]. | ||||||||||||||||||||||||||||
| 30 | 93 | ▶ | sub supported_parameters { | ||||||||||||||||||||||||||
|
Subroutine
supported_parameters: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 31 | return ( | ||||||||||||||||||||||||||||
| 32 | { | ||||||||||||||||||||||||||||
| 33 | 93 | name => 'allow', | |||||||||||||||||||||||||||
| 34 | description => 'The additional variables to allow.', | ||||||||||||||||||||||||||||
| 35 | default_string => $EMPTY, | ||||||||||||||||||||||||||||
| 36 | behavior => 'string list', | ||||||||||||||||||||||||||||
| 37 | list_always_present_values => | ||||||||||||||||||||||||||||
| 38 | [ qw< $_ @_ $1 $2 $3 $4 $5 $6 $7 $8 $9 _ $] > ], | ||||||||||||||||||||||||||||
| 39 | }, | ||||||||||||||||||||||||||||
| 40 | { | ||||||||||||||||||||||||||||
| 41 | name => 'string_mode', | ||||||||||||||||||||||||||||
| 42 | description => | ||||||||||||||||||||||||||||
| 43 | 'Controls checking interpolated strings for punctuation variables.', | ||||||||||||||||||||||||||||
| 44 | default_string => 'thorough', | ||||||||||||||||||||||||||||
| 45 | behavior => 'enumeration', | ||||||||||||||||||||||||||||
| 46 | enumeration_values => [ qw< simple disable thorough > ], | ||||||||||||||||||||||||||||
| 47 | enumeration_allow_multiple_values => 0, | ||||||||||||||||||||||||||||
| 48 | }, | ||||||||||||||||||||||||||||
| 49 | ); | ||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 89 | ▶ | sub default_severity { return $SEVERITY_LOW } | ||||||||||||||||||||||||||
|
Subroutine
default_severity: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 53 | 83 | ▶ | sub default_themes { return qw< core pbp cosmetic > } | ||||||||||||||||||||||||||
|
Subroutine
default_themes: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 31 | ▶ | sub applies_to { | ||||||||||||||||||||||||||
|
Subroutine
applies_to: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 56 | 31 | return qw< | |||||||||||||||||||||||||||
| 57 | PPI::Token::Magic | ||||||||||||||||||||||||||||
| 58 | PPI::Token::Quote::Double | ||||||||||||||||||||||||||||
| 59 | PPI::Token::Quote::Interpolate | ||||||||||||||||||||||||||||
| 60 | PPI::Token::QuoteLike::Command | ||||||||||||||||||||||||||||
| 61 | PPI::Token::QuoteLike::Backtick | ||||||||||||||||||||||||||||
| 62 | PPI::Token::QuoteLike::Regexp | ||||||||||||||||||||||||||||
| 63 | PPI::Token::QuoteLike::Readline | ||||||||||||||||||||||||||||
| 64 | PPI::Token::HereDoc | ||||||||||||||||||||||||||||
| 65 | >; | ||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | # This list matches the initialization of %PPI::Token::Magic::magic. | ||||||||||||||||||||||||||||
| 72 | ## no critic (ValuesAndExpressions::RequireInterpolationOfMetachars) | ||||||||||||||||||||||||||||
| 73 | 40 | Readonly::Array my @MAGIC_VARIABLES => | |||||||||||||||||||||||||||
| 74 | qw{ | ||||||||||||||||||||||||||||
| 75 | $1 $2 $3 $4 $5 $6 $7 $8 $9 | ||||||||||||||||||||||||||||
| 76 | $_ $& $` $' $+ @+ %+ $* $. $/ $| | ||||||||||||||||||||||||||||
| 77 | $\\ $" $; $% $= $- @- %- $) | ||||||||||||||||||||||||||||
| 78 | $~ $^ $: $? $! %! $@ $$ $< $> | ||||||||||||||||||||||||||||
| 79 | $( $0 $[ $] @_ @* | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | $^L $^A $^E $^C $^D $^F $^H | ||||||||||||||||||||||||||||
| 82 | $^I $^M $^N $^O $^P $^R $^S | ||||||||||||||||||||||||||||
| 83 | $^T $^V $^W $^X %^H | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | $::| | ||||||||||||||||||||||||||||
| 86 | }, | ||||||||||||||||||||||||||||
| 87 | q<$}>, | ||||||||||||||||||||||||||||
| 88 | q<$,>, | ||||||||||||||||||||||||||||
| 89 | q<$#>, | ||||||||||||||||||||||||||||
| 90 | q<$#+>, | ||||||||||||||||||||||||||||
| 91 | q<$#->; | ||||||||||||||||||||||||||||
| 92 | ## use critic | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | # The main regular expression for detecting magic variables. | ||||||||||||||||||||||||||||
| 95 | 40 | Readonly::Scalar my $MAGIC_REGEX => _create_magic_detector(); | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | # The magic vars in this array will be ignored in interpolated strings | ||||||||||||||||||||||||||||
| 98 | # in simple mode. See CONFIGURATION in the pod. | ||||||||||||||||||||||||||||
| 99 | 40 | Readonly::Array my @IGNORE_FOR_INTERPOLATION => | |||||||||||||||||||||||||||
| 100 | ( q{$'}, q{$$}, q{$#}, q{$:}, ); ## no critic ( RequireInterpolationOfMetachars, ProhibitQuotedWordLists ) | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 16 | ▶ | sub violates { | ||||||||||||||||||||||||||
|
Subroutine
violates: called CC=4 SCAR=15.2
Pod
documented
| |||||||||||||||||||||||||||||
| 105 | 16 | my ( $self, $elem, $doc ) = @_; | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 16 | ▶ | if ( $elem->isa('PPI::Token::Magic') ) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 108 | 15 | return _violates_magic( $self, $elem ); | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | elsif ( $elem->isa('PPI::Token::HereDoc') ) { | ||||||||||||||||||||||||||||
| 111 | 0 | return _violates_heredoc( $self, $elem ); | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | elsif ( is_ppi_regexp_element( $elem ) ) { # GitHub #843 | ||||||||||||||||||||||||||||
| 114 | 0 | return _violates_regexp( $self, $elem, $doc ); | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | #the remaining applies_to() classes are all interpolated strings | ||||||||||||||||||||||||||||
| 118 | 1 | return _violates_string( $self, $elem ); | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | # Helper functions for the four types of violations: code, quotes, heredoc, | ||||||||||||||||||||||||||||
| 124 | # regexp | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 15 | ▶ | sub _violates_magic { | ||||||||||||||||||||||||||
|
Subroutine
_violates_magic: called CC=2 SCAR=7.6
| |||||||||||||||||||||||||||||
| 127 | 15 | my ( $self, $elem, undef ) = @_; | |||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 15 | ▶ | if ( !exists $self->{_allow}->{$elem} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | 15 | return $self->_make_violation( $DESC, $EXPL, $elem ); | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 0 | return; # no violation | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 1 | ▶ | sub _violates_string { | ||||||||||||||||||||||||||
|
Subroutine
_violates_string: called CC=3 SCAR=12.7
| |||||||||||||||||||||||||||||
| 137 | 1 | my ( $self, $elem, undef ) = @_; | |||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | # RT #55604: Variables::ProhibitPunctuationVars gives false-positive on | ||||||||||||||||||||||||||||
| 140 | # qr// regexp's ending in '$' | ||||||||||||||||||||||||||||
| 141 | # We want to analyze the content of the string in the dictionary sense of | ||||||||||||||||||||||||||||
| 142 | # the word 'content'. We can not simply use the PPI content() method to | ||||||||||||||||||||||||||||
| 143 | # get this, because content() includes the delimiters. | ||||||||||||||||||||||||||||
| 144 | 1 | my $string; | |||||||||||||||||||||||||||
| 145 | 1 | ▶ | if ( $elem->can( 'string' ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | # If we have a string() method (currently only the PPI::Token::Quote | ||||||||||||||||||||||||||||
| 147 | # classes) use it to extract the content of the string. | ||||||||||||||||||||||||||||
| 148 | 1 | $string = $elem->string(); | |||||||||||||||||||||||||||
| 149 | } else { | ||||||||||||||||||||||||||||
| 150 | # Lacking string(), we fake it under the assumption that the content | ||||||||||||||||||||||||||||
| 151 | # of our element represents one of the 'normal' Perl strings, with a | ||||||||||||||||||||||||||||
| 152 | # single-character delimiter, possibly preceded by an operator like | ||||||||||||||||||||||||||||
| 153 | # 'qx' or 'qr'. If there is a leading operator, spaces may appear | ||||||||||||||||||||||||||||
| 154 | # after it. | ||||||||||||||||||||||||||||
| 155 | 0 | $string = $elem->content(); | |||||||||||||||||||||||||||
| 156 | 0 | $string =~ s/ \A \w* \s* . //smx; | |||||||||||||||||||||||||||
| 157 | 0 | chop $string; | |||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 1 | my %matches = _strings_helper( $self, $string ); | |||||||||||||||||||||||||||
| 161 | 1 | ▶ | if (%matches) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | 0 | return $self->_make_violation( $DESC . ' in interpolated string', $EXPL, $elem, \%matches ); | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 1 | return; # no violation | |||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 0 | ▶ | sub _violates_heredoc { | ||||||||||||||||||||||||||
|
Subroutine
_violates_heredoc: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 169 | 0 | my ( $self, $elem, undef ) = @_; | |||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 0 | ▶ | if ( $elem->{_mode} eq 'interpolate' or $elem->{_mode} eq 'command' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$elem->{"_mode"} eq "interpolate" or $elem->{"_mode"} eq "command"
MC/DC0%0/2
$elem->{"_mode"} eq "interpolate" or $elem->{"_mode"} eq "command"
$elem->{"_mode"} eq "interpolate"
$elem->{"_mode"} eq "command"
Truth table0%0/3
A or B
A= $elem->{"_mode"} eq "interpolate"B= $elem->{"_mode"} eq "command"
| |||||||||||||||||||||||||||||
| 172 | 0 | my $heredoc_string = join "\n", $elem->heredoc(); | |||||||||||||||||||||||||||
| 173 | 0 | my %matches = _strings_helper( $self, $heredoc_string ); | |||||||||||||||||||||||||||
| 174 | 0 | ▶ | if (%matches) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 175 | 0 | return $self->_make_violation( $DESC . ' in interpolated here-document', $EXPL, $elem, \%matches ); | |||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 0 | return; # no violation | |||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 0 | ▶ | sub _violates_regexp { # GitHub #843 (https://github.com/Perl-Critic/Perl-Critic/issues/843) | ||||||||||||||||||||||||||
|
Subroutine
_violates_regexp: not called CC=10 SCAR=47.0
| |||||||||||||||||||||||||||||
| 183 | 0 | my ( $self, $elem, $doc ) = @_; | |||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 0 | ▶ | return if ( $self->{_string_mode} eq 'disable' ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 0 | ▶ | my $pre = $doc->ppix_regexp_from_element( $elem ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 188 | or return; | ||||||||||||||||||||||||||||
| 189 | 0 | ▶ | $pre->failures() | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 190 | and return; | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | 0 | my @raw_matches; | |||||||||||||||||||||||||||
| 193 | 0 | ▶ | foreach my $code ( @{ $pre->find( 'PPIx::Regexp::Token::Code' ) || [] } ) { | ||||||||||||||||||||||||||
|
Condition0%0/2
$pre->find('PPIx::Regexp::Token::Code') || []
MC/DC0%0/1
$pre->find('PPIx::Regexp::Token::Code') || []
$pre->find('PPIx::Regexp::Token::Code')
Truth table0%0/2
A
A= $pre->find('PPIx::Regexp::Token::Code')
| |||||||||||||||||||||||||||||
| 194 | 0 | ▶ | my $code_doc = $code->ppi() | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 195 | or next; | ||||||||||||||||||||||||||||
| 196 | 0 | push @raw_matches, map { $_->symbol() } @{ | |||||||||||||||||||||||||||
| 197 | 0 | ▶ | $code_doc->find( 'PPI::Token::Magic' ) || [] }; | ||||||||||||||||||||||||||
|
Condition0%0/2
$code_doc->find('PPI::Token::Magic') || []
MC/DC0%0/1
$code_doc->find('PPI::Token::Magic') || []
$code_doc->find('PPI::Token::Magic')
Truth table0%0/2
A
A= $code_doc->find('PPI::Token::Magic')
| |||||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | 0 | my %matches = hashify( @raw_matches ); | |||||||||||||||||||||||||||
| 201 | 0 | delete @matches{ keys %{ $self->{_allow} } }; | |||||||||||||||||||||||||||
| 202 | 0 | ▶ | if ( $self->{_string_mode} eq 'simple' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 203 | 0 | delete @matches{@IGNORE_FOR_INTERPOLATION}; | |||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | 0 | ▶ | if ( keys %matches ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 207 | 0 | return $self->_make_violation( $DESC . ' in interpolated Regexp', $EXPL, $elem, \%matches ); | |||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | 0 | return; | |||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | # Helper functions specific to interpolated strings | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | 1 | ▶ | sub _strings_helper { | ||||||||||||||||||||||||||
|
Subroutine
_strings_helper: called CC=4 SCAR=22.6
| |||||||||||||||||||||||||||||
| 218 | 1 | my ( $self, $target_string, undef ) = @_; | |||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | 1 | ▶ | return if ( $self->{_string_mode} eq 'disable' ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 221 | return _strings_thorough( $self, $target_string ) | ||||||||||||||||||||||||||||
| 222 | 1 | ▶ | if $self->{_string_mode} eq 'thorough'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | # we are in string_mode = simple | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | 0 | my @raw_matches = map { _unbracket_variable_name( $_ ) } | |||||||||||||||||||||||||||
| 227 | $target_string =~ m/$MAGIC_REGEX/goxms; | ||||||||||||||||||||||||||||
| 228 | 0 | ▶ | return if not @raw_matches; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | 0 | my %matches = hashify(@raw_matches); | |||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | 0 | delete @matches{ keys %{ $self->{_allow} } }; | |||||||||||||||||||||||||||
| 233 | 0 | delete @matches{@IGNORE_FOR_INTERPOLATION}; | |||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | 0 | return %matches; | |||||||||||||||||||||||||||
| 236 | } | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | 1 | ▶ | sub _strings_thorough { | ||||||||||||||||||||||||||
|
Subroutine
_strings_thorough: called CC=11 SCAR=43.5
| |||||||||||||||||||||||||||||
| 239 | 1 | my ( $self, $target_string, undef ) = @_; | |||||||||||||||||||||||||||
| 240 | 1 | my %matches; | |||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | MATCH: | ||||||||||||||||||||||||||||
| 243 | 1 | ▶ | while ( my ($match) = $target_string =~ m/$MAGIC_REGEX/gcxms ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 244 | 0 | my $nextchar = substr $target_string, $LAST_MATCH_END[0], 1; | |||||||||||||||||||||||||||
| 245 | 0 | my $vname = _unbracket_variable_name( $match ); | |||||||||||||||||||||||||||
| 246 | 0 | my $c = $vname . $nextchar; | |||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | # These tests closely parallel those in PPI::Token::Magic, | ||||||||||||||||||||||||||||
| 249 | # from which the regular expressions were taken. | ||||||||||||||||||||||||||||
| 250 | # A degree of simplicity is sacrificed to maintain the parallel. | ||||||||||||||||||||||||||||
| 251 | # $c is so named by analogy to that module. | ||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | # possibly *not* a magic variable | ||||||||||||||||||||||||||||
| 254 | 0 | ▶ | if ($c =~ m/ ^ \$ .* [ \w : \$ { ] $ /xms) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 255 | ## no critic (RequireInterpolationOfMetachars) | ||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | 0 | ▶ | if ( | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$c =~ / ^(\$(?:\_[\w:]|::)) /msx or $c =~ / ^\$\'[\w] /msx
MC/DC0%0/2
$c =~ / ^(\$(?:\_[\w:]|::)) /msx or $c =~ / ^\$\'[\w] /msx
$c =~ / ^(\$(?:\_[\w:]|::)) /msx
$c =~ / ^\$\'[\w] /msx
Truth table0%0/3
A or B
A= $c =~ / ^(\$(?:\_[\w:]|::)) /msxB= $c =~ / ^\$\'[\w] /msx
| |||||||||||||||||||||||||||||
| 258 | $c =~ m/ ^(\$(?:\_[\w:]|::)) /xms | ||||||||||||||||||||||||||||
| 259 | or $c =~ m/ ^\$\'[\w] /xms ) | ||||||||||||||||||||||||||||
| 260 | { | ||||||||||||||||||||||||||||
| 261 | next MATCH | ||||||||||||||||||||||||||||
| 262 | 0 | ▶ | if $c !~ m/ ^\$\'\d$ /xms; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 263 | # It not $' followed by a digit. | ||||||||||||||||||||||||||||
| 264 | # So it's magic var with something immediately after. | ||||||||||||||||||||||||||||
| 265 | } | ||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | next MATCH | ||||||||||||||||||||||||||||
| 268 | 0 | ▶ | if $c =~ m/ ^\$\$\w /xms; # It's a scalar dereference | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 269 | next MATCH | ||||||||||||||||||||||||||||
| 270 | 0 | ▶ | if $c eq '$#$' | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$c eq "\$#\$" or $c eq "\$#{"
MC/DC0%0/2
$c eq "\$#\$" or $c eq "\$#{"
$c eq "\$#\$"
$c eq "\$#{"
Truth table0%0/3
A or B
A= $c eq "\$#\$"B= $c eq "\$#{"
| |||||||||||||||||||||||||||||
| 271 | or $c eq '$#{'; # It's an index dereferencing cast | ||||||||||||||||||||||||||||
| 272 | next MATCH | ||||||||||||||||||||||||||||
| 273 | 0 | ▶ | if $c =~ m/ ^(\$\#)\w /xms | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 274 | ; # It's an array index thingy, e.g. $#array_name | ||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | # PPI's checks for long escaped vars like $^WIDE_SYSTEM_CALLS | ||||||||||||||||||||||||||||
| 277 | # appear to be erroneous, and are omitted here. | ||||||||||||||||||||||||||||
| 278 | # if ( $c =~ m/^\$\^\w{2}$/xms ) { | ||||||||||||||||||||||||||||
| 279 | # } | ||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||
| 281 | 0 | ▶ | next MATCH if $c =~ m/ ^ \$ \# [{] /xms; # It's a $#{...} cast | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 282 | } | ||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||
| 284 | # The additional checking that PPI::Token::Magic does at this point | ||||||||||||||||||||||||||||
| 285 | # is not necessary here, in an interpolated string context. | ||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||
| 287 | 0 | $matches{$vname} = 1; | |||||||||||||||||||||||||||
| 288 | } | ||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||
| 290 | 1 | delete @matches{ keys %{ $self->{_allow} } }; | |||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||
| 292 | 1 | return %matches; | |||||||||||||||||||||||||||
| 293 | } | ||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||
| 295 | # RT #72910: A magic variable may appear in bracketed form; e.g. "$$" as | ||||||||||||||||||||||||||||
| 296 | # "${$}". Generate the bracketed form from the unbracketed form, and | ||||||||||||||||||||||||||||
| 297 | # return both. | ||||||||||||||||||||||||||||
| 298 | 2640 | ▶ | sub _bracketed_form_of_variable_name { | ||||||||||||||||||||||||||
|
Subroutine
_bracketed_form_of_variable_name: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 299 | 2640 | my ( $name ) = @_; | |||||||||||||||||||||||||||
| 300 | 2640 | ▶ | length $name > 1 | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 301 | or return ( $name ); | ||||||||||||||||||||||||||||
| 302 | 2640 | my $brktd = $name; | |||||||||||||||||||||||||||
| 303 | 2640 | substr $brktd, 1, 0, '{'; | |||||||||||||||||||||||||||
| 304 | 2640 | $brktd .= '}'; | |||||||||||||||||||||||||||
| 305 | 2640 | return( $name, $brktd ); | |||||||||||||||||||||||||||
| 306 | } | ||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||
| 308 | # RT #72910: Since we loaded both bracketed and unbracketed forms of the | ||||||||||||||||||||||||||||
| 309 | # punctuation variables into our detecting regex, we need to detect and | ||||||||||||||||||||||||||||
| 310 | # strip the brackets if they are present to recover the canonical name. | ||||||||||||||||||||||||||||
| 311 | 0 | ▶ | sub _unbracket_variable_name { | ||||||||||||||||||||||||||
|
Subroutine
_unbracket_variable_name: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 312 | 0 | my ( $name ) = @_; | |||||||||||||||||||||||||||
| 313 | 0 | ▶ | $name =~ m/ \A ( . ) [{] ( .+ ) [}] \z /smx | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 314 | and return "$1$2"; | ||||||||||||||||||||||||||||
| 315 | 0 | return $name; | |||||||||||||||||||||||||||
| 316 | } | ||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||
| 318 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||
| 320 | 40 | ▶ | sub _create_magic_detector { | ||||||||||||||||||||||||||
|
Subroutine
_create_magic_detector: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 321 | # Set up the regexp alternation for matching magic variables. | ||||||||||||||||||||||||||||
| 322 | # We can't process $config->{_allow} here because of a quirk in the | ||||||||||||||||||||||||||||
| 323 | # way Perl::Critic handles testing. | ||||||||||||||||||||||||||||
| 324 | # | ||||||||||||||||||||||||||||
| 325 | # The sort is needed so that, e.g., $^ doesn't mask out $^M | ||||||||||||||||||||||||||||
| 326 | my $magic_alternation = | ||||||||||||||||||||||||||||
| 327 | '(?:' | ||||||||||||||||||||||||||||
| 328 | . ( | ||||||||||||||||||||||||||||
| 329 | join | ||||||||||||||||||||||||||||
| 330 | q<|>, | ||||||||||||||||||||||||||||
| 331 | 5280 | map { quotemeta } | |||||||||||||||||||||||||||
| 332 | 25000 | reverse sort { length $a <=> length $b } | |||||||||||||||||||||||||||
| 333 | 2640 | map { _bracketed_form_of_variable_name( $_ ) } | |||||||||||||||||||||||||||
| 334 | 40 | grep { q<%> ne substr $_, 0, 1 } | |||||||||||||||||||||||||||
| 335 | @MAGIC_VARIABLES | ||||||||||||||||||||||||||||
| 336 | ) | ||||||||||||||||||||||||||||
| 337 | . ')'; | ||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||
| 339 | 40 | return qr< | |||||||||||||||||||||||||||
| 340 | (?: \A | [^\\] ) # beginning-of-string or any non-backslash | ||||||||||||||||||||||||||||
| 341 | (?: \\{2} )* # zero or more double-backslashes | ||||||||||||||||||||||||||||
| 342 | ( $magic_alternation ) # any magic punctuation variable | ||||||||||||||||||||||||||||
| 343 | >xsm; | ||||||||||||||||||||||||||||
| 344 | } | ||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||
| 346 | 15 | ▶ | sub _make_violation { | ||||||||||||||||||||||||||
|
Subroutine
_make_violation: called CC=2 SCAR=7.6
| |||||||||||||||||||||||||||||
| 347 | 15 | my ( $self, $desc, $expl, $elem, $vars ) = @_; | |||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||
| 349 | my $vname = 'HASH' eq ref $vars ? | ||||||||||||||||||||||||||||
| 350 | 0 | ▶ | join ', ', sort keys %{ $vars } : | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 351 | $elem->content(); | ||||||||||||||||||||||||||||
| 352 | 15 | return $self->violation( sprintf( $desc, $vname ), $expl, $elem ); | |||||||||||||||||||||||||||
| 353 | } | ||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||
| 355 | 40 | 1; | |||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||
| 357 | __END__ | ||||||||||||||||||||||||||||