blib/lib/Catalyst/Seal/Accessors.pm

filter: Statementstmt 91.1% 124 / 136 Branchbran 62.1% 41 / 66 Conditioncond 42.8% 9 / 21 MC/DCmcdc 14.2% 2 / 14 Subroutinesub 100% 17 / 17 Podpod n/a totaltotal 75.9% 193 / 254 CC 45 SCAR 22.0
CC
45
Cov
78%
_seal_readers
18.5
_seal_guarded_readers
10.1
_plain_write
8.1
CRAP 9.0
« blib/lib/Catalyst/Seal/Finalize.pm ↑ Summary blib/lib/Catalyst/Seal/ClassData.pm »
1 package Catalyst::Seal::Accessors;
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 B ();
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 12 ▶sub _seal_config {
Subroutine _seal_config: called CC=5 SCAR=18.0
14 12 my ($app) = @_;
15  
16 12 ▶ my $orig = $app->can('config') or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $orig = $app->can("config") or return 0 12 0 12
17  
18 12 my $value = eval { $app->config };
19 12 ▶ if ($@ || !defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ or not defined $value) 0 12 12
Condition33%1/3
$@ or not defined $value
l!l&&r!l&&!r
0012
MC/DC0%0/2
$@ or not defined $value
$@ not defined $value
Truth table33%1/3
A or not(B)
A= $@B= not defined $value
ABresult
1X1
011
000
20 0 Catalyst::Seal::note("could not resolve $app->config, not sealed: $@");
21 0 return 0;
22 }
23  
24 12 my $ok = eval {
25 19 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
26 12 Catalyst::Seal::_install_const($app, 'config', $value, $orig, undef);
27 12 1;
28 };
29 12 ▶ unless ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 12 12
30 0 Catalyst::Seal::note("could not seal $app->config: $@");
31 0 return 0;
32 }
33 12 return 1;
34 }
35  
36 19 my %DELEGATOR = (
37 req => 'request',
38 res => 'response',
39 comp => 'component',
40 );
41  
42 12 ▶sub _seal_delegators {
Subroutine _seal_delegators: called CC=6 SCAR=18.4
43 12 my ($app) = @_;
44 12 my $sealed = 0;
45  
46 12 for my $name (sort keys %DELEGATOR) {
47 36 my $target = $DELEGATOR{$name};
48  
49 36 ▶ my $from = $app->can($name) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $from = $app->can($name) or next 36 0 36
50 36 ▶ my $to = $app->can($target) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $to = $app->can($target) or next 36 0 36
51  
52 36 ▶ next if $from == $to;
Branch: 1 branch
BranchTrueFalseTotal
if $from == $to 0 36 36
53  
54 36 my $ok = eval {
55 19 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
56 19 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
57 36 *{"${app}::${name}"} = $to;
58 36 1;
59 };
60 36 ▶ unless ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 36 36
61 0 Catalyst::Seal::note("could not alias $app\::$name to $target: $@");
62 0 next;
63 }
64 36 $sealed++;
65 }
66  
67 12 return $sealed;
68 }
69  
70 12 ▶sub _reader_classes {
Subroutine _reader_classes: called CC=5 SCAR=18.2
71 12 my ($app) = @_;
72 12 ▶ my @classes = ($app->context_class || $app);
Condition33%1/3
$app->context_class || $app
l!l&&r!l&&!r
1200
MC/DC0%0/2
$app->context_class || $app
$app->context_class $app
Truth table33%1/3
A || B
A= $app->context_classB= $app
ABresult
1X1
011
000
73 12 ▶ push @classes, eval { $app->composed_request_class } || ();
Condition33%1/3
eval { do { $app->composed_request_class } } || ()
l!l&&r!l&&!r
1200
MC/DC0%0/2
eval { do { $app->composed_request_class } } || ()
eval { do { $app->composed_request_class } } ()
Truth table33%1/3
A || B
A= eval { do { $app->composed_request_class } }B= ()
ABresult
1X1
011
000
74 12 ▶ push @classes, eval { $app->composed_response_class } || ();
Condition33%1/3
eval { do { $app->composed_response_class } } || ()
l!l&&r!l&&!r
1200
MC/DC0%0/2
eval { do { $app->composed_response_class } } || ()
eval { do { $app->composed_response_class } } ()
Truth table33%1/3
A || B
A= eval { do { $app->composed_response_class } }B= ()
ABresult
1X1
011
000
75  
76 12 my %seen;
77 12 ▶ return grep { !$seen{$_}++ } grep { defined && length } @classes;
Condition33%1/3
defined $_ && length $_
!ll&&!rl&&r
0036
MC/DC0%0/2
defined $_ && length $_
defined $_ length $_
Truth table33%1/3
A && B
A= defined $_B= length $_
ABresult
0X0
100
111
78 }
79  
80 617 ▶sub _plain_write {
Subroutine _plain_write: called CC=8 SCAR=20.9
81 617 my ($attr, $name) = @_;
82  
83 617 my $write = $attr->get_write_method;
84 617 ▶ return 0 unless defined $write && $write eq $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $write and $write eq $name 205 412 617
Condition100%3/3
defined $write and $write eq $name
!ll&&!rl&&r
16936412
MC/DC100%2/2
defined $write and $write eq $name
defined $write $write eq $name
Truth table100%3/3
A and B
A= defined $writeB= $write eq $name
ABresult
0X0
100
111
85  
86 412 ▶ return 0 if $attr->has_type_constraint;
Branch: 1 branch
BranchTrueFalseTotal
if $attr->has_type_constraint 24 388 412
87 388 ▶ return 0 if $attr->has_trigger;
Branch: 1 branch
BranchTrueFalseTotal
if $attr->has_trigger 0 388 388
88 388 ▶ return 0 if $attr->should_coerce;
Branch: 1 branch
BranchTrueFalseTotal
if $attr->should_coerce 0 388 388
89 388 ▶ return 0 if $attr->is_weak_ref;
Branch: 1 branch
BranchTrueFalseTotal
if $attr->is_weak_ref 12 376 388
90 376 ▶ return 0 if $attr->has_initializer;
Branch: 1 branch
BranchTrueFalseTotal
if $attr->has_initializer 0 376 376
91  
92 376 return 1;
93 }
94  
95 37 ▶sub _seal_readers {
Subroutine _seal_readers: called CC=15 SCAR=29.2
96 37 my ($class) = @_;
97  
98 37 ▶ my $meta = Class::MOP::class_of($class) or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $meta = Class::MOP::class_of($class) or return 0 37 0 37
99 37 ▶ return 0 unless $meta->isa('Class::MOP::Class');
Branch: 1 branch
BranchTrueFalseTotal
unless $meta->isa("Class::MOP::Class") 0 37 37
100 37 ▶ unless ($meta->is_immutable) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($meta->is_immutable) 0 37 37
101 0 Catalyst::Seal::note("$class is mutable, its readers were not sealed");
102 0 return 0;
103 }
104  
105 37 my $sealed = 0;
106 37 for my $attr ($meta->get_all_attributes) {
107 725 ▶ my $name = eval { $attr->get_read_method } or next;
Branch: 1 branch
BranchTrueFalseTotal
my $name = eval { do { $attr->get_read_method } } or next 701 24 725
108 701 ▶ my $ref = eval { $attr->get_read_method_ref } or next;
Branch: 1 branch
BranchTrueFalseTotal
my $ref = eval { do { $attr->get_read_method_ref } } or next 701 0 701
109 701 ▶ next unless ref $ref && $ref->isa('Class::MOP::Method');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $ref and $ref->isa("Class::MOP::Method") 0 701 701
Condition33%1/3
ref $ref and $ref->isa("Class::MOP::Method")
!ll&&!rl&&r
00701
MC/DC0%0/2
ref $ref and $ref->isa("Class::MOP::Method")
ref $ref $ref->isa("Class::MOP::Method")
Truth table33%1/3
A and B
A= ref $refB= $ref->isa("Class::MOP::Method")
ABresult
0X0
100
111
110  
111 701 ▶ next if $ref->isa('Class::MOP::Method::Wrapped');
Branch: 1 branch
BranchTrueFalseTotal
if $ref->isa("Class::MOP::Method::Wrapped") 72 629 701
112  
113 629 ▶ my $body = eval { $ref->body } or next;
Branch: 1 branch
BranchTrueFalseTotal
my $body = eval { do { $ref->body } } or next 629 0 629
114 629 ▶ my $installed = $class->can($name) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $installed = $class->can($name) or next 629 0 629
115  
116 629 ▶ next unless $installed == $body;
Branch: 1 branch
BranchTrueFalseTotal
unless $installed == $body 12 617 629
117  
118 617 my $ok = eval {
119 19 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
120 617 ▶ Catalyst::Seal::_install_reader(
Branch: 1 branch
BranchTrueFalseTotal
$attr->is_lazy ? : 169 448 617
121 $class, $name, $attr->name, $body, ($attr->is_lazy ? 1 : 0),
122 _plain_write($attr, $name));
123 617 1;
124 };
125 617 ▶ unless ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 617 617
126 0 Catalyst::Seal::note("could not seal reader $class\::$name: $@");
127 0 next;
128 }
129 617 $sealed++;
130 }
131  
132 37 return $sealed;
133 }
134  
135 19 my @GUARDED = (
136 ['Catalyst::Response', 'headers'],
137 ['Catalyst::Response', 'status'],
138 );
139  
140 12 ▶sub _seal_guarded_readers {
Subroutine _seal_guarded_readers: called CC=9 SCAR=23.1
141 12 my $sealed = 0;
142  
143 12 for my $pair (@GUARDED) {
144 24 my ($class, $name) = @$pair;
145  
146 24 ▶ my $meta = Class::MOP::class_of($class) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $meta = Class::MOP::class_of($class) or next 24 0 24
147 24 ▶ next unless $meta->is_immutable;
Branch: 1 branch
BranchTrueFalseTotal
unless $meta->is_immutable 0 24 24
148  
149 24 ▶ my $attr = $meta->find_attribute_by_name($name) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $attr = $meta->find_attribute_by_name($name) or next 24 0 24
150 24 ▶ my $installed = $class->can($name) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $installed = $class->can($name) or next 24 0 24
151  
152 24 my $gv = B::svref_2object($installed)->GV;
153 24 ▶ next unless $gv->NAME =~ /\A_wrapped_/;
Branch: 1 branch
BranchTrueFalseTotal
unless $gv->NAME =~ /\A_wrapped_/ 0 24 24
154  
155 24 my $ok = eval {
156 19 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
157 24 ▶ Catalyst::Seal::_install_reader(
Branch: 1 branch
BranchTrueFalseTotal
$attr->is_lazy ? : 12 12 24
158 $class, $name, $attr->name, $installed,
159 ($attr->is_lazy ? 1 : 0));
160 24 1;
161 };
162 24 ▶ unless ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 24 24
163 0 Catalyst::Seal::note("could not seal guarded reader $class\::$name: $@");
164 0 next;
165 }
166 24 $sealed++;
167 }
168  
169 12 return $sealed;
170 }
171  
172 12 ▶Catalyst::Seal::register_step('accessors' => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
173 12 my ($app) = @_;
174  
175 12 require Class::MOP;
176  
177 12 my $config = _seal_config($app);
178 12 my $readers = 0;
179 12 $readers += _seal_readers($_) for _reader_classes($app);
180  
181 12 $readers += _seal_guarded_readers();
182  
183 12 my $delegators = _seal_delegators($app);
184  
185 12 ▶ Catalyst::Seal::note(
Branch: 1 branch
BranchTrueFalseTotal
if $Catalyst::Seal::DEBUG 0 12 12
186 "sealed config ($config), $readers reader(s), $delegators delegator(s)")
187 if $Catalyst::Seal::DEBUG;
188 12 return;
189 19 });
190  
191 19 1;
192  
193 __END__
« blib/lib/Catalyst/Seal/Finalize.pm ↑ Summary blib/lib/Catalyst/Seal/ClassData.pm »