Class.pm

filter: Statementstmt 96.3% 160 / 166 Branchbran 79.5% 35 / 44 Conditioncond 59.2% 16 / 27 MC/DCmcdc 31.2% 5 / 16 Subroutinesub 95.1% 39 / 41 Podpod 41.6% 5 / 12 totaltotal 84.9% 260 / 306 CC 38 SCAR 18.9
CC
38
Cov
89%
_make_subclass
15.2
_make_parents
11.1
import
11.1
CRAP 6.6
↑ Summary Class/Base.pm »
1 package Exception::Class;
2  
3 6 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 6 our $VERSION = '1.46';
9  
10 6 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use Exception::Class::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 ▶use Scalar::Util qw( blessed reftype );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 6 our $BASE_EXC_CLASS;
15 6 ▶BEGIN { $BASE_EXC_CLASS ||= 'Exception::Class::Base'; }
Subroutine BEGIN: called CC=2 SCAR=7.6
Condition50%1/2
$Exception::Class::BASE_EXC_CLASS ||= "Exception::Class::Base"
l!l
06
MC/DC0%0/1
$Exception::Class::BASE_EXC_CLASS ||= "Exception::Class::Base"
$Exception::Class::BASE_EXC_CLASS
Truth table50%1/2
A
A= $Exception::Class::BASE_EXC_CLASS
Aresult
11
00
16  
17 6 our %CLASSES;
18  
19 15 ▶sub import {
Subroutine import: called CC=11 SCAR=24.0
20 15 my $class = shift;
21  
22 ## no critic (Variables::ProhibitPackageVars)
23 15 local $Exception::Class::Caller = caller();
24  
25 15 my %c;
26  
27 my %needs_parent;
28 15 ▶ while ( my $subclass = shift ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $subclass = shift()) 25 15 40
29 25 ▶ my $def = ref $_[0] ? shift : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 15 10 25
30 $def->{isa}
31 = $def->{isa}
32 25 ▶ ? ( ref $def->{isa} ? $def->{isa} : [ $def->{isa} ] )
Branch: 2 branches
BranchTrueFalseTotal
$def->{'isa'} ? : 7 18 25
ref $def->{'isa'} ? : 0 7 7
33 : [];
34  
35 25 $c{$subclass} = $def;
36 }
37  
38 # We need to sort by length because if we check for keys in the
39 # Foo::Bar:: stash, this creates a "Bar::" key in the Foo:: stash!
40 MAKE_CLASSES:
41 15 foreach my $subclass ( sort { length $a <=> length $b } keys %c ) {
42 25 my $def = $c{$subclass};
43  
44 # We already made this one.
45 25 ▶ next if $CLASSES{$subclass};
Branch: 1 branch
BranchTrueFalseTotal
if $Exception::Class::CLASSES{$subclass} 0 25 25
46  
47 {
48 ## no critic (TestingAndDebugging::ProhibitNoStrict)
49 6 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
50 25 foreach my $parent ( @{ $def->{isa} } ) {
51 7 ▶ unless ( keys %{"$parent\::"} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (keys %{"${parent}::";}) 4 3 7
52 $needs_parent{$subclass} = {
53 parents => $def->{isa},
54 4 def => $def
55 };
56 4 next MAKE_CLASSES;
57 }
58 }
59 }
60  
61 $class->_make_subclass(
62 21 ▶ subclass => $subclass,
Condition50%1/2
$def || {}
l!l
210
MC/DC0%0/1
$def || {}
$def
Truth table50%1/2
A
A= $def
Aresult
11
00
63 def => $def || {},
64 );
65 }
66  
67 12 foreach my $subclass ( keys %needs_parent ) {
68  
69 # This will be used to spot circular references.
70 4 my %seen;
71 4 $class->_make_parents( \%needs_parent, $subclass, \%seen );
72 }
73 }
74  
75 5 ▶sub _make_parents {
Subroutine _make_parents: called CC=10 SCAR=24.1
76 5 my $class = shift;
77 5 my $needs = shift;
78 5 my $subclass = shift;
79 5 my $seen = shift;
80 5 my $child = shift; # Just for error messages.
81  
82 ## no critic (TestingAndDebugging::ProhibitNoStrict, TestingAndDebugging::ProhibitProlongedStrictureOverride)
83 6 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
84  
85 # What if someone makes a typo in specifying their 'isa' param?
86 # This should catch it. Either it's been made because it didn't
87 # have missing parents OR it's in our hash as needing a parent.
88 # If neither of these is true then the _only_ place it is
89 # mentioned is in the 'isa' param for some other class, which is
90 # not a good enough reason to make a new class.
91 die
92 "Class $subclass appears to be a typo as it is only specified in the 'isa' param for $child\n"
93 unless exists $needs->{$subclass}
94 || $CLASSES{$subclass}
95 0 ▶ || keys %{"$subclass\::"};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $needs->{$subclass} or $Exception::Class::CLASSES{$subclass} or keys %{"${subclass}::";} 0 5 5
Condition33%2/6
exists $needs->{$subclass} or $Exception::Class::CLASSES{$subclass} or keys %{"${subclass}::";}
l!l&&r!l&&!r
500
exists $needs->{$subclass} or $Exception::Class::CLASSES{$subclass}
l!l&&r!l&&!r
500
MC/DC0%0/3
exists $needs->{$subclass} or $Exception::Class::CLASSES{$subclass} or keys %{"${subclass}::";}
exists $needs->{$subclass} $Exception::Class::CLASSES{$subclass} keys %{"${subclass}::";}
Truth table25%1/4
A or B or C
A= exists $needs->{$subclass}B= $Exception::Class::CLASSES{$subclass}C= keys %{"${subclass}::";}
ABCresult
1XX1
01X1
0011
0000
96  
97 5 foreach my $c ( @{ $needs->{$subclass}{parents} } ) {
98  
99 # It's been made
100 5 ▶ next if $CLASSES{$c} || keys %{"$c\::"};
Branch: 1 branch
BranchTrueFalseTotal
if $Exception::Class::CLASSES{$c} or keys %{"${c}::";} 4 1 5
Condition66%2/3
$Exception::Class::CLASSES{$c} or keys %{"${c}::";}
l!l&&r!l&&!r
401
MC/DC50%1/2
$Exception::Class::CLASSES{$c} or keys %{"${c}::";}
$Exception::Class::CLASSES{$c} keys %{"${c}::";}
Truth table66%2/3
A or B
A= $Exception::Class::CLASSES{$c}B= keys %{"${c}::";}
ABresult
1X1
011
000
101  
102 die "There appears to be some circularity involving $subclass\n"
103 1 ▶ if $seen->{$subclass};
Branch: 1 branch
BranchTrueFalseTotal
if $seen->{$subclass} 0 1 1
104  
105 1 $seen->{$subclass} = 1;
106  
107 1 $class->_make_parents( $needs, $c, $seen, $subclass );
108 }
109  
110 5 ▶ return if $CLASSES{$subclass} || keys %{"$subclass\::"};
Branch: 1 branch
BranchTrueFalseTotal
if $Exception::Class::CLASSES{$subclass} or keys %{"${subclass}::";} 1 4 5
Condition66%2/3
$Exception::Class::CLASSES{$subclass} or keys %{"${subclass}::";}
l!l&&r!l&&!r
104
MC/DC50%1/2
$Exception::Class::CLASSES{$subclass} or keys %{"${subclass}::";}
$Exception::Class::CLASSES{$subclass} keys %{"${subclass}::";}
Truth table66%2/3
A or B
A= $Exception::Class::CLASSES{$subclass}B= keys %{"${subclass}::";}
ABresult
1X1
011
000
111  
112 $class->_make_subclass(
113 subclass => $subclass,
114 def => $needs->{$subclass}{def}
115 4 );
116 }
117  
118 25 ▶sub _make_subclass {
Subroutine _make_subclass: called CC=15 SCAR=27.2
119 25 my $class = shift;
120 25 my %p = @_;
121  
122 25 my $subclass = $p{subclass};
123 25 my $def = $p{def};
124  
125 25 my $isa;
126 25 ▶ if ( $def->{isa} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($def->{"isa"}) 25 0 25
127 25 ▶ $isa = ref $def->{isa} ? join q{ }, @{ $def->{isa} } : $def->{isa};
Branch: 1 branch
BranchTrueFalseTotal
ref $def->{'isa'} ? : 25 0 25
128 }
129 25 ▶ $isa ||= $BASE_EXC_CLASS;
Condition66%2/3
$isa ||= $Exception::Class::BASE_EXC_CLASS
l!l&&r!l&&!r
7180
MC/DC0%0/2
$isa ||= $Exception::Class::BASE_EXC_CLASS
$isa $Exception::Class::BASE_EXC_CLASS
Truth table66%2/3
A ||= B
A= $isaB= $Exception::Class::BASE_EXC_CLASS
ABresult
1X1
011
000
130  
131 25 my $version_name = 'VERSION';
132  
133 25 my $code = <<"EOPERL";
134 package $subclass;
135  
136 use base qw($isa);
137  
138 our \$$version_name ||= '1.1';
139  
140 1;
141  
142 EOPERL
143  
144 25 ▶ if ( $def->{description} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($def->{"description"}) 2 23 25
145 2 ( my $desc = $def->{description} ) =~ s/([\\\'])/\\$1/g;
146 2 $code .= <<"EOPERL";
147 sub description
148 {
149 return '$desc';
150 }
151 EOPERL
152 }
153  
154 25 my @fields;
155 25 ▶ if ( my $fields = $def->{fields} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $fields = $def->{"fields"}) 8 17 25
156 @fields
157 8 ▶ = ref $fields && reftype $fields eq 'ARRAY' ? @$fields : $fields;
Branch: 1 branch
BranchTrueFalseTotal
ref $fields && builtin::reftype($fields) eq 'ARRAY' ? : 7 1 8
Condition66%2/3
ref $fields and builtin::reftype($fields) eq "ARRAY"
!ll&&!rl&&r
107
MC/DC50%1/2
ref $fields and builtin::reftype($fields) eq "ARRAY"
ref $fields builtin::reftype($fields) eq "ARRAY"
Truth table66%2/3
A and B
A= ref $fieldsB= builtin::reftype($fields) eq "ARRAY"
ABresult
0X0
100
111
158  
159 $code
160 .= 'sub Fields { return ($_[0]->SUPER::Fields, '
161 8 . join( ', ', map {"'$_'"} @fields )
162 . ") }\n\n";
163  
164 8 foreach my $field (@fields) {
165 9 ▶ croak
Branch: 1 branch
BranchTrueFalseTotal
unless $field =~ /\A[a-z_][a-z0-9_]*\z/i 3 6 9
166 "Invalid field name <$field>. A field name must be a legal Perl identifier."
167 unless $field =~ /\A[a-z_][a-z0-9_]*\z/i;
168 6 $code .= sprintf( "sub %s { \$_[0]->{%s} }\n", $field, $field );
169 }
170 }
171  
172 22 ▶ if ( my $alias = $def->{alias} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $alias = $def->{"alias"}) 2 20 22
173 ## no critic (Variables::ProhibitPackageVars)
174 2 ▶ die 'Cannot make alias without caller'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Exception::Class::Caller 0 2 2
175 unless defined $Exception::Class::Caller;
176  
177 ## no critic (TestingAndDebugging::ProhibitNoStrict)
178 6 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
179 2 *{"$Exception::Class::Caller\::$alias"}
180 2 ▶ = sub { $subclass->throw(@_) };
Subroutine __ANON__: called CC=1 SCAR=0.0
181 }
182  
183 22 ▶ if ( my $defaults = $def->{defaults} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $defaults = $def->{"defaults"}) 0 22 22
184 0 $code
185 .= "sub _defaults { return shift->SUPER::_defaults, our \%_DEFAULTS }\n";
186 ## no critic (TestingAndDebugging::ProhibitNoStrict)
187 6 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
188 0 *{"$subclass\::_DEFAULTS"} = {%$defaults};
189 }
190  
191 ## no critic (BuiltinFunctions::ProhibitStringyEval, ErrorHandling::RequireCheckingReturnValueOfEval)
192 0 ▶ eval $code;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine Fields: not called CC=1 SCAR=0.0
Subroutine Fields: called CC=1 SCAR=0.0
Subroutine Fields: called CC=1 SCAR=0.0
Subroutine Fields: called CC=1 SCAR=0.0
Subroutine bar: called CC=1 SCAR=0.0
Subroutine description: called CC=1 SCAR=0.0
Subroutine foo: called CC=1 SCAR=0.0
Subroutine something: not called CC=1 SCAR=0.0
Subroutine thing: called CC=1 SCAR=0.0
Subroutine yip: called CC=1 SCAR=0.0
Pod documented
Pod documented
Pod documented
Pod documented
Pod undocumented
Pod documented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
193 22 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 22 22
194  
195 22 ( my $filename = "$subclass.pm" ) =~ s{::}{/}g;
196 22 ▶ $INC{$filename} ||= __FILE__;
Condition100%2/2
$INC{$filename} ||= __FILE__
l!l
121
MC/DC100%1/1
$INC{$filename} ||= __FILE__
$INC{$filename}
Truth table100%2/2
A
A= $INC{$filename}
Aresult
11
00
197  
198 22 $CLASSES{$subclass} = 1;
199 }
200  
201 5 ▶sub caught {
Subroutine caught: called CC=4 SCAR=13.9
Pod undocumented
202 5 my $e = $@;
203  
204 5 ▶ return $e unless $_[1];
Branch: 1 branch
BranchTrueFalseTotal
unless $_[1] 3 2 5
205  
206 2 ▶ return unless blessed($e) && $e->isa( $_[1] );
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($e) and $e->isa($_[1]) 1 1 2
Condition66%2/3
builtin::blessed($e) and $e->isa($_[1])
!ll&&!rl&&r
011
MC/DC50%1/2
builtin::blessed($e) and $e->isa($_[1])
builtin::blessed($e) $e->isa($_[1])
Truth table66%2/3
A and B
A= builtin::blessed($e)B= $e->isa($_[1])
ABresult
0X0
100
111
207 1 return $e;
208 }
209  
210 1 ▶sub Classes { sort keys %Exception::Class::CLASSES }
Subroutine Classes: called CC=1 SCAR=0.0
Pod undocumented
211  
212 6 1;
213  
214 # ABSTRACT: A module that allows you to declare real exception classes in Perl
215  
216 __END__
↑ Summary Class/Base.pm »