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

filter: Statementstmt 91.8% 45 / 49 Branchbran 35.7% 5 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 93.3% 14 / 15 Podpod 80.0% 4 / 5 totaltotal 81.9% 68 / 83 CC 10 SCAR 13.9
CC
10
Cov
79%
_parse_allow
7.9
violates
7.1
__ANON__
1.1
CRAP 4.0
« lib/Perl/Critic/Policy/BuiltinFunctions/RequireBlockGrep.pm ↑ Summary lib/Perl/Critic/Policy/Variables/ProhibitMatchVars.pm »
1 package Perl::Critic::Policy::ValuesAndExpressions::ProhibitInterpolationOfLiterals;
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  
8 40 ▶use List::SomeUtils qw(any);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 40 ▶use Perl::Critic::Utils qw{ :characters :severities :data_conversion };
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 40 our $VERSION = '1.156';
14  
15 #-----------------------------------------------------------------------------
16  
17 40 Readonly::Scalar my $DESC => q{Useless interpolation of literal string};
18 40 Readonly::Scalar my $EXPL => [51];
19  
20 #-----------------------------------------------------------------------------
21  
22 92 ▶sub supported_parameters {
Subroutine supported_parameters: called CC=1 SCAR=0.0
Pod undocumented
23 return (
24 {
25 92 name => 'allow',
26 description =>
27 'Kinds of delimiters to permit, e.g. "qq{", "qq(", "qq[", "qq/".',
28 default_string => $EMPTY,
29 parser => \&_parse_allow,
30 },
31 {
32 name => 'allow_if_string_contains_single_quote',
33 description =>
34 q<If the string contains ' characters, allow "" to quote it.>,
35 default_string => '0',
36 behavior => 'boolean',
37 },
38 );
39 }
40  
41 75 ▶sub default_severity { return $SEVERITY_LOWEST }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
42 83 ▶sub default_themes { return qw( core pbp cosmetic ) }
Subroutine default_themes: called CC=1 SCAR=0.0
Pod documented
43 31 ▶sub applies_to { return qw(PPI::Token::Quote::Double
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
44 PPI::Token::Quote::Interpolate) }
45  
46 #-----------------------------------------------------------------------------
47  
48 40 Readonly::Scalar my $MAX_SPECIFICATION_LENGTH => 3;
49  
50 90 ▶sub _parse_allow {
Subroutine _parse_allow: called CC=6 SCAR=20.7
51 90 my ($self, undef, $config_string) = @_;
52  
53 90 my @allow;
54  
55 90 ▶ if (defined $config_string) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $config_string) 2 88 90
56 2 @allow = words_from_string( $config_string );
57 #Try to be forgiving with the configuration...
58 2 for (@allow) {
59 0 ▶ m{ \A qq }xms || ($_ = 'qq' . $_)
Branch: 1 branch
BranchTrueFalseTotal
/ \A qq /msx or $_ = 'qq' . $_ 0 0 0
60 } #Add 'qq'
61 2 for (@allow) {
62 0 ▶ (length $_ <= $MAX_SPECIFICATION_LENGTH) || chop
Branch: 1 branch
BranchTrueFalseTotal
length $_ <= $MAX_SPECIFICATION_LENGTH or chop $_ 0 0 0
63 } #Chop closing char
64 }
65  
66 90 $self->{_allow} = \@allow;
67  
68 90 return;
69 }
70  
71 #-----------------------------------------------------------------------------
72  
73 1 ▶sub violates {
Subroutine violates: called CC=5 SCAR=19.6
Pod documented
74 1 my ( $self, $elem, undef ) = @_;
75  
76 # Skip if this string needs interpolation
77 1 ▶ return if _has_interpolation($elem);
Branch: 1 branch
BranchTrueFalseTotal
if _has_interpolation($elem) 0 1 1
78  
79 # Overlook allowed quote styles
80 0 ▶ return if any { $elem =~ m{ \A \Q$_\E }xms } @{ $self->{_allow} };
Subroutine __ANON__: not called CC=1 SCAR=0.6
Branch: 1 branch
BranchTrueFalseTotal
if &any(sub { $elem =~ / \A \Q$_\E /msx; } , @{$$self{"_allow"};}) 0 1 1
81  
82 # If the flag is set, allow "I'm here".
83 1 ▶ if ( $self->{_allow_if_string_contains_single_quote} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_allow_if_string_contains_single_quote"}) 0 1 1
84 0 ▶ return if index ($elem, $QUOTE) >= 0;
Branch: 1 branch
BranchTrueFalseTotal
if index($elem, $Perl::Critic::Policy::ValuesAndExpressions::ProhibitInterpolationOfLiterals::QUOTE) != -1 0 0 0
85 }
86  
87 # Must be a violation
88 1 return $self->violation( $DESC, $EXPL, $elem );
89 }
90  
91 #-----------------------------------------------------------------------------
92  
93 1 ▶sub _has_interpolation {
Subroutine _has_interpolation: called CC=1 SCAR=0.0
94 1 my $elem = shift;
95 1 return $elem =~ m<
96 (?: \A | [^\\] )
97 (?: \\{2} )*
98 (?: [\$\@] \S+ | \\[tnrfbae0xcNLuLUEQ] )
99 >xmso;
100 }
101  
102 40 1;
103  
104 __END__
« lib/Perl/Critic/Policy/BuiltinFunctions/RequireBlockGrep.pm ↑ Summary lib/Perl/Critic/Policy/Variables/ProhibitMatchVars.pm »