blib/lib/Class/Interfaces.pm

filter: Statementstmt 100% 53 / 53 Branchbran 100% 26 / 26 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 100% 8 / 8 Podpod n/a totaltotal 97.7% 88 / 90 CC 17 SCAR 22.9
CC
17
Cov
99%
import
15.0
_build_interface_package
2.0
_error_handler
2.0
CRAP 9.9
↑ Summary
1  
2 package Class::Interfaces;
3  
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 our $VERSION = '0.04';
8  
9 11 ▶sub import {
Subroutine import: called CC=15 SCAR=27.1
10 11 my $class = shift;
11 11 my %interfaces = @_;
12 11 foreach my $interface (keys %interfaces) {
13 # build the interface
14 16 my (@methods, @subclasses);
15 16 ▶ if (ref($interfaces{$interface}) eq 'HASH') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $interfaces{$interface} eq 'HASH') { } 5 11 16
elsif (ref $interfaces{$interface} eq 'ARRAY') { } 9 2 11
elsif (not defined $interfaces{$interface}) { } 1 1 2
16 5 my $interface_spec = $interfaces{$interface};
17 # if we have an isa
18 5 ▶ if (exists ${$interface_spec}{isa}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists do { $interface_spec }->{"isa"}) 3 2 5
19 # if is an array (multiple inheritance)
20 3 ▶ if (ref($interface_spec->{isa}) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $interface_spec->{'isa'} eq 'ARRAY') { } 1 2 3
21 1 @subclasses = @{$interface_spec->{isa}};
22 }
23 else {
24 # if its another kind of ref, its an error
25 2 ▶ (!ref($interface_spec->{isa}))
Branch: 1 branch
BranchTrueFalseTotal
ref $interface_spec->{"isa"} and $class->_error_handler("Interface ($interface) isa list must be an array reference") 1 1 2
26 || $class->_error_handler("Interface ($interface) isa list must be an array reference");
27 # otherwise its just a single item
28 1 @subclasses = $interface_spec->{isa};
29 }
30 }
31 4 ▶ if (exists ${$interface_spec}{methods}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists do { $interface_spec }->{"methods"}) 3 1 4
32 3 ▶ (ref($interface_spec->{methods}) eq 'ARRAY')
Branch: 1 branch
BranchTrueFalseTotal
ref $interface_spec->{"methods"} eq "ARRAY" or $class->_error_handler("Method list for Interface ($interface) must be an array reference") 2 1 3
33 || $class->_error_handler("Method list for Interface ($interface) must be an array reference");
34 2 @methods = @{$interface_spec->{methods}};
35 }
36 }
37 elsif (ref($interfaces{$interface}) eq 'ARRAY') {
38 9 @methods = @{$interfaces{$interface}};
39 }
40 elsif (!defined($interfaces{$interface})) {
41 # allow undefined here, this indicates an empty
42 # interface, sometimes called a marker interface
43 ;
44 }
45 else {
46 1 $class->_error_handler("Cannot use a " . $interfaces{$interface} . " to build an interface");
47 }
48 # now create the interfaces
49 13 my $package = $class->_build_interface_package($interface, @subclasses);
50 13 ▶ eval $package;
Subroutine other_method: called CC=1 SCAR=0.0
51 13 ▶ $class->_error_handler("Could not create Interface ($interface) because", $@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 2 11 13
52 11 eval {
53 11 my $method_stub = $class->can('_method_stub');
54 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
55 # without at least this VERSION declaration
56 # a Marker interface will not work with
57 # 'use base' basically it would complain
58 # that the package is empty.
59 # we only assign this if the VERSION is already
60 # empty too, so we don't step on any customizations
61 # done in subclasses.
62 11 ▶ ${"${interface}::"}{VERSION} ||= -1;
Condition50%1/2
do { "${interface}::" }->{"VERSION"} ||= -1
l!l
011
MC/DC0%0/1
do { "${interface}::" }->{"VERSION"} ||= -1
do { "${interface}::" }->{"VERSION"}
Truth table50%1/2
A
A= do { "${interface}::" }->{"VERSION"}
Aresult
11
00
63 # now we create all our methods :)
64 11 foreach my $method (@methods) {
65 12 ▶ ($method !~ /^(BEGIN|INIT|CHECK|END|DESTORY|AUTOLOAD|import|bootstrap)$/)
Branch: 1 branch
BranchTrueFalseTotal
$method =~ /^(BEGIN|INIT|CHECK|END|DESTORY|AUTOLOAD|import|bootstrap)$/ and $class->_error_handler("Cannot create an interface using reserved perl methods") 1 11 12
66 || $class->_error_handler("Cannot create an interface using reserved perl methods");
67 11 *{"${interface}::${method}"} = $method_stub;
68 }
69 };
70 11 ▶ $class->_error_handler("Could not create sub methods for Interface ($interface) because", $@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 10 11
71 }
72 }
73  
74 13 ▶sub _build_interface_package {
Subroutine _build_interface_package: called CC=2 SCAR=6.9
75 13 my ($class, $interface, @subclasses) = @_;
76 13 my $package = "package $interface;";
77 13 ▶ $package .= "\@${interface}::ISA = qw(" . (join " " => @subclasses) . ");" if @subclasses;
Branch: 1 branch
BranchTrueFalseTotal
if @subclasses 2 11 13
78 13 return $package;
79 }
80  
81 6 ▶sub _error_handler {
Subroutine _error_handler: called CC=2 SCAR=6.9
82 6 my ($class, $message, $sub_exception) = @_;
83 6 ▶ die "$message : $sub_exception" if $sub_exception;
Branch: 1 branch
BranchTrueFalseTotal
if $sub_exception 2 4 6
84 4 die "$message";
85 }
86  
87 3 ▶sub _method_stub { die "Method Not Implemented" }
Subroutine _method_stub: called CC=1 SCAR=0.0
88  
89 2 1;
90  
91 __END__
↑ Summary