/usr/local/lib/perl5/site_perl/5.44.0/Method/Generate/Constructor.pm

filter: Statementstmt 85.9% 116 / 135 Branchbran 63.0% 29 / 46 Conditioncond 43.9% 18 / 41 MC/DCmcdc 18.1% 4 / 22 Subroutinesub 96.3% 26 / 27 Podpod 0.0% 0 / 9 totaltotal 68.9% 193 / 280 CC 40 SCAR 20.8
CC
40
Cov
73%
__ANON__
25.6
register_attribute_specs
14.4
generate_method
10.1
CRAP 8.0
« /usr/local/lib/perl5/site_perl/5.44.0/Module/Implementation.pm ↑ Summary lib/Skilletor/Targets.pm »
1 package Method::Generate::Constructor;
2 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 7 ▶use Sub::Quote qw(quote_sub quotify);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use Sub::Defer;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use Moo::_Utils qw(_getstash _getglob _linear_isa);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use Carp::Heavy ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 7 ▶BEGIN { our @CARP_NOT = qw(Sub::Defer) }
Subroutine BEGIN: called CC=1 SCAR=0.0
12 7 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
13 7 local $Moo::sification::disabled = 1;
14 7 require Moo;
15 7 Moo->import;
16 }
17  
18 281 ▶sub register_attribute_specs {
Subroutine register_attribute_specs: called CC=10 SCAR=26.7
Pod undocumented
19 281 my ($self, @new_specs) = @_;
20 281 $self->assert_constructor;
21 281 ▶ my $specs = $self->{attribute_specs}||={};
Condition100%2/2
$self->{"attribute_specs"} ||= {}
l!l
23051
MC/DC100%1/1
$self->{"attribute_specs"} ||= {}
$self->{"attribute_specs"}
Truth table100%2/2
A
A= $self->{"attribute_specs"}
Aresult
11
00
22 281 my $ag = $self->accessor_generator;
23 281 ▶ while (my ($name, $new_spec) = splice @new_specs, 0, 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@new_specs, 0, 2)) 320 281 601
24 320 ▶ if ($name =~ s/^\+//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ s/^\+//) 0 320 320
25 0 ▶ 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") 0 0 0
26 or croak "has '+${name}' given but no ${name} attribute already exists";
27 0 $ag->merge_specs($new_spec, $old_spec);
28 }
29 320 ▶ 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"}) 0 320 320
Condition44%4/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
266540
$ag->has_default($name, $new_spec) || !exists $new_spec->{'init_arg'} || defined $new_spec->{'init_arg'}
l!l&&r!l&&!r
5400
$ag->has_default($name, $new_spec) or not exists $new_spec->{"init_arg"}
l!l&&r!l&&!r
0540
MC/DC0%0/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 table40%2/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 0 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 320 ▶ unless defined $new_spec->{index};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $new_spec->{"index"} 265 55 320
41 320 $specs->{$name} = $new_spec;
42 }
43 281 $self;
44 }
45  
46 5 ▶sub all_attribute_specs {
Subroutine all_attribute_specs: called CC=1 SCAR=0.0
Pod undocumented
47 $_[0]->{attribute_specs}
48 5 }
49  
50 325 ▶sub accessor_generator {
Subroutine accessor_generator: called CC=1 SCAR=0.0
Pod undocumented
51 $_[0]->{accessor_generator}
52 325 }
53  
54 26 ▶sub construction_string {
Subroutine construction_string: called CC=2 SCAR=7.1
Pod undocumented
55 26 my ($self) = @_;
56 $self->{construction_string}
57 26 ▶ ||= $self->_build_construction_string;
Condition66%2/3
$self->{"construction_string"} ||= $self->_build_construction_string
l!l&&r!l&&!r
3230
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 8 ▶sub buildall_generator {
Subroutine buildall_generator: called CC=1 SCAR=0.0
Pod undocumented
61 8 require Method::Generate::BuildAll;
62 8 Method::Generate::BuildAll->new;
63 }
64  
65 23 ▶sub _build_construction_string {
Subroutine _build_construction_string: called CC=2 SCAR=7.1
66 23 my ($self) = @_;
67 23 my $builder = $self->{construction_builder};
68 23 ▶ $builder ? $self->$builder
Branch: 1 branch
BranchTrueFalseTotal
$builder ? : 0 23 23
69 : 'bless('
70 .$self->accessor_generator->default_construction_string
71 .', $class);'
72 }
73  
74 51 ▶sub install_delayed {
Subroutine install_delayed: called CC=1 SCAR=1.4
Pod undocumented
75 51 my ($self) = @_;
76 51 $self->assert_constructor;
77 51 my $package = $self->{package};
78 51 my (undef, @isa) = @{_linear_isa($package)};
79 51 my $isa = join ',', @isa;
80 51 my (undef, $from_file, $from_line) = caller(Carp::short_error_loc());
81 21 ▶ my $constructor = defer_sub "${package}::new" => sub {
Subroutine __ANON__: called CC=7 SCAR=32.4
82 21 my (undef, @new_isa) = @{_linear_isa($package)};
83 21 ▶ if (join(',', @new_isa) ne $isa) {
Branch: 1 branch
BranchTrueFalseTotal
if (join(",", @new_isa) ne $isa) 0 21 21
84 0 my ($expected_new) = grep { *{_getglob($_.'::new')}{CODE} } @isa;
85 0 my ($found_new) = grep { *{_getglob($_.'::new')}{CODE} } @new_isa;
86 0 ▶ if (($found_new||'') ne ($expected_new||'')) {
Branch: 1 branch
BranchTrueFalseTotal
if (($found_new || "") ne ($expected_new || "")) 0 0 0
Condition0%0/4
$found_new || ''
l!l
00
$expected_new || ''
l!l
00
MC/DC0%0/1
$found_new || ''
$found_new
MC/DC0%0/1
$expected_new || ''
$expected_new
Truth table0%0/2
A
A= $found_new
Aresult
11
00
Truth table0%0/2
A
A= $expected_new
Aresult
11
00
87 0 ▶ $found_new ||= 'none';
Condition0%0/2
$found_new ||= "none"
l!l
00
MC/DC0%0/1
$found_new ||= "none"
$found_new
Truth table0%0/2
A
A= $found_new
Aresult
11
00
88 0 ▶ $expected_new ||= 'none';
Condition0%0/2
$expected_new ||= "none"
l!l
00
MC/DC0%0/1
$expected_new ||= "none"
$expected_new
Truth table0%0/2
A
A= $expected_new
Aresult
11
00
89 0 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 21 $package, 'new', $self->{attribute_specs}, { no_install => 1, no_defer => 1 }
97 );
98 21 $self->{inlined} = 1;
99 21 weaken($self->{constructor} = $constructor);
100 21 $constructor;
101 51 };
102 51 $self->{inlined} = 0;
103 51 weaken($self->{constructor} = $constructor);
104 51 $self;
105 }
106  
107 332 ▶sub current_constructor {
Subroutine current_constructor: called CC=1 SCAR=0.0
Pod undocumented
108 332 my ($self, $package) = @_;
109 332 return *{_getglob("${package}::new")}{CODE};
110 }
111  
112 332 ▶sub assert_constructor {
Subroutine assert_constructor: called CC=6 SCAR=18.8
Pod undocumented
113 332 my ($self) = @_;
114 332 ▶ my $package = $self->{package} or return 1;
Branch: 1 branch
BranchTrueFalseTotal
my $package = $self->{"package"} or return 1 332 0 332
115 332 ▶ my $current = $self->current_constructor($package)
Branch: 1 branch
BranchTrueFalseTotal
my $current = $self->current_constructor($package) or return 1 281 51 332
116 or return 1;
117 my $constructor = $self->{constructor}
118 281 ▶ or croak "Unknown constructor for $package already exists";
Branch: 1 branch
BranchTrueFalseTotal
my $constructor = $self->{"constructor"} or croak("Unknown constructor for $package already exists") 281 0 281
119 281 ▶ croak "Constructor for $package has been replaced with an unknown sub"
Branch: 1 branch
BranchTrueFalseTotal
if $constructor != $current 0 281 281
120 if $constructor != $current;
121 croak "Constructor for $package has been inlined and cannot be updated"
122 281 ▶ if $self->{inlined};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"inlined"} 0 281 281
123 }
124  
125 21 ▶sub generate_method {
Subroutine generate_method: called CC=8 SCAR=23.1
Pod undocumented
126 21 my ($self, $into, $name, $spec, $quote_opts) = @_;
127 $quote_opts = {
128 21 ▶ %{$quote_opts||{}},
Condition50%1/2
$quote_opts || {}
l!l
210
MC/DC0%0/1
$quote_opts || {}
$quote_opts
Truth table50%1/2
A
A= $quote_opts
Aresult
11
00
129 package => $into,
130 };
131 21 foreach my $no_init (grep !exists($spec->{$_}{init_arg}), keys %$spec) {
132 126 $spec->{$no_init}{init_arg} = $no_init;
133 }
134 21 local $self->{captures} = {};
135  
136 21 my $into_buildargs = $into->can('BUILDARGS');
137  
138 21 ▶ my $body
Branch: 2 branches
BranchTrueFalseTotal
$into_buildargs && $into_buildargs != \&Moo::Object::BUILDARGS ? : 0 21 21
$into->can('BUILD') ? : 8 13 21
Condition33%1/3
$into_buildargs and $into_buildargs != \&Moo::Object::BUILDARGS
!ll&&!rl&&r
0210
MC/DC0%0/2
$into_buildargs and $into_buildargs != \&Moo::Object::BUILDARGS
$into_buildargs $into_buildargs != \&Moo::Object::BUILDARGS
Truth table33%1/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 21 ▶ if ($into->can('DEMOLISH')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($into->can("DEMOLISH")) 0 21 21
156 0 require Method::Generate::DemolishAll;
157 0 Method::Generate::DemolishAll->new->generate_method($into);
158 }
159 quote_sub
160 "${into}::${name}" => $body,
161 21 ▶ $self->{captures}, $quote_opts||{}
Condition50%1/2
$quote_opts || {}
l!l
210
MC/DC0%0/1
$quote_opts || {}
$quote_opts
Truth table50%1/2
A
A= $quote_opts
Aresult
11
00
162 ;
163 }
164  
165 21 ▶sub _handle_subconstructor {
Subroutine _handle_subconstructor: called CC=2 SCAR=7.6
166 21 my ($self, $into, $name) = @_;
167 21 ▶ if (my $gen = $self->{subconstructor_handler}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $gen = $self->{'subconstructor_handler'}) { } 21 0 21
168 21 ' if ($class ne '.quotify($into).') {'."\n".
169 $gen.
170 ' }'."\n";
171 } else {
172 0 ''
173 }
174 }
175  
176 130 ▶sub _cap_call {
Subroutine _cap_call: called CC=2 SCAR=7.0
177 130 my ($self, $code, $captures) = @_;
178 130 ▶ @{$self->{captures}}{keys %$captures} = values %$captures if $captures;
Branch: 1 branch
BranchTrueFalseTotal
if $captures 130 0 130
179 130 $code;
180 }
181  
182 0 ▶sub _generate_args_via_buildargs {
Subroutine _generate_args_via_buildargs: not called CC=1 SCAR=6.9
183 0 my ($self) = @_;
184 0 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 21 ▶sub _generate_args {
Subroutine _generate_args: called CC=1 SCAR=0.0
191 21 my ($self) = @_;
192 21 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 21 ▶sub _assign_new {
Subroutine _assign_new: called CC=6 SCAR=17.9
208 21 my ($self, $spec) = @_;
209 21 my $ag = $self->accessor_generator;
210 21 my %test;
211 21 NAME: foreach my $name (sort keys %$spec) {
212 149 my $attr_spec = $spec->{$name};
213 next NAME unless defined($attr_spec->{init_arg})
214 149 ▶ 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) 19 130 149
Condition100%3/3
defined $attr_spec->{"init_arg"} or $ag->has_eager_default($name, $attr_spec)
l!l&&r!l&&!r
126419
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 130 $test{$name} = $attr_spec->{init_arg};
216 }
217 join '', map {
218 21 my $arg = $test{$_};
219 130 my $arg_key = quotify($arg);
220 130 ▶ my $test = defined $arg ? "exists \$args->{$arg_key}" : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $arg ? : 126 4 130
221 130 ▶ my $source = defined $arg ? "\$args->{$arg_key}" : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $arg ? : 126 4 130
222 130 my $attr_spec = $spec->{$_};
223 130 $self->_cap_call($ag->generate_populate_set(
224 '$new', $_, $attr_spec, $source, $test, $arg,
225 ));
226 } sort keys %test;
227 }
228  
229 21 ▶sub _check_required {
Subroutine _check_required: called CC=4 SCAR=14.2
230 21 my ($self, $spec) = @_;
231 my @required_init =
232 map $spec->{$_}{init_arg},
233 grep {
234 21 my $s = $spec->{$_}; # ignore required if default or builder set
235 $s->{required} and not($s->{builder} or exists $s->{default})
236 149 ▶ } sort keys %$spec;
Condition50%3/6
$s->{'required'} && !($s->{'builder'} || exists $s->{'default'})
!ll&&!rl&&r
135014
$s->{'builder'} || exists $s->{'default'}
l!l&&r!l&&!r
0014
MC/DC33%1/3
$s->{'required'} && !($s->{'builder'} || exists $s->{'default'})
$s->{'required'} $s->{'builder'} exists $s->{'default'}
Truth table50%2/4
A && not(B || C)
A= $s->{'required'}B= $s->{'builder'}C= exists $s->{'default'}
ABCresult
0XX0
11X0
1010
1001
237 21 ▶ return '' unless @required_init;
Branch: 1 branch
BranchTrueFalseTotal
unless @required_init 7 14 21
238 14 ' 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 7 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 7 ▶if ($INC{'Moo/HandleMoose.pm'} && !$Moo::sification::disabled) {
Branch: 1 branch
BranchTrueFalseTotal
if ($INC{"Moo/HandleMoose.pm"} and not $Moo::sification::disabled) 0 7 7
Condition33%1/3
$INC{"Moo/HandleMoose.pm"} and not $Moo::sification::disabled
!ll&&!rl&&r
700
MC/DC0%0/2
$INC{"Moo/HandleMoose.pm"} and not $Moo::sification::disabled
$INC{"Moo/HandleMoose.pm"} not $Moo::sification::disabled
Truth table33%1/3
A and not(B)
A= $INC{"Moo/HandleMoose.pm"}B= not $Moo::sification::disabled
ABresult
0X0
100
111
263 0 Moo::HandleMoose::inject_fake_metaclass_for(__PACKAGE__);
264 }
265  
266 7 1;
« /usr/local/lib/perl5/site_perl/5.44.0/Module/Implementation.pm ↑ Summary lib/Skilletor/Targets.pm »