script/perl-quote-fix

filter: Statementstmt 95.7% 90 / 94 Branchbran 87.5% 28 / 32 Conditioncond 50.0% 8 / 16 MC/DCmcdc 20.0% 2 / 10 Subroutinesub 100% 18 / 18 Podpod n/a totaltotal 85.8% 146 / 170 CC 25 SCAR 13.8
CC
25
Cov
89%
write_fixed
8.2
fix_in_place
5.0
run_in_place
5.0
CRAP 4.0
« 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
BranchTrueFalseTotal
unless defined $spec 5 3 8
18 3 ▶ if ($spec =~ /\A(\d+)-(\d+)\z/ && $1 <= $2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec =~ /\A(\d+)-(\d+)\z/u and $1 <= $2) 1 2 3
Condition100%3/3
$spec =~ /\A(\d+)-(\d+)\z/u and $1 <= $2
!ll&&!rl&&r
111
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
ABresult
0X0
100
111
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
BranchTrueFalseTotal
unless ($out) 1 5 6
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
!ll&&!rl&&r
005
MC/DC0%0/2
$ok &&= close $out
$ok close $out
Truth table33%1/3
A &&= B
A= $okB= close $out
ABresult
0X0
100
111
37 5 ▶ $ok &&= chmod $mode, $temp;
Condition33%1/3
$ok &&= chmod($mode, $temp)
!ll&&!rl&&r
005
MC/DC0%0/2
$ok &&= chmod($mode, $temp)
$ok chmod($mode, $temp)
Truth table33%1/3
A &&= B
A= $okB= chmod($mode, $temp)
ABresult
0X0
100
111
38 5 ▶ $ok &&= rename $temp, $file;
Condition33%1/3
$ok &&= rename($temp, $file)
!ll&&!rl&&r
005
MC/DC0%0/2
$ok &&= rename($temp, $file)
$ok rename($temp, $file)
Truth table33%1/3
A &&= B
A= $okB= rename($temp, $file)
ABresult
0X0
100
111
39 5 ▶ return 1 if $ok;
Branch: 1 branch
BranchTrueFalseTotal
if $ok 5 0 5
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
BranchTrueFalseTotal
if -l $file 1 7 8
48 8 my $fh;
49 8 ▶ unless (open $fh, "<", $file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open $fh, "<", $file) 1 7 8
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
BranchTrueFalseTotal
close $fh or warn "Cannot close ${file}: $!\n" 7 0 7
56 7 my $fixed = $fixer->fix($source);
57 7 ▶ return 1 if $fixed eq $source;
Branch: 1 branch
BranchTrueFalseTotal
if $fixed eq $source 1 6 7
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
BranchTrueFalseTotal
if defined $lines 1 6 7
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
BranchTrueFalseTotal
unless @ARGV 1 5 6
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
BranchTrueFalseTotal
unless fix_in_place($fixer, $file) 2 6 8
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
BranchTrueFalseTotal
unless (open $fh, "<", $file) 1 5 6
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 // ""
l!l
50
MC/DC0%0/1
$source // ""
$source
Truth table50%1/2
A
A= $source
Aresult
11
00
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
BranchTrueFalseTotal
unless (@ARGV) 1 5 6
90 1 ▶ my $source = do { local $/ = undef; <> }
Condition50%1/2
do { local $/ = undef; readline ARGV } // ""
l!l
10
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 }
Aresult
11
00
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
BranchTrueFalseTotal
&GetOptions(\%opt, "lines=s", "inplace", "help|h") or pod2usage(2) 15 0 15
101 15 ▶ pod2usage(0) if $opt{help};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"help"} 0 15 15
102  
103 15 ▶ run_in_place($opt{lines}) if $opt{inplace};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"inplace"} 7 8 15
104 8 my $lines = parse_lines($opt{lines});
105 6 run_filter($lines);
106 }
107  
108 15 main
109  
110 __END__
« lib/Perl/Critic/Policy/CodeLayout/ProhibitLongLines.pm ↑ Summary lib/Perl/Critic/Utils/SourceLocation.pm »