Spoon/Hooks.pm

filter: Statementstmt 50.9% 52 / 102 Branchbran 9.3% 3 / 32 Conditioncond 12.5% 1 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 65.0% 13 / 20 Podpod 0.0% 0 / 4 totaltotal 40.3% 69 / 171 CC 20 SCAR 25.2
CC
20
Cov
39%
__ANON__
30.0
assert_method
24.9
returned
20.0
CRAP 12.4
« Spoon/Registry.pm ↑ Summary Spoon/MetadataObject.pm »
1 package Spoon::Hooks;
2 2 ▶use Spoon::Base -Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶ 
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 const hook_class => 'Spoon::Hook';
5 2 const hooked_class => 'Spoon::Hooked';
6  
7 1 ▶sub add {
Subroutine add: called CC=1 SCAR=0.6
Pod undocumented
8 1 my ($target, %hooks) = @_;
9 1 my $original = $self->assert_method($target);
10 0 my $pre = $self->assert_method($hooks{pre});
11 0 my $post = $self->assert_method($hooks{post});
12 0 my $replacement = $self->new_hook_sub($original, $pre, $post);
13 0 my $hook_point = $self->get_full_name($target);
14 2 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
16 0 *$hook_point = $replacement;
17  
18 0 return $self->hooked_class->new(
19 hook_point => $hook_point,
20 original => $original,
21 replacement => $replacement,
22 );
23 }
24  
25 0 ▶sub new_hook_sub {
Subroutine new_hook_sub: not called CC=1 SCAR=6.9
Pod undocumented
26 0 my ($original, $pre, $post) = @_;
27 0 ▶ sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
28 0 my $hook = $self->hook_class->new(
29 code => $original,
30 pre => $pre,
31 post => $post,
32 );
33 0 ▶ $hook->returned([$hook->pre->(@_, $hook)])
Branch: 1 branch
BranchTrueFalseTotal
if $pre 0 0 0
34 if $pre;
35 0 ▶ my $code = $hook->code
Branch: 1 branch
BranchTrueFalseTotal
my $code = $hook->code or return $hook->returned 0 0 0
36 or return $hook->returned;
37 0 my $new_args = $hook->new_args;
38 0 ▶ @_ = @$new_args
Branch: 1 branch
BranchTrueFalseTotal
if $new_args 0 0 0
39 if $new_args;
40 0 $hook->returned([&$code(@_)]);
41 0 ▶ return $hook->post->(@_, $hook)
Branch: 1 branch
BranchTrueFalseTotal
if $hook->post 0 0 0
42 if $hook->post;
43 0 return $hook->returned;
44 }
45 0 }
46  
47 1 ▶sub assert_method {
Subroutine assert_method: called CC=7 SCAR=32.1
Pod undocumented
48 return shift
49 1 ▶ if not defined($_[0]) or ref($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
if not defined $_[0] or ref $_[0] 0 1 1
Condition33%1/3
not defined $_[0] or ref $_[0]
l!l&&r!l&&!r
001
MC/DC0%0/2
not defined $_[0] or ref $_[0]
not defined $_[0] ref $_[0]
Truth table33%1/3
not(A) or B
A= not defined $_[0]B= ref $_[0]
ABresult
1X1
011
000
50 1 my $full_name = $self->get_full_name(shift);
51 0 ▶ my ($package, $method) = ($full_name) =~ /(.*)::(.*)/
Branch: 1 branch
BranchTrueFalseTotal
() = $full_name =~ /(.*)::(.*)/ or die "Can't hook invalid fully qualified method name: '${full_name}'" 0 0 0
52 or die "Can't hook invalid fully qualified method name: '$full_name'";
53 0 ▶ unless ($package->can('new')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($package->can("new")) 0 0 0
54 0 eval "require $package";
55 0 undef($@);
56 0 ▶ die "Can't hook $full_name. Can't find package '$package'"
Branch: 1 branch
BranchTrueFalseTotal
unless $package->can("new") 0 0 0
57 unless $package->can('new');
58 }
59 0 my $sub = $full_name;
60 0 ▶ return \&$sub if defined &$sub;
Branch: 1 branch
BranchTrueFalseTotal
if defined &$sub 0 0 0
61 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
62 0 *$sub = eval <<END;
63 sub {
64 package $package;
65 my \$self = shift;
66 \$self->SUPER::$method(\@_);
67 };
68 END
69 0 return \&$sub;
70 }
71  
72 1 ▶sub get_full_name {
Subroutine get_full_name: called CC=3 SCAR=12.0
Pod undocumented
73 1 my $name = shift;
74 1 ▶ return $name if $name =~ /::/;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /::/ 0 1 1
75 1 ▶ if ($name =~ /(.*):(.*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /(.*):(.*)/) 1 0 1
76 1 my ($class_id, $method) = ($1, $2);
77 1 my $package = $self->hub->registry->lookup->classes->{$class_id};
78 0 return $package . '::' . $method;
79 }
80 0 return '';
81 }
82  
83 package Spoon::Hooked;
84 2 ▶use Spoon::Base -Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
85  
86 2 field 'hook_point';
87 2 field 'original';
88 2 field 'replacement';
89  
90 0 ▶sub unhook {
Subroutine unhook: not called CC=3 SCAR=17.7
91 my ($hook_point, $original, $replacement) =
92 0 @{$self}{qw(hook_point original replacement)};
93 0 %$self = ();
94 0 ▶ return unless defined $hook_point;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $hook_point 0 0 0
95 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
96 0 my $current = *$hook_point{CODE};
97  
98 0 ▶ die "Unhooking error for $hook_point"
Branch: 1 branch
BranchTrueFalseTotal
unless "$current" eq "$replacement" 0 0 0
99 unless "$current" eq "$replacement";
100 2 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
101 0 *$hook_point = $original;
102 0 return 1;
103 }
104  
105 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=1 SCAR=6.9
106 0 $self->unhook;
107 }
108  
109 package Spoon::Hook;
110 2 ▶use Spoon::Base -Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
111  
112 2 field 'code';
113 2 field 'pre';
114 2 field 'post';
115 2 field 'new_args';
116  
117 0 ▶sub returned {
Subroutine returned: not called CC=4 SCAR=30.0
118 0 ▶ $self->{returned} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
119 0 ▶ $self->{returned} ||= [];
Condition0%0/2
$self->{"returned"} ||= []
l!l
00
MC/DC0%0/1
$self->{"returned"} ||= []
$self->{"returned"}
Truth table0%0/2
A
A= $self->{"returned"}
Aresult
11
00
120 0 ▶ wantarray ? (@{$self->{returned}}) : $self->{returned}[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
121 }
122  
123 0 ▶sub returned_true {
Subroutine returned_true: not called CC=3 SCAR=24.8
124 0 ▶ @{$self->{returned}} && $self->{returned}[0] && 1;
Branch: 1 branch
BranchTrueFalseTotal
@{$self->{"returned"};} and $self->{"returned"}[0] and 1 0 0 0
Condition0%0/3
@{$self->{"returned"};} and $self->{"returned"}[0]
!ll&&!rl&&r
000
MC/DC0%0/2
@{$self->{"returned"};} and $self->{"returned"}[0]
@{$self->{"returned"};} $self->{"returned"}[0]
Truth table0%0/3
A and B
A= @{$self->{"returned"};}B= $self->{"returned"}[0]
ABresult
0X0
100
111
125 }
126  
127 0 ▶sub cancel {
Subroutine cancel: not called CC=1 SCAR=6.9
128 0 $self->code(undef);
129 0 return ();
130 }
131  
132 2 __END__
« Spoon/Registry.pm ↑ Summary Spoon/MetadataObject.pm »