| 1 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 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 | ▶ | use Perl::Critic::Utils; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | 5 | ▶ | use parent qw(Perl::Critic::Policy); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 15 | ▶ | sub default_severity { $SEVERITY_LOW } | ||||||||||||||||||||||||||
|
Subroutine
default_severity: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 33 | 0 | ▶ | sub default_themes { qw(tics) } | ||||||||||||||||||||||||||
|
Subroutine
default_themes: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 34 | 9 | ▶ | sub applies_to { 'PPI::Document' } | ||||||||||||||||||||||||||
|
Subroutine
applies_to: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 18 | ▶ | sub supported_parameters { qw(base_max hard_max pct_allowed) } | ||||||||||||||||||||||||||
|
Subroutine
supported_parameters: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 5 | my %_default = ( | |||||||||||||||||||||||||||
| 39 | base_max => 80, | ||||||||||||||||||||||||||||
| 40 | pct_allowed => 1, | ||||||||||||||||||||||||||||
| 41 | ); | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 9 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=8 SCAR=22.7
Pod
documented
| |||||||||||||||||||||||||||||
| 44 | 9 | my ($class, %arg) = @_; | |||||||||||||||||||||||||||
| 45 | 9 | my $self = $class->SUPER::new(%arg); | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 9 | my %merge = (%_default, %arg); | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | Carp::croak "base_max for Tics::ProhibitLongLines must be an int, one or more" | ||||||||||||||||||||||||||||
| 50 | 9 | ▶ | unless $merge{base_max} =~ /\A\d+\z/ and $merge{base_max} >= 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$merge{"base_max"} =~ /\A\d+\z/ and $merge{"base_max"} >= 1
MC/DC0%0/2
$merge{"base_max"} =~ /\A\d+\z/ and $merge{"base_max"} >= 1
$merge{"base_max"} =~ /\A\d+\z/
$merge{"base_max"} >= 1
Truth table33%1/3
A and B
A= $merge{"base_max"} =~ /\A\d+\z/B= $merge{"base_max"} >= 1
| |||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 9 | ▶ | $merge{hard_max} = $merge{base_max} * 1.5 unless exists $merge{hard_max}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | Carp::croak "base_max for Tics::ProhibitLongLines must be an int, one or more" | ||||||||||||||||||||||||||||
| 55 | 5 | ▶ | unless do { no warnings; ($merge{hard_max} = int($merge{hard_max})) >= 1 }; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | Carp::croak "pct_allowed for Tics::ProhibitLongLines must be a positive int" | ||||||||||||||||||||||||||||
| 58 | 9 | ▶ | unless $merge{pct_allowed} =~ /\A\d+\z/ and $merge{pct_allowed} >= 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$merge{"pct_allowed"} =~ /\A\d+\z/ and $merge{"pct_allowed"} >= 0
MC/DC0%0/2
$merge{"pct_allowed"} =~ /\A\d+\z/ and $merge{"pct_allowed"} >= 0
$merge{"pct_allowed"} =~ /\A\d+\z/
$merge{"pct_allowed"} >= 0
Truth table33%1/3
A and B
A= $merge{"pct_allowed"} =~ /\A\d+\z/B= $merge{"pct_allowed"} >= 0
| |||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 9 | $self->{$_} = $merge{$_} for $self->supported_parameters; | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 9 | bless $self => $class; | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 9 | ▶ | sub violates { | ||||||||||||||||||||||||||
|
Subroutine
violates: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 67 | 9 | my ($self, $elem, $doc) = @_; | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 9 | $elem->prune('PPI::Token::Data'); | |||||||||||||||||||||||||||
| 70 | 9 | $elem->prune('PPI::Token::End'); | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 9 | my @lines = split /(?:\x0d\x0a|\x0a\x0d|\x0d|\x0a)/, $elem->serialize; | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 9 | my @soft_violations; | |||||||||||||||||||||||||||
| 75 | my @hard_violations; | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 9 | my $base = $self->{base_max}; | |||||||||||||||||||||||||||
| 78 | 9 | my $limit = $self->{hard_max}; | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 9 | my $top = $elem->top(); | |||||||||||||||||||||||||||
| 81 | 9 | ▶ | my $fn = $top->can('filename') ? $top->filename() : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 9 | LINE: for my $ln (1 .. @lines) { | |||||||||||||||||||||||||||
| 84 | 546 | my $length = length $lines[ $ln - 1 ]; | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 546 | ▶ | next LINE unless $length > $base; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 15 | ▶ | if ($length > $limit) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | 2 | 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 | $viol->_set_location([ $ln, 1, 1, $ln, $fn ], $lines[ $ln - 1 ]); | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 2 | push @hard_violations, $viol; | |||||||||||||||||||||||||||
| 99 | } else { | ||||||||||||||||||||||||||||
| 100 | 13 | 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 | $viol->_set_location([ $ln, 1, 1, $ln, $fn ], $lines[ $ln - 1 ]); | |||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 13 | push @soft_violations, $viol; | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 9 | my $allowed = sprintf '%u', @lines * ($self->{pct_allowed} / 100); | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 9 | my $viols = @soft_violations + @hard_violations; | |||||||||||||||||||||||||||
| 116 | 9 | ▶ | if ($viols > $allowed) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | 4 | return(@hard_violations, @soft_violations); | |||||||||||||||||||||||||||
| 118 | } else { | ||||||||||||||||||||||||||||
| 119 | 5 | return @hard_violations; | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | { | ||||||||||||||||||||||||||||
| 124 | package # hide | ||||||||||||||||||||||||||||
| 125 | Perl::Critic::Tics::Violation::VirtualPos; | ||||||||||||||||||||||||||||
| 126 | 5 | ▶ | BEGIN {require Perl::Critic::Violation; our @ISA = 'Perl::Critic::Violation';} | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 127 | 15 | ▶ | sub _set_location { | ||||||||||||||||||||||||||
|
Subroutine
_set_location: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 128 | 15 | my ($self, $pos, $line) = @_; | |||||||||||||||||||||||||||
| 129 | 15 | $self->{__PACKAGE__}{pos} = $pos; | |||||||||||||||||||||||||||
| 130 | 15 | $self->{__PACKAGE__}{line} = $line; | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | 37 | ▶ | sub location { $_[0]->{__PACKAGE__}{pos} } | ||||||||||||||||||||||||||
|
Subroutine
location: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 133 | 2 | ▶ | sub source { $_[0]->{__PACKAGE__}{line} } | ||||||||||||||||||||||||||
|
Subroutine
source: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 5 | 1; | |||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | __END__ | ||||||||||||||||||||||||||||