blib/lib/JIP/Object.pm

filter: Statementstmt 99.2% 133 / 134 Branchbran 98.1% 53 / 54 Conditioncond 100% 8 / 8 MC/DCmcdc 100% 5 / 5 Subroutinesub 93.7% 30 / 32 Podpod 60.0% 6 / 10 totaltotal 96.7% 235 / 243 CC 32 SCAR 13.7
CC
32
Cov
99%
has
10.0
method
6.0
new
5.0
CRAP 4.0
↑ Summary
1 package JIP::Object;
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 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use English qw(-no_match_vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 our $VERSION = '0.031';
11 1 our $AUTOLOAD;
12  
13 0 ▶my $maybe_set_subname = sub { $ARG[1]; };
Subroutine __ANON__: not called CC=1 SCAR=1.2
14  
15 # Supported on Perl 5.22+
16 1 eval {
17 1 require Sub::Util;
18  
19 1 ▶ if (my $set_subname = Sub::Util->can('set_subname')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $set_subname = "Sub::Util"->can("set_subname")) 1 0 1
20 1 $maybe_set_subname = $set_subname;
21 }
22 };
23  
24 12 ▶sub new {
Subroutine new: called CC=5 SCAR=16.1
Pod documented
25 12 my ($class, %param) = @ARG;
26  
27 12 ▶ croak q{Class already blessed} if blessed $class;
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($class) 1 11 12
28  
29 11 my $proto;
30 11 ▶ if (exists $param{'proto'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $param{"proto"}) 2 9 11
31 2 $proto = $param{'proto'};
32  
33 2 ▶ croak q{Bad argument "proto"}
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($proto) || "") eq __PACKAGE__ 1 1 2
Condition100%2/2
builtin::blessed($proto) || ''
l!l
11
MC/DC100%1/1
builtin::blessed($proto) || ''
builtin::blessed($proto)
Truth table100%2/2
A
A= builtin::blessed($proto)
Aresult
11
00
34 unless (blessed $proto || q{}) eq __PACKAGE__;
35 }
36  
37 10 return bless({}, $class)
38 ->_set_stash({})
39 ->_set_meta({})
40 ->set_proto($proto);
41 }
42  
43 13 ▶sub has {
Subroutine has: called CC=10 SCAR=23.0
Pod documented
44 13 my ($self, $attr, %param) = @ARG;
45  
46 13 ▶ croak q{Can't call "has" as a class method} unless blessed $self;
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($self) 1 12 13
47  
48 12 ▶ croak q{Attribute not defined} unless defined $attr and length $attr;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $attr and length $attr 2 10 12
Condition100%3/3
defined $attr and length $attr
!ll&&!rl&&r
1110
MC/DC100%2/2
defined $attr and length $attr
defined $attr length $attr
Truth table100%3/3
A and B
A= defined $attrB= length $attr
ABresult
0X0
100
111
49  
50 10 my @patches;
51  
52 10 ▶ for my $each_attr (@{ ref $attr eq 'ARRAY' ? $attr : [$attr] }) {
Branch: 1 branch
BranchTrueFalseTotal
ref $attr eq 'ARRAY' ? : 1 9 10
53 11 ▶ croak sprintf(q{Attribute "%s" invalid}, $each_attr)
Branch: 1 branch
BranchTrueFalseTotal
unless $each_attr =~ /^[a-zA-Z_]\w*$/x 1 10 11
54 unless $each_attr =~ m{^[a-zA-Z_]\w*$}x;
55  
56 10 my %patch;
57  
58 # getter
59 15 ▶ $patch{_define_name_of_getter($each_attr, \%param)} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
60 15 my $self = shift;
61 15 return $self->_stash->{$attr};
62 10 };
63  
64 # setter
65 {
66 10 my $method_name = _define_name_of_setter($each_attr, \%param);
67  
68 10 ▶ if (exists $param{'default'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $param{'default'}) { } 2 8 10
69 2 my $default_value = $param{'default'};
70  
71 6 ▶ $patch{$method_name} = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
72 6 my $self = shift;
73  
74 6 ▶ if (@ARG == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@JIP::Object::ARG == 1) { } 4 2 6
elsif (ref $default_value eq 'CODE') { } 1 1 2
75 4 $self->_stash->{$attr} = shift;
76 }
77 elsif (ref $default_value eq 'CODE') {
78 1 $self->_stash->{$attr} = $maybe_set_subname->(
79 'default_value',
80 $default_value,
81 )->($self);
82 }
83 else {
84 1 $self->_stash->{$attr} = $default_value;
85 }
86  
87 6 return $self;
88 2 };
89 }
90 else {
91 8 ▶ $patch{$method_name} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
92 8 my ($self, $value) = @ARG;
93 8 $self->_stash->{$attr} = $value;
94 8 return $self;
95 8 };
96 }
97 }
98  
99 10 push @patches, \%patch;
100 }
101  
102 9 for my $each_patch (@patches) {
103 10 ▶ while (my ($method_name, $code) = each %{ $each_patch }) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$each_patch;}) 20 10 30
104 20 $self->_meta->{$method_name} = $maybe_set_subname->($method_name, $code);
105 }
106 }
107  
108 9 return $self;
109 }
110  
111 10 ▶sub method {
Subroutine method: called CC=6 SCAR=17.9
Pod documented
112 10 my ($self, $method_name, $code) = @ARG;
113  
114 10 ▶ croak q{Can't call "method" as a class method}
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($self) 1 9 10
115 unless blessed $self;
116  
117 9 ▶ croak q{First argument must be a non empty string}
Branch: 1 branch
BranchTrueFalseTotal
unless defined $method_name and length $method_name 2 7 9
Condition100%3/3
defined $method_name and length $method_name
!ll&&!rl&&r
117
MC/DC100%2/2
defined $method_name and length $method_name
defined $method_name length $method_name
Truth table100%3/3
A and B
A= defined $method_nameB= length $method_name
ABresult
0X0
100
111
118 unless defined $method_name and length $method_name;
119  
120 7 ▶ croak sprintf(q{First argument "%s" invalid}, $method_name)
Branch: 1 branch
BranchTrueFalseTotal
unless $method_name =~ /^[a-zA-Z_]\w*$/x 1 6 7
121 unless $method_name =~ m{^[a-zA-Z_]\w*$}x;
122  
123 6 ▶ croak q{Second argument must be a code ref}
Branch: 1 branch
BranchTrueFalseTotal
unless ref $code eq "CODE" 1 5 6
124 unless ref($code) eq 'CODE';
125  
126 5 $self->_meta->{$method_name} = $maybe_set_subname->($method_name, $code);
127  
128 5 return $self;
129 }
130  
131 45 ▶sub own_method {
Subroutine own_method: called CC=2 SCAR=6.9
Pod documented
132 45 my ($self, $method_name) = @ARG;
133  
134 45 ▶ return unless exists $self->_meta->{$method_name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->_meta->{$method_name} 6 39 45
135  
136 39 return $self->_meta->{$method_name};
137 }
138  
139 # http://perldoc.perl.org/perlobj.html#Default-UNIVERSAL-methods
140 10 ▶sub isa {
Subroutine isa: called CC=1 SCAR=0.0
Pod undocumented
141 1 ▶ no warnings 'misc';
Subroutine BEGIN: called CC=1 SCAR=0.0
142 10 goto &UNIVERSAL::isa;
143 }
144  
145 3 ▶sub DOES {
Subroutine DOES: called CC=1 SCAR=0.0
Pod undocumented
146 # DOES is equivalent to isa by default
147 3 goto &isa;
148 }
149  
150 3 ▶sub VERSION {
Subroutine VERSION: called CC=1 SCAR=0.0
Pod undocumented
151 1 ▶ no warnings 'misc';
Subroutine BEGIN: called CC=1 SCAR=0.0
152 3 goto &UNIVERSAL::VERSION;
153 }
154  
155 17 ▶sub can {
Subroutine can: called CC=2 SCAR=6.9
Pod undocumented
156 17 my ($self, $method_name) = @ARG;
157  
158 17 ▶ if (blessed $self) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($self)) { } 10 7 17
159 1 ▶ no warnings 'misc';
Subroutine BEGIN: called CC=1 SCAR=0.0
160 10 goto &UNIVERSAL::can;
161 }
162 else {
163 7 my $code;
164 1 ▶ no warnings 'misc';
Subroutine BEGIN: called CC=1 SCAR=0.0
165 7 $code = UNIVERSAL::can($self, $method_name);
166  
167 7 return $code;
168 }
169 }
170  
171 0 ▶sub DESTROY {}
Subroutine DESTROY: not called CC=1 SCAR=0.0
172  
173 38 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=4 SCAR=13.9
174 38 my ($self) = @ARG;
175  
176 38 ▶ croak q{Can't call "AUTOLOAD" as a class method} unless blessed $self;
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($self) 1 37 38
177  
178 37 my ($package, $method_name) = ($AUTOLOAD =~ m{^(.+)::([^:]+)$}x);
179 37 undef $AUTOLOAD;
180  
181 37 ▶ if (defined(my $code = $self->own_method($method_name))) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined(my $code = $self->own_method($method_name))) { } 35 2 37
elsif (defined(my $proto = $self->proto)) { } 1 1 2
182 35 goto &$code;
183 }
184 elsif (defined(my $proto = $self->proto)) {
185 1 shift @ARG;
186 1 $proto->$method_name(@ARG);
187 }
188 else {
189 1 croak(sprintf q{Can't locate object method "%s" in this instance}, $method_name);
190 }
191 }
192  
193 4 ▶sub proto {
Subroutine proto: called CC=1 SCAR=0.0
Pod documented
194 4 return $ARG[0]->{'proto'};
195 }
196  
197 11 ▶sub set_proto {
Subroutine set_proto: called CC=1 SCAR=0.0
Pod documented
198 11 $ARG[0]->{'proto'} = $ARG[1];
199 11 return $ARG[0];
200 }
201  
202 # private methods
203 14 ▶sub _define_name_of_getter {
Subroutine _define_name_of_getter: called CC=4 SCAR=13.9
204 14 my ($attr, $param) = @ARG;
205  
206 14 my $method_name;
207  
208 14 ▶ if (exists $param->{'get'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $param->{'get'}) { } 13 1 14
209 13 my $getter = $param->{'get'};
210  
211 13 ▶ if ($getter eq q{+}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($getter eq '+') { } 8 5 13
elsif ($getter eq '-') { } 3 2 5
212 8 $method_name = $attr;
213 }
214 elsif ($getter eq q{-}) {
215 3 $method_name = q{_}. $attr;
216 }
217 else {
218 2 $method_name = $getter;
219 }
220 }
221 else {
222 1 $method_name = $attr;
223 }
224  
225 14 return $method_name;
226 }
227  
228 14 ▶sub _define_name_of_setter {
Subroutine _define_name_of_setter: called CC=4 SCAR=13.9
229 14 my ($attr, $param) = @ARG;
230  
231 14 my $method_name;
232  
233 14 ▶ if (exists $param->{'set'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $param->{'set'}) { } 13 1 14
234 13 my $setter = $param->{'set'};
235  
236 13 ▶ if ($setter eq q{+}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($setter eq '+') { } 8 5 13
elsif ($setter eq '-') { } 3 2 5
237 8 $method_name = q{set_}. $attr;
238 }
239 elsif ($setter eq q{-}) {
240 3 $method_name = q{_set_}. $attr;
241 }
242 else {
243 2 $method_name = $setter;
244 }
245 }
246 else {
247 1 $method_name = q{set_}. $attr;
248 }
249  
250 14 return $method_name;
251 }
252  
253 109 ▶sub _meta {
Subroutine _meta: called CC=1 SCAR=0.0
254 109 return $ARG[0]->{'meta'};
255 }
256 10 ▶sub _set_meta {
Subroutine _set_meta: called CC=1 SCAR=0.0
257 10 $ARG[0]->{'meta'} = $ARG[1];
258 10 return $ARG[0];
259 }
260  
261 29 ▶sub _stash {
Subroutine _stash: called CC=1 SCAR=0.0
262 29 return $ARG[0]->{'stash'};
263 }
264 10 ▶sub _set_stash {
Subroutine _set_stash: called CC=1 SCAR=0.0
265 10 $ARG[0]->{'stash'} = $ARG[1];
266 10 return $ARG[0];
267 }
268  
269 1 1;
270  
271 __END__
↑ Summary