blib/lib/Class/Tiny.pm

filter: Statementstmt 98.4% 124 / 126 Branchbran 80.3% 45 / 56 Conditioncond 67.5% 25 / 37 MC/DCmcdc 47.6% 10 / 21 Subroutinesub 95.8% 23 / 24 Podpod 0.0% 0 / 10 totaltotal 82.8% 227 / 274 CC 53 SCAR 14.8
CC
53
Cov
89%
create_attributes
9.1
new
9.1
__ANON__
5.0
CRAP 4.4
↑ Summary
1 10 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 9 ▶no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 package Class::Tiny;
7 # ABSTRACT: Minimalist class construction
8  
9 9 our $VERSION = '1.008';
10  
11 9 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 # load as .pm to hide from min version scanners
14 9 ▶require( $] >= 5.010 ? "mro.pm" : "MRO/Compat.pm" ); ## no critic:
Branch: 1 branch
BranchTrueFalseTotal
$] >= 5.01 ? : 9 0 9
15  
16 9 my %CLASS_ATTRIBUTES;
17  
18 16 ▶sub import {
Subroutine import: called CC=2 SCAR=6.9
19 16 my $class = shift;
20 16 my $pkg = caller;
21 16 $class->prepare_class($pkg);
22 16 ▶ $class->create_attributes( $pkg, @_ ) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 16 1 17
23 }
24  
25 16 ▶sub prepare_class {
Subroutine prepare_class: called CC=2 SCAR=6.9
Pod undocumented
26 16 my ( $class, $pkg ) = @_;
27 16 ▶ @{"${pkg}::ISA"} = "Class::Tiny::Object" unless @{"${pkg}::ISA"};
Branch: 1 branch
BranchTrueFalseTotal
unless @{"${pkg}::ISA";} 9 8 17
28 }
29  
30 # adapted from Object::Tiny and Object::Tiny::RW
31 16 ▶sub create_attributes {
Subroutine create_attributes: called CC=8 SCAR=22.1
Pod undocumented
32 16 my ( $class, $pkg, @spec ) = @_;
33 16 ▶ my %defaults = map { ref $_ eq 'HASH' ? %$_ : ( $_ => undef ) } @spec;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'HASH' ? : 5 26 31
34 my @attr = grep {
35 16 ▶ defined and !ref and /^[^\W\d]\w*$/s
Condition44%4/9
defined $_ && !ref($_) && /^[^\W\d]\w*$/s || Carp::croak("Invalid accessor name '${_}'")
l!l
3510
defined $_ and not ref $_ and /^[^\W\d]\w*$/s
!ll&&!rl&&r
0035
defined $_ and not ref $_
!ll&&!rl&&r
0035
MC/DC0%0/3
defined $_ && !ref($_) && /^[^\W\d]\w*$/s || Carp::croak("Invalid accessor name '${_}'")
defined $_ not ref $_ /^[^\W\d]\w*$/s
Truth table25%1/4
A and not(B) and C
A= defined $_B= not ref $_C= /^[^\W\d]\w*$/s
ABCresult
1111
0XX0
10X0
1100
36 or Carp::croak "Invalid accessor name '$_'"
37 } keys %defaults;
38 16 $CLASS_ATTRIBUTES{$pkg}{$_} = $defaults{$_} for @attr;
39 16 $class->_gen_accessor( $pkg, $_ ) for grep { !*{"$pkg\::$_"}{CODE} } @attr;
40 16 ▶ Carp::croak("Failed to generate attributes for $pkg: $@\n") if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 16 16
41 }
42  
43 33 ▶sub _gen_accessor {
Subroutine _gen_accessor: called CC=2 SCAR=7.4
44 33 my ( $class, $pkg, $name ) = @_;
45 33 my $outer_default = $CLASS_ATTRIBUTES{$pkg}{$name};
46  
47 33 my $sub =
48 $class->__gen_sub_body( $name, defined($outer_default), ref($outer_default) );
49  
50 # default = outer_default avoids "won't stay shared" bug
51 0 ▶ eval "package $pkg; my \$default=\$outer_default; $sub"; ## no critic
Subroutine bar: called CC=2 SCAR=7.5
Subroutine foo: called CC=2 SCAR=7.5
Subroutine baz: called CC=2 SCAR=7.5
Subroutine wibble: called CC=4 SCAR=15.0
Subroutine wobble: called CC=4 SCAR=15.0
Subroutine zag: not called CC=2 SCAR=7.5
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Branch: 8 branches
BranchTrueFalseTotal
@_ == 1 ? : 34 0 34
@_ == 1 ? : 46 3 49
@_ == 1 ? : 18 9 27
@_ == 1 && exists $_[0]{'wibble'} ? : 7 3 10
@_ == 2 ? : 1 2 3
@_ == 1 && exists $_[0]{'wobble'} ? : 0 2 2
@_ == 2 ? : 0 1 1
@_ == 1 ? : 0 1 1
Condition44%4/9
@_ == 1 and exists $_[0]{"wibble"}
!ll&&!rl&&r
020
@_ == 1 and exists $_[0]{"wobble"}
!ll&&!rl&&r
021
@_ == 1 and exists $_[0]{"wibble"}
!ll&&!rl&&r
010
MC/DC0%0/2
@_ == 1 and exists $_[0]{"wibble"}
@_ == 1 exists $_[0]{"wibble"}
MC/DC50%1/2
@_ == 1 and exists $_[0]{"wobble"}
@_ == 1 exists $_[0]{"wobble"}
MC/DC0%0/2
@_ == 1 and exists $_[0]{"wibble"}
@_ == 1 exists $_[0]{"wibble"}
Truth table33%1/3
A and B
A= @_ == 1B= exists $_[0]{"wibble"}
ABresult
0X0
100
111
Truth table66%2/3
A and B
A= @_ == 1B= exists $_[0]{"wobble"}
ABresult
0X0
100
111
Truth table33%1/3
A and B
A= @_ == 1B= exists $_[0]{"wibble"}
ABresult
0X0
100
111
52 33 ▶ Carp::croak("Failed to generate attributes for $pkg: $@\n") if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 33 33
53 }
54  
55 # NOTE: overriding __gen_sub_body in a subclass of Class::Tiny is risky and
56 # could break if the internals of Class::Tiny need to change for any
57 # reason. That said, I currently see no reason why this would be likely to
58 # change.
59 #
60 # The generated sub body should assume that a '$default' variable will be
61 # in scope (i.e. when the sub is evaluated) with any default value/coderef
62 33 ▶sub __gen_sub_body {
Subroutine __gen_sub_body: called CC=4 SCAR=13.9
63 33 my ( $self, $name, $has_default, $default_type ) = @_;
64  
65 33 ▶ if ( $has_default && $default_type eq 'CODE' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($has_default and $default_type eq 'CODE') { } 4 29 33
elsif ($has_default) { } 4 25 29
Condition100%3/3
$has_default and $default_type eq "CODE"
!ll&&!rl&&r
2545
MC/DC100%2/2
$has_default and $default_type eq "CODE"
$has_default $default_type eq "CODE"
Truth table100%3/3
A and B
A= $has_defaultB= $default_type eq "CODE"
ABresult
0X0
100
111
66 4 return << "HERE";
67 sub $name {
68 return (
69 ( \@_ == 1 && exists \$_[0]{$name} )
70 ? ( \$_[0]{$name} )
71 : ( \$_[0]{$name} = ( \@_ == 2 ) ? \$_[1] : \$default->( \$_[0] ) )
72 );
73 }
74 HERE
75 }
76 elsif ($has_default) {
77 4 return << "HERE";
78 sub $name {
79 return (
80 ( \@_ == 1 && exists \$_[0]{$name} )
81 ? ( \$_[0]{$name} )
82 : ( \$_[0]{$name} = ( \@_ == 2 ) ? \$_[1] : \$default )
83 );
84 }
85 HERE
86 }
87 else {
88 25 return << "HERE";
89 sub $name {
90 return \@_ == 1 ? \$_[0]{$name} : ( \$_[0]{$name} = \$_[1] );
91 }
92 HERE
93 }
94 }
95  
96 13 ▶sub get_all_attributes_for {
Subroutine get_all_attributes_for: called CC=2 SCAR=6.9
Pod undocumented
97 13 my ( $class, $pkg ) = @_;
98 my %attr =
99 54 map { $_ => undef }
100 13 ▶ map { keys %{ $CLASS_ATTRIBUTES{$_} || {} } } @{ mro::get_linear_isa($pkg) };
Condition100%2/2
$CLASS_ATTRIBUTES{$_} || {}
l!l
2413
MC/DC100%1/1
$CLASS_ATTRIBUTES{$_} || {}
$CLASS_ATTRIBUTES{$_}
Truth table100%2/2
A
A= $CLASS_ATTRIBUTES{$_}
Aresult
11
00
101 13 return keys %attr;
102 }
103  
104 3 ▶sub get_all_attribute_defaults_for {
Subroutine get_all_attribute_defaults_for: called CC=4 SCAR=13.9
Pod undocumented
105 3 my ( $class, $pkg ) = @_;
106 3 my $defaults = {};
107 3 for my $p ( reverse @{ mro::get_linear_isa($pkg) } ) {
108 7 ▶ while ( my ( $k, $v ) = each %{ $CLASS_ATTRIBUTES{$p} || {} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$CLASS_ATTRIBUTES{$p} or {};}) 14 7 21
Condition100%2/2
$CLASS_ATTRIBUTES{$p} || {}
l!l
183
MC/DC100%1/1
$CLASS_ATTRIBUTES{$p} || {}
$CLASS_ATTRIBUTES{$p}
Truth table100%2/2
A
A= $CLASS_ATTRIBUTES{$p}
Aresult
11
00
109 14 $defaults->{$k} = $v;
110 }
111 }
112 3 return $defaults;
113 }
114  
115 package Class::Tiny::Object;
116 # ABSTRACT: Base class for classes built with Class::Tiny
117  
118 9 our $VERSION = '1.008';
119  
120 9 my ( %HAS_BUILDARGS, %BUILD_CACHE, %DEMOLISH_CACHE, %ATTR_CACHE );
121  
122 9 ▶my $_PRECACHE = sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
123 9 ▶ no warnings 'once'; # needed to avoid downstream warnings
Subroutine BEGIN: called CC=1 SCAR=0.0
124 9 my ($class) = @_;
125 my $linear_isa =
126 9 ▶ @{"$class\::ISA"} == 1 && ${"$class\::ISA"}[0] eq "Class::Tiny::Object"
Branch: 1 branch
BranchTrueFalseTotal
@{"${class}::ISA";} == 1 && do { "${class}::ISA" }->[0] eq 'Class::Tiny::Object' ? : 5 4 9
Condition100%3/3
@{"${class}::ISA";} == 1 and do { "${class}::ISA" }->[0] eq "Class::Tiny::Object"
!ll&&!rl&&r
135
MC/DC100%2/2
@{"${class}::ISA";} == 1 and do { "${class}::ISA" }->[0] eq "Class::Tiny::Object"
@{"${class}::ISA";} == 1 do { "${class}::ISA" }->[0] eq "Class::Tiny::Object"
Truth table100%3/3
A and B
A= @{"${class}::ISA";} == 1B= do { "${class}::ISA" }->[0] eq "Class::Tiny::Object"
ABresult
0X0
100
111
127 ? [$class]
128 : mro::get_linear_isa($class);
129 $DEMOLISH_CACHE{$class} = [
130 19 ▶ map { ( *{$_}{CODE} ) ? ( *{$_}{CODE} ) : () }
Branch: 1 branch
BranchTrueFalseTotal
*{$_;}{'CODE'} ? : 3 16 19
131 9 map { "$_\::DEMOLISH" } @$linear_isa
132 ];
133 $BUILD_CACHE{$class} = [
134 19 ▶ map { ( *{$_}{CODE} ) ? ( *{$_}{CODE} ) : () }
Branch: 1 branch
BranchTrueFalseTotal
*{$_;}{'CODE'} ? : 3 16 19
135 9 map { "$_\::BUILD" } reverse @$linear_isa
136 ];
137 9 $HAS_BUILDARGS{$class} = $class->can("BUILDARGS");
138 return $ATTR_CACHE{$class} =
139 9 { map { $_ => 1 } Class::Tiny->get_all_attributes_for($class) };
140 9 };
141  
142 41 ▶sub new {
Subroutine new: called CC=9 SCAR=22.1
143 41 my $class = shift;
144 41 ▶ my $valid_attrs = $ATTR_CACHE{$class} || $_PRECACHE->($class);
Condition66%2/3
$ATTR_CACHE{$class} || &$_PRECACHE($class)
l!l&&r!l&&!r
3290
MC/DC0%0/2
$ATTR_CACHE{$class} || &$_PRECACHE($class)
$ATTR_CACHE{$class} &$_PRECACHE($class)
Truth table66%2/3
A || B
A= $ATTR_CACHE{$class}B= &$_PRECACHE($class)
ABresult
1X1
011
000
145  
146 # handle hash ref or key/value arguments
147 41 my $args;
148 41 ▶ if ( $HAS_BUILDARGS{$class} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($HAS_BUILDARGS{$class}) { } 0 41 41
149 0 $args = $class->BUILDARGS(@_);
150 }
151 else {
152 41 ▶ if ( @_ == 1 && ref $_[0] ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 1 and ref $_[0]) { } 11 30 41
elsif (@_ % 2 == 0) { } 29 1 30
Condition66%2/3
@_ == 1 and ref $_[0]
!ll&&!rl&&r
30011
MC/DC50%1/2
@_ == 1 and ref $_[0]
@_ == 1 ref $_[0]
Truth table66%2/3
A and B
A= @_ == 1B= ref $_[0]
ABresult
0X0
100
111
153 11 my %copy = eval { %{ $_[0] } }; # try shallow copy
154 11 ▶ Carp::croak("Argument to $class->new() could not be dereferenced as a hash") if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 10 11
155 10 $args = \%copy;
156 }
157 elsif ( @_ % 2 == 0 ) {
158 29 $args = {@_};
159 }
160 else {
161 1 Carp::croak("$class->new() got an odd number of elements");
162 }
163 }
164  
165 # create object and invoke BUILD (unless we were given __no_BUILD__)
166 my $self =
167 39 bless { map { $_ => $args->{$_} } grep { exists $valid_attrs->{$_} } keys %$args },
168 $class;
169 39 ▶ $self->BUILDALL($args) if !delete $args->{__no_BUILD__} && @{ $BUILD_CACHE{$class} };
Branch: 1 branch
BranchTrueFalseTotal
if not delete $args->{"__no_BUILD__"} and @{$BUILD_CACHE{$class};} 10 29 39
Condition100%3/3
not delete $args->{"__no_BUILD__"} and @{$BUILD_CACHE{$class};}
!ll&&!rl&&r
12810
MC/DC100%2/2
not delete $args->{"__no_BUILD__"} and @{$BUILD_CACHE{$class};}
not delete $args->{"__no_BUILD__"} @{$BUILD_CACHE{$class};}
Truth table100%3/3
not(A) and B
A= not delete $args->{"__no_BUILD__"}B= @{$BUILD_CACHE{$class};}
ABresult
0X0
100
111
170  
171 37 return $self;
172 }
173  
174 10 ▶sub BUILDALL { $_->(@_) for @{ $BUILD_CACHE{ ref $_[0] } } }
Subroutine BUILDALL: called CC=2 SCAR=6.9
175  
176 # Adapted from Moo and its dependencies
177 9 ▶require Devel::GlobalDestruction unless defined ${^GLOBAL_PHASE};
Branch: 1 branch
BranchTrueFalseTotal
unless defined ${^GLOBAL_PHASE} 0 9 9
178  
179 39 ▶sub DESTROY {
Subroutine DESTROY: called CC=4 SCAR=13.9
180 39 my $self = shift;
181 39 my $class = ref $self;
182 39 ▶ my $in_global_destruction =
Branch: 1 branch
BranchTrueFalseTotal
defined ${^GLOBAL_PHASE} ? : 39 0 39
183 defined ${^GLOBAL_PHASE}
184 ? ${^GLOBAL_PHASE} eq 'DESTRUCT'
185 : Devel::GlobalDestruction::in_global_destruction();
186 39 for my $demolisher ( @{ $DEMOLISH_CACHE{$class} } ) {
187 16 my $e = do {
188 16 local ( $?, $@ );
189 16 eval { $demolisher->( $self, $in_global_destruction ) };
190 16 $@;
191 };
192 9 ▶ no warnings 'misc'; # avoid (in cleanup) warnings
Subroutine BEGIN: called CC=1 SCAR=0.0
193 16 ▶ die $e if $e; # rethrow
Branch: 1 branch
BranchTrueFalseTotal
if $e 0 16 16
194 }
195 }
196  
197 9 1;
198  
199  
200 # vim: ts=4 sts=4 sw=4 et:
201  
202 __END__
↑ Summary