blib/lib/Plack/Middleware/Text/Minify.pm

filter: Statementstmt 100% 51 / 51 Branchbran 87.5% 21 / 24 Conditioncond 100% 12 / 12 MC/DCmcdc 100% 6 / 6 Subroutinesub 100% 10 / 10 Podpod 100% 1 / 1 totaltotal 97.1% 101 / 104 CC 17 SCAR 19.3
CC
17
Cov
97%
__ANON__
11.1
call
7.0
BEGIN
1.0
CRAP 6.9
↑ Summary
1 package Plack::Middleware::Text::Minify;
2  
3 # ABSTRACT: remove HTML indentation on the fly
4  
5 8 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 8 ▶use parent qw/ Plack::Middleware /;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 8 ▶use Plack::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 8 ▶use Plack::Util::Accessor qw/ path type /;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 8 ▶use Ref::Util qw/ is_arrayref is_coderef /;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 8 ▶use Text::Minify::XS v0.7.8 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 # RECOMMEND PREREQ: Ref::Util::XS
16  
17 8 our $VERSION = 'v0.4.2';
18  
19 19 ▶sub call {
Subroutine call: called CC=7 SCAR=19.5
Pod documented
20 19 my ($self, $env) = @_;
21  
22 19 my $res = $self->app->($env);
23  
24 19 ▶ return $res if $env->{'psgix.no-minify'};
Branch: 1 branch
BranchTrueFalseTotal
if $env->{"psgix.no-minify"} 1 18 19
25  
26 18 my $method = $env->{REQUEST_METHOD};
27 18 ▶ unless ($method =~ /^(GET|POST)$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($method =~ /^(GET|POST)$/u) 3 15 18
28 3 return $res;
29 }
30  
31 15 ▶ if (my $match = $self->path) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $match = $self->path) 4 11 15
32  
33 4 my $path = $env->{PATH_INFO};
34  
35 4 ▶ unless ( ( is_coderef($match) && $match->( $path, $env ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (is_coderef($match) and &$match($path, $env) or $path =~ /$match/u) 2 2 4
Condition100%6/6
is_coderef($match) and &$match($path, $env) or $path =~ /$match/u
l!l&&r!l&&!r
112
is_coderef($match) and &$match($path, $env)
!ll&&!rl&&r
211
MC/DC100%3/3
is_coderef($match) and &$match($path, $env) or $path =~ /$match/u
is_coderef($match) &$match($path, $env) $path =~ /$match/u
Truth table80%4/5
A and B or C
A= is_coderef($match)B= &$match($path, $env)C= $path =~ /$match/u
ABCresult
11X1
0X11
1011
0X00
1000
36 || ( $path =~ $match ) )
37 {
38 2 return $res;
39 }
40 }
41  
42 return Plack::Util::response_cb(
43 $res,
44 13 ▶ sub {
Subroutine __ANON__: called CC=11 SCAR=24.0
45 13 my ($res) = @_;
46  
47 13 ▶ return unless is_arrayref($res);
Branch: 1 branch
BranchTrueFalseTotal
unless is_arrayref($res) 0 13 13
48  
49 13 ▶ return if @$res < 3;
Branch: 1 branch
BranchTrueFalseTotal
if @$res < 3 0 13 13
50  
51 13 ▶ return if Plack::Util::status_with_no_entity_body( $res->[0] );
Branch: 1 branch
BranchTrueFalseTotal
if Plack::Util::status_with_no_entity_body($res->[0]) 2 11 13
52  
53 11 my $type = Plack::Util::header_get( $res->[1], 'content-type' );
54 11 ▶ if ( my $match = $self->type ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $match = $self->type) { } 4 7 11
55 return
56 4 ▶ unless ( ( is_coderef($match) && $match->( $type, $res ) )
Branch: 1 branch
BranchTrueFalseTotal
unless is_coderef($match) and &$match($type, $res) or $type =~ /$match/u 2 2 4
Condition100%6/6
is_coderef($match) and &$match($type, $res) or $type =~ /$match/u
l!l&&r!l&&!r
112
is_coderef($match) and &$match($type, $res)
!ll&&!rl&&r
211
MC/DC100%3/3
is_coderef($match) and &$match($type, $res) or $type =~ /$match/u
is_coderef($match) &$match($type, $res) $type =~ /$match/u
Truth table80%4/5
A and B or C
A= is_coderef($match)B= &$match($type, $res)C= $type =~ /$match/u
ABCresult
11X1
0X11
1011
0X00
1000
57 || ( $type =~ $match ) );
58 }
59 else {
60 7 ▶ return unless $type =~ m{^text/};
Branch: 1 branch
BranchTrueFalseTotal
unless $type =~ m[^text/]u 2 5 7
61 }
62  
63 7 my $body = $res->[2];
64 7 ▶ if ( is_arrayref($body) ) { # no reason to call a function for each line in the body
Branch: 1 branch
BranchTrueFalseTotal
if (is_arrayref($body)) { } 6 1 7
65 6 $res->[2] = [ Text::Minify::XS::minify( join( "", @$body ) ) ];
66 }
67 else {
68 1 my $text = "";
69 1 ▶ Plack::Util::foreach( $body, sub { $text .= $_[0] } );
Subroutine __ANON__: called CC=1 SCAR=0.0
70 1 $res->[2] = [ Text::Minify::XS::minify($text) ];
71 }
72  
73 7 ▶ if (Plack::Util::header_exists( $res->[1], 'content-length' )) {
Branch: 1 branch
BranchTrueFalseTotal
if (Plack::Util::header_exists($res->[1], "content-length")) 7 0 7
74 7 Plack::Util::header_set( $res->[1], 'content-length', length( $res->[2][0] ) );
75 }
76  
77 7 return;
78 }
79 13 );
80  
81 }
82  
83  
84 8 1;
85  
86 __END__
↑ Summary