blib/lib/ex/monkeypatched.pm

filter: Statementstmt 100% 52 / 52 Branchbran 90.0% 18 / 20 Conditioncond 66.6% 12 / 18 MC/DCmcdc 40.0% 4 / 10 Subroutinesub 100% 11 / 11 Podpod 0.0% 0 / 1 totaltotal 86.6% 97 / 112 CC 20 SCAR 16.9
CC
20
Cov
91%
_parse_injections
9.3
import
7.0
_inject_methods
4.0
CRAP 5.4
↑ Summary
1 package ex::monkeypatched;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 ▶use Sub::Name qw<subname>;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Carp qw<croak>;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 3 our $VERSION = '0.03';
10  
11 10 ▶sub import {
Subroutine import: called CC=7 SCAR=19.5
12 10 my $invocant = shift;
13 10 ▶ my $norequire = @_ && $_[0] && $_[0] eq '-norequire' && shift;
Condition77%7/9
@_ && $_[0] && $_[0] eq '-norequire' && shift()
!ll&&!rl&&r
703
@_ and $_[0] and $_[0] eq "-norequire"
!ll&&!rl&&r
163
@_ and $_[0]
!ll&&!rl&&r
109
MC/DC50%2/4
@_ && $_[0] && $_[0] eq '-norequire' && shift()
@_ $_[0] $_[0] eq "-norequire" shift()
Truth table60%3/5
A and B and C && D
A= @_B= $_[0]C= $_[0] eq "-norequire"D= shift()
ABCDresult
0XXX0
10XX0
110X0
11100
11111
14 10 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 9 1 10
15 9 ▶ my @injections = _parse_injections(@_)
Branch: 1 branch
BranchTrueFalseTotal
my(@injections) = _parse_injections(@_) or croak("Usage: use $invocant \$class => %methods\nor: use $invocant (class => \$class, methods => \\%methods)\nor: use $invocant (method => \$name, implementations => \\%impl)") 8 1 9
16 or croak "Usage: use $invocant \$class => %methods
17 or: use $invocant (class => \$class, methods => \\%methods)
18 or: use $invocant (method => \$name, implementations => \\%impl)";
19 8 ▶ _require(map { $_->[0] } @injections)
Branch: 1 branch
BranchTrueFalseTotal
unless $norequire 5 3 8
20 if !$norequire;
21 6 _inject_methods(@injections);
22 }
23 }
24  
25 5 ▶sub _require {
Subroutine _require: called CC=2 SCAR=6.9
26 5 for (@_) {
27 8 (my $as_file = $_) =~ s{::|'}{/}g;
28 8 require "$as_file.pm"; # dies if no such file is found
29 }
30 }
31  
32 12 ▶sub _parse_injections {
Subroutine _parse_injections: called CC=9 SCAR=22.3
33  
34 12 ▶ if (@_ == 1 && ref $_[0] eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 1 and ref $_[0] eq 'HASH') { } 2 10 12
elsif (@_ % 2) { } 9 1 10
Condition66%2/3
@_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
1002
MC/DC50%1/2
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1 ref $_[0] eq "HASH"
Truth table66%2/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
35 2 my $opt = shift;
36 2 ▶ if (defined $opt->{class} && ref $opt->{methods} eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $opt->{'class'} and ref $opt->{'methods'} eq 'HASH') { } 1 1 2
elsif (defined $opt->{'method'} and ref $opt->{'implementations'} eq 'HASH') { } 1 0 1
Condition66%2/3
defined $opt->{"class"} and ref $opt->{"methods"} eq "HASH"
!ll&&!rl&&r
101
MC/DC50%1/2
defined $opt->{"class"} and ref $opt->{"methods"} eq "HASH"
defined $opt->{"class"} ref $opt->{"methods"} eq "HASH"
Truth table66%2/3
A and B
A= defined $opt->{"class"}B= ref $opt->{"methods"} eq "HASH"
ABresult
0X0
100
111
37 2 return map { [$opt->{class}, $_, $opt->{methods}{$_}] }
38 1 keys %{ $opt->{methods} };
39 }
40 ▶ elsif (defined $opt->{method} && ref $opt->{implementations} eq 'HASH') {
Condition33%1/3
defined $opt->{"method"} and ref $opt->{"implementations"} eq "HASH"
!ll&&!rl&&r
001
MC/DC0%0/2
defined $opt->{"method"} and ref $opt->{"implementations"} eq "HASH"
defined $opt->{"method"} ref $opt->{"implementations"} eq "HASH"
Truth table33%1/3
A and B
A= defined $opt->{"method"}B= ref $opt->{"implementations"} eq "HASH"
ABresult
0X0
100
111
41 2 return map { [$_, $opt->{method}, $opt->{implementations}{$_}] }
42 1 keys %{ $opt->{implementations} };
43 }
44 }
45 elsif (@_ % 2) {
46 9 my @injections;
47 9 my $target = shift;
48 9 ▶ push @injections, [$target, splice @_, 0, 2]
Branch: 1 branch
BranchTrueFalseTotal
@_ and do { push @injections, [$target, splice(@_, 0, 2)] } 13 9 22
49 while @_;
50 9 return @injections;
51 }
52  
53 1 return;
54 }
55  
56 3 ▶sub inject {
Subroutine inject: called CC=2 SCAR=7.1
Pod undocumented
57 3 my $invocant = shift;
58 3 ▶ my @injections = _parse_injections(@_)
Branch: 1 branch
BranchTrueFalseTotal
my(@injections) = _parse_injections(@_) or croak("Usage: $invocant->inject(\$class, %methods)\nor: $invocant->inject({ class => \$class, methods => \\%methods })\nor: $invocant->inject({ method => \$name, implementations => \\%impl })") 3 0 3
59 or croak "Usage: $invocant->inject(\$class, %methods)
60 or: $invocant->inject({ class => \$class, methods => \\%methods })
61 or: $invocant->inject({ method => \$name, implementations => \\%impl })";
62 3 _inject_methods(@injections);
63 }
64  
65 9 ▶sub _inject_methods {
Subroutine _inject_methods: called CC=4 SCAR=13.9
66 9 for (@_) {
67 15 my ($target, $name, undef) = @$_;
68 15 ▶ croak qq[Can't monkey-patch: $target already has a method "$name"]
Branch: 1 branch
BranchTrueFalseTotal
if $target->can($name) 3 12 15
69 if $target->can($name);
70 }
71 6 _install_subroutine(@$_) for @_;
72 }
73  
74 10 ▶sub _install_subroutine {
Subroutine _install_subroutine: called CC=1 SCAR=0.0
75 10 my ($target, $name, $code) = @_;
76 10 my $full_name = "$target\::$name";
77 10 my $renamed_code = subname($full_name, $code);
78 3 ▶ no strict qw<refs>;
Subroutine BEGIN: called CC=1 SCAR=0.0
79 10 *$full_name = $renamed_code;
80 }
81  
82 3 1;
83 __END__
↑ Summary