« lib/Perl/Critic/Policy/CodeLayout/ProhibitLongLines.pm
↑ Summary
lib/Perl/Critic/Utils/SourceLocation.pm »
| 1 | #!/usr/bin/env perl | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 15 | ▶ | use v5.26.0; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 15 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 15 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 15 | ▶ | use feature qw( signatures ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 15 | ▶ | use experimental qw( signatures ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 15 | ▶ | use Cwd qw( abs_path ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 15 | ▶ | use File::Basename qw( dirname ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 15 | ▶ | use File::Temp qw( tempfile ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 15 | ▶ | use Getopt::Long qw( GetOptions ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 15 | ▶ | use Pod::Usage qw( pod2usage ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 15 | ▶ | use Perl::Critic::PJCJ::Fixer (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 8 | ▶ | sub parse_lines ($spec) { | ||||||||||||||||||||||||||
|
Subroutine
parse_lines: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 17 | 8 | ▶ | return unless defined $spec; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 18 | 3 | ▶ | if ($spec =~ /\A(\d+)-(\d+)\z/ && $1 <= $2) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$spec =~ /\A(\d+)-(\d+)\z/u and $1 <= $2
MC/DC100%2/2
$spec =~ /\A(\d+)-(\d+)\z/u and $1 <= $2
$spec =~ /\A(\d+)-(\d+)\z/u
$1 <= $2
Truth table100%3/3
A and B
A= $spec =~ /\A(\d+)-(\d+)\z/uB= $1 <= $2
| |||||||||||||||||||||||||||||
| 19 | 1 | return [$1, $2]; | |||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||
| 21 | pod2usage( | ||||||||||||||||||||||||||||
| 22 | 2 | -message => "--lines requires an ascending range such as 10-20", | |||||||||||||||||||||||||||
| 23 | -exitval => 2, | ||||||||||||||||||||||||||||
| 24 | ); | ||||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 6 | ▶ | sub write_fixed ($file, $fixed, $mode) { | ||||||||||||||||||||||||||
|
Subroutine
write_fixed: called CC=6 SCAR=21.0
| |||||||||||||||||||||||||||||
| 28 | 6 | my ($out, $temp) = eval { | |||||||||||||||||||||||||||
| 29 | 6 | tempfile("perl-quote-fix-XXXXXX", DIR => dirname($file), UNLINK => 0) | |||||||||||||||||||||||||||
| 30 | }; | ||||||||||||||||||||||||||||
| 31 | 6 | ▶ | unless ($out) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | 1 | warn "Cannot create temporary file for $file: $@"; | |||||||||||||||||||||||||||
| 33 | 1 | return 0; | |||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | 5 | my $ok = print {$out} $fixed; | |||||||||||||||||||||||||||
| 36 | 5 | ▶ | $ok &&= close $out; | ||||||||||||||||||||||||||
|
Condition33%1/3
$ok &&= close $out
MC/DC0%0/2
$ok &&= close $out
$ok
close $out
Truth table33%1/3
A &&= B
A= $okB= close $out
| |||||||||||||||||||||||||||||
| 37 | 5 | ▶ | $ok &&= chmod $mode, $temp; | ||||||||||||||||||||||||||
|
Condition33%1/3
$ok &&= chmod($mode, $temp)
MC/DC0%0/2
$ok &&= chmod($mode, $temp)
$ok
chmod($mode, $temp)
Truth table33%1/3
A &&= B
A= $okB= chmod($mode, $temp)
| |||||||||||||||||||||||||||||
| 38 | 5 | ▶ | $ok &&= rename $temp, $file; | ||||||||||||||||||||||||||
|
Condition33%1/3
$ok &&= rename($temp, $file)
MC/DC0%0/2
$ok &&= rename($temp, $file)
$ok
rename($temp, $file)
Truth table33%1/3
A &&= B
A= $okB= rename($temp, $file)
| |||||||||||||||||||||||||||||
| 39 | 5 | ▶ | return 1 if $ok; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | 0 | my $error = $!; | |||||||||||||||||||||||||||
| 41 | 0 | unlink $temp; | |||||||||||||||||||||||||||
| 42 | 0 | warn "Cannot write $file: $error\n"; | |||||||||||||||||||||||||||
| 43 | 0 | 0 | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 8 | ▶ | sub fix_in_place ($fixer, $file) { | ||||||||||||||||||||||||||
|
Subroutine
fix_in_place: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 47 | 8 | ▶ | $file = abs_path($file) if -l $file; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 8 | my $fh; | |||||||||||||||||||||||||||
| 49 | 8 | ▶ | unless (open $fh, "<", $file) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 1 | warn "Cannot read $file: $!\n"; | |||||||||||||||||||||||||||
| 51 | 1 | return 0; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | 7 | my $source = do { local $/ = undef; <$fh> }; | |||||||||||||||||||||||||||
| 54 | 7 | my $mode = (stat $fh)[2] & 07777; | |||||||||||||||||||||||||||
| 55 | 7 | ▶ | close $fh or warn "Cannot close $file: $!\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 7 | my $fixed = $fixer->fix($source); | |||||||||||||||||||||||||||
| 57 | 7 | ▶ | return 1 if $fixed eq $source; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | 6 | write_fixed($file, $fixed, $mode) | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 7 | ▶ | sub run_in_place ($lines) { | ||||||||||||||||||||||||||
|
Subroutine
run_in_place: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 62 | 7 | ▶ | pod2usage( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | -message => "--inplace cannot be combined with --lines", | ||||||||||||||||||||||||||||
| 64 | -exitval => 2, | ||||||||||||||||||||||||||||
| 65 | ) if defined $lines; | ||||||||||||||||||||||||||||
| 66 | 6 | ▶ | pod2usage(-message => "--inplace requires file arguments", -exitval => 2) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | unless @ARGV; | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 5 | my $fixer = Perl::Critic::PJCJ::Fixer->new; | |||||||||||||||||||||||||||
| 70 | 5 | my $status = 0; | |||||||||||||||||||||||||||
| 71 | 5 | for my $file (@ARGV) { | |||||||||||||||||||||||||||
| 72 | 8 | ▶ | $status = 1 unless fix_in_place($fixer, $file); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | 5 | exit $status; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 6 | ▶ | sub read_source ($file) { | ||||||||||||||||||||||||||
|
Subroutine
read_source: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 78 | 6 | my $fh; | |||||||||||||||||||||||||||
| 79 | 6 | ▶ | unless (open $fh, "<", $file) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 1 | warn "Cannot read $file: $!\n"; | |||||||||||||||||||||||||||
| 81 | 1 | exit 1; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | 5 | my $source = do { local $/ = undef; <$fh> }; | |||||||||||||||||||||||||||
| 84 | 5 | ▶ | $source // "" | ||||||||||||||||||||||||||
|
Condition50%1/2
$source // ""
MC/DC0%0/1
$source // ""
$source
Truth table50%1/2
A
A= $source
| |||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 6 | ▶ | sub run_filter ($lines) { | ||||||||||||||||||||||||||
|
Subroutine
run_filter: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 88 | 6 | my $fixer = Perl::Critic::PJCJ::Fixer->new; | |||||||||||||||||||||||||||
| 89 | 6 | ▶ | unless (@ARGV) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 1 | ▶ | my $source = do { local $/ = undef; <> } | ||||||||||||||||||||||||||
|
Condition50%1/2
do {
local $/ = undef;
readline ARGV
} // ""
MC/DC0%0/1
do {
local $/ = undef;
readline ARGV
} // ""
do {
local $/ = undef;
readline ARGV
}
Truth table50%1/2
A
A= do {
local $/ = undef;
readline ARGV
}
| |||||||||||||||||||||||||||||
| 91 | // ""; | ||||||||||||||||||||||||||||
| 92 | 1 | print $fixer->fix($source, lines => $lines); | |||||||||||||||||||||||||||
| 93 | 1 | return; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | 5 | print $fixer->fix(read_source($_), lines => $lines) for @ARGV; | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 15 | ▶ | sub main () { | ||||||||||||||||||||||||||
|
Subroutine
main: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||
| 99 | 15 | my %opt; | |||||||||||||||||||||||||||
| 100 | 15 | ▶ | GetOptions(\%opt, "lines=s", "inplace", "help|h") or pod2usage(2); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 15 | ▶ | pod2usage(0) if $opt{help}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 15 | ▶ | run_in_place($opt{lines}) if $opt{inplace}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | 8 | my $lines = parse_lines($opt{lines}); | |||||||||||||||||||||||||||
| 105 | 6 | run_filter($lines); | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 15 | main | |||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | __END__ | ||||||||||||||||||||||||||||