File Coverage

blib/lib/Perl/Critic/Policy/CodeLayout/ProhibitHashBarewords.pm
Criterion Covered Total %
statement 21 22 95.4
branch 5 6 83.3
condition n/a
subroutine 7 8 87.5
pod 4 4 100.0
total 37 40 92.5


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::CodeLayout::ProhibitHashBarewords;
2              
3 2     2   1277503 use strict;
  2         5  
  2         69  
4 2     2   9 use warnings;
  2         3  
  2         48  
5 2     2   10 use Perl::Critic::Utils;
  2         4  
  2         54  
6 2     2   1995 use base 'Perl::Critic::Policy';
  2         3  
  2         1888  
7              
8             our $VERSION = '0.07';
9              
10              
11 8     8 1 87 sub default_severity { return $SEVERITY_MEDIUM }
12 0     0 1 0 sub default_themes { return qw(itch) }
13 3     3 1 86821 sub applies_to { return 'PPI::Token::Word' }
14              
15             sub violates {
16 16     16 1 2062 my ( $self, $elem ) = @_;
17              
18             #we only want the check hash keys
19 16 100       45 return if !is_hash_key($elem);
20              
21 9 100       634 return if is_method_call($elem);
22 8 50       234 return if is_function_call($elem);
23              
24 8         2244 my $desc = q{Hash key with bareword};
25 8         13 my $expl = q{Place quotes on all hash key barewords};
26 8         45 return $self->violation( $desc, $expl, $elem );
27             }
28              
29             1;
30              
31             __END__
32              
33             =pod
34              
35             =head1 NAME
36              
37             Perl::Critic::Policy::CodeLayout::ProhibitHashBarewords
38              
39             =head1 AFFILIATION
40              
41             This policy is part of L<Perl::Critic::Itch>.
42              
43             =head1 VERSION
44              
45             0.07
46              
47             =head1 DESCRIPTION
48              
49             This Policy forces (single) quotes on all hash keys barewords.
50              
51             When specifying constant string hash keys, you should use (single) quotes. E.g., $my_hash{'some_key'}
52              
53             This is the appropriate choice because it results in consistent formatting and if you forget to use quotes sometimes, you have to remember to add them when your key contains internal hyphens, spaces, or other special characters.
54              
55             Quoted keys are also more likely to be syntax-highlighted by your editor.
56              
57              
58              
59             =head1 INTERFACE
60              
61             Standard for a L<Perl::Critic::Policy>.
62              
63             =head1 ACKNOWLEDGMENTS
64              
65             Thanks to
66              
67             =over 4
68              
69             =item * Jose Carlos Pereira for pointing me in the right direction!
70              
71             =item * All Perl::Critic::Policy contributors. Their code examples were quite useful.
72              
73             =back
74