Policy/Community/SplitQuotedPattern.pm

filter: Statementstmt 96.7% 30 / 31 Branchbran 83.3% 5 / 6 Conditioncond 90.4% 19 / 21 MC/DCmcdc 77.7% 7 / 9 Subroutinesub 90.9% 10 / 11 Podpod 80.0% 4 / 5 totaltotal 90.3% 75 / 83 CC 11 SCAR 18.5
CC
11
Cov
93%
violates
11.1
default_themes
2.0
BEGIN
1.0
CRAP 6.3
« Community/Utils.pm ↑ Summary Policy/Community/PackageMatchesFilename.pm »
1 package Perl::Critic::Policy::Community::SplitQuotedPattern;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Perl::Critic::Utils qw(:severities :classification :ppi);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use parent 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 our $VERSION = 'v1.0.4';
10  
11 1 ▶use constant DESC => 'split() called with a quoted pattern';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use constant EXPL => 'The first argument to split() is a regex pattern, not a string (other than the space character special case). Use slashes to quote the pattern argument.';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 6 ▶sub supported_parameters {
Subroutine supported_parameters: called CC=1 SCAR=0.0
Pod undocumented
15 (
16 {
17 6 name => 'allow_unquoted_patterns',
18 description => 'Allow unquoted expressions as the pattern argument to split',
19 default_string => '0',
20 behavior => 'boolean',
21 },
22 )
23 }
24 8 ▶sub default_severity { $SEVERITY_LOWEST }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
25 0 ▶sub default_themes { 'community' }
Subroutine default_themes: not called CC=1 SCAR=6.9
Pod documented
26 6 ▶sub applies_to { 'PPI::Token::Word' }
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
27  
28 31 ▶sub violates {
Subroutine violates: called CC=11 SCAR=24.1
Pod documented
29 31 my ($self, $elem) = @_;
30 31 ▶ return () unless $elem eq 'split' and is_function_call $elem;
Branch: 1 branch
BranchTrueFalseTotal
unless $elem eq "split" and is_function_call($elem) 2 29 31
Condition66%2/3
$elem eq "split" and is_function_call($elem)
!ll&&!rl&&r
2029
MC/DC50%1/2
$elem eq "split" and is_function_call($elem)
$elem eq "split" is_function_call($elem)
Truth table66%2/3
A and B
A= $elem eq "split"B= is_function_call($elem)
ABresult
0X0
100
111
31
32 29 my @args = parse_arg_list $elem;
33 29 ▶ return () unless @args;
Branch: 1 branch
BranchTrueFalseTotal
unless @args 0 29 29
34
35 29 my $pattern = $args[0][0];
36 return $self->violation(DESC, EXPL, $elem)
37 unless $pattern->isa('PPI::Token::Regexp::Match')
38 or $pattern->isa('PPI::Token::QuoteLike::Regexp')
39 or ($pattern->isa('PPI::Token::Quote') and (!length $pattern->string or $pattern->string eq ' '))
40 29 ▶ or ($self->{_allow_unquoted_patterns} and !$pattern->isa('PPI::Token::Quote'));
Branch: 1 branch
BranchTrueFalseTotal
unless $pattern->isa("PPI::Token::Regexp::Match") or $pattern->isa("PPI::Token::QuoteLike::Regexp") or $pattern->isa("PPI::Token::Quote") and !length($pattern->string) || $pattern->string eq " " or $self->{"_allow_unquoted_patterns"} and not $pattern->isa("PPI::Token::Quote") 8 21 29
Condition94%17/18
$pattern->isa("PPI::Token::Regexp::Match") or $pattern->isa("PPI::Token::QuoteLike::Regexp") or $pattern->isa("PPI::Token::Quote") and !length($pattern->string) || $pattern->string eq " " or $self->{"_allow_unquoted_patterns"} and not $pattern->isa("PPI::Token::Quote")
l!l&&r!l&&!r
1748
$pattern->isa("PPI::Token::Regexp::Match") or $pattern->isa("PPI::Token::QuoteLike::Regexp") or $pattern->isa("PPI::Token::Quote") and !length($pattern->string) || $pattern->string eq " "
l!l&&r!l&&!r
9812
$pattern->isa("PPI::Token::Regexp::Match") or $pattern->isa("PPI::Token::QuoteLike::Regexp")
l!l&&r!l&&!r
7220
$pattern->isa("PPI::Token::Quote") and !length($pattern->string) || $pattern->string eq " "
!ll&&!rl&&r
848
not length $pattern->string or $pattern->string eq " "
l!l&&r!l&&!r
444
$self->{"_allow_unquoted_patterns"} and not $pattern->isa("PPI::Token::Quote")
!ll&&!rl&&r
804
MC/DC85%6/7
$pattern->isa("PPI::Token::Regexp::Match") or $pattern->isa("PPI::Token::QuoteLike::Regexp") or $pattern->isa("PPI::Token::Quote") and !length($pattern->string) || $pattern->string eq " " or $self->{"_allow_unquoted_patterns"} and not $pattern->isa("PPI::Token::Quote")
$pattern->isa("PPI::Token::Regexp::Match") $pattern->isa("PPI::Token::QuoteLike::Regexp") $pattern->isa("PPI::Token::Quote") not length $pattern->string $pattern->string eq " " $self->{"_allow_unquoted_patterns"} not $pattern->isa("PPI::Token::Quote")
Truth table70%7/10
A or B or C and not(D) or E or F and not(G)
A= $pattern->isa("PPI::Token::Regexp::Match")B= $pattern->isa("PPI::Token::QuoteLike::Regexp")C= $pattern->isa("PPI::Token::Quote")D= not length $pattern->stringE= $pattern->string eq " "F= $self->{"_allow_unquoted_patterns"}G= not $pattern->isa("PPI::Token::Quote")
ABCDEFGresult
1XXXXXX1
01XXXXX1
0011XXX1
00101XX1
000XX111
00100111
000XX0X0
000XX100
001000X0
00100100
41
42 21 return ();
43 }
44  
45 1 1;
46  
47 =head1 NAME
48  
49 Perl::Critic::Policy::Community::SplitQuotedPattern - Quote the split() pattern
50 argument with regex slashes
51  
52 =head1 DESCRIPTION
53  
54 The first argument to the C<split()> function is a regex pattern, not a string.
55 It is commonly passed as a quoted string which does not make this clear, and
56 can lead to bugs when the string unintentionally contains unescaped regex
57 metacharacters. Regardless of the method of quoting, it will be parsed as a
58 pattern (apart from the space character special case described below). Use
59 slashes to quote this argument to make it clear that it is a regex pattern.
60  
61 Note that the special case of passing a single space character must be passed
62 as a quoted string, not a pattern. Additionally, this policy does not warn
63 about passing an empty string as this is a common idiom to split a string into
64 individual characters which does not risk containing regex metacharacters.
65  
66 By default, this policy also prohibits unquoted patterns such as scalar
67 variables, since this does not indicate that the argument is interpreted as a
68 regex pattern and not a string (unless it is a string containing a single space
69 character).
70  
71 split 'foo', $string; # not ok
72 split '.', $string; # not ok
73 split $pat, $string; # not ok
74 split /foo/, $string; # ok
75 split /./, $string; # ok
76 split /$pat/, $string; # ok
77 split qr/./, $string; # ok
78 split ' ', $string; # ok (and distinct from split / /)
79  
80 This policy is similar to the core policy
81 L<Perl::Critic::Policy::BuiltinFunctions::ProhibitStringySplit>, but
82 additionally allows empty string split patterns, and disallows unquoted split
83 patterns by default.
84  
85 =head1 AFFILIATION
86  
87 This policy is part of L<Perl::Critic::Community>.
88  
89 =head1 CONFIGURATION
90  
91 This policy can be configured to allow passing unquoted patterns (such as
92 scalar variables), by putting an entry in a F<.perlcritic> file like this:
93  
94 [Community::SplitQuotedPattern]
95 allow_unquoted_patterns = 1
96  
97 =head1 AUTHOR
98  
99 Dan Book, C<dbook@cpan.org>
100  
101 =head1 COPYRIGHT AND LICENSE
102  
103 Copyright 2024, Dan Book.
104  
105 This library is free software; you may redistribute it and/or modify it under
106 the terms of the Artistic License version 2.0.
107  
108 =head1 SEE ALSO
109  
110 L<Perl::Critic>
« Community/Utils.pm ↑ Summary Policy/Community/PackageMatchesFilename.pm »