lib/Perl/Critic/Policy/ValuesAndExpressions/RequireInterpolationOfMetachars.pm

filter: Statementstmt 73.7% 59 / 80 Branchbran 9.0% 2 / 22 Conditioncond 16.6% 1 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 82.3% 14 / 17 Podpod 83.3% 5 / 6 totaltotal 60.0% 81 / 135 CC 14 SCAR 24.1
CC
14
Cov
57%
violates
25.1
_looks_like_email_address
20.0
_looks_like_use_vars
20.0
CRAP 11.1
« lib/Perl/Critic/Policy/Documentation/RequirePodSections.pm ↑ Summary lib/Perl/Critic/Policy/ControlStructures/ProhibitPostfixControls.pm »
1 package Perl::Critic::Policy::ValuesAndExpressions::RequireInterpolationOfMetachars;
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  
7 40 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 40 ▶use List::SomeUtils qw(any);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 40 ▶use Perl::Critic::Utils qw< :booleans :characters :severities >;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 40 ▶use parent 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 #-----------------------------------------------------------------------------
14  
15 40 our $VERSION = '1.156';
16  
17 #-----------------------------------------------------------------------------
18  
19 40 Readonly::Scalar my $DESC => q<String *may* require interpolation>;
20 40 Readonly::Scalar my $EXPL => [ 51 ];
21  
22 #-----------------------------------------------------------------------------
23  
24 91 ▶sub supported_parameters {
Subroutine supported_parameters: called CC=1 SCAR=0.0
Pod undocumented
25 return (
26 {
27 91 name => 'rcs_keywords',
28 description => 'RCS keywords to ignore in potential interpolation.',
29 default_string => $EMPTY,
30 behavior => 'string list',
31 },
32 );
33 }
34  
35 74 ▶sub default_severity { return $SEVERITY_LOWEST }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
36 83 ▶sub default_themes { return qw(core pbp cosmetic) }
Subroutine default_themes: called CC=1 SCAR=0.0
Pod documented
37  
38 31 ▶sub applies_to {
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
39 31 return qw< PPI::Token::Quote::Single PPI::Token::Quote::Literal >;
40 }
41  
42 #-----------------------------------------------------------------------------
43  
44 47 ▶sub initialize_if_enabled {
Subroutine initialize_if_enabled: called CC=2 SCAR=7.8
Pod documented
45 47 my ($self, undef) = @_;
46  
47 47 my $rcs_keywords = $self->{_rcs_keywords};
48 47 my @rcs_keywords = keys %{$rcs_keywords};
49  
50 47 ▶ if (@rcs_keywords) {
Branch: 1 branch
BranchTrueFalseTotal
if (@rcs_keywords) 0 47 47
51 0 my $rcs_regexes = [ map { qr/ \$ $_ [^\n\$]* \$ /xms } @rcs_keywords ];
52 0 $self->{_rcs_regexes} = $rcs_regexes;
53 }
54  
55 47 return $TRUE;
56 }
57  
58 71 ▶sub violates {
Subroutine violates: called CC=6 SCAR=32.2
Pod documented
59 71 my ( $self, $elem, undef ) = @_;
60  
61 # The string() method strips off the quotes
62 71 my $string = $elem->string();
63 71 ▶ return if not _needs_interpolation($string);
Branch: 1 branch
BranchTrueFalseTotal
unless _needs_interpolation($string) 71 0 71
64 0 ▶ return if _looks_like_email_address($string);
Branch: 1 branch
BranchTrueFalseTotal
if _looks_like_email_address($string) 0 0 0
65 0 ▶ return if _looks_like_use_vars($elem);
Branch: 1 branch
BranchTrueFalseTotal
if _looks_like_use_vars($elem) 0 0 0
66  
67 0 my $rcs_regexes = $self->{_rcs_regexes};
68 0 ▶ return if $rcs_regexes && any { $string =~ m/$_/xms } @{$rcs_regexes};
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if $rcs_regexes and &any(sub { $string =~ /$_/msx; } , @{$rcs_regexes;}) 0 0 0
Condition0%0/3
$rcs_regexes and &any(sub { $string =~ /$_/msx; } , @{$rcs_regexes;})
!ll&&!rl&&r
000
MC/DC0%0/2
$rcs_regexes and &any(sub { $string =~ /$_/msx; } , @{$rcs_regexes;})
$rcs_regexes &any(sub { $string =~ /$_/msx; } , @{$rcs_regexes;})
Truth table0%0/3
A and B
A= $rcs_regexesB= &any(sub { $string =~ /$_/msx; } , @{$rcs_regexes;})
ABresult
0X0
100
111
69  
70 0 return $self->violation( $DESC, $EXPL, $elem );
71 }
72  
73 #-----------------------------------------------------------------------------
74  
75 71 ▶sub _needs_interpolation {
Subroutine _needs_interpolation: called CC=2 SCAR=8.1
76 71 my ($string) = @_;
77  
78 return
79 # Contains a $ or @ not followed by "{}".
80 71 ▶ $string =~ m< [\$\@] (?! [{] [}] ) \S+ >xms
Condition33%1/3
$string =~ / [\$\@] (?! [{] [}] ) \S+ /msx || $string =~ / (?: \A | [^\\] ) (?: \\{2} )* \\ [tnrfbae01234567xcNluLUEQ] /msx
l!l&&r!l&&!r
0071
MC/DC0%0/2
$string =~ / [\$\@] (?! [{] [}] ) \S+ /msx || $string =~ / (?: \A | [^\\] ) (?: \\{2} )* \\ [tnrfbae01234567xcNluLUEQ] /msx
$string =~ / [\$\@] (?! [{] [}] ) \S+ /msx $string =~ / (?: \A | [^\\] ) (?: \\{2} )* \\ [tnrfbae01234567xcNluLUEQ] /msx
Truth table33%1/3
A || B
A= $string =~ / [\$\@] (?! [{] [}] ) \S+ /msxB= $string =~ / (?: \A | [^\\] ) (?: \\{2} )* \\ [tnrfbae01234567xcNluLUEQ] /msx
ABresult
1X1
011
000
81 # Contains metachars
82 # Note that \1 ... are not documented (that I can find), but are
83 # treated the same way as \0 by S_scan_const in toke.c, at least
84 # for regular double-quotish strings. Not, obviously, where
85 # regexes are involved.
86 || $string =~ m<
87 (?: \A | [^\\] )
88 (?: \\{2} )*
89 \\ [tnrfbae01234567xcNluLUEQ]
90 >xms;
91 }
92  
93 #-----------------------------------------------------------------------------
94  
95 # Stolen from Email::Address, which is deprecated. Since we are not modifying
96 # the original code at all, we are less stringent in being Critic-compliant.
97  
98 ## no critic ( RegularExpressions::RequireDotMatchAnything )
99 ## no critic ( RegularExpressions::RequireLineBoundaryMatching )
100 ## no critic ( RegularExpressions::ProhibitEscapedMetacharacters )
101  
102 40 my $CTL = q{\x00-\x1F\x7F}; ## no critic ( ValuesAndExpressions::RequireInterpolationOfMetachars )
103 40 my $special = q{()<>\\[\\]:;@\\\\,."}; ## no critic ( ValuesAndExpressions::RequireInterpolationOfMetachars )
104  
105 40 my $text = qr/[^\x0A\x0D]/x;
106 40 my $quoted_pair = qr/\\$text/x;
107 40 my $ctext = qr/(?>[^()\\]+)/x;
108 40 my $ccontent = qr/$ctext|$quoted_pair/x;
109 40 my $comment = qr/\s*\((?:\s*$ccontent)*\s*\)\s*/x;
110 40 my $cfws = qr/$comment|\s+/x;
111 40 my $atext = qq/[^$CTL$special\\s]/;
112 40 my $dot_atom_text = qr/$atext+(?:\.$atext+)*/x;
113 40 my $dot_atom = qr/$cfws*$dot_atom_text$cfws*/x;
114 40 my $qtext = qr/[^\\"]/x;
115 40 my $qcontent = qr/$qtext|$quoted_pair/x;
116 40 my $quoted_string = qr/$cfws*"$qcontent*"$cfws*/x;
117 40 my $local_part = qr/$dot_atom|$quoted_string/x;
118 40 my $dtext = qr/[^\[\]\\]/x;
119 40 my $dcontent = qr/$dtext|$quoted_pair/x;
120 40 my $domain_literal = qr/$cfws*\[(?:\s*$dcontent)*\s*\]$cfws*/x;
121 40 my $domain = qr/$dot_atom|$domain_literal/x;
122 40 my $addr_spec = qr/$local_part\@$domain/x;
123  
124 0 ▶sub _looks_like_email_address {
Subroutine _looks_like_email_address: not called CC=4 SCAR=30.0
125 0 my ($string) = @_;
126  
127 0 ▶ return if index ($string, q<@>) < 0;
Branch: 1 branch
BranchTrueFalseTotal
if index($string, "\@") == -1 0 0 0
128 0 ▶ return if $string =~ m< \W \@ >xms;
Branch: 1 branch
BranchTrueFalseTotal
if $string =~ / \W \@ /msx 0 0 0
129 0 ▶ return if $string =~ m< \A \@ \w+ \b >xms;
Branch: 1 branch
BranchTrueFalseTotal
if $string =~ / \A \@ \w+ \b /msx 0 0 0
130  
131 0 return $string =~ $addr_spec;
132 }
133  
134 #-----------------------------------------------------------------------------
135  
136 0 ▶sub _looks_like_use_vars {
Subroutine _looks_like_use_vars: not called CC=4 SCAR=30.0
137 0 my ($elem) = @_;
138  
139 0 my $statement = $elem;
140 0 ▶ while ( not $statement->isa('PPI::Statement::Include') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($statement->isa("PPI::Statement::Include")) 0 0 0
141 0 ▶ $statement = $statement->parent() or return;
Branch: 1 branch
BranchTrueFalseTotal
$statement = $statement->parent or return 0 0 0
142 }
143  
144 0 ▶ return if $statement->type() ne q<use>;
Branch: 1 branch
BranchTrueFalseTotal
if $statement->type ne "use" 0 0 0
145 0 return $statement->module() eq q<vars>;
146 }
147  
148 40 1;
149  
150 __END__
« lib/Perl/Critic/Policy/Documentation/RequirePodSections.pm ↑ Summary lib/Perl/Critic/Policy/ControlStructures/ProhibitPostfixControls.pm »