blib/lib/Attribute/Signature.pm

filter: Statementstmt 91.9% 125 / 136 Branchbran 70.0% 56 / 80 Conditioncond 61.9% 13 / 21 MC/DCmcdc 42.8% 6 / 14 Subroutinesub 100% 19 / 19 Podpod 71.4% 5 / 7 totaltotal 80.8% 224 / 277 CC 48 SCAR 28.1
CC
48
Cov
82%
__ANON__
28.0
__ANON__
23.1
returns
4.4
CRAP 16.7
↑ Summary
1 package Attribute::Signature;
2
3 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 #use warnings::register;
6
7 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Scalar::Util qw ( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
9
10 #use Data::Dumper;
11 1 ▶use Attribute::Handlers;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use attributes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 #local $^W=0;
14
15 1 our $VERSION = '1.11';
16 1 my $SIGNATURES = {};
17
18 28 ▶sub UNIVERSAL::with : ATTR(CODE,INIT) {
Subroutine with: called CC=4 SCAR=14.0
Pod undocumented
19 28 my ($package, $symbol, $referent, $attr, $data) = @_;
20
21 28 my $large = *{$symbol}{NAME};
22 28 my $subname = substr($large, rindex($large, ':') + 1);
23
24 1 ▶ no warnings qw( redefine );
Subroutine BEGIN: called CC=1 SCAR=0.0
25
26 ## make sure we have an array ref, so its easier
27 28 ▶ if (!ref($data)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $data) 0 28 28
28 0 $data = [ $data ];
29 }
30
31 ## save this for later use
32 28 $SIGNATURES->{$package}->{with}->{$subname} = $data;
33
34 28 my $attributes = { map { ($_, 1) } attributes::get( $referent ) };
35
36 28 ▶ if ($attributes->{method}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attributes->{"method"}) 1 27 28
37 1 ▶ print "Signature on sub $subname is for a method\n" if $::AS_DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $main::AS_DEBUG 0 1 1
38 1 unshift @$data, $package; ## put a placeholder in the front
39 }
40
41 28 ▶ *{$symbol} = sub {
Subroutine __ANON__: called CC=21 SCAR=31.4
42 117 my $i = 0;
43 117 my $count = scalar(@_);
44
45 117 ▶ if ($attributes->{method}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attributes->{"method"}) 3 114 117
46 3 $i = 1;
47 }
48
49 117 ▶ if ($count != scalar(@$data)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count != scalar @$data) 20 97 117
50 20 ▶ if ($attributes->{method}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attributes->{'method'}) { } 1 19 20
51 1 croak("invalid number of arguments passed to method $subname");
52 } else {
53 19 croak("invalid number of arguments passed to subroutine $subname ($count passed, ".scalar(@$data)." required");
54 }
55 }
56
57 97 my $m = 0;
58 97 ▶ print "Comparisons\n" if $::AS_DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $main::AS_DEBUG 0 97 97
59 97 ▶ print "\tSignature\tValue\n" if $::AS_DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $main::AS_DEBUG 0 97 97
60 97 my @failed;
61 97 ▶ while($i <= $count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $count) 201 0 201
62 201 ▶ print "\t$data->[$i]\t\t$_[$i]\n" if $::AS_DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $main::AS_DEBUG 0 201 201
63 201 ▶ last unless $data->[$i];
Branch: 1 branch
BranchTrueFalseTotal
unless $data->[$i] 97 104 201
64 104 my $ok=0;
65 104 ▶ if (lc($data->[$i]) eq $data->[$i]) {
Branch: 3 branches
BranchTrueFalseTotal
if (lc $data->[$i] eq $data->[$i]) { } 72 32 104
elsif (builtin::blessed($_[$i]) and $_[$i]->isa($data->[$i])) { } 0 32 32
elsif (not builtin::blessed($_[$i]) and ref $_[$i] eq $data->[$i]) { } 8 24 32
66 ## here we are checking for little types
67 72 my $type = $data->[$i];
68 72 ▶ if (Attribute::Signature->can( $type )) {
Branch: 1 branch
BranchTrueFalseTotal
if ("Attribute::Signature"->can($type)) 72 0 72
69 72 ▶ if (Attribute::Signature->$type( $_[$i] )) {
Branch: 1 branch
BranchTrueFalseTotal
if ("Attribute::Signature"->$type($_[$i])) 49 23 72
70 49 $ok++;
71 }
72 }
73 ▶ } elsif ((blessed($_[$i])) && $_[$i]->isa( $data->[$i]) ) {
Condition33%1/3
builtin::blessed($_[$i]) and $_[$i]->isa($data->[$i])
!ll&&!rl&&r
3200
MC/DC0%0/2
builtin::blessed($_[$i]) and $_[$i]->isa($data->[$i])
builtin::blessed($_[$i]) $_[$i]->isa($data->[$i])
Truth table33%1/3
A and B
A= builtin::blessed($_[$i])B= $_[$i]->isa($data->[$i])
ABresult
0X0
100
111
74 # || string($_[$i])
75 0 $ok++;
76 ▶ } elsif (!blessed($_[$i]) && ref($_[$i]) eq $data->[$i]) {
Condition66%2/3
not builtin::blessed($_[$i]) and ref $_[$i] eq $data->[$i]
!ll&&!rl&&r
0248
MC/DC50%1/2
not builtin::blessed($_[$i]) and ref $_[$i] eq $data->[$i]
not builtin::blessed($_[$i]) ref $_[$i] eq $data->[$i]
Truth table66%2/3
not(A) and B
A= not builtin::blessed($_[$i])B= ref $_[$i] eq $data->[$i]
ABresult
0X0
100
111
77 8 $ok++;
78 }
79 104 ▶ if ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) { } 57 47 104
80 57 $m++ ;
81 } else {
82 47 push @failed,$i;
83 }
84 104 $i++;
85 }
86
87 97 ▶ if ($attributes->{method}) { $m++; }
Branch: 1 branch
BranchTrueFalseTotal
if ($attributes->{"method"}) 2 95 97
88
89 97 ▶ print "Out of band:\n\tCount\tMatched\n\t$count\t$m\n" if defined $::AS_DEBUG && $::AS_DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if defined $main::AS_DEBUG and $main::AS_DEBUG 0 97 97
Condition33%1/3
defined $main::AS_DEBUG and $main::AS_DEBUG
!ll&&!rl&&r
9700
MC/DC0%0/2
defined $main::AS_DEBUG and $main::AS_DEBUG
defined $main::AS_DEBUG $main::AS_DEBUG
Truth table33%1/3
A and B
A= defined $main::AS_DEBUGB= $main::AS_DEBUG
ABresult
0X0
100
111
90
91 97 ▶ if ($m != $count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($m != $count) { } 47 50 97
92 47 croak("call to $subname does not match signature (failed args:".join(',',@failed).")");
93 } else {
94 #$referent->( @_ );
95 50 goto &$referent;
96 }
97 28 };
98 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
99
100 28 ▶sub UNIVERSAL::returns : ATTR(CODE,INIT) {
Subroutine returns: called CC=4 SCAR=14.8
Pod undocumented
101 28 my ($package, $symbol, $referent, $attr, $data) = @_;
102
103 28 my $large = *{$symbol}{NAME};
104 28 my $subname = substr($large, rindex($large, ':') + 1);
105
106 1 ▶ no warnings qw( redefine );
Subroutine BEGIN: called CC=1 SCAR=0.0
107
108 ## make sure we have an array ref, so its easier
109 28 ▶ if (!ref($data)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $data) 0 28 28
110 0 $data = [ $data ];
111 }
112
113 ## save this for later use
114 28 $SIGNATURES->{$package}->{returns}->{$subname} = $data;
115
116 0 my $attributes = { map { ($_, 1) } attributes::get( $referent ) };
117
118 28 ▶ if ($attributes->{method}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attributes->{"method"}) 0 28 28
119 0 ▶ print "Signature on sub $subname is for a method\n" if $::AS_DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $main::AS_DEBUG 0 0 0
120 0 unshift @$data, $package; ## put a placeholder in the front
121 }
122
123 28 ▶ *{$symbol} = sub {
Subroutine __ANON__: called CC=20 SCAR=33.3
124
125 96 my @return = $referent->( @_ );
126
127 29 my $i = 0;
128 29 my $count = scalar(@return);
129
130 29 ▶ if ($count != scalar(@$data)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count != scalar @$data) 0 29 29
131 0 ▶ if ($attributes->{method}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attributes->{'method'}) { } 0 0 0
132 0 croak("invalid number of arguments returned from method $subname");
133 } else {
134 0 croak("invalid number of arguments returned from subroutine $subname");
135 }
136 }
137
138 29 my $m = 0;
139 29 ▶ print "ReturnComparisons\n" if $::AS_DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $main::AS_DEBUG 0 29 29
140 29 ▶ print "\tSignature\tValue\n" if $::AS_DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $main::AS_DEBUG 0 29 29
141 29 ▶ while($i <= $count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $count) 60 0 60
142 60 ▶ print "\t$data->[$i]\t\t$return[$i]\n" if $::AS_DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $main::AS_DEBUG 0 60 60
143 60 ▶ last unless $data->[$i];
Branch: 1 branch
BranchTrueFalseTotal
unless $data->[$i] 29 31 60
144 31 ▶ if (lc($data->[$i]) eq $data->[$i]) {
Branch: 4 branches
BranchTrueFalseTotal
if (lc $data->[$i] eq $data->[$i]) { } 23 8 31
elsif ($data->[$i] eq 'REF' and ref $return[$i]) { } 1 7 8
elsif (builtin::blessed($return[$i]) and $return[$i]->isa($data->[$i])) { } 0 7 7
elsif (not builtin::blessed($return[$i]) and ref $return[$i] eq $data->[$i]) { } 3 4 7
145 ## here we are checking for little types
146 23 my $type = $data->[$i];
147 23 ▶ if (Attribute::Signature->can( $type )) {
Branch: 1 branch
BranchTrueFalseTotal
if ("Attribute::Signature"->can($type)) 23 0 23
148 23 ▶ if (Attribute::Signature->$type( $return[$i] )) {
Branch: 1 branch
BranchTrueFalseTotal
if ("Attribute::Signature"->$type($return[$i])) 19 4 23
149 19 $m++;
150 }
151 }
152 ▶ } elsif ($data->[$i] eq 'REF' && ref($return[$i])) {
Condition100%3/3
$data->[$i] eq "REF" and ref $return[$i]
!ll&&!rl&&r
611
MC/DC100%2/2
$data->[$i] eq "REF" and ref $return[$i]
$data->[$i] eq "REF" ref $return[$i]
Truth table100%3/3
A and B
A= $data->[$i] eq "REF"B= ref $return[$i]
ABresult
0X0
100
111
153 1 $m++;
154 # } elsif ((blessed($return[$i]) || string($return[$i])) && $return[$i]->isa( $data->[$i]) ) {
155 ▶ } elsif (blessed($return[$i]) && $return[$i]->isa( $data->[$i]) ) {
Condition33%1/3
builtin::blessed($return[$i]) and $return[$i]->isa($data->[$i])
!ll&&!rl&&r
700
MC/DC0%0/2
builtin::blessed($return[$i]) and $return[$i]->isa($data->[$i])
builtin::blessed($return[$i]) $return[$i]->isa($data->[$i])
Truth table33%1/3
A and B
A= builtin::blessed($return[$i])B= $return[$i]->isa($data->[$i])
ABresult
0X0
100
111
156 0 $m++;
157 ▶ } elsif (!blessed($return[$i]) && ref($return[$i]) eq $data->[$i]) {
Condition66%2/3
not builtin::blessed($return[$i]) and ref $return[$i] eq $data->[$i]
!ll&&!rl&&r
043
MC/DC50%1/2
not builtin::blessed($return[$i]) and ref $return[$i] eq $data->[$i]
not builtin::blessed($return[$i]) ref $return[$i] eq $data->[$i]
Truth table66%2/3
not(A) and B
A= not builtin::blessed($return[$i])B= ref $return[$i] eq $data->[$i]
ABresult
0X0
100
111
158 3 $m++;
159 } else {
160 # no match
161 }
162 31 $i++;
163 }
164
165 0 ▶ if ($attributes->{method}) { $m++; }
Branch: 1 branch
BranchTrueFalseTotal
if ($attributes->{"method"}) 0 29 29
166
167 29 ▶ print "ReturnOut of band:\n\tCount\tMatched\n\t$count\t$m\n" if $::AS_DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $main::AS_DEBUG 0 29 29
168
169 29 ▶ if ($m != $count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($m != $count) { } 8 21 29
170 8 croak("Arguments returned from $subname do not match signature $m != $count");
171 } else {
172 21 $referent->( @_ );
173 }
174 28 };
175 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
176
177 2 ▶sub getSignature {
Subroutine getSignature: called CC=2 SCAR=6.9
Pod documented
178 2 my $class = shift;
179 2 my $fqsn = shift;
180
181 ## this is my sub && package
182 2 my $subname = substr($fqsn, rindex($fqsn, ':') + 1);
183 2 my $package = substr($fqsn, 0, rindex($fqsn, '::'));
184
185 2 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 1 1 2
186 1 return($SIGNATURES->{$package}->{with}->{$subname}, $SIGNATURES->{$package}->{returns}->{$subname});
187 } else {
188 1 return $SIGNATURES->{$package}->{with}->{$subname};
189 }
190 }
191
192 45 ▶sub string {
Subroutine string: called CC=1 SCAR=0.0
Pod documented
193 45 my $class = shift;
194 45 return not ref $_[0];
195 }
196
197 20 ▶sub number {
Subroutine number: called CC=2 SCAR=6.9
Pod documented
198 20 my $class = shift;
199 20 ▶ return $class->float($_[0]) || $class->integer($_[0]);
Condition100%3/3
$class->float($_[0]) || $class->integer($_[0])
l!l&&r!l&&!r
4106
MC/DC100%2/2
$class->float($_[0]) || $class->integer($_[0])
$class->float($_[0]) $class->integer($_[0])
Truth table100%3/3
A || B
A= $class->float($_[0])B= $class->integer($_[0])
ABresult
1X1
011
000
200 }
201
202 37 ▶sub float {
Subroutine float: called CC=1 SCAR=0.0
Pod documented
203 37 my $class = shift;
204 37 return $_[0] =~ /^\d*\.\d*$/;
205 }
206
207 29 ▶sub integer {
Subroutine integer: called CC=1 SCAR=0.0
Pod documented
208 29 my $class = shift;
209 29 return $_[0] =~ /^\d+$/;
210 }
211
212 1 1;
213
214 __END__
↑ Summary