File Coverage

blib/lib/Perl/Critic/Policy/ValuesAndExpressions/ProhibitEmptyQuotes.pm
Criterion Covered Total %
statement 25 25 100.0
branch 2 2 100.0
condition n/a
subroutine 11 11 100.0
pod 4 5 80.0
total 42 43 97.6


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::ValuesAndExpressions::ProhibitEmptyQuotes;
2              
3 40     40   27024 use 5.010001;
  40         170  
4 40     40   242 use strict;
  40         101  
  40         846  
5 40     40   238 use warnings;
  40         105  
  40         1192  
6 40     40   212 use Readonly;
  40         99  
  40         3311  
7              
8 40     40   251 use Perl::Critic::Utils qw{ :severities };
  40         117  
  40         2204  
9 40     40   5264 use parent 'Perl::Critic::Policy';
  40         119  
  40         253  
10              
11             our $VERSION = '1.146';
12              
13             #-----------------------------------------------------------------------------
14              
15             Readonly::Scalar my $EMPTY_RX => qr{\A ["'] \s* ['"] \z}xms;
16             Readonly::Scalar my $DESC => q<Quotes used with a string containing no non-whitespace characters>;
17             Readonly::Scalar my $EXPL => [ 53 ];
18              
19             #-----------------------------------------------------------------------------
20              
21 92     92 0 1683 sub supported_parameters { return () }
22 116     116 1 539 sub default_severity { return $SEVERITY_LOW }
23 84     84 1 344 sub default_themes { return qw(core pbp cosmetic) }
24 33     33 1 112 sub applies_to { return 'PPI::Token::Quote' }
25              
26             #-----------------------------------------------------------------------------
27              
28             sub violates {
29 88     88 1 297 my ( $self, $elem, undef ) = @_;
30 88 100       448 if ( $elem =~ $EMPTY_RX ) {
31 42         448 return $self->violation( $DESC, $EXPL, $elem );
32             }
33 46         405 return; #ok!
34             }
35              
36             1;
37              
38             __END__
39              
40             #-----------------------------------------------------------------------------
41              
42             =pod
43              
44             =head1 NAME
45              
46             Perl::Critic::Policy::ValuesAndExpressions::ProhibitEmptyQuotes - Write C<q{}> instead of C<''>.
47              
48             =head1 AFFILIATION
49              
50             This Policy is part of the core L<Perl::Critic|Perl::Critic>
51             distribution.
52              
53              
54             =head1 DESCRIPTION
55              
56             Don't use quotes for an empty string or any string that is pure
57             whitespace. Instead, use C<q{}> to improve legibility. Better still,
58             created named values like this. Use the C<x> operator to repeat
59             characters.
60              
61             $message = ''; #not ok
62             $message = ""; #not ok
63             $message = " "; #not ok
64              
65             $message = q{}; #better
66             $message = q{ } #better
67              
68             $EMPTY = q{};
69             $message = $EMPTY; #best
70              
71             $SPACE = q{ };
72             $message = $SPACE x 5; #best
73              
74              
75             =head1 CONFIGURATION
76              
77             This Policy is not configurable except for the standard options.
78              
79              
80             =head1 SEE ALSO
81              
82             L<Perl::Critic::Policy::ValuesAndExpressions::ProhibitNoisyStrings|Perl::Critic::Policy::ValuesAndExpressions::ProhibitNoisyStrings>
83              
84             =head1 AUTHOR
85              
86             Jeffrey Ryan Thalhammer <jeff@imaginative-software.com>
87              
88             =head1 COPYRIGHT
89              
90             Copyright (c) 2005-2011 Imaginative Software Systems. All rights reserved.
91              
92             This program is free software; you can redistribute it and/or modify
93             it under the same terms as Perl itself. The full text of this license
94             can be found in the LICENSE file included with this module.
95              
96             =cut
97              
98             # Local Variables:
99             # mode: cperl
100             # cperl-indent-level: 4
101             # fill-column: 78
102             # indent-tabs-mode: nil
103             # c-indentation-style: bsd
104             # End:
105             # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :