blib/lib/Function/Return.pm

filter: Statementstmt 81.5% 163 / 200 Branchbran 55.0% 65 / 118 Conditioncond 100% 4 / 4 MC/DCmcdc 100% 2 / 2 Subroutinesub 96.7% 30 / 31 Podpod 100% 3 / 3 totaltotal 74.5% 267 / 358 CC 114 SCAR 23.0
CC
114
Cov
72%
wrap_sub
18.1
case_multi_int_undef
16.7
case_multi_int_undef_few
16.7
CRAP 9.9
↑ Summary
1 package Function::Return;
2  
3 14 ▶use v5.14.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 14 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 14 our $VERSION = "0.15";
7  
8 14 ▶use Attribute::Handlers;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 14 ▶use B::Hooks::EndOfScope;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 14 ▶use Scope::Upper ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 14 ▶use Sub::Meta;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 14 ▶use Sub::Meta::Library;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 14 ▶use Sub::Meta::Finder::FunctionParameters;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 14 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 14 my @RETURN_ARGS;
18 my %NO_CHECK;
19  
20 20 ▶sub import {
Subroutine import: called CC=3 SCAR=11.0
21 20 my $class = shift;
22 20 my %args = @_;
23  
24 20 ▶ my $pkg = $args{pkg} ? $args{pkg} : scalar caller;
Branch: 1 branch
BranchTrueFalseTotal
$args{'pkg'} ? : 1 19 20
25 20 ▶ $NO_CHECK{$pkg} = !!$args{no_check} if exists $args{no_check};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"no_check"} 3 17 20
26  
27 {
28 # allow importing package to use attribute
29 14 ▶ no strict qw(refs);
Subroutine BEGIN: called CC=1 SCAR=0.0
30 20 my $MODIFY_CODE_ATTRIBUTES = \&Attribute::Handlers::UNIVERSAL::MODIFY_CODE_ATTRIBUTES;
31 20 *{"${pkg}::MODIFY_CODE_ATTRIBUTES"} = $MODIFY_CODE_ATTRIBUTES;
32 20 *{"${pkg}::_ATTR_CODE_Return"} = $class->can('Return');
33 }
34  
35 #
36 # How to install meta information
37 # 1. At the BEGIN phase, write down the meta information via the `Return` attribute.
38 # 2. At the compile phase, install the meta information in bulk via this `import` subroutine.
39 #
40 # In short,
41 # once Function::Return#import is compiled, the meta-information can be retrieved.
42 #
43 # The Reason Why?
44 #
45 # First NG CASE:
46 # At the **CHECK** phase, write down the meta information via the Return attribute. (Attribute::Handler's default case)
47 # Then, cannot support lazy load.
48 # Ref: case_lazy_load.t
49 #
50 # Second NG CASE:
51 # At the compile phase, install the meta information in **each** via this **Return** attribute.
52 # Then, unable to retrieve meta information for Function::Return from places that are compiled before the Return attribute.
53 # Ref: case_load_and_get_meta.t
54 #
55 20 ▶ on_scope_end {
Subroutine __ANON__: called CC=5 SCAR=16.1
56 20 ▶ while (my $args = shift @RETURN_ARGS) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $args = shift @RETURN_ARGS) 36 20 56
57 36 my ($pkg, $sub, $types) = @$args;
58 36 ▶ my $no_check = exists $NO_CHECK{$pkg} ? $NO_CHECK{$pkg} : ($ENV{FUNCTION_RETURN_NO_CHECK}//0);
Branch: 1 branch
BranchTrueFalseTotal
exists $NO_CHECK{$pkg} ? : 9 27 36
Condition100%2/2
$ENV{"FUNCTION_RETURN_NO_CHECK"} // 0
l!l
225
MC/DC100%1/1
$ENV{"FUNCTION_RETURN_NO_CHECK"} // 0
$ENV{"FUNCTION_RETURN_NO_CHECK"}
Truth table100%2/2
A
A= $ENV{"FUNCTION_RETURN_NO_CHECK"}
Aresult
11
00
59  
60 36 ▶ if ($no_check) {
Branch: 1 branch
BranchTrueFalseTotal
if ($no_check) { } 9 27 36
61 9 $class->_register_submeta($pkg, $sub, $types);
62 }
63 else {
64 27 $class->_register_submeta_and_install($pkg, $sub, $types);
65 }
66 }
67 20 };
68  
69 20 return;
70 }
71  
72 36 ▶sub Return :ATTR(CODE,BEGIN) {
Subroutine Return: called CC=2 SCAR=6.9
Pod documented
73 36 my $class = __PACKAGE__;
74 36 my ($pkg, undef, $sub, undef, $types) = @_;
75 36 ▶ $types //= [];
Condition100%2/2
$types //= []
l!l
2610
MC/DC100%1/1
$types //= []
$types
Truth table100%2/2
A
A= $types
Aresult
11
00
76  
77 36 push @RETURN_ARGS => [$pkg, $sub, $types];
78 36 return;
79 14 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
80  
81 14 ▶sub meta {
Subroutine meta: called CC=1 SCAR=0.0
Pod documented
82 14 my ($sub) = @_;
83 14 Sub::Meta::Library->get($sub);
84 }
85  
86 44 ▶sub wrap_sub {
Subroutine wrap_sub: called CC=10 SCAR=29.0
Pod documented
87 44 my ($class, $sub, $types) = @_;
88  
89 44 my $meta = Sub::Meta->new(sub => $sub);
90 44 my $shortname = $meta->subname;
91  
92 { # check type
93 44 my $file = $meta->file;
94 44 my $line = $meta->line;
95 44 for my $type (@$types) {
96 46 for (qw/check get_message/) {
97 91 ▶ die "Invalid type: $type. require `$_` method at $file line $line.\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $type->can($_) 1 90 91
98 unless $type->can($_)
99 }
100 }
101 }
102  
103 43 my @src;
104 43 ▶ push @src => sprintf('_croak "Required list context in fun %s because of multiple return values function" if !wantarray;', $shortname) if @$types > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @$types > 1 13 30 43
105  
106 # force LIST context.
107 43 push @src => 'my @ret = &Scope::Upper::uplevel($sub, @_, &Scope::Upper::CALLER(0));';
108  
109 # return Empty List
110 43 ▶ push @src => 'return if !@ret;' if @$types == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @$types == 0 11 32 43
111  
112 # check count
113 43 ▶ push @src => sprintf(q|_croak qq!Too few return values for fun %s (expected %s, got @{[map { defined $_ ? $_ : 'undef' } @ret]})! if @ret < %d;|,
Branch: 1 branch
BranchTrueFalseTotal
if @$types > 0 32 11 43
114 $shortname, "@$types", scalar @$types) if @$types > 0;
115  
116 43 push @src => sprintf(q|_croak qq!Too many return values for fun %s (expected %s, got @{[map { defined $_ ? $_ : 'undef' } @ret]})! if @ret > %d;|,
117 $shortname, "@$types", scalar @$types);
118  
119 # type check
120 43 for my $i (0 .. $#$types) {
121 45 push @src => sprintf(q|_croak qq!Invalid return in fun %s: return %d: @{[$types->[%d]->get_message($ret[%d])]}! unless $types->[%d]->check($ret[%d]);|, $shortname, $i, $i, $i, $i,$i)
122 }
123  
124 43 ▶ push @src => 'return @ret;' if @$types > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @$types > 1 13 30 43
125 43 ▶ push @src => 'return $ret[0];' if @$types == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @$types == 1 19 24 43
126  
127 43 my $src = join "\n", @src;
128 0 ▶ my $code = eval "sub { $src }"; ## no critic
Subroutine case_empty_list: called CC=4 SCAR=18.2
Subroutine case_empty_list_few: called CC=6 SCAR=23.9
Subroutine case_empty_type: called CC=4 SCAR=18.2
Subroutine case_int: called CC=6 SCAR=23.9
Subroutine case_invalid_int: called CC=6 SCAR=23.9
Subroutine case_multi_attributes: not called CC=4 SCAR=18.2
Subroutine case_multi_int_undef: called CC=8 SCAR=28.2
Subroutine case_multi_int_undef_few: called CC=8 SCAR=28.2
Subroutine case_multi_int_undef_many: called CC=8 SCAR=28.2
Branch: 45 branches
BranchTrueFalseTotal
unless @ret 2 4 6
if @ret > 0 1 0 1
defined $_ ? : 1 3 4
if @ret < 1 1 0 1
defined $_ ? : 2 1 3
if @ret > 1 1 3 4
defined $_ ? : 0 0 0
unless $types->[0]->check($ret[0]) 0 3 3
unless @ret 0 1 1
if @ret > 0 2 2 4
defined $_ ? : 1 0 1
if @ret < 1 0 1 1
defined $_ ? : 0 0 0
if @ret > 1 0 1 1
defined $_ ? : 0 0 0
unless $types->[0]->check($ret[0]) 0 1 1
if @ret < 1 0 1 1
defined $_ ? : 0 0 0
if @ret > 1 0 1 1
defined $_ ? : 0 0 0
unless $types->[0]->check($ret[0]) 1 0 1
unless @ret 0 0 0
if @ret > 0 0 0 0
defined $_ ? : 0 0 0
unless wantarray 2 1 3
if @ret < 2 0 1 1
defined $_ ? : 0 0 0
if @ret > 2 0 1 1
defined $_ ? : 0 0 0
unless $types->[0]->check($ret[0]) 0 1 1
unless $types->[1]->check($ret[1]) 0 1 1
unless wantarray 2 1 3
if @ret < 2 1 0 1
defined $_ ? : 1 0 1
if @ret > 2 0 0 0
defined $_ ? : 0 0 0
unless $types->[0]->check($ret[0]) 0 0 0
unless $types->[1]->check($ret[1]) 0 0 0
unless wantarray 2 1 3
if @ret < 2 0 1 1
defined $_ ? : 0 0 0
if @ret > 2 1 0 1
defined $_ ? : 2 1 3
unless $types->[0]->check($ret[0]) 0 0 0
unless $types->[1]->check($ret[1]) 0 0 0
129 43 return $code;
130 }
131  
132 28 ▶sub _croak {
Subroutine _croak: called CC=1 SCAR=0.0
133 28 my (undef, $file, $line) = caller 1;
134 28 die @_, " at $file line $line.\n"
135 }
136  
137 9 ▶sub _register_submeta {
Subroutine _register_submeta: called CC=2 SCAR=6.9
138 9 my ($class, $pkg, $sub, $types) = @_;
139  
140 9 my $meta = Sub::Meta->new(sub => $sub, stashname => $pkg);
141 9 $meta->set_returns(_normalize_types($types));
142  
143 9 ▶ if (my $materials = Sub::Meta::Finder::FunctionParameters::find_materials($sub)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $materials = Sub::Meta::Finder::FunctionParameters::find_materials($sub)) 2 7 9
144 2 $meta->set_is_method($materials->{is_method});
145 2 $meta->set_parameters($materials->{parameters});
146 }
147  
148 9 Sub::Meta::Library->register($sub, $meta);
149 9 return;
150 }
151  
152 27 ▶sub _register_submeta_and_install {
Subroutine _register_submeta_and_install: called CC=2 SCAR=6.9
153 27 my ($class, $pkg, $sub, $types) = @_;
154  
155 27 my $original_meta = Sub::Meta->new(sub => $sub);
156 27 my $wrapped = $class->wrap_sub($sub, $types);
157  
158 27 my $meta = Sub::Meta->new(sub => $wrapped, stashname => $pkg);
159 27 $meta->set_returns(_normalize_types($types));
160  
161 27 ▶ if (my $materials = Sub::Meta::Finder::FunctionParameters::find_materials($sub)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $materials = Sub::Meta::Finder::FunctionParameters::find_materials($sub)) 4 23 27
162 4 $meta->set_is_method($materials->{is_method});
163 4 $meta->set_parameters($materials->{parameters});
164 }
165  
166 27 $meta->apply_meta($original_meta);
167 27 Sub::Meta::Library->register($wrapped, $meta);
168  
169 {
170 14 ▶ no strict qw(refs);
Subroutine BEGIN: called CC=1 SCAR=0.0
171 14 ▶ no warnings qw(redefine);
Subroutine BEGIN: called CC=1 SCAR=0.0
172 27 *{$meta->fullname} = $wrapped;
173 }
174 27 return;
175 }
176  
177 36 ▶sub _normalize_types {
Subroutine _normalize_types: called CC=2 SCAR=6.9
178 36 my $types = shift;
179 36 ▶ if (@$types == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$types == 1) { } 21 15 36
180 21 return $types->[0];
181 }
182 else {
183 15 return $types;
184 }
185 }
186  
187 14 1;
188 __END__
↑ Summary