blib/lib/JIP/ClassField.pm

filter: Statementstmt 98.9% 95 / 96 Branchbran 96.6% 29 / 30 Conditioncond 88.8% 8 / 9 MC/DCmcdc 83.3% 5 / 6 Subroutinesub 94.7% 18 / 19 Podpod 0.0% 0 / 3 totaltotal 95.0% 155 / 163 CC 20 SCAR 15.6
CC
20
Cov
98%
attr
11.0
_define_name_of_getter
4.0
_define_name_of_setter
4.0
CRAP 4.7
↑ Summary
1 package JIP::ClassField;
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 English qw(-no_match_vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 our $VERSION = '0.051';
10  
11 0 ▶my $maybe_set_subname = sub { $ARG[1]; };
Subroutine __ANON__: not called CC=1 SCAR=1.2
12  
13 # Supported on Perl 5.22+
14 1 eval {
15 1 require Sub::Util;
16  
17 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
18 1 $maybe_set_subname = $set_subname;
19 }
20 };
21  
22 17 ▶sub attr {
Subroutine attr: called CC=11 SCAR=24.0
Pod undocumented
23 17 my ($self, $attr, %param) = @ARG;
24  
25 17 ▶ my $class = ref $self || $self;
Condition66%2/3
ref $self || $self
l!l&&r!l&&!r
0152
MC/DC50%1/2
ref $self || $self
ref $self $self
Truth table66%2/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
26  
27 17 ▶ croak q{Class not defined}
Branch: 1 branch
BranchTrueFalseTotal
unless defined $class and length $class 2 15 17
Condition100%3/3
defined $class and length $class
!ll&&!rl&&r
1115
MC/DC100%2/2
defined $class and length $class
defined $class length $class
Truth table100%3/3
A and B
A= defined $classB= length $class
ABresult
0X0
100
111
28 unless defined $class && length $class;
29  
30 15 ▶ croak q{Attribute not defined}
Branch: 1 branch
BranchTrueFalseTotal
unless defined $attr and length $attr 2 13 15
Condition100%3/3
defined $attr and length $attr
!ll&&!rl&&r
1113
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
31 unless defined $attr && length $attr;
32  
33 13 my @patches;
34  
35 13 ▶ for my $each_attr (@{ ref $attr eq 'ARRAY' ? $attr : [$attr] }) {
Branch: 1 branch
BranchTrueFalseTotal
ref $attr eq 'ARRAY' ? : 1 12 13
36 14 ▶ croak sprintf(q{Attribute "%s" invalid}, $each_attr)
Branch: 1 branch
BranchTrueFalseTotal
unless $each_attr =~ /^[a-zA-Z_]\w*$/x 1 13 14
37 unless $each_attr =~ m{^[a-zA-Z_]\w*$}x;
38  
39 13 my %patch;
40  
41 11 ▶ $patch{_define_name_of_getter($each_attr, \%param)} = sub {
Subroutine attr_2: called CC=1 SCAR=0.0
42 11 my $self = shift;
43 11 return $self->{$each_attr};
44 13 };
45  
46 {
47 13 my $method_name = _define_name_of_setter($each_attr, \%param);
48  
49 13 ▶ if (exists $param{'default'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $param{'default'}) { } 2 11 13
50 2 my $default_value = $param{'default'};
51  
52 6 ▶ $patch{$method_name} = sub {
Subroutine set_attr_6: called CC=3 SCAR=11.0
53 6 my $self = shift;
54  
55 6 ▶ if (@ARG == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@JIP::ClassField::ARG == 1) { } 3 3 6
56 3 $self->{$each_attr} = shift;
57 }
58 else {
59 3 ▶ $self->{$each_attr} = ref($default_value) eq 'CODE' ?
Branch: 1 branch
BranchTrueFalseTotal
ref $default_value eq 'CODE' ? : 1 2 3
60 $default_value->($self) : $default_value;
61 }
62  
63 6 return $self;
64 2 };
65 }
66 else {
67 5 ▶ $patch{$method_name} = sub {
Subroutine set_answer: called CC=1 SCAR=0.0
68 5 my ($self, $value) = @ARG;
69 5 $self->{$each_attr} = $value;
70 5 return $self;
71 11 };
72 }
73 }
74  
75 13 push @patches, \%patch;
76 }
77  
78 12 monkey_patch($class, %{ $_ }) for @patches;
79 }
80  
81 15 ▶sub monkey_patch {
Subroutine monkey_patch: called CC=2 SCAR=6.9
Pod undocumented
82 15 my ($class, %patch) = @ARG;
83  
84 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
85 1 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
86  
87 15 ▶ while (my ($method_name, $value) = each %patch) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %patch) 28 15 43
88 28 my $full_name = $class .q{::}. $method_name;
89  
90 28 *{$full_name} = $maybe_set_subname->($full_name, $value);
91 }
92  
93 15 return 1;
94 }
95  
96 1 ▶sub cleanup_namespace {
Subroutine cleanup_namespace: called CC=1 SCAR=0.0
Pod undocumented
97 1 my @names = @ARG;
98 1 my $caller = caller;
99  
100 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
101 1 my $ref = \%{ $caller .'::' };
102  
103 1 map { delete $ref->{$_} } @names;
104  
105 1 return 1;
106 }
107  
108 2 ▶sub import {
Subroutine import: called CC=1 SCAR=0.0
109 2 my $caller = caller;
110  
111 2 ▶ return monkey_patch($caller, 'has', sub { attr($caller, @ARG) });
Subroutine has: called CC=1 SCAR=0.0
112 }
113  
114 17 ▶sub _define_name_of_getter {
Subroutine _define_name_of_getter: called CC=4 SCAR=13.9
115 17 my ($attr, $param) = @ARG;
116  
117 17 my $method_name;
118  
119 17 ▶ if (exists $param->{'get'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $param->{'get'}) { } 15 2 17
120 15 my $getter = $param->{'get'};
121  
122 15 ▶ if ($getter eq q{+}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($getter eq '+') { } 10 5 15
elsif ($getter eq '-') { } 3 2 5
123 10 $method_name = $attr;
124 }
125 elsif ($getter eq q{-}) {
126 3 $method_name = q{_}. $attr;
127 }
128 else {
129 2 $method_name = $getter;
130 }
131 }
132 else {
133 2 $method_name = $attr;
134 }
135  
136 17 return $method_name;
137 }
138  
139 17 ▶sub _define_name_of_setter {
Subroutine _define_name_of_setter: called CC=4 SCAR=13.9
140 17 my ($attr, $param) = @ARG;
141  
142 17 my $method_name;
143  
144 17 ▶ if (exists $param->{'set'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $param->{'set'}) { } 15 2 17
145 15 my $setter = $param->{'set'};
146  
147 15 ▶ if ($setter eq q{+}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($setter eq '+') { } 10 5 15
elsif ($setter eq '-') { } 3 2 5
148 10 $method_name = q{set_}. $attr;
149 }
150 elsif ($setter eq q{-}) {
151 3 $method_name = q{_set_}. $attr;
152 }
153 else {
154 2 $method_name = $setter;
155 }
156 }
157 else {
158 2 $method_name = q{set_}. $attr;
159 }
160  
161 17 return $method_name;
162 }
163  
164 1 1;
165  
166 __END__
↑ Summary