File Coverage

blib/lib/Perl/Critic/Policy/Tics/ProhibitLongLines.pm
Criterion Covered Total %
statement 59 60 98.3
branch 11 16 68.7
condition 2 6 33.3
subroutine 14 15 93.3
pod 5 6 83.3
total 91 103 88.3


line stmt bran cond sub pod time code
1 5     5   2932 use strict;
  5         13  
  5         142  
2 5     5   26 use warnings;
  5         12  
  5         211  
3             package Perl::Critic::Policy::Tics::ProhibitLongLines 0.010;
4             # ABSTRACT: 80 x 40 for life!
5              
6             #pod =head1 DESCRIPTION
7             #pod
8             #pod Please keep your code to about eighty columns wide, the One True Terminal
9             #pod Width. Going over that occasionally is okay, but only once in a while.
10             #pod
11             #pod This policy always throws a violation for extremely long lines. It will also
12             #pod throw a violation if there are too many lines that are slightly longer than the
13             #pod preferred maximum length. If a only few lines exceed the preferred maximum
14             #pod width, they're let slide and only extremely long lines are violations.
15             #pod
16             #pod =head1 CONFIGURATION
17             #pod
18             #pod There are three configuration options for this policy:
19             #pod
20             #pod base_max - the preferred maximum line length (default: 80)
21             #pod hard_max - the length beyond which a line is "extremely long"
22             #pod (default: base_max * 1.5)
23             #pod
24             #pod pct_allowed - the percentage of total lines which may fall between base_max
25             #pod and hard_max before those violations are reported (default: 1)
26             #pod
27             #pod =cut
28              
29 5     5   26 use Perl::Critic::Utils;
  5         11  
  5         70  
30 5     5   4050 use parent qw(Perl::Critic::Policy);
  5         11  
  5         26  
31              
32 15     15 1 185 sub default_severity { $SEVERITY_LOW }
33 0     0 1 0 sub default_themes { qw(tics) }
34 9     9 1 513860 sub applies_to { 'PPI::Document' }
35              
36 18     18 0 876 sub supported_parameters { qw(base_max hard_max pct_allowed) }
37              
38             my %_default = (
39             base_max => 80,
40             pct_allowed => 1,
41             );
42              
43             sub new {
44 9     9 1 99485 my ($class, %arg) = @_;
45 9         77 my $self = $class->SUPER::new(%arg);
46              
47 9         22483 my %merge = (%_default, %arg);
48              
49             Carp::croak "base_max for Tics::ProhibitLongLines must be an int, one or more"
50 9 50 33     119 unless $merge{base_max} =~ /\A\d+\z/ and $merge{base_max} >= 1;
51              
52 9 50       52 $merge{hard_max} = $merge{base_max} * 1.5 unless exists $merge{hard_max};
53              
54             Carp::croak "base_max for Tics::ProhibitLongLines must be an int, one or more"
55 5 50   5   1239 unless do { no warnings; ($merge{hard_max} = int($merge{hard_max})) >= 1 };
  5         10  
  5         2731  
  9         21  
  9         48  
56              
57             Carp::croak "pct_allowed for Tics::ProhibitLongLines must be a positive int"
58 9 50 33     76 unless $merge{pct_allowed} =~ /\A\d+\z/ and $merge{pct_allowed} >= 0;
59              
60 9         38 $self->{$_} = $merge{$_} for $self->supported_parameters;
61              
62 9         49 bless $self => $class;
63             }
64              
65              
66             sub violates {
67 9     9 1 150 my ($self, $elem, $doc) = @_;
68              
69 9         94 $elem->prune('PPI::Token::Data');
70 9         87186 $elem->prune('PPI::Token::End');
71              
72 9         82445 my @lines = split /(?:\x0d\x0a|\x0a\x0d|\x0d|\x0a)/, $elem->serialize;
73              
74 9         30686 my @soft_violations;
75             my @hard_violations;
76              
77 9         35 my $base = $self->{base_max};
78 9         25 my $limit = $self->{hard_max};
79              
80 9         75 my $top = $elem->top();
81 9 50       282 my $fn = $top->can('filename') ? $top->filename() : undef;
82              
83 9         60 LINE: for my $ln (1 .. @lines) {
84 546         765 my $length = length $lines[ $ln - 1 ];
85              
86 546 100       1059 next LINE unless $length > $base;
87              
88 15 100       41 if ($length > $limit) {
89 2         22 my $viol = Perl::Critic::Tics::Violation::VirtualPos->new(
90             "Line is over hard length limit of $limit characters.",
91             "Keep lines to about $limit columns wide.",
92             $doc,
93             $self->get_severity,
94             );
95              
96 2         3677 $viol->_set_location([ $ln, 1, 1, $ln, $fn ], $lines[ $ln - 1 ]);
97              
98 2         7 push @hard_violations, $viol;
99             } else {
100 13         99 my $viol = Perl::Critic::Tics::Violation::VirtualPos->new(
101             "Line is over base length limit of $base characters.",
102             "Keep lines to about $limit columns wide.",
103             $doc,
104             $self->get_severity,
105             );
106              
107 13         29879 $viol->_set_location([ $ln, 1, 1, $ln, $fn ], $lines[ $ln - 1 ]);
108              
109 13         37 push @soft_violations, $viol;
110             }
111             }
112              
113 9         103 my $allowed = sprintf '%u', @lines * ($self->{pct_allowed} / 100);
114              
115 9         29 my $viols = @soft_violations + @hard_violations;
116 9 100       45 if ($viols > $allowed) {
117 4         40 return(@hard_violations, @soft_violations);
118             } else {
119 5         57 return @hard_violations;
120             }
121             }
122              
123             {
124             package # hide
125             Perl::Critic::Tics::Violation::VirtualPos;
126 5     5   56 BEGIN {require Perl::Critic::Violation; our @ISA = 'Perl::Critic::Violation';}
  5         700  
127             sub _set_location {
128 15     15   44 my ($self, $pos, $line) = @_;
129 15         51 $self->{__PACKAGE__}{pos} = $pos;
130 15         34 $self->{__PACKAGE__}{line} = $line;
131             }
132 37     37   15561 sub location { $_[0]->{__PACKAGE__}{pos} }
133 2     2   1014 sub source { $_[0]->{__PACKAGE__}{line} }
134             }
135              
136             1;
137              
138             __END__
139              
140             =pod
141              
142             =encoding UTF-8
143              
144             =head1 NAME
145              
146             Perl::Critic::Policy::Tics::ProhibitLongLines - 80 x 40 for life!
147              
148             =head1 VERSION
149              
150             version 0.010
151              
152             =head1 DESCRIPTION
153              
154             Please keep your code to about eighty columns wide, the One True Terminal
155             Width. Going over that occasionally is okay, but only once in a while.
156              
157             This policy always throws a violation for extremely long lines. It will also
158             throw a violation if there are too many lines that are slightly longer than the
159             preferred maximum length. If a only few lines exceed the preferred maximum
160             width, they're let slide and only extremely long lines are violations.
161              
162             =head1 PERL VERSION
163              
164             This library should run on perls released even a long time ago. It should work
165             on any version of perl released in the last five years.
166              
167             Although it may work on older versions of perl, no guarantee is made that the
168             minimum required version will not be increased. The version may be increased
169             for any reason, and there is no promise that patches will be accepted to lower
170             the minimum required perl.
171              
172             =head1 CONFIGURATION
173              
174             There are three configuration options for this policy:
175              
176             base_max - the preferred maximum line length (default: 80)
177             hard_max - the length beyond which a line is "extremely long"
178             (default: base_max * 1.5)
179              
180             pct_allowed - the percentage of total lines which may fall between base_max
181             and hard_max before those violations are reported (default: 1)
182              
183             =head1 AUTHOR
184              
185             Ricardo SIGNES <cpan@semiotic.systems>
186              
187             =head1 COPYRIGHT AND LICENSE
188              
189             This software is copyright (c) 2007 by Ricardo SIGNES.
190              
191             This is free software; you can redistribute it and/or modify it under
192             the same terms as the Perl 5 programming language system itself.
193              
194             =cut