blib/lib/Perl/Critic/Policy/RegularExpressions/RequireExtendedFormatting.pm

filter: Statementstmt 90.6% 29 / 32 Branchbran 25.0% 2 / 8 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 11 / 11 Podpod 80.0% 4 / 5 totaltotal 77.0% 47 / 61 CC 6 SCAR 17.8
CC
6
Cov
74%
violates
14.2
BEGIN
1.0
BEGIN
1.0
CRAP 6.0
« blib/lib/Perl/Critic/Policy/TestingAndDebugging/RequireUseWarnings.pm ↑ Summary blib/lib/Perl/Critic/Policy/Variables/ProhibitConditionalDeclarations.pm »
1 package Perl::Critic::Policy::RegularExpressions::RequireExtendedFormatting;
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 Perl::Critic::Utils qw{ :severities };
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 40 ▶use parent 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 40 our $VERSION = '1.150';
13  
14 #-----------------------------------------------------------------------------
15  
16 40 Readonly::Scalar my $DESC => q{Regular expression without "/x" flag};
17 40 Readonly::Scalar my $EXPL => [ 236 ];
18  
19 #-----------------------------------------------------------------------------
20  
21 sub supported_parameters {
22 return (
23 {
24 92 ▶ name => 'minimum_regex_length_to_complain_about',
Subroutine supported_parameters: called CC=1 SCAR=0.0
Pod undocumented
25 description =>
26 q<The number of characters that a regular expression must contain before this policy will complain.>,
27 behavior => 'integer',
28 default_string => '0',
29 integer_minimum => 0,
30 },
31 {
32 name => 'strict',
33 description =>
34 q<Should regexes that only contain whitespace and word characters be complained about?>,
35 behavior => 'boolean',
36 default_string => '0',
37 },
38 );
39 }
40  
41 74 ▶sub default_severity { return $SEVERITY_MEDIUM }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
42 86 ▶sub default_themes { return qw< core pbp maintenance > }
Subroutine default_themes: called CC=1 SCAR=0.0
Pod documented
43 sub applies_to {
44 31 ▶ return qw<
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
45 PPI::Token::Regexp::Match
46 PPI::Token::Regexp::Substitute
47 PPI::Token::QuoteLike::Regexp
48 >;
49 }
50  
51 #-----------------------------------------------------------------------------
52  
53 sub violates {
54 1 ▶ my ( $self, $elem, $doc ) = @_;
Subroutine violates: called CC=6 SCAR=26.5
Pod documented
55  
56 1 my $match = $elem->get_match_string();
57 1 ▶ return if length $match <= $self->{_minimum_regex_length_to_complain_about};
Branch: 1 branch
BranchTrueFalseTotal
if length $match <= $self->{"_minimum_regex_length_to_complain_about"} 0 1 1
58 1 ▶ return if not $self->{_strict} and $match =~ m< \A [\s\w]* \z >xms;
Branch: 1 branch
BranchTrueFalseTotal
if not $self->{"_strict"} and $match =~ / \A [\s\w]* \z /msx 1 0 1
Condition33%1/3
not $self->{"_strict"} and $match =~ / \A [\s\w]* \z /msx
!ll&&!rl&&r
001
MC/DC0%0/2
not $self->{"_strict"} and $match =~ / \A [\s\w]* \z /msx
not $self->{"_strict"} $match =~ / \A [\s\w]* \z /msx
Truth table33%1/3
not(A) and B
A= not $self->{"_strict"}B= $match =~ / \A [\s\w]* \z /msx
ABresult
0X0
100
111
59  
60 0 ▶ my $re = $doc->ppix_regexp_from_element( $elem )
Branch: 1 branch
BranchTrueFalseTotal
my $re = $doc->ppix_regexp_from_element($elem) or return 0 0 0
61 or return;
62 0 ▶ $re->modifier_asserted( 'x' )
Branch: 1 branch
BranchTrueFalseTotal
$re->modifier_asserted("x") or return $self->violation($DESC, $EXPL, $elem) 0 0 0
63 or return $self->violation( $DESC, $EXPL, $elem );
64  
65 0 return; # ok!;
66 }
67  
68 40 1;
69  
70 __END__
71  
72 #-----------------------------------------------------------------------------
73  
74 =pod
75  
76 =head1 NAME
77  
78 Perl::Critic::Policy::RegularExpressions::RequireExtendedFormatting - Always use the C</x> modifier with regular expressions.
79  
80  
81 =head1 AFFILIATION
82  
83 This Policy is part of the core L<Perl::Critic|Perl::Critic>
84 distribution.
85  
86  
87 =head1 DESCRIPTION
88  
89 Extended regular expression formatting allows you mix whitespace and
90 comments into the pattern, thus making them much more readable.
91  
92 # Match a single-quoted string efficiently...
93  
94 m{'[^\\']*(?:\\.[^\\']*)*'}; #Huh?
95  
96 # Same thing with extended format...
97  
98 m{
99 ' # an opening single quote
100 [^\\'] # any non-special chars (i.e. not backslash or single quote)
101 (?: # then all of...
102 \\ . # any explicitly backslashed char
103 [^\\']* # followed by a non-special char
104 )* # ...repeated zero or more times
105 ' # a closing single quote
106 }x;
107  
108  
109 =head1 CONFIGURATION
110  
111 You might find that putting a C</x> on short regular expressions to be
112 excessive. An exception can be made for them by setting
113 C<minimum_regex_length_to_complain_about> to the minimum match length
114 you'll allow without a C</x>. The length only counts the regular
115 expression, not the braces or operators.
116  
117 [RegularExpressions::RequireExtendedFormatting]
118 minimum_regex_length_to_complain_about = 5
119  
120 $num =~ m<(\d+)>; # ok, only 5 characters
121 $num =~ m<\d\.(\d+)>; # not ok, 9 characters
122  
123 This option defaults to 0.
124  
125 Because using C</x> on a regex which has whitespace in it can make it
126 harder to read (you have to escape all that innocent whitespace), by
127 default, you can have a regular expression that only contains
128 whitespace and word characters without the modifier. If you want to
129 restrict this, turn on the C<strict> option.
130  
131 [RegularExpressions::RequireExtendedFormatting]
132 strict = 1
133  
134 $string =~ m/Basset hounds got long ears/; # no longer ok
135  
136 This option defaults to false.
137  
138  
139 =head1 NOTES
140  
141 For common regular expressions like e-mail addresses, phone numbers,
142 dates, etc., have a look at the L<Regexp::Common|Regexp::Common> module.
143 Also, be cautions about slapping modifier flags onto existing regular
144 expressions, as they can drastically alter their meaning. See
145 L<http://www.perlmonks.org/?node_id=484238> for an interesting
146 discussion on the effects of blindly modifying regular expression
147 flags.
148  
149  
150 =head1 TO DO
151  
152 Add an exemption for regular expressions that contain C<\Q> at the
153 front and don't use C<\E> until the very end, if at all.
154  
155  
156 =head1 AUTHOR
157  
158 Jeffrey Ryan Thalhammer <jeff@imaginative-software.com>
159  
160  
161 =head1 COPYRIGHT
162  
163 Copyright (c) 2005-2011 Imaginative Software Systems. All rights reserved.
164  
165 This program is free software; you can redistribute it and/or modify
166 it under the same terms as Perl itself. The full text of this license
167 can be found in the LICENSE file included with this module.
168  
169 =cut
170  
171 # Local Variables:
172 # mode: cperl
173 # cperl-indent-level: 4
174 # fill-column: 78
175 # indent-tabs-mode: nil
176 # c-indentation-style: bsd
177 # End:
178 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :
« blib/lib/Perl/Critic/Policy/TestingAndDebugging/RequireUseWarnings.pm ↑ Summary blib/lib/Perl/Critic/Policy/Variables/ProhibitConditionalDeclarations.pm »