blib/lib/Hazy.pm

filter: Statementstmt 98.9% 91 / 92 Branchbran 73.6% 28 / 38 Conditioncond 80.9% 17 / 21 MC/DCmcdc 72.7% 8 / 11 Subroutinesub 100% 11 / 11 Podpod 0.0% 0 / 6 totaltotal 86.5% 155 / 179 CC 29 SCAR 19.9
CC
29
Cov
90%
_read_spec
13.0
new
7.1
process
6.3
CRAP 7.3
↑ Summary
1 package Hazy;
2  
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use 5.012;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use Cwd qw/abs_path/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 our $VERSION = '0.05';
8  
9 sub new {
10 6 ▶ my ( $pkg, @new ) = @_;
Subroutine new: called CC=7 SCAR=19.6
Pod undocumented
11 6 ▶ my %args = scalar @new == 1 ? @{ shift @new } : @new;
Branch: 1 branch
BranchTrueFalseTotal
scalar @new == 1 ? : 0 6 6
12 6 ▶ $args{file_name} //= 'test';
Condition100%2/2
$args{"file_name"} //= "test"
l!l
15
MC/DC100%1/1
$args{"file_name"} //= "test"
$args{"file_name"}
Truth table100%2/2
A
A= $args{"file_name"}
Aresult
11
00
13 6 ▶ $args{find} //= 'css';
Condition100%2/2
$args{"find"} //= "css"
l!l
42
MC/DC100%1/1
$args{"find"} //= "css"
$args{"find"}
Truth table100%2/2
A
A= $args{"find"}
Aresult
11
00
14 6 my $path = abs_path( [caller()]->[1] );
15 6 $args{abs_path} = substr($path, 0, rindex($path, '/'));
16 6 ▶ if ($args{write_dir}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"write_dir"}) 1 5 6
17 1 $args{write_dir} = sprintf "%s/%s", $args{abs_path}, $args{write_dir};
18 1 ▶ unless(-d $args{write_dir}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $args{"write_dir"}) 1 0 1
19 1 my $dir = '';
20 map {
21 9 ▶ (-e ($dir .= "/$_")) or mkdir $dir;
Branch: 1 branch
BranchTrueFalseTotal
-e ($dir .= "/$_") or mkdir $dir 2 7 9
22 1 } (split /\//, $args{write_dir});
23 }
24 }
25 6 bless {%args}, $pkg;
26 }
27  
28 sub process {
29 1 ▶ $_[1] //= $_[0]->{read_dir} // die 'No read_dir provided';
Subroutine process: called CC=6 SCAR=18.4
Pod undocumented
Condition40%2/5
$_[1] //= $_[0]{"read_dir"} // die("No read_dir provided")
l!l&&r!l&&!r
010
$_[0]{"read_dir"} // die("No read_dir provided")
l!l
10
MC/DC0%0/2
$_[1] //= $_[0]{"read_dir"} // die("No read_dir provided")
$_[1] $_[0]{"read_dir"}
Truth table33%1/3
A //= B
A= $_[1]B= $_[0]{"read_dir"}
ABresult
1X1
011
000
30 1 $_[1] = sprintf "%s/%s", $_[0]->{abs_path}, $_[1];
31 1 my ( $spec, @files ) = $_[0]->lookup_dir( $_[1] );
32 1 my $build_css;
33 1 for my $css_file (@files) {
34 2 ▶ open my $fh, "<$css_file" or die "Cannot open $css_file";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<$css_file" or die "Cannot open $css_file" 0 2 2
35 2 my $css = do { local $/; <$fh> };
36 2 $css = $_[0]->make_replacements( $spec, $css );
37 2 $build_css .= $css;
38 }
39 my $write = exists $_[0]->{write_dir}
40 ? sprintf "%s/%s", $_[0]->{write_dir}, $_[0]->{file_name}
41 1 ▶ : $_[0]->{file_name};
Branch: 1 branch
BranchTrueFalseTotal
exists $_[0]{'write_dir'} ? : 1 0 1
42 1 write_file( "$write.css", $build_css );
43 1 write_file( "$write.min.css", $_[0]->min_css($build_css) );
44 1 return 1;
45 }
46  
47 sub write_file {
48 2 ▶ open( my $fh, '>', $_[0] ) or die "could not open file $_[0]";
Subroutine write_file: called CC=2 SCAR=7.1
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $_[0] or die "could not open file $_[0]" 0 2 2
49 2 print $fh $_[1];
50 2 close $fh;
51 }
52  
53 sub make_replacements {
54 5 ▶ my $regx = join "|", map { quotemeta($_) } keys %{ $_[1] };
Subroutine make_replacements: called CC=3 SCAR=11.2
Pod undocumented
55 5 ▶ return $_[2] unless $regx;
Branch: 1 branch
BranchTrueFalseTotal
unless $regx 0 5 5
56 5 $_[2] =~ s/($regx)/$_[1]->{$1}/g;
57 5 ▶ ( !$_[2] =~ m/\n$/ ) and $_[2] .= "\n";
Branch: 1 branch
BranchTrueFalseTotal
!$_[2] =~ /\n$/u and $_[2] .= "\n" 0 5 5
58 5 return $_[2];
59 }
60  
61 sub lookup_dir {
62 4 ▶ my $look = $_[0]->{find};
Subroutine lookup_dir: called CC=3 SCAR=11.1
Pod undocumented
63 4 ▶ opendir( my $dh, $_[1] ) or die "Could not open dir - $_[1]";
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $_[1] or die "Could not open dir - $_[1]" 0 4 4
64 22 my %files = map { $_ => sprintf "%s/%s", $_[1], $_ }
65 4 grep { /config|\.$look$/ } readdir $dh;
66 4 closedir($dh);
67 4 ▶ my $spec = delete $files{config} or die 'no config found';
Branch: 1 branch
BranchTrueFalseTotal
my $spec = delete $files{"config"} or die "no config found" 0 4 4
68 4 return ( _read_spec($spec), sort values %files );
69 }
70  
71 sub min_css {
72 4 ▶ $_[1] =~ s/[\s]{2,}|[\t\r\n]+//g;
Subroutine min_css: called CC=1 SCAR=0.0
Pod undocumented
73 4 my %minify = (' {', '{', '{ ', '{', ' }', '}', '} ', '}', ': ',
74 ':', ';}', '}', ' ,', ',', ', ', ',', '( ', '(', ' )', ')' );
75 4 my $regx = join "|", map { quotemeta($_) } sort keys %minify;
76 4 $_[1] =~ s/($regx)/$minify{$1}/g;
77 4 $_[1];
78 }
79  
80 sub _read_spec {
81 9 ▶ my ( %spec, %arg );
Subroutine _read_spec: called CC=13 SCAR=25.7
82 9 open( my $fh, "<$_[0]" );
83 9 $arg{end} = ';';
84 9 ▶ while ( sysread( $fh, $arg{buffer}, 1 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (sysread $fh, $arg{"buffer"}, 1) 235 9 244
85 235 ▶ if ( ! exists $arg{value} && $arg{buffer} =~ m/\s/ ) { next }
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $arg{"value"} and $arg{"buffer"} =~ /\s/u) 30 205 235
Condition100%3/3
not exists $arg{"value"} and $arg{"buffer"} =~ /\s/u
!ll&&!rl&&r
9710830
MC/DC100%2/2
not exists $arg{"value"} and $arg{"buffer"} =~ /\s/u
not exists $arg{"value"} $arg{"buffer"} =~ /\s/u
Truth table100%3/3
not(A) and B
A= not exists $arg{"value"}B= $arg{"buffer"} =~ /\s/u
ABresult
0X0
100
111
86 205 ▶ if ( !$arg{flag} && !$arg{multi} && $arg{buffer} eq ':' ) { $arg{flag} = 1; next; }
Branch: 1 branch
BranchTrueFalseTotal
if (not $arg{"flag"} || $arg{"multi"} and $arg{"buffer"} eq ":") 15 190 205
Condition83%5/6
not $arg{"flag"} || $arg{"multi"} and $arg{"buffer"} eq ":"
!ll&&!rl&&r
1137715
$arg{'flag'} || $arg{'multi'}
l!l&&r!l&&!r
113092
MC/DC66%2/3
not $arg{"flag"} || $arg{"multi"} and $arg{"buffer"} eq ":"
$arg{'flag'} $arg{'multi'} $arg{"buffer"} eq ":"
Truth table75%3/4
not(A || B) and C
A= $arg{'flag'}B= $arg{'multi'}C= $arg{"buffer"} eq ":"
ABCresult
1XX0
01X0
0000
0011
87 190 ▶ if ($arg{buffer} eq $arg{end}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg{"buffer"} eq $arg{"end"}) 15 175 190
88 15 map { $arg{$_} =~ s/^\s+|\s+$// } qw/key value/;
89 15 $spec{"$arg{key}"} = $arg{value};
90 15 map { delete $arg{$_} } qw/key value flag multi/;
91 15 $arg{end} = ';';
92 15 next;
93 }
94 175 ▶ if ( exists $arg{flag} && ! exists $arg{value} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $arg{"flag"} and not exists $arg{"value"}) 16 159 175
Condition100%3/3
exists $arg{"flag"} and not exists $arg{"value"}
!ll&&!rl&&r
778216
MC/DC100%2/2
exists $arg{"flag"} and not exists $arg{"value"}
exists $arg{"flag"} not exists $arg{"value"}
Truth table100%3/3
A and not(B)
A= exists $arg{"flag"}B= not exists $arg{"value"}
ABresult
0X0
100
111
95 16 ▶ next if ($arg{buffer} =~ m/\s/);
Branch: 1 branch
BranchTrueFalseTotal
if $arg{"buffer"} =~ /\s/u 0 16 16
96 16 ▶ if ($arg{buffer} =~ m/[\@\$\^\&\*\{\/\\\~\`\>\<\+\_\]\[\?\|\"\'\=\!]/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg{"buffer"} =~ m([\@\$\^\&\*\{/\\\~\`\>\<\+\_\]\[\?\|\"\'\=\!])u) 1 15 16
97 1 $arg{multi} = 1;
98 1 $arg{end} = $arg{buffer};
99 1 next;
100 }
101 }
102 174 ▶ exists $arg{flag} ? ( $arg{value} .= $arg{buffer} ) : ( $arg{key} .= $arg{buffer} );
Branch: 1 branch
BranchTrueFalseTotal
exists $arg{'flag'} ? : 97 77 174
103 }
104 9 close($fh);
105 9 return \%spec;
106 }
107  
108 6 1;
109  
110 __END__
↑ Summary