File Coverage

blib/lib/Perl/Critic/Policy/Tics/ProhibitManyArrows.pm
Criterion Covered Total %
statement 35 36 97.2
branch 10 12 83.3
condition 1 3 33.3
subroutine 10 11 90.9
pod 5 6 83.3
total 61 68 89.7


line stmt bran cond sub pod time code
1 6     6   1613408 use strict;
  6         16  
  6         206  
2 6     6   33 use warnings;
  6         13  
  6         461  
3             package Perl::Critic::Policy::Tics::ProhibitManyArrows;
4             # ABSTRACT: (this => is => not => good)
5             $Perl::Critic::Policy::Tics::ProhibitManyArrows::VERSION = '0.009';
6             #pod =head1 DESCRIPTION
7             #pod
8             #pod You are not clever if you do this:
9             #pod
10             #pod my %hash = (key1=>value1=>key2=>value2=>key3=>'value3');
11             #pod
12             #pod You are even more not clever if you do this:
13             #pod
14             #pod my %hash = (key1=>value1=>key2=>value2=>key3=>value3=>);
15             #pod
16             #pod =head1 CONFIGURATION
17             #pod
18             #pod There is one parameter for this policy, F<max_allowed>, which specifies the
19             #pod maximum number of fat arrows that may appear as item separators. The default
20             #pod is two. If you really hate the fat arrow, and never want to see it, you can
21             #pod set F<max_allowed> to zero and make any occurance of C<< => >> illegal.
22             #pod
23             #pod Here are some examples of code that would fail with various F<max_allowed>
24             #pod values:
25             #pod
26             #pod max_allowed failing code
27             #pod 0 (foo => bar)
28             #pod 1 (foo => bar => baz)
29             #pod 2 (foo => bar => baz => quux)
30             #pod
31             #pod =cut
32              
33 6     6   49 use Perl::Critic::Utils;
  6         14  
  6         176  
34 6     6   6061 use parent qw(Perl::Critic::Policy);
  6         13  
  6         54  
35              
36             my $DESCRIPTION = q{Too many fat-arrow-separated values in a row};
37             my $EXPLANATION = q{Fat arrows should separate pairs, not produce long chains
38             of values};
39              
40 6     6 1 77 sub default_severity { $SEVERITY_MEDIUM }
41 0     0 1 0 sub default_themes { qw(tics) }
42 11     11 1 51794 sub applies_to { 'PPI::Token::Operator' }
43              
44 11     11 0 713 sub supported_parameters { qw(max_allowed) }
45              
46             sub new {
47 11     11 1 42353 my ($class, %arg) = @_;
48 11         77 my $self = $class->SUPER::new(%arg);
49              
50 11 50       15817 $arg{max_allowed} = 2 unless defined $arg{max_allowed};
51              
52 11 50 33     113 Carp::croak "max_allowed for Tics::ProhibitManyArrows must be a positive int"
53             unless $arg{max_allowed} =~ /\A\d+\z/ and $arg{max_allowed} >= 0;
54              
55 11         52 $self->{max_allowed} = $arg{max_allowed};
56 11         63 bless $self => $class;
57             }
58              
59 12     12   55 sub _max_allowed { $_[0]->{max_allowed} }
60              
61             sub violates {
62 46     46 1 2192 my ($self, $elem, $doc) = @_;
63              
64 46 100       122 return unless $elem eq '=>';
65 24 100       313 return if eval { $elem->sprevious_sibling->sprevious_sibling } eq '=>';
  24         81  
66              
67 12         494 my $in_a_row = 1;
68              
69 12         19 my $start = $elem;
70 12         18 while (my $next = eval { $start->snext_sibling->snext_sibling }) {
  24         67  
71 18 100       642 last unless $next eq '=>';
72 12         145 $in_a_row++;
73 12         21 $start = $next;
74             }
75              
76 12 100       310 return unless $in_a_row > $self->_max_allowed;
77              
78             # Must be a violation...
79 6         30 return $self->violation($DESCRIPTION, $EXPLANATION, $start);
80             }
81              
82             1;
83              
84             __END__
85              
86             =pod
87              
88             =encoding UTF-8
89              
90             =head1 NAME
91              
92             Perl::Critic::Policy::Tics::ProhibitManyArrows - (this => is => not => good)
93              
94             =head1 VERSION
95              
96             version 0.009
97              
98             =head1 DESCRIPTION
99              
100             You are not clever if you do this:
101              
102             my %hash = (key1=>value1=>key2=>value2=>key3=>'value3');
103              
104             You are even more not clever if you do this:
105              
106             my %hash = (key1=>value1=>key2=>value2=>key3=>value3=>);
107              
108             =head1 CONFIGURATION
109              
110             There is one parameter for this policy, F<max_allowed>, which specifies the
111             maximum number of fat arrows that may appear as item separators. The default
112             is two. If you really hate the fat arrow, and never want to see it, you can
113             set F<max_allowed> to zero and make any occurance of C<< => >> illegal.
114              
115             Here are some examples of code that would fail with various F<max_allowed>
116             values:
117              
118             max_allowed failing code
119             0 (foo => bar)
120             1 (foo => bar => baz)
121             2 (foo => bar => baz => quux)
122              
123             =head1 AUTHOR
124              
125             Ricardo SIGNES <rjbs@cpan.org>
126              
127             =head1 COPYRIGHT AND LICENSE
128              
129             This software is copyright (c) 2007 by Ricardo SIGNES.
130              
131             This is free software; you can redistribute it and/or modify it under
132             the same terms as the Perl 5 programming language system itself.
133              
134             =cut