lib/Kephra/IO/LocalFile.pm

filter: Statementstmt 28.8% 15 / 52 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 55.5% 5 / 9 Podpod 0.0% 0 / 4 totaltotal 18.6% 20 / 107 CC 16 SCAR 37.4
CC
16
Cov
17%
read
90.0
write
30.0
normalize_path
12.0
CRAP 42.2
« lib/Kephra/App/Editor/Move.pm ↑ Summary lib/Kephra/App/Editor/Tool.pm »
1 1 ▶use v5.12;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Encode::Guess;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 package Kephra::IO::LocalFile;
8  
9  
10 0 ▶sub dir_from_path {
Subroutine dir_from_path: not called CC=2 SCAR=17.9
Pod undocumented
11 0 my $path = shift;
12 0 ▶ return unless defined $path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path 0 0 0
13 0 my ($volume, $directories, $file) = File::Spec->splitpath( $path );
14 0 File::Spec->catdir( $volume, $directories );
15 }
16  
17 0 ▶sub normalize_path {
Subroutine normalize_path: not called CC=3 SCAR=24.8
Pod undocumented
18 0 my $path = shift;
19 0 ▶ return unless defined $path and $path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path and $path 0 0 0
Condition0%0/3
defined $path and $path
!ll&&!rl&&r
000
MC/DC0%0/2
defined $path and $path
defined $path $path
Truth table0%0/3
A and B
A= defined $pathB= $path
ABresult
0X0
100
111
20  
21 0 $path = File::Spec->canonpath( $path );
22 0 local $/ = "\r\n";
23 0 chomp( $path );
24 0 return $path;
25 }
26  
27 0 ▶sub read {
Subroutine read: not called CC=9 SCAR=45.0
Pod undocumented
28 0 my $path = normalize_path( shift );
29 0 my $encoding;
30 0 ▶ return warning("can't load nonexising file") unless defined $path and -e $path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path and -e $path 0 0 0
Condition0%0/3
defined $path and -e $path
!ll&&!rl&&r
000
MC/DC0%0/2
defined $path and -e $path
defined $path -e $path
Truth table0%0/3
A and B
A= defined $pathB= -e $path
ABresult
0X0
100
111
31 0 ▶ return warning("can't read $path") unless -r $path;
Branch: 1 branch
BranchTrueFalseTotal
unless -r $path 0 0 0
32 0 open my $FH, '<', $path;
33 0 binmode($FH);
34 0 my $content = do { local $/; <$FH> };
35 0 ▶ if ($content) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content) 0 0 0
36 0 my @guesses = qw/utf-8 iso8859-1 latin1/;
37 0 my $guess = Encode::Guess::guess_encoding( $content, @guesses );
38 0 ▶ if ( ref($guess) and ref($guess) =~ m/^Encode::/ ) { $encoding = $guess->name }
Branch: 3 branches
BranchTrueFalseTotal
if (ref $guess and ref($guess) =~ /^Encode::/u) { } 0 0 0
elsif ($guess =~ /utf8/u) { } 0 0 0
elsif ($guess =~ /or/u) { } 0 0 0
Condition0%0/3
ref $guess and ref($guess) =~ /^Encode::/u
!ll&&!rl&&r
000
MC/DC0%0/2
ref $guess and ref($guess) =~ /^Encode::/u
ref $guess ref($guess) =~ /^Encode::/u
Truth table0%0/3
A and B
A= ref $guessB= ref($guess) =~ /^Encode::/u
ABresult
0X0
100
111
39 0 elsif ( $guess =~ m/utf8/ ) { $encoding = 'utf-8' }
40 elsif ( $guess =~ m/or/ ) {
41 0 my @suggest_encodings = split /\sor\s/, "$guess";
42 0 $encoding = $suggest_encodings[0];
43 0 } else { $encoding = 'utf-8' }
44 0 $content = Encode::decode( $encoding, $content );
45 }
46 0 return $content, $encoding;
47 }
48  
49 0 ▶sub write {
Subroutine write: not called CC=5 SCAR=34.0
Pod undocumented
50 0 my ($path, $encoding, $content) = @_;
51 0 $path = normalize_path( $path );
52 0 ▶ $encoding = 'utf-8' unless defined $encoding;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $encoding 0 0 0
53 0 ▶ return say "need a file path to write into" unless $path;
Branch: 1 branch
BranchTrueFalseTotal
unless $path 0 0 0
54 0 ▶ return say "can't overwrite $path" if -e $path and not -w $path;
Branch: 1 branch
BranchTrueFalseTotal
if -e $path and not -w $path 0 0 0
Condition0%0/3
-e $path and not -w $path
!ll&&!rl&&r
000
MC/DC0%0/2
-e $path and not -w $path
-e $path not -w $path
Truth table0%0/3
A and not(B)
A= -e $pathB= not -w $path
ABresult
0X0
100
111
55 0 open my $FH, "> :raw :encoding($encoding)", $path;
56 0 print $FH $content;
57 }
58  
59  
60 1 1;
« lib/Kephra/App/Editor/Move.pm ↑ Summary lib/Kephra/App/Editor/Tool.pm »