blib/lib/Types/QuacksLike.pm

filter: Statementstmt 78.7% 63 / 80 Branchbran 34.2% 13 / 38 Conditioncond 40.0% 12 / 30 MC/DCmcdc 18.7% 3 / 16 Subroutinesub 91.6% 11 / 12 Podpod n/a totaltotal 57.9% 102 / 176 CC 31 SCAR 30.2
CC
31
Cov
59%
_methods_of
39.9
__ANON__
20.0
_methods_from_package
15.7
CRAP 20.6
↑ Summary
1 package Types::QuacksLike;
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 3 our $VERSION = '0.001001';
6 3 $VERSION =~ tr/_//d;
7  
8 3 ▶use Type::Library -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Types::Standard qw(ClassName Object);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 ▶BEGIN {
Subroutine BEGIN: called CC=3 SCAR=15.1
12 3 ▶ if ("$]" >= 5.010_000) {
Branch: 1 branch
BranchTrueFalseTotal
if ("$]" >= 5.01) { } 3 0 3
13 3 require mro;
14 3 *_linear_isa = \&mro::get_linear_isa;
15 }
16 else {
17 0 local $@;
18 # we don't care about order so we can ignore c3
19 0 ▶ eval <<'END_CODE' or die $@;
Branch: 1 branch
BranchTrueFalseTotal
eval " sub _linear_isa(\$;\$) {\n my \$class = shift;\n my \@check = (\$class);\n my \@lin;\n\n my %found;\n while (defined(my \$check = shift \@check)) {\n push \@lin, \$check;\n no strict 'refs';\n unshift \@check, grep !\$found{\$_}++, \@{\"\$check\\::ISA\"};\n }\n\n return \\\@lin;\n }\n" or die $@ 0 0 0
20 sub _linear_isa($;$) {
21 my $class = shift;
22 my @check = ($class);
23 my @lin;
24  
25 my %found;
26 while (defined(my $check = shift @check)) {
27 push @lin, $check;
28 no strict 'refs';
29 unshift @check, grep !$found{$_}++, @{"$check\::ISA"};
30 }
31  
32 return \@lin;
33 }
34 END_CODE
35 }
36 }
37  
38 3 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=10.9
39 3 local $@;
40 3 ▶ if (eval { require Sub::Util; defined &Sub::Util::subname }) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { require Sub::Util; defined &Sub::Util::subname } }) { } 3 0 3
41 7 ▶ *_stash_name = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
42 7 my $name = Sub::Util::subname($_[0]);
43 7 $name =~ s{::[^:]+\z}{};
44 7 $name;
45 3 };
46 }
47 else {
48 0 require B;
49 0 ▶ *_stash_name = sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
50 0 my ($coderef) = @_;
51 0 ▶ ref $coderef or return;
Branch: 1 branch
BranchTrueFalseTotal
ref $coderef or return 0 0 0
52 0 my $cv = B::svref_2object($coderef);
53 0 ▶ $cv->isa('B::CV') or return;
Branch: 1 branch
BranchTrueFalseTotal
$cv->isa("B::CV") or return 0 0 0
54 0 ▶ $cv->GV->isa('B::SPECIAL') and return;
Branch: 1 branch
BranchTrueFalseTotal
$cv->GV->isa("B::SPECIAL") and return 0 0 0
55 0 $cv->GV->STASH->NAME;
56 0 };
57 }
58 }
59  
60 7 ▶sub _methods_from_package {
Subroutine _methods_from_package: called CC=10 SCAR=27.5
61 7 my $package = shift;
62 3 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
63 7 ▶ my $does
Branch: 2 branches
BranchTrueFalseTotal
$package->can('does') ? : 0 7 7
$package->can('DOES') ? : 7 0 7
64 = $package->can('does') ? 'does'
65 : $package->can('DOES') ? 'DOES'
66 : undef;
67 7 my $stash = \%{"${package}::"};
68 return
69 grep {
70 7 my $code = \&{"${package}::$_"};
71 7 ▶ my $code_stash = _stash_name($code) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $code_stash = _stash_name($code) or next 7 0 7
72  
73 7 ▶ /\A\(/
Condition25%3/12
/\A\(/ || $code_stash eq $package || $code_stash eq 'constant' || $does && $package->$does($code_stash)
l!l&&r!l&&!r
700
/\A\(/ or $code_stash eq $package or $code_stash eq "constant"
l!l&&r!l&&!r
700
/\A\(/ or $code_stash eq $package
l!l&&r!l&&!r
070
$does and $package->$does($code_stash)
!ll&&!rl&&r
000
MC/DC0%0/5
/\A\(/ || $code_stash eq $package || $code_stash eq 'constant' || $does && $package->$does($code_stash)
/\A\(/ $code_stash eq $package $code_stash eq "constant" $does $package->$does($code_stash)
Truth table16%1/6
A or B or C || D and E
A= /\A\(/B= $code_stash eq $packageC= $code_stash eq "constant"D= $doesE= $package->$does($code_stash)
ABCDEresult
1XXXX1
01XXX1
001XX1
000111
0000X0
000100
74 or $code_stash eq $package
75 or $code_stash eq 'constant'
76 or $does && $package->$does($code_stash)
77 }
78 grep {
79 7 my $entry = $stash->{$_};
80 31 ▶ defined $entry && ref $entry ne 'HASH' && exists &{"${package}::$_"};
Condition50%3/6
defined $entry && ref $entry ne 'HASH' && exists &{"${package}::$_";}
!ll&&!rl&&r
0247
defined $entry and ref $entry ne "HASH"
!ll&&!rl&&r
0031
MC/DC33%1/3
defined $entry && ref $entry ne 'HASH' && exists &{"${package}::$_";}
defined $entry ref $entry ne "HASH" exists &{"${package}::$_";}
Truth table50%2/4
A and B && C
A= defined $entryB= ref $entry ne "HASH"C= exists &{"${package}::$_";}
ABCresult
0XX0
10X0
1100
1111
81 } keys %$stash;
82 }
83  
84 8 ▶sub _methods_of {
Subroutine _methods_of: called CC=15 SCAR=36.9
85 8 my $package = shift;
86 8 my @methods;
87 8 ▶ if ($INC{'Moo/Role.pm'} && Moo::Role->is_role($package)) {
Branch: 3 branches
BranchTrueFalseTotal
if ($INC{'Moo/Role.pm'} and 'Moo::Role'->is_role($package)) { } 2 6 8
elsif ($INC{'Role/Tiny.pm'} and 'Role::Tiny'->is_role($package)) { } 2 4 6
elsif ($INC{'Class/MOP.pm'} and my $meta = Class::MOP::class_of($package)) { } 0 4 4
Condition66%2/3
$INC{"Moo/Role.pm"} and "Moo::Role"->is_role($package)
!ll&&!rl&&r
602
MC/DC50%1/2
$INC{"Moo/Role.pm"} and "Moo::Role"->is_role($package)
$INC{"Moo/Role.pm"} "Moo::Role"->is_role($package)
Truth table66%2/3
A and B
A= $INC{"Moo/Role.pm"}B= "Moo::Role"->is_role($package)
ABresult
0X0
100
111
88 2 @methods = Moo::Role->methods_provided_by($package);
89 }
90 ▶ elsif ($INC{'Role/Tiny.pm'} && Role::Tiny->is_role($package)) {
Condition66%2/3
$INC{"Role/Tiny.pm"} and "Role::Tiny"->is_role($package)
!ll&&!rl&&r
402
MC/DC50%1/2
$INC{"Role/Tiny.pm"} and "Role::Tiny"->is_role($package)
$INC{"Role/Tiny.pm"} "Role::Tiny"->is_role($package)
Truth table66%2/3
A and B
A= $INC{"Role/Tiny.pm"}B= "Role::Tiny"->is_role($package)
ABresult
0X0
100
111
91 2 @methods = Role::Tiny->methods_provided_by($package);
92 }
93 ▶ elsif ($INC{'Class/MOP.pm'} and my $meta = Class::MOP::class_of($package)) {
Condition33%1/3
$INC{"Class/MOP.pm"} and my $meta = Class::MOP::class_of($package)
!ll&&!rl&&r
400
MC/DC0%0/2
$INC{"Class/MOP.pm"} and my $meta = Class::MOP::class_of($package)
$INC{"Class/MOP.pm"} my $meta = Class::MOP::class_of($package)
Truth table33%1/3
A and B
A= $INC{"Class/MOP.pm"}B= my $meta = Class::MOP::class_of($package)
ABresult
0X0
100
111
94 # classes
95 0 ▶ if ($meta->can('get_all_method_names')) {
Branch: 3 branches
BranchTrueFalseTotal
if ($meta->can('get_all_method_names')) { } 0 0 0
elsif ($meta->can('get_method_list')) { } 0 0 0
elsif ($meta->can('list_all_symbols')) { } 0 0 0
96 0 @methods = $meta->get_all_method_names;
97 }
98 # roles
99 elsif ($meta->can('get_method_list')) {
100 0 @methods = $meta->get_method_list;
101 }
102 # packages
103 elsif ($meta->can('list_all_symbols')) {
104 0 @methods = $meta->list_all_symbols('CODE');
105 }
106 }
107 else {
108 4 my $moo_method;
109 4 ▶ if ($INC{'Moo.pm'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($INC{"Moo.pm"}) 0 4 4
110 0 ▶ $moo_method = Moo->can('is_class') ? 'is_class' : '_accessor_maker_for';
Branch: 1 branch
BranchTrueFalseTotal
'Moo'->can('is_class') ? : 0 0 0
111 }
112  
113 4 my %s;
114 4 for my $isa (@{_linear_isa($package)}) {
115 7 ▶ if ($moo_method && Moo->$moo_method($isa)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($moo_method and 'Moo'->$moo_method($isa)) { } 0 7 7
Condition33%1/3
$moo_method and "Moo"->$moo_method($isa)
!ll&&!rl&&r
700
MC/DC0%0/2
$moo_method and "Moo"->$moo_method($isa)
$moo_method "Moo"->$moo_method($isa)
Truth table33%1/3
A and B
A= $moo_methodB= "Moo"->$moo_method($isa)
ABresult
0X0
100
111
116 0 push @methods, grep !$s{$_}++, keys %{ Moo->_concrete_methods_of($isa) };
117 }
118 else {
119 7 push @methods, grep !$s{$_}++, _methods_from_package($isa);
120 }
121 }
122 }
123  
124 8 return grep !/\A_/, sort @methods;
125 }
126  
127 3 my $meta = __PACKAGE__->meta;
128 3 my $class_name = ClassName;
129  
130 $meta->add_type({
131 name => "QuacksLike",
132 parent => Object,
133 8 ▶ constraint_generator => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
134 8 my @packages = map $class_name->assert_return($_), @_;
135 8 ▶ return Object unless @packages;
Branch: 1 branch
BranchTrueFalseTotal
unless @packages 0 8 8
136  
137 8 my %s;
138 8 my @methods = sort grep !$s{$_}++, map _methods_of($_), @packages;
139  
140 8 require Type::Tiny::Duck;
141 8 return Type::Tiny::Duck->new(
142 methods => \@methods,
143 display_name => sprintf('QuacksLike[%s]', join q[,], map qq{"$_"}, @packages),
144 );
145 },
146 3 });
147  
148 3 1;
149 __END__
↑ Summary