Policy/Bangs/ProhibitUselessRegexModifiers.pm

filter: Statementstmt 95.7% 45 / 47 Branchbran 94.4% 17 / 18 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 92.8% 13 / 14 Podpod 80.0% 4 / 5 totaltotal 94.3% 84 / 89 CC 11 SCAR 10.6
CC
11
Cov
96%
violates
6.0
_previously_assigned_quote_like_operator
3.0
_find_previous_quote_like_regexp
3.0
CRAP 2.9
« Policy/Bangs/ProhibitDebuggingModules.pm ↑ Summary Policy/Bangs/ProhibitNoPlan.pm »
1 package Perl::Critic::Policy::Bangs::ProhibitUselessRegexModifiers;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 5 ▶use Perl::Critic::Utils qw{ :severities :classification :ppi };
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use base 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 5 our $VERSION = '1.14';
11  
12 5 Readonly::Scalar my $DESC => q{Prohibits adding "m" modifier to compiled regular expressions where it does nothing};
13 5 Readonly::Scalar my $EXPL => <<'EOF';
14 There is a bug in 5.8.x in that /$re/sm would incorrectly apply the
15 /sm modifiers to a regular expression. This makes the code work, but
16 for the wrong reason. In 5.10.0, this bug is "fixed" so that the
17 modifier no longer works, but no warning is emitted to tell you that
18 the modifiers are ignored.
19 http://perlbuzz.com/mechanix/2007/12/code-broken-by-regex-fixes-in.html
20 EOF
21  
22  
23 10 ▶sub supported_parameters { return () }
Subroutine supported_parameters: called CC=1 SCAR=0.0
Pod undocumented
24 2 ▶sub default_severity { return $SEVERITY_HIGH }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
25 0 ▶sub default_themes { return qw( bangs bugs ) }
Subroutine default_themes: not called CC=1 SCAR=6.9
Pod documented
26 7 ▶sub applies_to { return 'PPI::Token::Regexp' }
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
27  
28  
29 7 ▶sub violates {
Subroutine violates: called CC=6 SCAR=17.9
Pod documented
30 7 my ( $self, $elem, undef ) = @_;
31  
32  
33 # We throw a violation if all these conditions are true:
34 # 1) there's an 'm' modifier
35 # 2) the *only* thing in the regex is a compiled regex from a previous qr().
36 # 3) the modifiers are not the same in both places
37 7 my %mods = $elem->get_modifiers();
38 7 ▶ if ( $mods{'m'} || $mods{'s'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mods{"m"} or $mods{"s"}) 5 2 7
Condition100%3/3
$mods{"m"} or $mods{"s"}
l!l&&r!l&&!r
412
MC/DC100%2/2
$mods{"m"} or $mods{"s"}
$mods{"m"} $mods{"s"}
Truth table100%3/3
A or B
A= $mods{"m"}B= $mods{"s"}
ABresult
1X1
011
000
39 5 my $match = $elem->get_match_string();
40 5 ▶ if ( $match =~ /^\$\w+$/smx ) { # It looks like a single variable in there
Branch: 1 branch
BranchTrueFalseTotal
if ($match =~ /^\$\w+$/msx) 4 1 5
41 4 ▶ if ( my $qr = _previously_assigned_quote_like_operator( $elem, $match ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $qr = _previously_assigned_quote_like_operator($elem, $match)) 3 1 4
42 # don't violate if both regexes are modified in the same way
43 3 ▶ if ( _sorted_modifiers( $elem ) ne _sorted_modifiers( $qr ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_sorted_modifiers($elem) ne _sorted_modifiers($qr)) 2 1 3
44 2 return $self->violation( $DESC, $EXPL, $elem );
45 }
46 }
47 }
48 }
49 5 return; #ok!;
50 }
51  
52 4 ▶sub _previously_assigned_quote_like_operator {
Subroutine _previously_assigned_quote_like_operator: called CC=3 SCAR=11.1
53 4 my ( $elem, $match ) = @_;
54  
55 4 ▶ my $qlop = _find_previous_quote_like_regexp( $elem ) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $qlop = _find_previous_quote_like_regexp($elem) or return 3 1 4
56  
57 # find if this previous quote-like-regexp assigned to the variable in $match
58 3 my $parent = $qlop->parent();
59 3 ▶ if ( $parent->find_any( sub { $_[1]->isa( 'PPI::Token::Symbol' ) and
Subroutine __ANON__: called CC=2 SCAR=7.0
Branch: 2 branches
BranchTrueFalseTotal
if ($parent->find_any(sub { $_[1]->isa("PPI::Token::Symbol") and $_[1]->content eq $match; } )) 3 0 3
$_[1]->isa("PPI::Token::Symbol") and $_[1]->content eq $match 3 6 9
60 $_[1]->content eq $match } ) ) {
61 3 return $qlop;
62 }
63 0 return;
64 }
65  
66  
67 4 ▶sub _find_previous_quote_like_regexp {
Subroutine _find_previous_quote_like_regexp: called CC=3 SCAR=11.0
68 4 my $elem = shift;
69  
70 4 my $qlop = $elem;
71 4 ▶ while ( ! $qlop->isa( 'PPI::Token::QuoteLike::Regexp' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($qlop->isa("PPI::Token::QuoteLike::Regexp")) 52 3 55
72 # We use previous_token instead of sprevious_sibling to get into previous statements.
73 52 ▶ $qlop = $qlop->previous_token() or return;
Branch: 1 branch
BranchTrueFalseTotal
$qlop = $qlop->previous_token or return 51 1 52
74 }
75 3 return $qlop;
76 }
77  
78 6 ▶sub _sorted_modifiers {
Subroutine _sorted_modifiers: called CC=1 SCAR=0.0
79 6 my $elem = shift;
80  
81 6 my %mods = $elem->get_modifiers();
82 6 return join( '', sort keys %mods );
83 }
84  
85 5 1;
86  
87 __END__
« Policy/Bangs/ProhibitDebuggingModules.pm ↑ Summary Policy/Bangs/ProhibitNoPlan.pm »