lib/Sub/Override.pm

filter: Statementstmt 99.3% 151 / 152 Branchbran 96.4% 27 / 28 Conditioncond 91.6% 11 / 12 MC/DCmcdc 85.7% 6 / 7 Subroutinesub 100% 33 / 33 Podpod 100% 6 / 6 totaltotal 98.3% 234 / 238 CC 20 SCAR 8.9
CC
20
Cov
98%
restore
6.0
_get_fully_qualified_sub_name
5.0
DESTROY
3.0
CRAP 2.4
↑ Summary
1 package Sub::Override;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Scalar::Util qw(set_prototype);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 our $VERSION = '0.12';
10  
11 7 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
12 7 my $class = shift;
13 7 my $self = bless {}, $class;
14 7 ▶ $self->replace(@_) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 2 5 7
15 7 return $self;
16 }
17  
18 {
19 1 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
20 # because override() was a better name and this is what it should have been
21 # called.
22 1 *override = *replace{CODE};
23 }
24  
25 11 ▶sub replace {
Subroutine replace: called CC=2 SCAR=6.9
Pod documented
26 11 my ( $self, $sub_to_replace, $new_sub ) = @_;
27 11 $sub_to_replace = $self->_get_fully_qualified_sub_name($sub_to_replace);
28 11 $self->_ensure_code_slot_exists($sub_to_replace)->_validate_sub_ref($new_sub);
29 {
30 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
31 9 ▶ $self->{$sub_to_replace} ||= *$sub_to_replace{CODE};
Condition100%2/2
$self->{$sub_to_replace} ||= *$sub_to_replace{"CODE"}
l!l
27
MC/DC100%1/1
$self->{$sub_to_replace} ||= *$sub_to_replace{"CODE"}
$self->{$sub_to_replace}
Truth table100%2/2
A
A= $self->{$sub_to_replace}
Aresult
11
00
32 1 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
33 9 *$sub_to_replace = $new_sub;
34 }
35 9 return $self;
36 }
37  
38 2 ▶sub inject {
Subroutine inject: called CC=1 SCAR=0.0
Pod documented
39 2 my ( $self, $sub_to_inject, $new_sub ) = @_;
40 2 $sub_to_inject = $self->_get_fully_qualified_sub_name($sub_to_inject);
41 2 $self->_ensure_code_slot_does_not_exist($sub_to_inject)->_validate_sub_ref($new_sub);
42 {
43 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
44 1 $self->{$sub_to_inject} = undef;
45 1 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
46 1 *$sub_to_inject = $new_sub;
47 }
48 1 return $self;
49 }
50  
51 4 ▶sub inherit {
Subroutine inherit: called CC=1 SCAR=0.0
Pod documented
52 4 my ( $self, $sub_to_inherit, $new_sub ) = @_;
53 4 $sub_to_inherit = $self->_get_fully_qualified_sub_name($sub_to_inherit);
54 4 $self->_ensure_code_slot_exists_in_parent_class($sub_to_inherit)->_validate_sub_ref($new_sub);
55 {
56 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
57 1 $self->{$sub_to_inherit} = undef;
58 1 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
59 1 *$sub_to_inherit = $new_sub;
60 }
61 1 return $self;
62 }
63  
64 2 ▶sub wrap {
Subroutine wrap: called CC=3 SCAR=11.0
Pod documented
65 2 my ( $self, $sub_to_replace, $new_sub ) = @_;
66 2 $sub_to_replace = $self->_get_fully_qualified_sub_name($sub_to_replace);
67 2 $self->_ensure_code_slot_exists($sub_to_replace)->_validate_sub_ref($new_sub);
68 {
69 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
70 2 ▶ $self->{$sub_to_replace} ||= *$sub_to_replace{CODE};
Condition50%1/2
$self->{$sub_to_replace} ||= *$sub_to_replace{"CODE"}
l!l
02
MC/DC0%0/1
$self->{$sub_to_replace} ||= *$sub_to_replace{"CODE"}
$self->{$sub_to_replace}
Truth table50%1/2
A
A= $self->{$sub_to_replace}
Aresult
11
00
71  
72 # passing $sub_to_replace directly to arguments prevents early destruction.
73 2 my $weakened_sub_to_replace = $self->{$sub_to_replace};
74 2 ▶ my $code = sub { unshift(@_, $weakened_sub_to_replace); goto &$new_sub };
Subroutine __ANON__: called CC=1 SCAR=0.0
75 2 my $prototype = prototype($self->{$sub_to_replace});
76 2 ▶ set_prototype(\&$code, $prototype) if defined $prototype;
Branch: 1 branch
BranchTrueFalseTotal
if defined $prototype 1 1 2
77  
78 1 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
79 2 *$sub_to_replace = $code;
80 }
81 2 return $self;
82 }
83  
84 10 ▶sub restore {
Subroutine restore: called CC=6 SCAR=17.9
Pod documented
85 10 my ( $self, $name_of_sub ) = @_;
86 10 $name_of_sub = $self->_get_fully_qualified_sub_name($name_of_sub);
87 10 ▶ if ( !$name_of_sub && 1 == keys %$self ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $name_of_sub and 1 == keys %$self) 1 9 10
Condition100%3/3
not $name_of_sub and 1 == keys %$self
!ll&&!rl&&r
811
MC/DC100%2/2
not $name_of_sub and 1 == keys %$self
not $name_of_sub 1 == keys %$self
Truth table100%3/3
not(A) and B
A= not $name_of_subB= 1 == keys %$self
ABresult
0X0
100
111
88 1 ($name_of_sub) = keys %$self;
89 }
90 croak(
91 10 ▶ sprintf 'You must provide the name of a sub to restore: (%s)' => join
Branch: 1 branch
BranchTrueFalseTotal
unless $name_of_sub 1 9 10
92 ', ' => sort keys %$self )
93 unless $name_of_sub;
94 croak("Cannot restore a sub that was not replaced ($name_of_sub)")
95 9 ▶ unless exists $self->{$name_of_sub};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{$name_of_sub} 2 7 9
96  
97 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
98 1 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
99 7 my $maybe_sub_ref = delete $self->{$name_of_sub};
100 7 ▶ if ( defined $maybe_sub_ref ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $maybe_sub_ref) { } 5 2 7
101 5 *$name_of_sub = $maybe_sub_ref;
102 }
103 else {
104 2 undef *$name_of_sub;
105 }
106 7 return $self;
107 }
108  
109 7 ▶sub DESTROY {
Subroutine DESTROY: called CC=3 SCAR=11.1
110 7 my $self = shift;
111 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
112 # "misc" suppresses warning: 'Undefined value assigned to typeglob'
113 1 ▶ no warnings 'redefine', 'misc';
Subroutine BEGIN: called CC=1 SCAR=0.0
114 7 ▶ while ( my ( $sub_name, $maybe_sub_ref ) = each %$self ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$self) 4 7 11
115 4 ▶ if ( defined $maybe_sub_ref ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $maybe_sub_ref) { } 4 0 4
116 4 *$sub_name = $maybe_sub_ref;
117 }
118 else {
119 0 undef *$sub_name;
120 }
121 }
122 }
123  
124 29 ▶sub _get_fully_qualified_sub_name {
Subroutine _get_fully_qualified_sub_name: called CC=5 SCAR=16.1
125 29 my ( $self, $subname ) = @_;
126 29 ▶ if ( ( $subname || '' ) =~ /^\w+$/ ) { # || "" for suppressing test warnings
Branch: 1 branch
BranchTrueFalseTotal
if (($subname || "") =~ /^\w+$/) 14 15 29
Condition100%2/2
$subname || ''
l!l
272
MC/DC100%1/1
$subname || ''
$subname
Truth table100%2/2
A
A= $subname
Aresult
11
00
127 14 my $package = do {
128 14 my $call_level = 0;
129 14 my $this_package;
130 14 ▶ while ( !$this_package || __PACKAGE__ eq $this_package ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $this_package or __PACKAGE__ eq $this_package) 29 14 43
Condition100%3/3
not $this_package or __PACKAGE__ eq $this_package
l!l&&r!l&&!r
141514
MC/DC100%2/2
not $this_package or __PACKAGE__ eq $this_package
not $this_package __PACKAGE__ eq $this_package
Truth table100%3/3
not(A) or B
A= not $this_packageB= __PACKAGE__ eq $this_package
ABresult
1X1
011
000
131 29 ($this_package) = caller($call_level);
132 29 $call_level++;
133 }
134 14 $this_package;
135 };
136 14 $subname = "${package}::$subname";
137 }
138 29 return $subname;
139 };
140  
141 14 ▶sub _validate_sub_ref {
Subroutine _validate_sub_ref: called CC=2 SCAR=6.9
142 14 my ( $self, $sub_ref ) = @_;
143 14 ▶ unless ( 'CODE' eq ref $sub_ref ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ("CODE" eq ref $sub_ref) 1 13 14
144 1 croak("($sub_ref) must be a code reference");
145 }
146 13 return $self;
147 };
148  
149 13 ▶sub _ensure_code_slot_exists {
Subroutine _ensure_code_slot_exists: called CC=2 SCAR=6.9
150 13 my ( $self, $code_slot ) = @_;
151 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
152 13 ▶ unless ( defined *{$code_slot}{CODE} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined *{$code_slot;}{"CODE"}) 1 12 13
153 1 croak("Cannot replace non-existent sub ($code_slot)");
154 }
155 12 return $self;
156 };
157  
158 6 ▶sub _ensure_code_slot_does_not_exist {
Subroutine _ensure_code_slot_does_not_exist: called CC=2 SCAR=6.9
159 6 my ( $self, $code_slot ) = @_;
160 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
161 6 ▶ if ( defined *{$code_slot}{CODE} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined *{$code_slot;}{"CODE"}) 3 3 6
162 3 croak("Cannot create a sub that already exists ($code_slot)");
163 }
164 3 return $self;
165 };
166  
167 4 ▶sub _ensure_code_slot_exists_in_parent_class {
Subroutine _ensure_code_slot_exists_in_parent_class: called CC=2 SCAR=6.9
168 4 my ( $self, $code_slot ) = @_;
169 4 $self->_ensure_code_slot_does_not_exist($code_slot);
170 {
171 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
172 2 my $class = *{$code_slot}{PACKAGE};
173 2 my $method = *{$code_slot}{NAME};
174 2 ▶ croak("Sub does not exist in parent class ($code_slot)")
Branch: 1 branch
BranchTrueFalseTotal
unless $class->can($method) 1 1 2
175 unless $class->can($method);
176 }
177 1 return $self;
178 };
179  
180 1 1;
181  
182 __END__
↑ Summary