File Coverage

blib/lib/Perl/Critic/Policy/Lax/ProhibitEmptyQuotes/ExceptAsFallback.pm
Criterion Covered Total %
statement 21 22 95.4
branch 6 6 100.0
condition 5 6 83.3
subroutine 7 8 87.5
pod 4 4 100.0
total 43 46 93.4


line stmt bran cond sub pod time code
1 7     7   3999 use strict;
  7         15  
  7         189  
2 7     7   33 use warnings;
  7         11  
  7         347  
3             package Perl::Critic::Policy::Lax::ProhibitEmptyQuotes::ExceptAsFallback 0.014;
4             # ABSTRACT: empty quotes are okay as the fallback on the rhs of ||
5              
6             #pod =head1 DESCRIPTION
7             #pod
8             #pod Sure, C<""> can be confusing when crammed into the middle of a big list of
9             #pod values, and a bunch of spaces is even worse. It's really common, though, to
10             #pod write this code to get a default, false, defined string:
11             #pod
12             #pod my $value = $got || '';
13             #pod
14             #pod It's got a certain charm about it that just isn't manifested by these:
15             #pod
16             #pod my $value = $got || $EMPTY;
17             #pod my $value = $got || q{};
18             #pod
19             #pod This policy prohibits all-whitespace strings constructed by single or double
20             #pod quotes, except for the empty string when it follows the high-precedence "or" or "defined or" operators.
21             #pod
22             #pod =cut
23              
24 7     7   36 use Perl::Critic::Utils;
  7         14  
  7         105  
25 7     7   4958 use parent qw(Perl::Critic::Policy);
  7         12  
  7         36  
26              
27             my $DESCRIPTION = q{Quotes used with an empty string, and not as a fallback};
28             my $EXPLANATION = "Unless you're using the ||'' idiom, use a quotish form.";
29              
30             my $empty_rx = qr{\A ["'] (\s*) ['"] \z}x;
31              
32 4     4 1 40 sub default_severity { $SEVERITY_LOW }
33 0     0 1 0 sub default_themes { qw(lax) }
34 10     10 1 75867 sub applies_to { 'PPI::Token::Quote' }
35              
36             sub violates {
37 10     10 1 176 my ($self, $element, undef) = @_;
38              
39 10         45 my ($content) = $element =~ $empty_rx;
40 10 100       71 return unless defined $content;
41              
42             # If the string is truly empty and comes after || or //, that's cool.
43 8 100 66     52 if (not length $content and my $prev = $element->sprevious_sibling) {
44             return if $prev->isa('PPI::Token::Operator')
45 7 100 100     279 && grep { $prev eq $_ } ('||', '//');
  12         98  
46             }
47              
48 4         36 return $self->violation($DESCRIPTION, $EXPLANATION, $element);
49             }
50              
51             1;
52              
53             __END__
54              
55             =pod
56              
57             =encoding UTF-8
58              
59             =head1 NAME
60              
61             Perl::Critic::Policy::Lax::ProhibitEmptyQuotes::ExceptAsFallback - empty quotes are okay as the fallback on the rhs of ||
62              
63             =head1 VERSION
64              
65             version 0.014
66              
67             =head1 DESCRIPTION
68              
69             Sure, C<""> can be confusing when crammed into the middle of a big list of
70             values, and a bunch of spaces is even worse. It's really common, though, to
71             write this code to get a default, false, defined string:
72              
73             my $value = $got || '';
74              
75             It's got a certain charm about it that just isn't manifested by these:
76              
77             my $value = $got || $EMPTY;
78             my $value = $got || q{};
79              
80             This policy prohibits all-whitespace strings constructed by single or double
81             quotes, except for the empty string when it follows the high-precedence "or" or "defined or" operators.
82              
83             =head1 PERL VERSION
84              
85             This library should run on perls released even a long time ago. It should work
86             on any version of perl released in the last five years.
87              
88             Although it may work on older versions of perl, no guarantee is made that the
89             minimum required version will not be increased. The version may be increased
90             for any reason, and there is no promise that patches will be accepted to lower
91             the minimum required perl.
92              
93             =head1 AUTHOR
94              
95             Ricardo Signes <cpan@semiotic.systems>
96              
97             =head1 COPYRIGHT AND LICENSE
98              
99             This software is copyright (c) 2022 by Ricardo Signes <cpan@semiotic.systems>.
100              
101             This is free software; you can redistribute it and/or modify it under
102             the same terms as the Perl 5 programming language system itself.
103              
104             =cut