Accessor.pm

filter: Statementstmt 95.5% 130 / 136 Branchbran 72.9% 35 / 48 Conditioncond 60.5% 23 / 38 MC/DCmcdc 40.0% 10 / 25 Subroutinesub 100% 26 / 26 Podpod 85.7% 12 / 14 totaltotal 82.2% 236 / 287 CC 39 SCAR 23.2
CC
39
Cov
85%
_mk_accessors
24.4
get
4.9
new
3.2
CRAP 10.2
↑ Summary Accessor/Faster.pm »
1 package Class::Accessor;
2 6 require 5.00502;
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 $Class::Accessor::VERSION = '0.51';
5  
6 11 ▶sub new {
Subroutine new: called CC=3 SCAR=11.7
Pod documented
7 return bless
8 defined $_[1]
9 11 ▶ ? {%{$_[1]}} # make a copy of $fields.
Branch: 1 branch
BranchTrueFalseTotal
defined $_[1] ? : 8 3 11
Condition33%1/3
ref $_[0] || $_[0]
l!l&&r!l&&!r
0110
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0] $_[0]
Truth table33%1/3
A || B
A= ref $_[0]B= $_[0]
ABresult
1X1
011
000
10 : {},
11 ref $_[0] || $_[0];
12 }
13  
14 13 ▶sub mk_accessors {
Subroutine mk_accessors: called CC=1 SCAR=0.0
Pod documented
15 13 my($self, @fields) = @_;
16  
17 13 $self->_mk_accessors('rw', @fields);
18 }
19  
20 6 ▶if (eval { require Sub::Name }) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { require Sub::Name } }) 0 6 6
21 0 Sub::Name->import;
22 }
23  
24 {
25 6 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 2 ▶ sub import {
Subroutine import: called CC=3 SCAR=11.0
28 2 my ($class, @what) = @_;
29 2 my $caller = caller;
30 2 for (@what) {
31 1 ▶ if (/^(?:antlers|moose-?like)$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^(?:antlers|moose-?like)$/i) 1 0 1
32 1 ▶ *{"${caller}::has"} = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
33 5 my ($f, %args) = @_;
34 5 ▶ $caller->_mk_accessors(($args{is}||"rw"), $f);
Condition100%2/2
$args{'is'} || 'rw'
l!l
41
MC/DC100%1/1
$args{'is'} || 'rw'
$args{'is'}
Truth table100%2/2
A
A= $args{'is'}
Aresult
11
00
35 1 };
36 1 ▶ *{"${caller}::extends"} = sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
37 1 @{"${caller}::ISA"} = @_;
38 1 ▶ unless (grep $_->can("_mk_accessors"), @_) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep $_->can("_mk_accessors"), @_) 1 0 1
39 1 push @{"${caller}::ISA"}, $class;
40 }
41 1 };
42 # we'll use their @ISA as a default, in case it happens to be
43 # set already
44 1 &{"${caller}::extends"}(@{"${caller}::ISA"});
45 }
46 }
47 }
48  
49 3 ▶ sub follow_best_practice {
Subroutine follow_best_practice: called CC=2 SCAR=7.1
Pod documented
50 3 my($self) = @_;
51 3 ▶ my $class = ref $self || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
030
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
52 3 *{"${class}::accessor_name_for"} = \&best_practice_accessor_name_for;
53 3 *{"${class}::mutator_name_for"} = \&best_practice_mutator_name_for;
54 }
55  
56 38 ▶ sub _mk_accessors {
Subroutine _mk_accessors: called CC=23 SCAR=32.0
57 38 my($self, $access, @fields) = @_;
58 38 ▶ my $class = ref $self || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
0380
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
59 38 ▶ my $ra = $access eq 'rw' || $access eq 'ro';
Condition100%3/3
$access eq 'rw' || $access eq 'ro'
l!l&&r!l&&!r
161111
MC/DC100%2/2
$access eq 'rw' || $access eq 'ro'
$access eq 'rw' $access eq 'ro'
Truth table100%3/3
A || B
A= $access eq 'rw'B= $access eq 'ro'
ABresult
1X1
011
000
60 38 ▶ my $wa = $access eq 'rw' || $access eq 'wo';
Condition100%3/3
$access eq 'rw' || $access eq 'wo'
l!l&&r!l&&!r
161111
MC/DC100%2/2
$access eq 'rw' || $access eq 'wo'
$access eq 'rw' $access eq 'wo'
Truth table100%3/3
A || B
A= $access eq 'rw'B= $access eq 'wo'
ABresult
1X1
011
000
61  
62 38 foreach my $field (@fields) {
63 56 my $accessor_name = $self->accessor_name_for($field);
64 56 my $mutator_name = $self->mutator_name_for($field);
65 56 ▶ if( $accessor_name eq 'DESTROY' or $mutator_name eq 'DESTROY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($accessor_name eq "DESTROY" or $mutator_name eq "DESTROY") 4 52 56
Condition66%2/3
$accessor_name eq "DESTROY" or $mutator_name eq "DESTROY"
l!l&&r!l&&!r
4052
MC/DC50%1/2
$accessor_name eq "DESTROY" or $mutator_name eq "DESTROY"
$accessor_name eq "DESTROY" $mutator_name eq "DESTROY"
Truth table66%2/3
A or B
A= $accessor_name eq "DESTROY"B= $mutator_name eq "DESTROY"
ABresult
1X1
011
000
66 4 $self->_carp("Having a data accessor named DESTROY in '$class' is unwise.");
67 }
68 56 ▶ if ($accessor_name eq $mutator_name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($accessor_name eq $mutator_name) { } 38 18 56
69 38 my $accessor;
70 38 ▶ if ($ra && $wa) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ra and $wa) { } 22 16 38
elsif ($ra) { } 8 8 16
Condition100%3/3
$ra and $wa
!ll&&!rl&&r
8822
MC/DC100%2/2
$ra and $wa
$ra $wa
Truth table100%3/3
A and B
A= $raB= $wa
ABresult
0X0
100
111
71 22 $accessor = $self->make_accessor($field);
72 } elsif ($ra) {
73 8 $accessor = $self->make_ro_accessor($field);
74 } else {
75 8 $accessor = $self->make_wo_accessor($field);
76 }
77 38 my $fullname = "${class}::$accessor_name";
78 38 my $subnamed = 0;
79 38 ▶ unless (defined &{$fullname}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined &{$fullname;}) 32 6 38
80 32 ▶ subname($fullname, $accessor) if defined &subname;
Branch: 1 branch
BranchTrueFalseTotal
if defined &Class::Accessor::subname 0 32 32
81 32 $subnamed = 1;
82 32 *{$fullname} = $accessor;
83 }
84 38 ▶ if ($accessor_name eq $field) {
Branch: 1 branch
BranchTrueFalseTotal
if ($accessor_name eq $field) 38 0 38
85 # the old behaviour
86 38 my $alias = "${class}::_${field}_accessor";
87 38 ▶ subname($alias, $accessor) if defined &subname and not $subnamed;
Branch: 1 branch
BranchTrueFalseTotal
if defined &Class::Accessor::subname and not $subnamed 0 38 38
Condition33%1/3
defined &Class::Accessor::subname and not $subnamed
!ll&&!rl&&r
3800
MC/DC0%0/2
defined &Class::Accessor::subname and not $subnamed
defined &Class::Accessor::subname not $subnamed
Truth table33%1/3
A and not(B)
A= defined &Class::Accessor::subnameB= not $subnamed
ABresult
0X0
100
111
88 38 ▶ *{$alias} = $accessor unless defined &{$alias};
Branch: 1 branch
BranchTrueFalseTotal
unless defined &{$alias;} 38 0 38
89 }
90 } else {
91 18 my $fullaccname = "${class}::$accessor_name";
92 18 my $fullmutname = "${class}::$mutator_name";
93 18 ▶ if ($ra and not defined &{$fullaccname}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ra and not defined &{$fullaccname;}) 12 6 18
Condition66%2/3
$ra and not defined &{$fullaccname;}
!ll&&!rl&&r
6012
MC/DC50%1/2
$ra and not defined &{$fullaccname;}
$ra not defined &{$fullaccname;}
Truth table66%2/3
A and not(B)
A= $raB= not defined &{$fullaccname;}
ABresult
0X0
100
111
94 12 my $accessor = $self->make_ro_accessor($field);
95 12 ▶ subname($fullaccname, $accessor) if defined &subname;
Branch: 1 branch
BranchTrueFalseTotal
if defined &Class::Accessor::subname 0 12 12
96 12 *{$fullaccname} = $accessor;
97 }
98 18 ▶ if ($wa and not defined &{$fullmutname}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wa and not defined &{$fullmutname;}) 12 6 18
Condition66%2/3
$wa and not defined &{$fullmutname;}
!ll&&!rl&&r
6012
MC/DC50%1/2
$wa and not defined &{$fullmutname;}
$wa not defined &{$fullmutname;}
Truth table66%2/3
A and not(B)
A= $waB= not defined &{$fullmutname;}
ABresult
0X0
100
111
99 12 my $mutator = $self->make_wo_accessor($field);
100 12 ▶ subname($fullmutname, $mutator) if defined &subname;
Branch: 1 branch
BranchTrueFalseTotal
if defined &Class::Accessor::subname 0 12 12
101 12 *{$fullmutname} = $mutator;
102 }
103 }
104 }
105 }
106  
107 }
108  
109 6 ▶sub mk_ro_accessors {
Subroutine mk_ro_accessors: called CC=1 SCAR=0.0
Pod documented
110 10 my($self, @fields) = @_;
111  
112 10 $self->_mk_accessors('ro', @fields);
113 }
114  
115 10 ▶sub mk_wo_accessors {
Subroutine mk_wo_accessors: called CC=1 SCAR=0.0
Pod documented
116 10 my($self, @fields) = @_;
117  
118 10 $self->_mk_accessors('wo', @fields);
119 }
120  
121 9 ▶sub best_practice_accessor_name_for {
Subroutine best_practice_accessor_name_for: called CC=1 SCAR=0.0
Pod undocumented
122 9 my ($class, $field) = @_;
123 9 return "get_$field";
124 }
125  
126 9 ▶sub best_practice_mutator_name_for {
Subroutine best_practice_mutator_name_for: called CC=1 SCAR=0.0
Pod undocumented
127 9 my ($class, $field) = @_;
128 9 return "set_$field";
129 }
130  
131 38 ▶sub accessor_name_for {
Subroutine accessor_name_for: called CC=1 SCAR=0.0
Pod documented
132 38 my ($class, $field) = @_;
133 38 return $field;
134 }
135  
136 38 ▶sub mutator_name_for {
Subroutine mutator_name_for: called CC=1 SCAR=0.0
Pod documented
137 38 my ($class, $field) = @_;
138 38 return $field;
139 }
140  
141 11 ▶sub set {
Subroutine set: called CC=3 SCAR=11.3
Pod documented
142 11 my($self, $key) = splice(@_, 0, 2);
143  
144 11 ▶ if(@_ == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 1) { } 9 2 11
elsif (@_ > 1) { } 2 0 2
145 9 $self->{$key} = $_[0];
146 }
147 elsif(@_ > 1) {
148 2 $self->{$key} = [@_];
149 }
150 else {
151 0 $self->_croak("Wrong number of arguments received");
152 }
153 }
154  
155 19 ▶sub get {
Subroutine get: called CC=3 SCAR=16.0
Pod documented
156 19 my $self = shift;
157  
158 19 ▶ if(@_ == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 1) { } 19 0 19
elsif (@_ > 1) { } 0 0 0
159 19 return $self->{$_[0]};
160 }
161 elsif( @_ > 1 ) {
162 0 return @{$self}{@_};
163 }
164 else {
165 0 $self->_croak("Wrong number of arguments received");
166 }
167 }
168  
169 10 ▶sub make_accessor {
Subroutine make_accessor: called CC=1 SCAR=0.0
Pod documented
170 10 my ($class, $field) = @_;
171  
172 16 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
173 16 my $self = shift;
174  
175 16 ▶ if(@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 5 11 16
176 5 return $self->set($field, @_);
177 } else {
178 11 return $self->get($field);
179 }
180 10 };
181 }
182  
183 7 ▶sub make_ro_accessor {
Subroutine make_ro_accessor: called CC=1 SCAR=0.0
Pod documented
184 7 my($class, $field) = @_;
185  
186 8 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
187 8 my $self = shift;
188  
189 8 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 2 6 8
190 2 my $caller = caller;
191 2 $self->_croak("'$caller' cannot alter the value of '$field' on objects of class '$class'");
192 }
193 else {
194 6 return $self->get($field);
195 }
196 7 };
197 }
198  
199 7 ▶sub make_wo_accessor {
Subroutine make_wo_accessor: called CC=1 SCAR=0.0
Pod documented
200 7 my($class, $field) = @_;
201  
202 7 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
203 7 my $self = shift;
204  
205 7 ▶ unless (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 5 2 7
206 2 my $caller = caller;
207 2 $self->_croak("'$caller' cannot access the value of '$field' on objects of class '$class'");
208 }
209 else {
210 5 return $self->set($field, @_);
211 }
212 7 };
213 }
214  
215  
216 6 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
217  
218 4 ▶sub _carp {
Subroutine _carp: called CC=2 SCAR=7.6
219 4 my ($self, $msg) = @_;
220 4 ▶ Carp::carp($msg || $self);
Condition33%1/3
$msg || $self
l!l&&r!l&&!r
400
MC/DC0%0/2
$msg || $self
$msg $self
Truth table33%1/3
A || B
A= $msgB= $self
ABresult
1X1
011
000
221 4 return;
222 }
223  
224 8 ▶sub _croak {
Subroutine _croak: called CC=2 SCAR=9.2
225 8 my ($self, $msg) = @_;
226 8 ▶ Carp::croak($msg || $self);
Condition33%1/3
$msg || $self
l!l&&r!l&&!r
800
MC/DC0%0/2
$msg || $self
$msg $self
Truth table33%1/3
A || B
A= $msgB= $self
ABresult
1X1
011
000
227 0 return;
228 }
229  
230 6 1;
231  
232 __END__
↑ Summary Accessor/Faster.pm »