lib/Perl/Critic/Policy/CodeLayout/ProhibitLongLines.pm

filter: Statementstmt 100% 118 / 118 Branchbran 84.7% 39 / 46 Conditioncond 68.7% 22 / 32 MC/DCmcdc 30.0% 6 / 20 Subroutinesub 100% 23 / 23 Podpod 100% 5 / 5 totaltotal 87.3% 213 / 244 CC 38 SCAR 18.5
CC
38
Cov
91%
_gitattr_lookup
12.3
violates
10.0
_lookup_needed
6.3
CRAP 6.3
« lib/Perl/Critic/Policy/ValuesAndExpressions/RequireConsistentQuoting.pm ↑ Summary script/perl-quote-fix »
1 package Perl::Critic::Policy::CodeLayout::ProhibitLongLines v0.3.0;
2  
3 7 ▶use v5.26.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use feature "signatures";
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use experimental "signatures";
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 7 ▶use parent qw( Perl::Critic::Policy );
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 7 ▶use Encode qw( decode FB_CROAK );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 7 ▶use File::Basename qw( basename dirname );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 7 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 7 ▶use List::Util qw( any );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 7 ▶use Perl::Critic::Utils qw( $SEVERITY_MEDIUM words_from_string );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 7 ▶use Perl::Critic::Utils::SourceLocation ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 7 my $Expl = "Keep lines under the configured maximum for readability";
19  
20 7 my %Lookup_needed; # abs dir -> can a lookup possibly answer?
21 my %Attr_value; # "$attr\0$abs_file" -> computed override (may be undef)
22  
23 26 ▶sub _parse_allow_lines_matching ($self, $parameter, $config_string) {
Subroutine _parse_allow_lines_matching: called CC=5 SCAR=16.2
24 26 ▶ my $string = $config_string // $parameter->get_default_string // "";
Condition60%3/5
($config_string // $parameter->get_default_string) // ""
l!l
260
$config_string // $parameter->get_default_string
l!l&&r!l&&!r
7190
MC/DC0%0/2
($config_string // $parameter->get_default_string) // ""
$config_string $parameter->get_default_string
Truth table66%2/3
A // B
A= $config_stringB= $parameter->get_default_string
ABresult
1X1
011
000
25 26 my @patterns;
26 26 for my $pattern (words_from_string($string)) {
27 9 my $re = eval { qr/$pattern/ };
28 9 ▶ if (!defined $re) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $re) 2 7 9
29 2 my $error = $@;
30 2 chomp $error;
31 2 $self->throw_parameter_value_exception(
32 "allow_lines_matching", $pattern,
33 undef, "is not a valid regular expression: $error",
34 );
35 }
36 7 push @patterns, $re;
37 }
38 24 $self->__set_parameter_value($parameter, \@patterns);
39 return
40 24 }
41  
42 27 ▶sub supported_parameters ($self) { (
Subroutine supported_parameters: called CC=1 SCAR=0.0
Pod documented
43 {
44 27 name => "max_line_length",
45 description => "Maximum allowed line length in characters",
46 default_string => "80",
47 behavior => "integer",
48 integer_minimum => 1,
49 }, {
50 name => "allow_lines_matching",
51 description => "Regex patterns for lines exempt from length check",
52 default_string => "",
53 parser => \&_parse_allow_lines_matching,
54 }, {
55 name => "gitattributes_line_length",
56 description => "Git attribute name for per-file line length override",
57 default_string => "custom-line-length",
58 behavior => "string",
59 },
60 ) }
61  
62 37 ▶sub default_severity ($self) { $SEVERITY_MEDIUM }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
63 2 ▶sub default_themes ($self) { qw( cosmetic formatting pjcj ) }
Subroutine default_themes: called CC=1 SCAR=0.0
Pod documented
64  
65 47 ▶sub applies_to ($self) { "PPI::Document" }
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
66  
67 8 ▶sub _lookup_needed ($dir) {
Subroutine _lookup_needed: called CC=6 SCAR=18.5
68 8 while (1) {
69 9 ▶ return 1 if -e "$dir/.gitattributes";
Branch: 1 branch
BranchTrueFalseTotal
if -e "$dir/.gitattributes" 7 2 9
70 2 ▶ if (-e "$dir/.git") {
Branch: 1 branch
BranchTrueFalseTotal
if (-e "$dir/.git") 1 1 2
71 1 ▶ return 1 if !-d "$dir/.git" || -e "$dir/.git/info/attributes";
Branch: 1 branch
BranchTrueFalseTotal
if not -d "$dir/.git" or -e "$dir/.git/info/attributes" 0 1 1
Condition33%1/3
not -d "$dir/.git" or -e "$dir/.git/info/attributes"
l!l&&r!l&&!r
001
MC/DC0%0/2
not -d "$dir/.git" or -e "$dir/.git/info/attributes"
not -d "$dir/.git" -e "$dir/.git/info/attributes"
Truth table33%1/3
not(A) or B
A= not -d "$dir/.git"B= -e "$dir/.git/info/attributes"
ABresult
1X1
011
000
72 1 return 0;
73 }
74 1 my $parent = dirname($dir);
75 1 ▶ return 0 if $parent eq $dir;
Branch: 1 branch
BranchTrueFalseTotal
if $parent eq $dir 0 1 1
76 1 $dir = $parent;
77 }
78 }
79  
80 10 ▶sub _gitattr_lookup ($self, $attr, $filename) {
Subroutine _gitattr_lookup: called CC=12 SCAR=25.1
81 10 my $dir = dirname($filename);
82 10 my $abs_dir = File::Spec->rel2abs($dir);
83 10 ▶ $Lookup_needed{$abs_dir} //= _lookup_needed($abs_dir);
Condition66%2/3
$Lookup_needed{$abs_dir} //= _lookup_needed($abs_dir)
l!l&&r!l&&!r
280
MC/DC0%0/2
$Lookup_needed{$abs_dir} //= _lookup_needed($abs_dir)
$Lookup_needed{$abs_dir} _lookup_needed($abs_dir)
Truth table66%2/3
A //= B
A= $Lookup_needed{$abs_dir}B= _lookup_needed($abs_dir)
ABresult
1X1
011
000
84 10 ▶ return unless $Lookup_needed{$abs_dir};
Branch: 1 branch
BranchTrueFalseTotal
unless $Lookup_needed{$abs_dir} 1 9 10
85  
86 9 my $base = basename($filename);
87 9 my $output = eval {
88 9 ▶ open my $saved_err, ">&", \*STDERR or return;
Branch: 1 branch
BranchTrueFalseTotal
open my $saved_err, ">&", \*STDERR or return 9 0 9
89 9 my $quiet = open STDERR, ">", File::Spec->devnull;
90 9 my $opened = open my $fh, "-|", "git", "-C", $dir, "check-attr", $attr,
91 "--", $base;
92 9 ▶ if ($quiet) {
Branch: 1 branch
BranchTrueFalseTotal
if ($quiet) 9 0 9
93 # Restore our own STDERR; keep going even if the dup back fails, since
94 # abandoning the lookup would not make a broken STDERR any better
95 9 ▶ open STDERR, ">&", $saved_err or warn "Cannot restore STDERR: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, ">&", $saved_err or warn "Cannot restore STDERR: $!\n" 9 0 9
96 }
97 9 ▶ return unless $opened;
Branch: 1 branch
BranchTrueFalseTotal
unless $opened 0 9 9
98 9 my $result = do { local $/ = undef; <$fh> };
99 9 ▶ close $fh or return;
Branch: 1 branch
BranchTrueFalseTotal
close $fh or return 8 1 9
100 8 $result
101 };
102 9 ▶ return unless defined $output && $output =~ /: \Q$attr\E: (.+)$/m;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $output and $output =~ /: \Q$attr\E: (.+)$/mu 1 8 9
Condition66%2/3
defined $output and $output =~ /: \Q$attr\E: (.+)$/mu
!ll&&!rl&&r
108
MC/DC50%1/2
defined $output and $output =~ /: \Q$attr\E: (.+)$/mu
defined $output $output =~ /: \Q$attr\E: (.+)$/mu
Truth table66%2/3
A and B
A= defined $outputB= $output =~ /: \Q$attr\E: (.+)$/mu
ABresult
0X0
100
111
103  
104 8 my $value = $1;
105 8 ▶ return "ignore" if $value eq "ignore";
Branch: 1 branch
BranchTrueFalseTotal
if $value eq "ignore" 2 6 8
106 6 ▶ return $value if $value =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /^\d+$/u 4 2 6
107 return
108 2 }
109  
110 64 ▶sub _get_gitattr_line_length ($self, $filename) {
Subroutine _get_gitattr_line_length: called CC=6 SCAR=17.9
111 64 ▶ return unless defined $filename && length $filename;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $filename and length $filename 49 15 64
Condition100%3/3
defined $filename and length $filename
!ll&&!rl&&r
48115
MC/DC100%2/2
defined $filename and length $filename
defined $filename length $filename
Truth table100%3/3
A and B
A= defined $filenameB= length $filename
ABresult
0X0
100
111
112 15 my $attr = $self->{_gitattributes_line_length};
113 15 ▶ return unless defined $attr && length $attr;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $attr and length $attr 1 14 15
Condition66%2/3
defined $attr and length $attr
!ll&&!rl&&r
0114
MC/DC50%1/2
defined $attr and length $attr
defined $attr length $attr
Truth table66%2/3
A and B
A= defined $attrB= length $attr
ABresult
0X0
100
111
114  
115 14 my $key = join "\0", $attr, File::Spec->rel2abs($filename);
116 14 ▶ return $Attr_value{$key} if exists $Attr_value{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $Attr_value{$key} 4 10 14
117 10 $Attr_value{$key} = $self->_gitattr_lookup($attr, $filename)
118 }
119  
120 30 ▶sub _first_tokens_by_line ($self, $doc) {
Subroutine _first_tokens_by_line: called CC=4 SCAR=14.1
121 30 my %first;
122 30 for my $token ($doc->tokens) {
123 368 my $line = $token->line_number;
124 368 ▶ next unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 0 368 368
125 368 ▶ $first{$line} //= $token;
Condition66%2/3
$first{$line} //= $token
l!l&&r!l&&!r
305630
MC/DC0%0/2
$first{$line} //= $token
$first{$line} $token
Truth table66%2/3
A //= B
A= $first{$line}B= $token
ABresult
1X1
011
000
126 }
127 30 \%first
128 }
129  
130 53 ▶sub violates ($self, $elem, $doc) {
Subroutine violates: called CC=10 SCAR=23.0
Pod documented
131 53 my $override = $self->_get_gitattr_line_length($doc->filename);
132 53 ▶ return if defined $override && $override eq "ignore";
Branch: 1 branch
BranchTrueFalseTotal
if defined $override and $override eq "ignore" 1 52 53
Condition100%3/3
defined $override and $override eq "ignore"
!ll&&!rl&&r
4931
MC/DC100%2/2
defined $override and $override eq "ignore"
defined $override $override eq "ignore"
Truth table100%3/3
A and B
A= defined $overrideB= $override eq "ignore"
ABresult
0X0
100
111
133  
134 52 ▶ my $max_length = $override // $self->{_max_line_length};
Condition66%2/3
$override // $self->{"_max_line_length"}
l!l&&r!l&&!r
3490
MC/DC0%0/2
$override // $self->{"_max_line_length"}
$override $self->{"_max_line_length"}
Truth table66%2/3
A // B
A= $overrideB= $self->{"_max_line_length"}
ABresult
1X1
011
000
135 52 my $patterns = $self->{_allow_lines_matching};
136 52 my $source = $doc->serialize;
137  
138 # PPI serializes source as octets, so decode to characters before measuring
139 # line lengths. Keep the octets if the source is not valid UTF-8.
140 52 my $decoded = eval { decode("UTF-8", $source, FB_CROAK) };
141 52 ▶ $source = $decoded if defined $decoded;
Branch: 1 branch
BranchTrueFalseTotal
if defined $decoded 47 5 52
142  
143 52 my @lines = split /\n/, $source;
144  
145 52 my @violations;
146 my $token_map;
147  
148 52 for my $line_num (0 .. $#lines) {
149 111 my $length = length $lines[$line_num];
150 111 ▶ if ($length > $max_length) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length > $max_length) 39 72 111
151 39 ▶ next if any { $lines[$line_num] =~ $_ } @$patterns;
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if &any(sub { $lines[$line_num] =~ /$_/u; } , @$patterns) 4 35 39
152 35 my $violation_desc
153 = "Line is $length characters long (exceeds $max_length)";
154  
155 # Anchor at the first token on this line for accurate reporting
156 35 ▶ $token_map //= $self->_first_tokens_by_line($doc);
Condition66%2/3
$token_map //= $self->_first_tokens_by_line($doc)
l!l&&r!l&&!r
5300
MC/DC0%0/2
$token_map //= $self->_first_tokens_by_line($doc)
$token_map $self->_first_tokens_by_line($doc)
Truth table66%2/3
A //= B
A= $token_mapB= $self->_first_tokens_by_line($doc)
ABresult
1X1
011
000
157 35 my $line_token = $token_map->{ $line_num + 1 };
158  
159 # If no token found, create synthetic element with correct line number
160 35 ▶ if (!$line_token) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($line_token) 6 29 35
161 6 $line_token = Perl::Critic::Utils::SourceLocation->new(
162 line_number => $line_num + 1,
163 column_number => 1,
164 content => $lines[$line_num],
165 filename => $doc->filename,
166 );
167 }
168  
169 35 push @violations, $self->violation($violation_desc, $Expl, $line_token);
170 }
171 }
172  
173 @violations
174 52 }
175  
176 7 "
177 I know you have a little life in you yet
178 I know you have a lot of strength left
179 "
180  
181 __END__
« lib/Perl/Critic/Policy/ValuesAndExpressions/RequireConsistentQuoting.pm ↑ Summary script/perl-quote-fix »