blib/lib/Moose/Meta/Method/Destructor.pm

filter: Statementstmt 100% 54 / 54 Branchbran 90.0% 9 / 10 Conditioncond 66.6% 4 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 15 / 15 Podpod 66.6% 2 / 3 totaltotal 93.4% 86 / 92 CC 8 SCAR 6.9
CC
8
Cov
96%
new
4.0
is_needed
3.0
_initialize_body
2.0
CRAP 2.0
« blib/lib/Moose/Util/TypeConstraints/Builtins.pm ↑ Summary blib/lib/Class/MOP/Method/Accessor.pm »
1 package Moose::Meta::Method::Destructor;
2 388 our $VERSION = '2.4001';
3  
4 388 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 388 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 388 ▶use Devel::GlobalDestruction ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 388 ▶use Scalar::Util 'blessed', 'weaken';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 388 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 388 ▶use parent 'Moose::Meta::Method',
Subroutine BEGIN: called CC=1 SCAR=0.0
12 388 'Class::MOP::Method::Inlined';
13  
14 388 ▶use Moose::Util 'throw_exception';
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 759 ▶sub new {
Subroutine new: called CC=4 SCAR=13.9
Pod documented
17 759 my $class = shift;
18 759 my %options = @_;
19  
20 759 ▶ (ref $options{options} eq 'HASH')
Branch: 1 branch
BranchTrueFalseTotal
ref $options{"options"} eq "HASH" or throw_exception("MustPassAHashOfOptions", "params", \%options, "class", $class) 758 1 759
21 || throw_exception( MustPassAHashOfOptions => params => \%options,
22 class => $class
23 );
24  
25 ($options{package_name} && $options{name})
26 758 ▶ || throw_exception( MustSupplyPackageNameAndName => params => \%options,
Branch: 1 branch
BranchTrueFalseTotal
$options{"package_name"} and $options{"name"} or throw_exception("MustSupplyPackageNameAndName", "params", \%options, "class", $class) 757 1 758
Condition66%2/3
$options{"package_name"} and $options{"name"}
!ll&&!rl&&r
10757
MC/DC50%1/2
$options{"package_name"} and $options{"name"}
$options{"package_name"} $options{"name"}
Truth table66%2/3
A and B
A= $options{"package_name"}B= $options{"name"}
ABresult
0X0
100
111
27 class => $class
28 );
29  
30 my $self = bless {
31 # from our superclass
32 'body' => undef,
33 'package_name' => $options{package_name},
34 'name' => $options{name},
35 # ...
36 'options' => $options{options},
37 'definition_context' => $options{definition_context},
38 'associated_metaclass' => $options{metaclass},
39 757 } => $class;
40  
41 # we don't want this creating
42 # a cycle in the code, if not
43 # needed
44 757 weaken($self->{'associated_metaclass'});
45  
46 757 $self->_initialize_body;
47  
48 756 return $self;
49 }
50  
51 ## accessors
52  
53 757 ▶sub options { (shift)->{'options'} }
Subroutine options: called CC=1 SCAR=0.0
Pod undocumented
54  
55 ## method
56  
57 775 ▶sub is_needed {
Subroutine is_needed: called CC=3 SCAR=11.0
Pod documented
58 775 my $self = shift;
59 775 my $metaclass = shift;
60  
61 775 ▶ ( blessed $metaclass && $metaclass->isa('Class::MOP::Class') )
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($metaclass) and $metaclass->isa("Class::MOP::Class") or throw_exception("MethodExpectedAMetaclassObject", "metaclass", $metaclass, "class", $self) 774 1 775
Condition66%2/3
builtin::blessed($metaclass) and $metaclass->isa("Class::MOP::Class")
!ll&&!rl&&r
10774
MC/DC50%1/2
builtin::blessed($metaclass) and $metaclass->isa("Class::MOP::Class")
builtin::blessed($metaclass) $metaclass->isa("Class::MOP::Class")
Truth table66%2/3
A and B
A= builtin::blessed($metaclass)B= $metaclass->isa("Class::MOP::Class")
ABresult
0X0
100
111
62 || throw_exception( MethodExpectedAMetaclassObject => metaclass => $metaclass,
63 class => $self
64 );
65  
66 774 return $metaclass->find_method_by_name("DEMOLISHALL");
67 }
68  
69 757 ▶sub _initialize_body {
Subroutine _initialize_body: called CC=2 SCAR=6.9
70 757 my $self = shift;
71 # TODO:
72 # the %options should also include a both
73 # a call 'initializer' and call 'SUPER::'
74 # options, which should cover approx 90%
75 # of the possible use cases (even if it
76 # requires some adaption on the part of
77 # the author, after all, nothing is free)
78  
79 757 my $class = $self->associated_metaclass->name;
80 757 my @source = (
81 'sub {',
82 'my $self = shift;',
83 'return ' . $self->_generate_fallback_destructor('$self'),
84 'if Scalar::Util::blessed($self) ne \'' . $class . '\';',
85 $self->_generate_DEMOLISHALL('$self'),
86 'return;',
87 '}',
88 );
89 757 ▶ warn join("\n", @source) if $self->options->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->options->{"debug"} 0 757 757
90  
91 757 ▶ my $code = try {
Subroutine __ANON__: called CC=1 SCAR=0.0
92 757 $self->_compile_code(source => \@source);
93 }
94 1 ▶ catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
95 1 my $source = join("\n", @source);
96 1 throw_exception( CouldNotEvalDestructor => method_destructor_object => $self,
97 source => $source,
98 error => $_
99 );
100 757 };
101  
102 756 $self->{'body'} = $code;
103 }
104  
105 757 ▶sub _generate_fallback_destructor {
Subroutine _generate_fallback_destructor: called CC=1 SCAR=0.0
106 757 my $self = shift;
107 757 my ($inv) = @_;
108  
109 757 return $inv . '->Moose::Object::DESTROY(@_)';
110 }
111  
112 756 ▶sub _generate_DEMOLISHALL {
Subroutine _generate_DEMOLISHALL: called CC=2 SCAR=6.9
113 756 my $self = shift;
114 756 my ($inv) = @_;
115  
116 756 my @methods = $self->associated_metaclass->find_all_methods_by_name('DEMOLISH');
117 756 ▶ return unless @methods;
Branch: 1 branch
BranchTrueFalseTotal
unless @methods 746 10 756
118  
119 return (
120 'local $?;',
121 'my $igd = Devel::GlobalDestruction::in_global_destruction;',
122 'Try::Tiny::try {',
123 10 (map { $inv . '->' . $_->{class} . '::DEMOLISH($igd);' } @methods),
124 '}',
125 'Try::Tiny::catch {',
126 'die $_;',
127 '};',
128 );
129 }
130  
131  
132 388 1;
133  
134 # ABSTRACT: Method Meta Object for destructors
135  
136 __END__
« blib/lib/Moose/Util/TypeConstraints/Builtins.pm ↑ Summary blib/lib/Class/MOP/Method/Accessor.pm »