blib/lib/Method/Generate/Constructor.pm

filter: Statementstmt 100% 135 / 135 Branchbran 100% 46 / 46 Conditioncond 82.9% 34 / 41 MC/DCmcdc 63.6% 14 / 22 Subroutinesub 100% 27 / 27 Podpod 0.0% 0 / 9 totaltotal 91.4% 256 / 280 CC 40 SCAR 16.2
CC
40
Cov
97%
register_attribute_specs
10.0
generate_method
8.0
__ANON__
7.1
CRAP 5.1
« blib/lib/Moo/_Utils.pm ↑ Summary blib/lib/Moo.pm »
1 package Method::Generate::Constructor;
2 85 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 85 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 85 ▶use Sub::Quote qw(quote_sub quotify);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 85 ▶use Sub::Defer;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 85 ▶use Moo::_Utils qw(_getstash _getglob _linear_isa);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 85 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 85 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 85 ▶use Carp::Heavy ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 85 ▶BEGIN { our @CARP_NOT = qw(Sub::Defer) }
Subroutine BEGIN: called CC=1 SCAR=0.0
12 85 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
13 85 local $Moo::sification::disabled = 1;
14 85 require Moo;
15 85 Moo->import;
16 }
17  
18 731 ▶sub register_attribute_specs {
Subroutine register_attribute_specs: called CC=10 SCAR=23.0
Pod undocumented
19 731 my ($self, @new_specs) = @_;
20 731 $self->assert_constructor;
21 729 ▶ my $specs = $self->{attribute_specs}||={};
Condition100%2/2
$self->{"attribute_specs"} ||= {}
l!l
401328
MC/DC100%1/1
$self->{"attribute_specs"} ||= {}
$self->{"attribute_specs"}
Truth table100%2/2
A
A= $self->{"attribute_specs"}
Aresult
11
00
22 729 my $ag = $self->accessor_generator;
23 729 ▶ while (my ($name, $new_spec) = splice @new_specs, 0, 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@new_specs, 0, 2)) 896 726 1622
24 896 ▶ if ($name =~ s/^\+//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ s/^\+//) 15 881 896
25 15 ▶ my $old_spec = $specs->{$name}
Branch: 1 branch
BranchTrueFalseTotal
my $old_spec = $specs->{$name} or croak("has '+${name}' given but no $name attribute already exists") 13 2 15
26 or croak "has '+${name}' given but no ${name} attribute already exists";
27 13 $ag->merge_specs($new_spec, $old_spec);
28 }
29 894 ▶ if ($new_spec->{required}
Branch: 1 branch
BranchTrueFalseTotal
if ($new_spec->{"required"} and not $ag->has_default($name, $new_spec) || !exists $new_spec->{"init_arg"} || defined $new_spec->{"init_arg"}) 1 893 894
Condition100%9/9
$new_spec->{"required"} and not $ag->has_default($name, $new_spec) || !exists $new_spec->{"init_arg"} || defined $new_spec->{"init_arg"}
!ll&&!rl&&r
866271
$ag->has_default($name, $new_spec) || !exists $new_spec->{'init_arg'} || defined $new_spec->{'init_arg'}
l!l&&r!l&&!r
2161
$ag->has_default($name, $new_spec) or not exists $new_spec->{"init_arg"}
l!l&&r!l&&!r
5167
MC/DC100%4/4
$new_spec->{"required"} and not $ag->has_default($name, $new_spec) || !exists $new_spec->{"init_arg"} || defined $new_spec->{"init_arg"}
$new_spec->{"required"} $ag->has_default($name, $new_spec) not exists $new_spec->{"init_arg"} defined $new_spec->{'init_arg'}
Truth table100%5/5
A and not(B or not(C) || D)
A= $new_spec->{"required"}B= $ag->has_default($name, $new_spec)C= not exists $new_spec->{"init_arg"}D= defined $new_spec->{'init_arg'}
ABCDresult
0XXX0
11XX0
101X0
10010
10001
30 && !(
31 $ag->has_default($name, $new_spec)
32 || !exists $new_spec->{init_arg}
33 || defined $new_spec->{init_arg}
34 )
35 ) {
36 1 croak "You cannot have a required attribute (${name})"
37 . " without a default, builder, or an init_arg";
38 }
39 $new_spec->{index} = scalar keys %$specs
40 893 ▶ unless defined $new_spec->{index};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $new_spec->{"index"} 811 82 893
41 893 $specs->{$name} = $new_spec;
42 }
43 726 $self;
44 }
45  
46 154 ▶sub all_attribute_specs {
Subroutine all_attribute_specs: called CC=1 SCAR=0.0
Pod undocumented
47 $_[0]->{attribute_specs}
48 154 }
49  
50 1197 ▶sub accessor_generator {
Subroutine accessor_generator: called CC=1 SCAR=0.0
Pod undocumented
51 $_[0]->{accessor_generator}
52 1197 }
53  
54 293 ▶sub construction_string {
Subroutine construction_string: called CC=2 SCAR=7.1
Pod undocumented
55 293 my ($self) = @_;
56 $self->{construction_string}
57 293 ▶ ||= $self->_build_construction_string;
Condition66%2/3
$self->{"construction_string"} ||= $self->_build_construction_string
l!l&&r!l&&!r
602330
MC/DC0%0/2
$self->{"construction_string"} ||= $self->_build_construction_string
$self->{"construction_string"} $self->_build_construction_string
Truth table66%2/3
A ||= B
A= $self->{"construction_string"}B= $self->_build_construction_string
ABresult
1X1
011
000
58 }
59  
60 17 ▶sub buildall_generator {
Subroutine buildall_generator: called CC=1 SCAR=0.0
Pod undocumented
61 17 require Method::Generate::BuildAll;
62 17 Method::Generate::BuildAll->new;
63 }
64  
65 233 ▶sub _build_construction_string {
Subroutine _build_construction_string: called CC=2 SCAR=6.9
66 233 my ($self) = @_;
67 233 my $builder = $self->{construction_builder};
68 233 ▶ $builder ? $self->$builder
Branch: 1 branch
BranchTrueFalseTotal
$builder ? : 19 214 233
69 : 'bless('
70 .$self->accessor_generator->default_construction_string
71 .', $class);'
72 }
73  
74 333 ▶sub install_delayed {
Subroutine install_delayed: called CC=1 SCAR=0.0
Pod undocumented
75 333 my ($self) = @_;
76 333 $self->assert_constructor;
77 328 my $package = $self->{package};
78 328 my (undef, @isa) = @{_linear_isa($package)};
79 328 my $isa = join ',', @isa;
80 328 my (undef, $from_file, $from_line) = caller(Carp::short_error_loc());
81 254 ▶ my $constructor = defer_sub "${package}::new" => sub {
Subroutine __ANON__: called CC=7 SCAR=19.6
82 254 my (undef, @new_isa) = @{_linear_isa($package)};
83 254 ▶ if (join(',', @new_isa) ne $isa) {
Branch: 1 branch
BranchTrueFalseTotal
if (join(",", @new_isa) ne $isa) 3 251 254
84 3 my ($expected_new) = grep { *{_getglob($_.'::new')}{CODE} } @isa;
85 3 my ($found_new) = grep { *{_getglob($_.'::new')}{CODE} } @new_isa;
86 3 ▶ if (($found_new||'') ne ($expected_new||'')) {
Branch: 1 branch
BranchTrueFalseTotal
if (($found_new || "") ne ($expected_new || "")) 2 1 3
Condition50%2/4
$found_new || ''
l!l
30
$expected_new || ''
l!l
30
MC/DC0%0/1
$found_new || ''
$found_new
MC/DC0%0/1
$expected_new || ''
$expected_new
Truth table50%1/2
A
A= $found_new
Aresult
11
00
Truth table50%1/2
A
A= $expected_new
Aresult
11
00
87 2 ▶ $found_new ||= 'none';
Condition50%1/2
$found_new ||= "none"
l!l
20
MC/DC0%0/1
$found_new ||= "none"
$found_new
Truth table50%1/2
A
A= $found_new
Aresult
11
00
88 2 ▶ $expected_new ||= 'none';
Condition50%1/2
$expected_new ||= "none"
l!l
20
MC/DC0%0/1
$expected_new ||= "none"
$expected_new
Truth table50%1/2
A
A= $expected_new
Aresult
11
00
89 2 croak "Expected parent constructor of $package to be"
90 . " $expected_new, but found $found_new: changing the inheritance"
91 . " chain (\@ISA) at runtime (after $from_file line $from_line) is unsupported";
92 }
93 }
94  
95 my $constructor = $self->generate_method(
96 252 $package, 'new', $self->{attribute_specs}, { no_install => 1, no_defer => 1 }
97 );
98 252 $self->{inlined} = 1;
99 252 weaken($self->{constructor} = $constructor);
100 252 $constructor;
101 328 };
102 328 $self->{inlined} = 0;
103 328 weaken($self->{constructor} = $constructor);
104 328 $self;
105 }
106  
107 1067 ▶sub current_constructor {
Subroutine current_constructor: called CC=1 SCAR=0.0
Pod undocumented
108 1067 my ($self, $package) = @_;
109 1067 return *{_getglob("${package}::new")}{CODE};
110 }
111  
112 1067 ▶sub assert_constructor {
Subroutine assert_constructor: called CC=6 SCAR=17.9
Pod undocumented
113 1067 my ($self) = @_;
114 1067 ▶ my $package = $self->{package} or return 1;
Branch: 1 branch
BranchTrueFalseTotal
my $package = $self->{"package"} or return 1 1065 2 1067
115 1065 ▶ my $current = $self->current_constructor($package)
Branch: 1 branch
BranchTrueFalseTotal
my $current = $self->current_constructor($package) or return 1 739 326 1065
116 or return 1;
117 my $constructor = $self->{constructor}
118 739 ▶ or croak "Unknown constructor for $package already exists";
Branch: 1 branch
BranchTrueFalseTotal
my $constructor = $self->{"constructor"} or croak("Unknown constructor for $package already exists") 734 5 739
119 734 ▶ croak "Constructor for $package has been replaced with an unknown sub"
Branch: 1 branch
BranchTrueFalseTotal
if $constructor != $current 1 733 734
120 if $constructor != $current;
121 croak "Constructor for $package has been inlined and cannot be updated"
122 733 ▶ if $self->{inlined};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"inlined"} 2 731 733
123 }
124  
125 254 ▶sub generate_method {
Subroutine generate_method: called CC=8 SCAR=20.8
Pod undocumented
126 254 my ($self, $into, $name, $spec, $quote_opts) = @_;
127 $quote_opts = {
128 254 ▶ %{$quote_opts||{}},
Condition100%2/2
$quote_opts || {}
l!l
2522
MC/DC100%1/1
$quote_opts || {}
$quote_opts
Truth table100%2/2
A
A= $quote_opts
Aresult
11
00
129 package => $into,
130 };
131 254 foreach my $no_init (grep !exists($spec->{$_}{init_arg}), keys %$spec) {
132 729 $spec->{$no_init}{init_arg} = $no_init;
133 }
134 254 local $self->{captures} = {};
135  
136 254 my $into_buildargs = $into->can('BUILDARGS');
137  
138 254 ▶ my $body
Branch: 2 branches
BranchTrueFalseTotal
$into_buildargs && $into_buildargs != \&Moo::Object::BUILDARGS ? : 12 242 254
$into->can('BUILD') ? : 17 237 254
Condition100%3/3
$into_buildargs and $into_buildargs != \&Moo::Object::BUILDARGS
!ll&&!rl&&r
1522712
MC/DC100%2/2
$into_buildargs and $into_buildargs != \&Moo::Object::BUILDARGS
$into_buildargs $into_buildargs != \&Moo::Object::BUILDARGS
Truth table100%3/3
A and B
A= $into_buildargsB= $into_buildargs != \&Moo::Object::BUILDARGS
ABresult
0X0
100
111
139 = ' my $invoker = CORE::shift();'."\n"
140 . ' my $class = CORE::ref($invoker) ? CORE::ref($invoker) : $invoker;'."\n"
141 . $self->_handle_subconstructor($into, $name)
142 . ( $into_buildargs && $into_buildargs != \&Moo::Object::BUILDARGS
143 ? $self->_generate_args_via_buildargs
144 : $self->_generate_args
145 )
146 . $self->_check_required($spec)
147 . ' my $new = '.$self->construction_string.";\n"
148 . $self->_assign_new($spec)
149 . ( $into->can('BUILD')
150 ? $self->buildall_generator->buildall_body_for( $into, '$new', '$args' )
151 : ''
152 )
153 . ' return $new;'."\n";
154  
155 254 ▶ if ($into->can('DEMOLISH')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($into->can("DEMOLISH")) 4 250 254
156 4 require Method::Generate::DemolishAll;
157 4 Method::Generate::DemolishAll->new->generate_method($into);
158 }
159 quote_sub
160 "${into}::${name}" => $body,
161 254 ▶ $self->{captures}, $quote_opts||{}
Condition50%1/2
$quote_opts || {}
l!l
2540
MC/DC0%0/1
$quote_opts || {}
$quote_opts
Truth table50%1/2
A
A= $quote_opts
Aresult
11
00
162 ;
163 }
164  
165 254 ▶sub _handle_subconstructor {
Subroutine _handle_subconstructor: called CC=2 SCAR=6.9
166 254 my ($self, $into, $name) = @_;
167 254 ▶ if (my $gen = $self->{subconstructor_handler}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $gen = $self->{'subconstructor_handler'}) { } 252 2 254
168 252 ' if ($class ne '.quotify($into).') {'."\n".
169 $gen.
170 ' }'."\n";
171 } else {
172 2 ''
173 }
174 }
175  
176 780 ▶sub _cap_call {
Subroutine _cap_call: called CC=2 SCAR=6.9
177 780 my ($self, $code, $captures) = @_;
178 780 ▶ @{$self->{captures}}{keys %$captures} = values %$captures if $captures;
Branch: 1 branch
BranchTrueFalseTotal
if $captures 779 1 780
179 780 $code;
180 }
181  
182 12 ▶sub _generate_args_via_buildargs {
Subroutine _generate_args_via_buildargs: called CC=1 SCAR=0.0
183 12 my ($self) = @_;
184 12 q{ my $args = $class->BUILDARGS(@_);}."\n"
185 .q{ Carp::croak("BUILDARGS did not return a hashref") unless CORE::ref($args) eq 'HASH';}
186 ."\n";
187 }
188  
189 # inlined from Moo::Object - update that first.
190 242 ▶sub _generate_args {
Subroutine _generate_args: called CC=1 SCAR=0.0
191 242 my ($self) = @_;
192 242 return <<'_EOA';
193 my $args = scalar @_ == 1
194 ? CORE::ref $_[0] eq 'HASH'
195 ? { %{ $_[0] } }
196 : Carp::croak("Single parameters to new() must be a HASH ref"
197 . " data => ". $_[0])
198 : @_ % 2
199 ? Carp::croak("The new() method for $class expects a hash reference or a"
200 . " key/value list. You passed an odd number of arguments")
201 : {@_}
202 ;
203 _EOA
204  
205 }
206  
207 254 ▶sub _assign_new {
Subroutine _assign_new: called CC=6 SCAR=17.9
208 254 my ($self, $spec) = @_;
209 254 my $ag = $self->accessor_generator;
210 254 my %test;
211 254 NAME: foreach my $name (sort keys %$spec) {
212 785 my $attr_spec = $spec->{$name};
213 next NAME unless defined($attr_spec->{init_arg})
214 785 ▶ or $ag->has_eager_default($name, $attr_spec);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $attr_spec->{"init_arg"} or $ag->has_eager_default($name, $attr_spec) 6 779 785
Condition100%3/3
defined $attr_spec->{"init_arg"} or $ag->has_eager_default($name, $attr_spec)
l!l&&r!l&&!r
77636
MC/DC100%2/2
defined $attr_spec->{"init_arg"} or $ag->has_eager_default($name, $attr_spec)
defined $attr_spec->{"init_arg"} $ag->has_eager_default($name, $attr_spec)
Truth table100%3/3
A or B
A= defined $attr_spec->{"init_arg"}B= $ag->has_eager_default($name, $attr_spec)
ABresult
1X1
011
000
215 779 $test{$name} = $attr_spec->{init_arg};
216 }
217 join '', map {
218 254 my $arg = $test{$_};
219 779 my $arg_key = quotify($arg);
220 779 ▶ my $test = defined $arg ? "exists \$args->{$arg_key}" : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $arg ? : 776 3 779
221 779 ▶ my $source = defined $arg ? "\$args->{$arg_key}" : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $arg ? : 776 3 779
222 779 my $attr_spec = $spec->{$_};
223 779 $self->_cap_call($ag->generate_populate_set(
224 '$new', $_, $attr_spec, $source, $test, $arg,
225 ));
226 } sort keys %test;
227 }
228  
229 254 ▶sub _check_required {
Subroutine _check_required: called CC=4 SCAR=13.9
230 254 my ($self, $spec) = @_;
231 my @required_init =
232 map $spec->{$_}{init_arg},
233 grep {
234 254 my $s = $spec->{$_}; # ignore required if default or builder set
235 $s->{required} and not($s->{builder} or exists $s->{default})
236 785 ▶ } sort keys %$spec;
Condition100%6/6
$s->{'required'} && !($s->{'builder'} || exists $s->{'default'})
!ll&&!rl&&r
758423
$s->{'builder'} || exists $s->{'default'}
l!l&&r!l&&!r
2223
MC/DC100%3/3
$s->{'required'} && !($s->{'builder'} || exists $s->{'default'})
$s->{'required'} $s->{'builder'} exists $s->{'default'}
Truth table100%4/4
A && not(B || C)
A= $s->{'required'}B= $s->{'builder'}C= exists $s->{'default'}
ABCresult
0XX0
11X0
1010
1001
237 254 ▶ return '' unless @required_init;
Branch: 1 branch
BranchTrueFalseTotal
unless @required_init 236 18 254
238 18 ' if (my @missing = grep !exists $args->{$_}, '
239 .join(', ', map quotify($_), @required_init).') {'."\n"
240 .q{ Carp::croak("Missing required arguments: ".CORE::join(', ', sort @missing));}."\n"
241 ." }\n";
242 }
243  
244 # bootstrap our own constructor
245 sub new {
246 my $class = shift;
247 delete _getstash(__PACKAGE__)->{new};
248 bless $class->BUILDARGS(@_), $class;
249 }
250 85 Moo->_constructor_maker_for(__PACKAGE__)
251 ->register_attribute_specs(
252 attribute_specs => {
253 is => 'ro',
254 reader => 'all_attribute_specs',
255 },
256 accessor_generator => { is => 'ro' },
257 construction_string => { is => 'lazy' },
258 construction_builder => { is => 'bare' },
259 subconstructor_handler => { is => 'ro' },
260 package => { is => 'bare' },
261 );
262 85 ▶if ($INC{'Moo/HandleMoose.pm'} && !$Moo::sification::disabled) {
Branch: 1 branch
BranchTrueFalseTotal
if ($INC{"Moo/HandleMoose.pm"} and not $Moo::sification::disabled) 25 60 85
Condition66%2/3
$INC{"Moo/HandleMoose.pm"} and not $Moo::sification::disabled
!ll&&!rl&&r
60025
MC/DC50%1/2
$INC{"Moo/HandleMoose.pm"} and not $Moo::sification::disabled
$INC{"Moo/HandleMoose.pm"} not $Moo::sification::disabled
Truth table66%2/3
A and not(B)
A= $INC{"Moo/HandleMoose.pm"}B= not $Moo::sification::disabled
ABresult
0X0
100
111
263 25 Moo::HandleMoose::inject_fake_metaclass_for(__PACKAGE__);
264 }
265  
266 85 1;
« blib/lib/Moo/_Utils.pm ↑ Summary blib/lib/Moo.pm »