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 5     5   3624 use strict;
  5         13  
  5         145  
2 5     5   25 use warnings;
  5         11  
  5         226  
3             package Perl::Critic::Policy::Tics::ProhibitManyArrows 0.010;
4             # ABSTRACT: (this => is => not => good)
5              
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 5     5   26 use Perl::Critic::Utils;
  5         9  
  5         75  
34 5     5   4023 use parent qw(Perl::Critic::Policy);
  5         27  
  5         50  
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 81 sub default_severity { $SEVERITY_MEDIUM }
41 0     0 1 0 sub default_themes { qw(tics) }
42 11     11 1 73867 sub applies_to { 'PPI::Token::Operator' }
43              
44 11     11 0 1038 sub supported_parameters { qw(max_allowed) }
45              
46             sub new {
47 11     11 1 63772 my ($class, %arg) = @_;
48 11         70 my $self = $class->SUPER::new(%arg);
49              
50 11 50       28781 $arg{max_allowed} = 2 unless defined $arg{max_allowed};
51              
52             Carp::croak "max_allowed for Tics::ProhibitManyArrows must be a positive int"
53 11 50 33     119 unless $arg{max_allowed} =~ /\A\d+\z/ and $arg{max_allowed} >= 0;
54              
55 11         35 $self->{max_allowed} = $arg{max_allowed};
56 11         50 bless $self => $class;
57             }
58              
59 12     12   45 sub _max_allowed { $_[0]->{max_allowed} }
60              
61             sub violates {
62 46     46 1 2654 my ($self, $elem, $doc) = @_;
63              
64 46 100       122 return unless $elem eq '=>';
65 24 100       290 return if eval { $elem->sprevious_sibling->sprevious_sibling } eq '=>';
  24         78  
66              
67 12         697 my $in_a_row = 1;
68              
69 12         23 my $start = $elem;
70 12         26 while (my $next = eval { $start->snext_sibling->snext_sibling }) {
  24         65  
71 18 100       861 last unless $next eq '=>';
72 12         151 $in_a_row++;
73 12         34 $start = $next;
74             }
75              
76 12 100       612 return unless $in_a_row > $self->_max_allowed;
77              
78             # Must be a violation...
79 6         28 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.010
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 PERL VERSION
109              
110             This library should run on perls released even a long time ago. It should work
111             on any version of perl released in the last five years.
112              
113             Although it may work on older versions of perl, no guarantee is made that the
114             minimum required version will not be increased. The version may be increased
115             for any reason, and there is no promise that patches will be accepted to lower
116             the minimum required perl.
117              
118             =head1 CONFIGURATION
119              
120             There is one parameter for this policy, F<max_allowed>, which specifies the
121             maximum number of fat arrows that may appear as item separators. The default
122             is two. If you really hate the fat arrow, and never want to see it, you can
123             set F<max_allowed> to zero and make any occurance of C<< => >> illegal.
124              
125             Here are some examples of code that would fail with various F<max_allowed>
126             values:
127              
128             max_allowed failing code
129             0 (foo => bar)
130             1 (foo => bar => baz)
131             2 (foo => bar => baz => quux)
132              
133             =head1 AUTHOR
134              
135             Ricardo SIGNES <cpan@semiotic.systems>
136              
137             =head1 COPYRIGHT AND LICENSE
138              
139             This software is copyright (c) 2007 by Ricardo SIGNES.
140              
141             This is free software; you can redistribute it and/or modify it under
142             the same terms as the Perl 5 programming language system itself.
143              
144             =cut