Class.pm

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