blib/lib/Catalyst/Seal/Modifiers.pm

filter: Statementstmt 92.1% 187 / 203 Branchbran 66.2% 49 / 74 Conditioncond 50.0% 16 / 32 MC/DCmcdc 20.0% 4 / 20 Subroutinesub 100% 30 / 30 Podpod 100% 3 / 3 totaltotal 79.8% 289 / 362 CC 60 SCAR 19.6
CC
60
Cov
82%
_candidate_classes
15.8
_short_circuit_guard
14.5
flatten_class
7.0
CRAP 7.1
« blib/lib/Catalyst/Seal/ClassData.pm ↑ Summary blib/lib/Catalyst/Seal/Immutable.pm »
1 package Catalyst::Seal::Modifiers;
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 19 ▶use Sub::Util 1.40 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 19 ▶use Catalyst::Seal ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 19 ▶use Catalyst::Seal::Guard ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 19 our $VERSION = '0.04';
13  
14 19 our @SKIP_CLASS = (qr/\A Catalyst::Log (?: :: | \z ) /x);
15  
16 19 my %FLAT;
17  
18 2 ▶sub flattened {
Subroutine flattened: called CC=1 SCAR=0.0
Pod documented
19 2 _reap();
20 2 return map { "$_->{class}::$_->{name}" } values %FLAT;
21 }
22  
23 285 ▶sub _installed {
Subroutine _installed: called CC=1 SCAR=0.0
24 285 my ($class, $name) = @_;
25 19 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
26 285 return *{"${class}::${name}"}{CODE};
27 }
28  
29 14 ▶sub _reap {
Subroutine _reap: called CC=4 SCAR=15.6
30 14 for my $addr (keys %FLAT) {
31 1 my $rec = $FLAT{$addr};
32 1 my $installed = _installed($rec->{class}, $rec->{name});
33 1 ▶ next if $installed && Scalar::Util::refaddr($installed) == Scalar::Util::refaddr($rec->{flat});
Branch: 1 branch
BranchTrueFalseTotal
if $installed and builtin::refaddr($installed) == builtin::refaddr($rec->{"flat"}) 1 0 1
Condition33%1/3
$installed and builtin::refaddr($installed) == builtin::refaddr($rec->{"flat"})
!ll&&!rl&&r
001
MC/DC0%0/2
$installed and builtin::refaddr($installed) == builtin::refaddr($rec->{"flat"})
$installed builtin::refaddr($installed) == builtin::refaddr($rec->{"flat"})
Truth table33%1/3
A and B
A= $installedB= builtin::refaddr($installed) == builtin::refaddr($rec->{"flat"})
ABresult
0X0
100
111
34 0 delete $FLAT{$addr};
35 }
36 14 return;
37 }
38  
39 161 ▶sub _skip_class {
Subroutine _skip_class: called CC=3 SCAR=11.0
40 161 my ($class) = @_;
41 161 for my $re (@SKIP_CLASS) {
42 161 ▶ return 1 if $class =~ $re;
Branch: 1 branch
BranchTrueFalseTotal
if $class =~ /$re/ 1 160 161
43 }
44 160 return 0;
45 }
46  
47 12 ▶sub _candidate_classes {
Subroutine _candidate_classes: called CC=14 SCAR=27.6
48 12 my ($app) = @_;
49  
50 12 my @roots = ($app);
51 12 ▶ my $components = eval { $app->components } || {};
Condition50%1/2
eval { do { $app->components } } || {}
l!l
120
MC/DC0%0/1
eval { do { $app->components } } || {}
eval { do { $app->components } }
Truth table50%1/2
A
A= eval { do { $app->components } }
Aresult
11
00
52 12 ▶ push @roots, map { ref $_ || $_ } values %$components;
Condition66%2/3
ref $_ || $_
l!l&&r!l&&!r
36120
MC/DC0%0/2
ref $_ || $_
ref $_ $_
Truth table66%2/3
A || B
A= ref $_B= $_
ABresult
1X1
011
000
53  
54 12 for my $acc (qw(request_class response_class context_class
55 dispatcher_class engine_class stats_class)) {
56 72 ▶ next unless $app->can($acc);
Branch: 1 branch
BranchTrueFalseTotal
unless $app->can($acc) 0 72 72
57 72 my $class = eval { $app->$acc };
58 72 ▶ push @roots, $class if defined $class && !ref $class;
Branch: 1 branch
BranchTrueFalseTotal
if defined $class and not ref $class 60 12 72
Condition66%2/3
defined $class and not ref $class
!ll&&!rl&&r
12060
MC/DC50%1/2
defined $class and not ref $class
defined $class not ref $class
Truth table66%2/3
A and not(B)
A= defined $classB= not ref $class
ABresult
0X0
100
111
59 }
60  
61 12 my (%seen, @classes);
62 12 for my $root (@roots) {
63 120 ▶ next unless defined $root && length $root;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $root and length $root 0 120 120
Condition33%1/3
defined $root and length $root
!ll&&!rl&&r
00120
MC/DC0%0/2
defined $root and length $root
defined $root length $root
Truth table33%1/3
A and B
A= defined $rootB= length $root
ABresult
0X0
100
111
64 120 ▶ my $meta = Class::MOP::class_of($root) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $meta = Class::MOP::class_of($root) or next 120 0 120
65 120 ▶ next unless $meta->isa('Class::MOP::Class');
Branch: 1 branch
BranchTrueFalseTotal
unless $meta->isa("Class::MOP::Class") 0 120 120
66 120 for my $class ($meta->linearized_isa) {
67 384 ▶ next if $seen{$class}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$class}++ 228 156 384
68 156 push @classes, $class;
69 }
70 }
71 12 return @classes;
72 }
73  
74 160 ▶sub _wrapped_methods {
Subroutine _wrapped_methods: called CC=5 SCAR=16.3
75 160 my ($meta) = @_;
76  
77 160 my $map = eval { $meta->_method_map };
78 160 ▶ unless (ref $map eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $map eq "HASH") 0 160 160
79 0 Catalyst::Seal::note(
80 "no method map on " . $meta->name . ", its modifiers were not flattened");
81 0 return ();
82 }
83  
84 160 my @wrapped;
85 160 for my $name (sort keys %$map) {
86 4277 my $method = $map->{$name};
87 4277 ▶ next unless Scalar::Util::blessed($method)
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($method) and $method->isa("Class::MOP::Method::Wrapped") 3994 283 4277
Condition100%3/3
builtin::blessed($method) and $method->isa("Class::MOP::Method::Wrapped")
!ll&&!rl&&r
6723322283
MC/DC100%2/2
builtin::blessed($method) and $method->isa("Class::MOP::Method::Wrapped")
builtin::blessed($method) $method->isa("Class::MOP::Method::Wrapped")
Truth table100%3/3
A and B
A= builtin::blessed($method)B= $method->isa("Class::MOP::Method::Wrapped")
ABresult
0X0
100
111
88 && $method->isa('Class::MOP::Method::Wrapped');
89 283 push @wrapped, [ $name, $method ];
90 }
91 160 return @wrapped;
92 }
93  
94 354 ▶sub _modifier_table {
Subroutine _modifier_table: called CC=6 SCAR=19.1
95 354 my ($method) = @_;
96 354 my $table = $method->{modifier_table};
97 354 ▶ return undef unless ref $table eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $table eq "HASH" 0 354 354
98 354 ▶ return undef unless ref $table->{cache} eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $table->{"cache"} eq "CODE" 0 354 354
99 354 ▶ return undef unless ref $table->{before} eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $table->{"before"} eq "ARRAY" 0 354 354
100 354 ▶ return undef unless ref $table->{after} eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $table->{"after"} eq "ARRAY" 0 354 354
101 354 ▶ return undef unless ref $table->{around} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $table->{"around"} eq "HASH" 0 354 354
102 354 return $table;
103 }
104  
105 161 ▶sub flatten_class {
Subroutine flatten_class: called CC=7 SCAR=19.5
Pod documented
106 161 my ($class) = @_;
107  
108 161 ▶ return 0 if _skip_class($class);
Branch: 1 branch
BranchTrueFalseTotal
if _skip_class($class) 1 160 161
109  
110 160 ▶ my $meta = Class::MOP::class_of($class) or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $meta = Class::MOP::class_of($class) or return 0 160 0 160
111 160 ▶ return 0 unless $meta->isa('Class::MOP::Class');
Branch: 1 branch
BranchTrueFalseTotal
unless $meta->isa("Class::MOP::Class") 0 160 160
112  
113 160 ▶ my @wrapped = _wrapped_methods($meta) or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my(@wrapped) = _wrapped_methods($meta) or return 0 88 72 160
114 88 ▶ unless ($meta->is_immutable) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($meta->is_immutable) 1 87 88
115 1 Catalyst::Seal::note(
116 "$class is mutable, its " . scalar(@wrapped) . " wrapped method(s) were not flattened");
117 1 return 0;
118 }
119  
120 87 my $count = 0;
121 87 $count += _flatten_method($class, @$_) for @wrapped;
122 87 return $count;
123 }
124  
125 282 ▶sub _flatten_method {
Subroutine _flatten_method: called CC=6 SCAR=19.2
126 282 my ($class, $name, $method) = @_;
127  
128 282 ▶ return 0 if $FLAT{ Scalar::Util::refaddr $method };
Branch: 1 branch
BranchTrueFalseTotal
if $FLAT{builtin::refaddr($method)} 0 282 282
129  
130 282 my $table = _modifier_table($method);
131 282 ▶ unless ($table) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($table) 0 282 282
132 0 Catalyst::Seal::note(
133 "${class}::${name} has a modifier table this release does not know, not flattened");
134 0 return 0;
135 }
136  
137 282 my $installed = _installed($class, $name);
138 282 ▶ unless ($installed && Scalar::Util::refaddr($installed) == Scalar::Util::refaddr($method->body)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($installed and builtin::refaddr($installed) == builtin::refaddr($method->body)) 0 282 282
Condition33%1/3
$installed and builtin::refaddr($installed) == builtin::refaddr($method->body)
!ll&&!rl&&r
00282
MC/DC0%0/2
$installed and builtin::refaddr($installed) == builtin::refaddr($method->body)
$installed builtin::refaddr($installed) == builtin::refaddr($method->body)
Truth table33%1/3
A and B
A= $installedB= builtin::refaddr($installed) == builtin::refaddr($method->body)
ABresult
0X0
100
111
139 0 Catalyst::Seal::note(
140 "${class}::${name} is not the body the metaclass has, not flattened");
141 0 return 0;
142 }
143  
144 282 my $flat = $table->{cache};
145  
146 Sub::Util::set_subname(
147 $method->package_name . '::_wrapped_' . $method->name => $flat)
148 282 ▶ unless Scalar::Util::refaddr($flat) == Scalar::Util::refaddr($table->{orig});
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::refaddr($flat) == builtin::refaddr($table->{"orig"}) 282 0 282
149  
150 {
151 19 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
152 19 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
153 282 *{"${class}::${name}"} = $flat;
154 }
155  
156 282 $FLAT{ Scalar::Util::refaddr $method } = {
157 class => $class,
158 name => $name,
159 flat => $flat,
160 method => $method,
161 };
162 282 _install_unflatten_hook();
163 282 return 1;
164 }
165  
166 10 ▶sub unflatten_method {
Subroutine unflatten_method: called CC=5 SCAR=16.3
Pod documented
167 10 my ($method) = @_;
168  
169 10 ▶ return 0 unless Scalar::Util::blessed($method);
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($method) 1 9 10
170 9 ▶ my $rec = $FLAT{ Scalar::Util::refaddr $method } or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $rec = $FLAT{builtin::refaddr($method)} or return 0 2 7 9
171 2 my ($class, $name) = ($rec->{class}, $rec->{name});
172  
173 2 my $installed = _installed($rec->{class}, $rec->{name});
174 2 ▶ if ($installed && Scalar::Util::refaddr($installed) == Scalar::Util::refaddr($rec->{flat})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($installed and builtin::refaddr($installed) == builtin::refaddr($rec->{'flat'})) { } 2 0 2
Condition33%1/3
$installed and builtin::refaddr($installed) == builtin::refaddr($rec->{"flat"})
!ll&&!rl&&r
002
MC/DC0%0/2
$installed and builtin::refaddr($installed) == builtin::refaddr($rec->{"flat"})
$installed builtin::refaddr($installed) == builtin::refaddr($rec->{"flat"})
Truth table33%1/3
A and B
A= $installedB= builtin::refaddr($installed) == builtin::refaddr($rec->{"flat"})
ABresult
0X0
100
111
175 19 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
176 19 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
177 2 *{"${class}::${name}"} = $method->body;
178 2 Catalyst::Seal::note("${class}::${name} un-flattened, a modifier was added after seal");
179 }
180 else {
181 0 Catalyst::Seal::note(
182 "${class}::${name} was replaced after it was flattened, left alone");
183 }
184  
185 2 delete $FLAT{ Scalar::Util::refaddr $method };
186 2 return 1;
187 }
188  
189 19 my $HOOKED = 0;
190  
191 282 ▶sub _install_unflatten_hook {
Subroutine _install_unflatten_hook: called CC=3 SCAR=11.0
192 282 ▶ return if $HOOKED++;
Branch: 1 branch
BranchTrueFalseTotal
if $HOOKED++ 268 14 282
193 14 for my $kind (qw(before after around)) {
194 42 my $sym = "Class::MOP::Method::Wrapped::add_${kind}_modifier";
195 19 ▶ my $orig = do { no strict 'refs'; \&{$sym} };
Subroutine BEGIN: called CC=1 SCAR=0.0
196 19 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
197 19 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
198 42 ▶ *{$sym} = sub {
Subroutine __ANON__: called CC=4 SCAR=14.2
199 8 unflatten_method($_[0]);
200 8 ▶ if (Scalar::Util::blessed($_[0]) && $_[0]->can('name')
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($_[0]) and $_[0]->can("name") and $_[0]->name eq "BUILD") 2 6 8
Condition50%3/6
builtin::blessed($_[0]) and $_[0]->can("name") and $_[0]->name eq "BUILD"
!ll&&!rl&&r
062
builtin::blessed($_[0]) and $_[0]->can("name")
!ll&&!rl&&r
008
MC/DC33%1/3
builtin::blessed($_[0]) and $_[0]->can("name") and $_[0]->name eq "BUILD"
builtin::blessed($_[0]) $_[0]->can("name") $_[0]->name eq "BUILD"
Truth table50%2/4
A and B and C
A= builtin::blessed($_[0])B= $_[0]->can("name")C= $_[0]->name eq "BUILD"
ABCresult
0XX0
10X0
1100
1111
201 && $_[0]->name eq 'BUILD') {
202 2 require Catalyst::Seal::Construct;
203 2 Catalyst::Seal::Construct::_unseal_constructors();
204 }
205 8 goto &$orig;
206 42 };
207 }
208 14 return;
209 }
210  
211 19 my @GUARDED_SETTERS = qw(status headers content_encoding content_length content_type);
212  
213 19 my %GUARD_DONE;
214  
215 24 ▶sub _short_circuit_guard {
Subroutine _short_circuit_guard: called CC=10 SCAR=26.7
216 24 my ($key, $names, $args) = @_;
217  
218 24 ▶ return 0 if $GUARD_DONE{$key}++;
Branch: 1 branch
BranchTrueFalseTotal
if $GUARD_DONE{$key}++ 0 24 24
219  
220 24 ▶ my $meta = Class::MOP::class_of('Catalyst::Response') or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $meta = Class::MOP::class_of("Catalyst::Response") or return 0 24 0 24
221  
222 24 my (@lists, %count);
223 24 for my $name (@$names) {
224 72 my $method = eval { $meta->get_method($name) };
225 72 ▶ unless (Scalar::Util::blessed($method) && $method->isa('Class::MOP::Method::Wrapped')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($method) and $method->isa("Class::MOP::Method::Wrapped")) 0 72 72
Condition33%1/3
builtin::blessed($method) and $method->isa("Class::MOP::Method::Wrapped")
!ll&&!rl&&r
0072
MC/DC0%0/2
builtin::blessed($method) and $method->isa("Class::MOP::Method::Wrapped")
builtin::blessed($method) $method->isa("Class::MOP::Method::Wrapped")
Truth table33%1/3
A and B
A= builtin::blessed($method)B= $method->isa("Class::MOP::Method::Wrapped")
ABresult
0X0
100
111
226 0 Catalyst::Seal::note("Catalyst::Response::$name is not wrapped, guard not patched");
227 0 return 0;
228 }
229 72 ▶ my $table = _modifier_table($method) or do {
Branch: 1 branch
BranchTrueFalseTotal
my $table = _modifier_table($method) or do { Catalyst::Seal::note("Catalyst::Response::$name has an unknown modifier table, guard not patched"); return 0 } 72 0 72
230 0 Catalyst::Seal::note("Catalyst::Response::$name has an unknown modifier table, guard not patched");
231 0 return 0;
232 };
233 72 my $before = $table->{before};
234 72 ▶ unless (@$before == 1) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$before == 1) 0 72 72
235 0 Catalyst::Seal::note(sprintf
236 "Catalyst::Response::%s has %d before modifiers, not the one this patch was written against",
237 $name, scalar @$before);
238 0 return 0;
239 }
240 72 push @lists, $before;
241 72 $count{ Scalar::Util::refaddr $before->[0] }++;
242 }
243  
244 24 ▶ unless (keys(%count) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
unless (keys %count == 1) 0 24 24
245 0 Catalyst::Seal::note(
246 "the Catalyst::Response guard is not one modifier shared by @$names, not patched");
247 0 return 0;
248 }
249  
250 24 my $stock = $lists[0][0];
251 24 ▶ my $fast = sub { return unless @_ > $args; goto &$stock };
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
unless @_ > $args 875 949 1824
252 24 $_->[0] = $fast for @lists;
253 24 return 1;
254 }
255  
256 12 ▶Catalyst::Seal::register_step('response-guard' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
257 12 require Class::MOP;
258 12 require Catalyst::Response;
259  
260 12 _short_circuit_guard('Catalyst::Response::setter_guard',
261 \@GUARDED_SETTERS, 1);
262 12 _short_circuit_guard('Catalyst::Response::header_guard',
263 ['header'], 2);
264 12 return;
265 19 });
266  
267 12 ▶Catalyst::Seal::register_step('modifiers' => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
268 12 my ($app) = @_;
269 12 require Class::MOP;
270 12 require Class::MOP::Method::Wrapped;
271 12 _reap();
272 12 my $count = 0;
273 12 $count += flatten_class($_) for _candidate_classes($app);
274 12 ▶ Catalyst::Seal::note("flattened $count wrapped method(s)") if $Catalyst::Seal::DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Catalyst::Seal::DEBUG 0 12 12
275 12 return;
276 19 });
277  
278 19 1;
279  
280 __END__
« blib/lib/Catalyst/Seal/ClassData.pm ↑ Summary blib/lib/Catalyst/Seal/Immutable.pm »