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   3417 use strict;
  7         8  
  7         163  
2 7     7   23 use warnings;
  7         7  
  7         296  
3             package Perl::Critic::Policy::Lax::ProhibitEmptyQuotes::ExceptAsFallback;
4             # ABSTRACT: empty quotes are okay as the fallback on the rhs of ||
5             $Perl::Critic::Policy::Lax::ProhibitEmptyQuotes::ExceptAsFallback::VERSION = '0.013';
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   22 use Perl::Critic::Utils;
  7         8  
  7         78  
25 7     7   3359 use parent qw(Perl::Critic::Policy);
  7         7  
  7         41  
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 31 sub default_severity { $SEVERITY_LOW }
33 0     0 1 0 sub default_themes { qw(lax) }
34 10     10 1 46500 sub applies_to { 'PPI::Token::Quote' }
35              
36             sub violates {
37 10     10 1 135 my ($self, $element, undef) = @_;
38              
39 10         38 my ($content) = $element =~ $empty_rx;
40 10 100       60 return unless defined $content;
41              
42             # If the string is truly empty and comes after || or //, that's cool.
43 8 100 66     34 if (not length $content and my $prev = $element->sprevious_sibling) {
44             return if $prev->isa('PPI::Token::Operator')
45 7 100 100     173 && grep { $prev eq $_ } ('||', '//');
  12         74  
46             }
47              
48 4         32 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.013
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 AUTHOR
84              
85             Ricardo Signes <rjbs@cpan.org>
86              
87             =head1 COPYRIGHT AND LICENSE
88              
89             This software is copyright (c) 2017 by Ricardo Signes <rjbs@cpan.org>.
90              
91             This is free software; you can redistribute it and/or modify it under
92             the same terms as the Perl 5 programming language system itself.
93              
94             =cut