blib/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
« blib/lib/Perl/Critic/Policy/Documentation/RequirePodSections.pm ↑ Summary blib/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.150';
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 sub supported_parameters {
25 return (
26 {
27 91 ▶ name => 'rcs_keywords',
Subroutine supported_parameters: called CC=1 SCAR=0.0
Pod undocumented
28 description => 'RCS keywords to ignore in potential interpolation.',
29 default_string => $EMPTY,
30 behavior => 'string list',
31 },
32 );
33 }
34  
35 75 ▶sub default_severity { return $SEVERITY_LOWEST }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
36 84 ▶sub default_themes { return qw(core pbp cosmetic) }
Subroutine default_themes: called CC=1 SCAR=0.0
Pod documented
37  
38 sub applies_to {
39 31 ▶ return qw< PPI::Token::Quote::Single PPI::Token::Quote::Literal >;
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
40 }
41  
42 #-----------------------------------------------------------------------------
43  
44 sub initialize_if_enabled {
45 47 ▶ my ($self, $config) = @_;
Subroutine initialize_if_enabled: called CC=2 SCAR=7.8
Pod documented
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 sub violates {
59 71 ▶ my ( $self, $elem, undef ) = @_;
Subroutine violates: called CC=6 SCAR=32.2
Pod documented
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 {
!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 sub _needs_interpolation {
76 71 ▶ my ($string) = @_;
Subroutine _needs_interpolation: called CC=2 SCAR=8.1
77  
78 return
79 # Contains a $ or @ not followed by "{}".
80 71 ▶ $string =~ m< [\$\@] (?! [{] [}] ) \S+ >xms
Condition33%1/3
$string =~ / [\$\@] (?! [{] [}] ) \S+ /msx || $string =~ /
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 sub _looks_like_email_address {
125 0 ▶ my ($string) = @_;
Subroutine _looks_like_email_address: not called CC=4 SCAR=30.0
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 sub _looks_like_use_vars {
137 0 ▶ my ($elem) = @_;
Subroutine _looks_like_use_vars: not called CC=4 SCAR=30.0
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__
151  
152 #-----------------------------------------------------------------------------
153  
154 =pod
155  
156 =for stopwords RCS
157  
158 =head1 NAME
159  
160 Perl::Critic::Policy::ValuesAndExpressions::RequireInterpolationOfMetachars - Warns that you might have used single quotes when you really wanted double-quotes.
161  
162  
163 =head1 AFFILIATION
164  
165 This Policy is part of the core L<Perl::Critic|Perl::Critic>
166 distribution.
167  
168  
169 =head1 DESCRIPTION
170  
171 This policy warns you if you use single-quotes or C<q//> with a string
172 that has unescaped metacharacters that may need interpolation. Its
173 hard to know for sure if a string really should be interpolated
174 without looking into the symbol table. This policy just makes an
175 educated guess by looking for metacharacters and sigils which usually
176 indicate that the string should be interpolated.
177  
178  
179 =head2 Exceptions
180  
181 =over
182  
183 =item *
184  
185 Variable names to C<use vars>:
186  
187 use vars '$x'; # ok
188 use vars ('$y', '$z'); # ok
189 use vars qw< $a $b >; # ok
190  
191  
192 =item *
193  
194 Things that look like e-mail addresses:
195  
196 print 'john@foo.com'; # ok
197 $address = 'suzy.bar@baz.net'; # ok
198  
199 =back
200  
201  
202 =head1 CONFIGURATION
203  
204 The C<rcs_keywords> option allows you to stop this policy from complaining
205 about things that look like RCS variables, for example, in deriving values for
206 C<$VERSION> variables.
207  
208 For example, if you've got code like
209  
210 our ($VERSION) = (q<$Revision$> =~ m/(\d+)/mx);
211  
212 You can specify
213  
214 [ValuesAndExpressions::RequireInterpolationOfMetachars]
215 rcs_keywords = Revision
216  
217 in your F<.perlcriticrc> to provide an exemption.
218  
219  
220 =head1 NOTES
221  
222 Perl's own C<warnings> pragma also warns you about this.
223  
224  
225 =head1 SEE ALSO
226  
227 L<Perl::Critic::Policy::ValuesAndExpressions::ProhibitInterpolationOfLiterals|Perl::Critic::Policy::ValuesAndExpressions::ProhibitInterpolationOfLiterals>
228  
229  
230 =head1 AUTHOR
231  
232 Jeffrey Ryan Thalhammer <jeff@imaginative-software.com>
233  
234  
235 =head1 COPYRIGHT
236  
237 Copyright (c) 2005-2023 Imaginative Software Systems.
238  
239 This program is free software; you can redistribute it and/or modify
240 it under the same terms as Perl itself. The full text of this license
241 can be found in the LICENSE file included with this module.
242  
243 =cut
244  
245 # Local Variables:
246 # mode: cperl
247 # cperl-indent-level: 4
248 # fill-column: 78
249 # indent-tabs-mode: nil
250 # c-indentation-style: bsd
251 # End:
252 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :
« blib/lib/Perl/Critic/Policy/Documentation/RequirePodSections.pm ↑ Summary blib/lib/Perl/Critic/Policy/ControlStructures/ProhibitPostfixControls.pm »