blib/lib/MooX/XSConstructor.pm

filter: Statementstmt 91.3% 105 / 115 Branchbran 70.0% 21 / 30 Conditioncond 73.3% 11 / 15 MC/DCmcdc 55.5% 5 / 9 Subroutinesub 100% 16 / 16 Podpod 100% 3 / 3 totaltotal 85.6% 161 / 188 CC 25 SCAR 22.1
CC
25
Cov
86%
setup_for
16.9
is_suitable_class
8.9
import
3.0
CRAP 9.1
↑ Summary
1 4 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package MooX::XSConstructor;
6  
7 4 our $AUTHORITY = 'cpan:TOBYINK';
8 4 our $VERSION = '0.003003';
9  
10 4 ▶use Class::Method::Modifiers 'install_modifier';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use Moo 2.004000 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use Moo::Object ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use Hook::AfterRuntime;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 # Options that either XSCON can handle, or have no effect on the
16 # constructor at all.
17 4 my $safe_spec = qr/\A(
18 index |
19 is | reader | writer | accessor | predicate | clearer |
20 handles | handles_via |
21 init_arg | required |
22 isa | coerce |
23 builder | default | lazy |
24 trigger |
25 weak_ref |
26 documentation
27 )\z/x;
28  
29 # Options that have no effect on the constructor at all, but need
30 # to be deleted so they don't confuse XSCON.
31 4 my $delete_from_spec = qr/\A(
32 index |
33 is | reader | writer | accessor | predicate | clearer |
34 handles | handles_via |
35 documentation
36 )\z/x;
37  
38 12 ▶sub is_suitable_class {
Subroutine is_suitable_class: called CC=8 SCAR=21.9
Pod documented
39 12 my ( $self, $klass ) = @_;
40
41 12 ▶ return unless Moo->is_class( $klass );
Branch: 1 branch
BranchTrueFalseTotal
unless "Moo"->is_class($klass) 1 11 12
42
43 11 my $constructor_maker = Moo->_constructor_maker_for( $klass );
44 11 do {
45 # If the class's constructor generator is Method::Generate::Constructor,
46 # then we can predict its behaviour. If it's something unknown, we
47 # don't know what it might do internally, so cannot safely replace
48 # it with XSCON.
49 11 my @allowed_constructor_makers = (
50 'Method::Generate::Constructor',
51 );
52
53 # This is one trait that might be applied to the constructor maker
54 # which we *know* we are able to handle.
55 11 ▶ if ( $INC{'MooX/StrictConstructor/Role/Constructor/Late.pm'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($INC{"MooX/StrictConstructor/Role/Constructor/Late.pm"}) 0 11 11
56 0 require Role::Tiny;
57 0 require Method::Generate::Constructor;
58 0 my $trait = 'MooX::StrictConstructor::Role::Constructor::Late';
59 push @allowed_constructor_makers, map {
60 0 Role::Tiny->create_class_with_roles( $_, $trait );
61 } @allowed_constructor_makers;
62 }
63  
64 11 my $maker_class = ref $constructor_maker;
65 11 ▶ return unless grep { $maker_class eq $_ } @allowed_constructor_makers;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$maker_class eq $_;} @allowed_constructor_makers 0 11 11
66 };
67
68 # Preliminary sanity check for accessor maker, because it is
69 # used by the constructor genmerator to generate attribute set
70 # statements. Just check it's theoretically sound here, but we
71 # still need to check it for each attribute.
72 11 my $accessor_maker = Moo->_accessor_maker_for( $klass );
73 11 do {
74 11 my $generated = $accessor_maker->_generate_core_set( q{$XXX}, 'yyy', {}, q{"zzz"} );
75 11 my $expected = q{$XXX->{"yyy"} = "zzz"};
76 11 ▶ return if $generated ne $expected;
Branch: 1 branch
BranchTrueFalseTotal
if $generated ne $expected 0 11 11
77 };
78
79 # Loop through attributes...
80 11 my %spec = %{ $constructor_maker->all_attribute_specs };
81 11 my @attributes = sort { $spec{$a}{index} <=> $spec{$b}{index} } keys %spec;
82 11 for my $attr (@attributes) {
83 # Check they don't have any options we don't understand.
84 15 ▶ if (my @unsafe = grep { $_ !~ $safe_spec } keys %{ $spec{$attr} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@unsafe) = grep({not $_ =~ /$safe_spec/x;} keys %{$spec{$attr};})) 0 15 15
85 0 return;
86 }
87
88 # Check that the accessor maker generates a predictable
89 # attribute set statement for this attribute.
90 15 require B;
91 15 my $generated = $accessor_maker->_generate_core_set( q{$XXX}, $attr, $spec{$attr}, q{"zzz"} );
92 15 my $expected = sprintf q{$XXX->{%s} = "zzz"}, B::perlstring($attr);
93 15 ▶ return if $generated ne $expected;
Branch: 1 branch
BranchTrueFalseTotal
if $generated ne $expected 0 15 15
94 }
95
96 # If we got this far, we're good.
97 11 return "yay!";
98 }
99  
100 # Called by the import method.
101 7 ▶sub setup_for {
Subroutine setup_for: called CC=16 SCAR=28.3
Pod documented
102 7 my ( $self, $klass ) = @_;
103
104 7 ▶ return unless $self->is_suitable_class( $klass );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_suitable_class($klass) 0 7 7
105
106 # Get attribute specs
107 7 my $maker = Moo->_constructor_maker_for( $klass );
108 7 my %spec = %{ $maker->all_attribute_specs };
109
110 # Transform it into arguments which XSCON can handle
111 my @args =
112 map {
113 10 my $slot = $_;
114 10 my %xs_spec = %{ $spec{$slot} };
115 10 for my $k ( keys %xs_spec ) {
116 37 ▶ delete $xs_spec{$k} if $k =~ $delete_from_spec;
Branch: 1 branch
BranchTrueFalseTotal
if $k =~ /$delete_from_spec/x 30 7 37
117 }
118 10 ▶ if ( $xs_spec{lazy} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xs_spec{"lazy"}) 0 10 10
119 0 delete $xs_spec{$_} for qw/ lazy builder default /;
120 }
121 10 ( $slot => \%xs_spec );
122 }
123 7 sort { $spec{$a}{index} <=> $spec{$b}{index} }
124 keys %spec;
125
126 7 ▶ $maker->DOES( 'MooX::StrictConstructor::Role::Constructor::Late' )
Branch: 1 branch
BranchTrueFalseTotal
$maker->DOES("MooX::StrictConstructor::Role::Constructor::Late") and push @args, "!!" 0 7 7
127 and push @args, '!!';
128  
129 # Keep track of old constructor, just in case.
130 7 my $old = $klass->can( 'new' );
131
132 # Call XSCON to replace the existing constructor method
133 7 my $ok = eval {
134 7 require Class::XSConstructor;
135 7 Class::XSConstructor->VERSION( 0.018001 );
136 7 local $Class::XSConstructor::REDEFINE = !!1;
137 7 Class::XSConstructor->import( [ $klass, 'new' ], @args );
138 7 1;
139 };
140
141
142 7 ▶ if ( $ok and my $meta = Class::XSConstructor::get_metadata( $klass ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok and my $meta = Class::XSConstructor::get_metadata($klass)) { } 7 0 7
Condition33%1/3
$ok and my $meta = Class::XSConstructor::get_metadata($klass)
!ll&&!rl&&r
007
MC/DC0%0/2
$ok and my $meta = Class::XSConstructor::get_metadata($klass)
$ok my $meta = Class::XSConstructor::get_metadata($klass)
Truth table33%1/3
A and B
A= $okB= my $meta = Class::XSConstructor::get_metadata($klass)
ABresult
0X0
100
111
143 7 ▶ if ( defined $meta->{foreignclass}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $meta->{"foreignclass"} and $meta->{"foreignclass"} eq "Moo::Object" || $self->is_suitable_class($meta->{"foreignclass"})) 5 2 7
Condition100%6/6
defined $meta->{"foreignclass"} and $meta->{"foreignclass"} eq "Moo::Object" || $self->is_suitable_class($meta->{"foreignclass"})
!ll&&!rl&&r
115
$meta->{"foreignclass"} eq "Moo::Object" or $self->is_suitable_class($meta->{"foreignclass"})
l!l&&r!l&&!r
141
MC/DC100%3/3
defined $meta->{"foreignclass"} and $meta->{"foreignclass"} eq "Moo::Object" || $self->is_suitable_class($meta->{"foreignclass"})
defined $meta->{"foreignclass"} $meta->{"foreignclass"} eq "Moo::Object" $self->is_suitable_class($meta->{"foreignclass"})
Truth table100%4/4
A and B or C
A= defined $meta->{"foreignclass"}B= $meta->{"foreignclass"} eq "Moo::Object"C= $self->is_suitable_class($meta->{"foreignclass"})
ABCresult
0XX0
1000
11X1
1011
144 and ( $meta->{foreignclass} eq 'Moo::Object' or $self->is_suitable_class( $meta->{foreignclass} ) ) ) {
145 5 delete $meta->{$_} for qw/
146 foreignbuildall
147 foreignconstructor
148 foreignclass
149 foreignbuildargs
150 /;
151 }
152 7 ▶ if ( $meta->{buildargs} and $meta->{buildargs} == \&Moo::Object::BUILDARGS ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($meta->{"buildargs"} and $meta->{"buildargs"} == \&Moo::Object::BUILDARGS) 6 1 7
Condition66%2/3
$meta->{"buildargs"} and $meta->{"buildargs"} == \&Moo::Object::BUILDARGS
!ll&&!rl&&r
106
MC/DC50%1/2
$meta->{"buildargs"} and $meta->{"buildargs"} == \&Moo::Object::BUILDARGS
$meta->{"buildargs"} $meta->{"buildargs"} == \&Moo::Object::BUILDARGS
Truth table66%2/3
A and B
A= $meta->{"buildargs"}B= $meta->{"buildargs"} == \&Moo::Object::BUILDARGS
ABresult
0X0
100
111
153 6 $meta->{has_standard_buildargs} = !!1;
154 6 delete $meta->{buildargs};
155 }
156 7 $klass->XSCON_CLEAR_CONSTRUCTOR_CACHE;
157 }
158 else {
159 # If there was a failure, restore old constructor.
160 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
161 4 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
162 0 *{"${klass}::new"} = $old;
163 0 return;
164 }
165
166 7 ▶ if ( $klass->can('DEMOLISH') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($klass->can("DEMOLISH")) 1 6 7
167 1 require Class::XSDestructor;
168 1 local $Class::XSDestructor::REDEFINE = !!1;
169 1 Class::XSDestructor->import( [ $klass, 'DESTROY' ] );
170 }
171
172 7 return $klass;
173 }
174  
175 7 ▶sub import {
Subroutine import: called CC=3 SCAR=11.0
176 7 my $self = shift;
177 7 my $caller = caller;
178 7 ▶ if ( @_ and $_[0] eq '-wrapconstructor' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ and $_[0] eq '-wrapconstructor') { } 1 6 7
Condition66%2/3
@_ and $_[0] eq "-wrapconstructor"
!ll&&!rl&&r
601
MC/DC50%1/2
@_ and $_[0] eq "-wrapconstructor"
@_ $_[0] eq "-wrapconstructor"
Truth table66%2/3
A and B
A= @_B= $_[0] eq "-wrapconstructor"
ABresult
0X0
100
111
179 1 ▶ after_runtime {
Subroutine __ANON__: called CC=1 SCAR=0.0
180 1 ▶ install_modifier $caller => around => new => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
181 1 my $pp_constructor = shift;
182 1 my $object = $pp_constructor->( @_ );
183 1 $self->setup_for( $caller );
184 1 return $object;
185 1 };
186 1 };
187 }
188 else {
189 6 ▶ after_runtime {
Subroutine __ANON__: called CC=1 SCAR=0.0
190 6 $self->setup_for( $caller )
191 6 };
192 }
193 }
194  
195 4 ▶sub is_xs {
Subroutine is_xs: called CC=1 SCAR=0.0
Pod documented
196 4 require B;
197 4 !! B::svref_2object( shift )->XSUB;
198 }
199  
200 4 __PACKAGE__
201 __END__
↑ Summary