blib/lib/Catalyst/Seal/Finalize.pm

filter: Statementstmt 96.0% 96 / 100 Branchbran 75.8% 44 / 58 Conditioncond 68.0% 34 / 50 MC/DCmcdc 48.1% 13 / 27 Subroutinesub 93.3% 14 / 15 Podpod 0.0% 0 / 2 totaltotal 79.7% 201 / 252 CC 48 SCAR 26.2
CC
48
Cov
84%
_finalize_encoding
24.7
_decide
14.1
_destroy_is_stock
7.8
CRAP 13.8
« blib/lib/Catalyst/Seal/Construct.pm ↑ Summary blib/lib/Catalyst/Seal/Accessors.pm »
1 package Catalyst::Seal::Finalize;
2  
3 19 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 19 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 19 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 19 ▶use Catalyst::Seal ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 19 ▶use Catalyst::Seal::Guard ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 19 our $VERSION = '0.04';
12  
13 19 my %DECISION;
14  
15 19 our $MAX_KEYS = 512;
16 19 my $CAPPED = 0;
17  
18 7 ▶sub _clear { %DECISION = (); $CAPPED = 0; return }
Subroutine _clear: called CC=1 SCAR=0.0
19 1 ▶sub capped { $CAPPED }
Subroutine capped: called CC=1 SCAR=0.0
Pod undocumented
20 2 ▶sub memo_size { scalar keys %DECISION }
Subroutine memo_size: called CC=1 SCAR=0.0
Pod undocumented
21  
22 125 ▶sub _decide {
Subroutine _decide: called CC=14 SCAR=26.5
23 125 my ($res, $cenc, $ectype, $mime) = @_;
24  
25 125 my $ct = $res->content_type;
26 125 my $charset = $res->content_type_charset;
27  
28 125 my $has_manual_charset = 0;
29 125 ▶ if (defined $charset && length $charset) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $charset and length $charset) 4 121 125
Condition66%2/3
defined $charset and length $charset
!ll&&!rl&&r
12104
MC/DC50%1/2
defined $charset and length $charset
defined $charset length $charset
Truth table66%2/3
A and B
A= defined $charsetB= length $charset
ABresult
0X0
100
111
30 4 ▶ $has_manual_charset =
Branch: 1 branch
BranchTrueFalseTotal
defined $mime && uc $charset ne uc $mime ? : 3 1 4
Condition66%2/3
defined $mime and uc $charset ne uc $mime
!ll&&!rl&&r
103
MC/DC50%1/2
defined $mime and uc $charset ne uc $mime
defined $mime uc $charset ne uc $mime
Truth table66%2/3
A and B
A= defined $mimeB= uc $charset ne uc $mime
ABresult
0X0
100
111
31 (defined $mime && uc($charset) ne uc($mime)) ? 1 : 0;
32 }
33  
34 125 my $encodable = 0;
35 125 ▶ if (defined $mime) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $mime) 123 2 125
36 123 ▶ my $type_ok = (defined $ct && $ct =~ m/$ectype/) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $ct && $ct =~ /$ectype/ ? : 121 2 123
Condition66%2/3
defined $ct and $ct =~ /$ectype/
!ll&&!rl&&r
02121
MC/DC50%1/2
defined $ct and $ct =~ /$ectype/
defined $ct $ct =~ /$ectype/
Truth table66%2/3
A and B
A= defined $ctB= $ct =~ /$ectype/
ABresult
0X0
100
111
37 123 ▶ my $enc_ok = (!defined $cenc || !length $cenc || $cenc eq 'identity') ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
!(defined $cenc && length $cenc) || $cenc eq 'identity' ? : 122 1 123
Condition83%5/6
not defined $cenc && length $cenc or $cenc eq "identity"
l!l&&r!l&&!r
12111
defined $cenc && length $cenc
!ll&&!rl&&r
12102
MC/DC66%2/3
not defined $cenc && length $cenc or $cenc eq "identity"
defined $cenc length $cenc $cenc eq "identity"
Truth table75%3/4
not(A && B) or C
A= defined $cencB= length $cencC= $cenc eq "identity"
ABCresult
0XX1
10X1
1111
1100
38 123 ▶ $encodable = ($type_ok && !$has_manual_charset && $enc_ok) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$type_ok && !$has_manual_charset && $enc_ok ? : 117 6 123
Condition100%6/6
$type_ok and not $has_manual_charset and $enc_ok
!ll&&!rl&&r
51117
$type_ok and not $has_manual_charset
!ll&&!rl&&r
23118
MC/DC100%3/3
$type_ok and not $has_manual_charset and $enc_ok
$type_ok not $has_manual_charset $enc_ok
Truth table100%4/4
A and not(B) and C
A= $type_okB= not $has_manual_charsetC= $enc_ok
ABCresult
0XX0
10X0
1100
1111
39 }
40  
41 125 return { encodable => $encodable, charset => $charset, ct => $ct };
42 }
43  
44 367 ▶sub _finalize_encoding {
Subroutine _finalize_encoding: called CC=22 SCAR=32.1
45 367 my $c = shift;
46 367 ▶ my $res = $c->res || return;
Condition50%1/2
$c->res || (return)
l!l
3670
MC/DC0%0/1
$c->res || (return)
$c->res
Truth table50%1/2
A
A= $c->res
Aresult
11
00
47  
48 367 my $ctx = $res->_context;
49 367 ▶ return _stock_finalize_encoding($c) if !$ctx || $ctx != $c;
Branch: 1 branch
BranchTrueFalseTotal
if not $ctx or $ctx != $c 0 367 367
Condition33%1/3
not $ctx or $ctx != $c
l!l&&r!l&&!r
00367
MC/DC0%0/2
not $ctx or $ctx != $c
not $ctx $ctx != $c
Truth table33%1/3
not(A) or B
A= not $ctxB= $ctx != $c
ABresult
1X1
011
000
50  
51 367 my $enc = $c->encoding;
52 367 ▶ my $mime = $enc ? $enc->mime_name : undef;
Branch: 1 branch
BranchTrueFalseTotal
$enc ? : 166 201 367
53 367 my $cenc = $res->content_encoding;
54 367 my $ectype = $res->encodable_content_type;
55  
56 367 my $raw = $res->headers->header('Content-Type');
57  
58 367 ▶ my $key = join "\0",
Branch: 4 branches
BranchTrueFalseTotal
defined $raw ? : 366 1 367
defined $cenc ? : 2 365 367
defined $ectype ? : 367 0 367
defined $mime ? : 166 201 367
59 (defined $raw ? $raw : ''),
60 (defined $cenc ? $cenc : ''),
61 (defined $ectype ? "$ectype" : ''),
62 (defined $mime ? $mime : '');
63  
64 367 my $d = $DECISION{$key};
65 367 ▶ unless ($d) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($d) 125 242 367
66 125 $d = _decide($res, $cenc, $ectype, $mime);
67 125 ▶ if (keys %DECISION < $MAX_KEYS) {
Branch: 2 branches
BranchTrueFalseTotal
if (keys %DECISION < $Catalyst::Seal::Finalize::MAX_KEYS) { } 28 97 125
elsif (not $CAPPED++) { } 1 96 97
68 28 $DECISION{$key} = $d;
69 }
70 elsif (!$CAPPED++) {
71 1 Catalyst::Seal::note(
72 "encoding decision memo reached $MAX_KEYS keys, no longer caching");
73 }
74 }
75  
76 367 ▶ if ($d->{charset} && defined $mime && uc($mime) ne uc($d->{charset})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d->{"charset"} and defined $mime and uc $mime ne uc $d->{"charset"}) 3 364 367
Condition83%5/6
$d->{"charset"} and defined $mime and uc $mime ne uc $d->{"charset"}
!ll&&!rl&&r
36403
$d->{"charset"} and defined $mime
!ll&&!rl&&r
1642003
MC/DC66%2/3
$d->{"charset"} and defined $mime and uc $mime ne uc $d->{"charset"}
$d->{"charset"} defined $mime uc $mime ne uc $d->{"charset"}
Truth table75%3/4
A and B and C
A= $d->{"charset"}B= defined $mimeC= uc $mime ne uc $d->{"charset"}
ABCresult
0XX0
10X0
1100
1111
77 3 my $lc = lc $d->{charset};
78 3 $c->log->debug("Catalyst encoding config is set to encode in '" .
79 $mime . "', content type is '$lc', not encoding ");
80 }
81  
82 367 ▶ if ($d->{encodable} && defined($res->body) && ref(\$res->body) eq 'SCALAR') {
Branch: 1 branch
BranchTrueFalseTotal
if ($d->{"encodable"} and defined $res->body and ref \$res->body eq "SCALAR") 157 210 367
Condition100%6/6
$d->{"encodable"} and defined $res->body and ref \$res->body eq "SCALAR"
!ll&&!rl&&r
2082157
$d->{"encodable"} and defined $res->body
!ll&&!rl&&r
2071159
MC/DC100%3/3
$d->{"encodable"} and defined $res->body and ref \$res->body eq "SCALAR"
$d->{"encodable"} defined $res->body ref \$res->body eq "SCALAR"
Truth table100%4/4
A and B and C
A= $d->{"encodable"}B= defined $res->bodyC= ref \$res->body eq "SCALAR"
ABCresult
0XX0
10X0
1100
1111
83 157 $res->body( $enc->encode( $res->body, $c->_encode_check ) );
84  
85 $res->content_type($d->{ct} . "; charset=" . $mime)
86 unless ($d->{charset} ||
87 157 ▶ ($ctx && $res->finalized_headers && !$res->_has_response_cb));
Branch: 1 branch
BranchTrueFalseTotal
unless $d->{"charset"} or $ctx and $res->finalized_headers and not $res->_has_response_cb 157 0 157
Condition33%3/9
$d->{"charset"} or $ctx and $res->finalized_headers and not $res->_has_response_cb
l!l&&r!l&&!r
00157
$ctx and $res->finalized_headers and not $res->_has_response_cb
!ll&&!rl&&r
15700
$ctx and $res->finalized_headers
!ll&&!rl&&r
01570
MC/DC0%0/4
$d->{"charset"} or $ctx and $res->finalized_headers and not $res->_has_response_cb
$d->{"charset"} $ctx $res->finalized_headers not $res->_has_response_cb
Truth table20%1/5
A or B and C and not(D)
A= $d->{"charset"}B= $ctxC= $res->finalized_headersD= not $res->_has_response_cb
ABCDresult
1XXX1
01111
00XX0
010X0
01100
88 }
89  
90 367 return;
91 }
92  
93 0 ▶sub _stock_finalize_encoding {
Subroutine _stock_finalize_encoding: not called CC=2 SCAR=17.9
94 0 my ($c) = @_;
95 0 ▶ my $orig = $Catalyst::Seal::Guard::ORIGINAL{'Catalyst::finalize_encoding'}
Branch: 1 branch
BranchTrueFalseTotal
my $orig = $Catalyst::Seal::Guard::ORIGINAL{"Catalyst::finalize_encoding"} or return 0 0 0
96 or return;
97 0 return $orig->($c);
98 }
99  
100 388 ▶sub _response_destroy {
Subroutine _response_destroy: called CC=3 SCAR=11.0
101 388 my $self = shift;
102 388 ▶ return $self->Moose::Object::DESTROY(@_)
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($self) ne "Catalyst::Response" 2 386 388
103 if Scalar::Util::blessed($self) ne 'Catalyst::Response';
104  
105 386 local $?;
106 386 my $igd = Devel::GlobalDestruction::in_global_destruction();
107 386 my $ok = eval { $self->Catalyst::Response::DEMOLISH($igd); 1 };
108 386 ▶ die $@ unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 386 386
109 386 return;
110 }
111  
112 12 ▶sub _destroy_is_stock {
Subroutine _destroy_is_stock: called CC=7 SCAR=20.5
113 12 ▶ my $cv = Catalyst::Response->can('DESTROY') or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $cv = "Catalyst::Response"->can("DESTROY") or return 0 12 0 12
114 12 ▶ return 0 unless Catalyst::Response->can('DEMOLISH');
Branch: 1 branch
BranchTrueFalseTotal
unless "Catalyst::Response"->can("DEMOLISH") 0 12 12
115  
116 12 my $text = eval {
117 12 require B::Deparse;
118 12 B::Deparse->new->coderef2text($cv);
119 };
120 12 ▶ return 0 unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 12 12
121  
122 12 for my $want (qw(
123 Moose::Object::DESTROY
124 in_global_destruction
125 Try::Tiny::try
126 DEMOLISH
127 )) {
128 48 ▶ return 0 unless index($text, $want) >= 0;
Branch: 1 branch
BranchTrueFalseTotal
unless index($text, $want) != -1 0 48 48
129 }
130 12 ▶ return 0 if index($text, 'DEMOLISHALL') >= 0;
Branch: 1 branch
BranchTrueFalseTotal
if index($text, "DEMOLISHALL") != -1 0 12 12
131 12 return 1;
132 }
133  
134 19 my $PATCHED = 0;
135  
136 12 ▶Catalyst::Seal::register_step('finalize' => sub {
Subroutine __ANON__: called CC=5 SCAR=16.8
137 12 my ($app) = @_;
138  
139 12 ▶ return if $PATCHED++;
Branch: 1 branch
BranchTrueFalseTotal
if $PATCHED++ 0 12 12
140  
141 12 my $enc = Catalyst::Seal::Guard::replace(
142 'Catalyst::finalize_encoding' => \&_finalize_encoding);
143  
144 12 my $destroy = 0;
145 12 require Devel::GlobalDestruction;
146 12 require Scalar::Util;
147 12 ▶ if (_destroy_is_stock()) {
Branch: 1 branch
BranchTrueFalseTotal
if (_destroy_is_stock()) { } 12 0 12
148 19 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
149 12 ▶ $Catalyst::Seal::Guard::ORIGINAL{'Catalyst::Response::DESTROY'}
Condition33%1/3
$Catalyst::Seal::Guard::ORIGINAL{"Catalyst::Response::DESTROY"} ||= "Catalyst::Response"->can("DESTROY")
l!l&&r!l&&!r
0120
MC/DC0%0/2
$Catalyst::Seal::Guard::ORIGINAL{"Catalyst::Response::DESTROY"} ||= "Catalyst::Response"->can("DESTROY")
$Catalyst::Seal::Guard::ORIGINAL{"Catalyst::Response::DESTROY"} "Catalyst::Response"->can("DESTROY")
Truth table33%1/3
A ||= B
A= $Catalyst::Seal::Guard::ORIGINAL{"Catalyst::Response::DESTROY"}B= "Catalyst::Response"->can("DESTROY")
ABresult
1X1
011
000
150 ||= Catalyst::Response->can('DESTROY');
151 12 *Catalyst::Response::DESTROY = \&_response_destroy;
152 12 $destroy = 1;
153 }
154 else {
155 0 Catalyst::Seal::note(
156 'Catalyst::Response::DESTROY is not the shape we replace, left alone');
157 }
158  
159 12 ▶ Catalyst::Seal::note("finalize: encoding=$enc destroy=$destroy")
Branch: 1 branch
BranchTrueFalseTotal
if $Catalyst::Seal::DEBUG 0 12 12
160 if $Catalyst::Seal::DEBUG;
161 12 return;
162 19 });
163  
164 19 1;
165  
166 __END__
« blib/lib/Catalyst/Seal/Construct.pm ↑ Summary blib/lib/Catalyst/Seal/Accessors.pm »