blib/lib/DBIO/Storage/Composed.pm

filter: Statementstmt 100% 97 / 97 Branchbran 90.9% 20 / 22 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 100% 16 / 16 Podpod 100% 4 / 4 totaltotal 97.1% 138 / 142 CC 18 SCAR 11.8
CC
18
Cov
98%
_own_methods
6.0
compose
5.0
_assert_no_layer_collisions
4.0
CRAP 3.2
« blib/lib/DBIO/DeploymentHandler/HandlesVersioning.pm ↑ Summary blib/lib/DBIO/Storage/DateTimeFormat.pm »
1 package DBIO::Storage::Composed;
2 # ABSTRACT: Runtime C3 composition of storage extension layers over a base storage class
3  
4 374 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 374 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 374 our $VERSION = '0.900007';
8  
9 374 ▶use mro;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 374 ▶use B ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 374 ▶use DBIO::Componentised ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 374 ▶use DBIO::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 374 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15  
16 # name => registry entry { base => $base, layers => [ @layers ] }.
17 # Keyed by the synthesised package name so it is idempotent for a given
18 # (base, layers) tuple and recoverable from ref($instance) at rebless time.
19 374 our %COMPOSED;
20  
21 # Method names a layer must not define: the driver storage owns them.
22 374 my %DRIVER_OWNED = map { $_ => 1 } qw(dbio_deploy_class);
23  
24  
25 61 ▶sub compose {
Subroutine compose: called CC=5 SCAR=16.1
Pod documented
26 61 my ($class, $base, $layers) = @_;
27 61 ▶ $layers ||= [];
Condition50%1/2
$layers ||= []
l!l
610
MC/DC0%0/1
$layers ||= []
$layers
Truth table50%1/2
A
A= $layers
Aresult
11
00
28 61 my @layers = @$layers;
29  
30 61 ▶ return $base unless @layers;
Branch: 1 branch
BranchTrueFalseTotal
unless @layers 1 60 61
31  
32 my $pkg = 'DBIO::Storage::Composed::'
33 60 . join('__', map { (my $s = $_) =~ s/::/_/g; $s } @layers, $base);
34  
35 60 ▶ return $pkg if $COMPOSED{$pkg};
Branch: 1 branch
BranchTrueFalseTotal
if $DBIO::Storage::Composed::COMPOSED{$pkg} 25 35 60
36  
37 35 DBIO::Componentised->ensure_class_loaded($base);
38 35 DBIO::Componentised->ensure_class_loaded($_) for @layers;
39  
40 35 $class->_assert_no_driver_owned_methods(\@layers);
41 31 $class->_assert_no_layer_collisions(\@layers);
42  
43 {
44 374 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
45 28 @{"${pkg}::ISA"} = (@layers, $base);
46 }
47 28 mro::set_mro($pkg, 'c3');
48  
49 28 $COMPOSED{$pkg} = { base => $base, layers => [@layers] };
50  
51 28 return $pkg;
52 }
53  
54  
55 1190 ▶sub composition_of {
Subroutine composition_of: called CC=2 SCAR=7.1
Pod documented
56 1190 my ($class, $pkg) = @_;
57 1190 ▶ return undef unless defined $pkg;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pkg 0 1190 1190
58 1190 return $COMPOSED{$pkg};
59 }
60  
61  
62 12 ▶sub layers_of {
Subroutine layers_of: called CC=2 SCAR=6.9
Pod documented
63 12 my ($class, $pkg) = @_;
64 12 ▶ my $entry = $class->composition_of($pkg) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $entry = $class->composition_of($pkg) or return 11 1 12
65 11 return @{ $entry->{layers} };
66 }
67  
68  
69 4 ▶sub recompose {
Subroutine recompose: called CC=2 SCAR=6.9
Pod documented
70 4 my ($class, $pkg, $new_base) = @_;
71 4 ▶ my $entry = $class->composition_of($pkg)
Branch: 1 branch
BranchTrueFalseTotal
my $entry = $class->composition_of($pkg) or "DBIO::Exception"->throw("recompose: '${pkg}' is not a composed storage class") 3 1 4
72 or DBIO::Exception->throw(
73 "recompose: '$pkg' is not a composed storage class"
74 );
75 3 return $class->compose($new_base, $entry->{layers});
76 }
77  
78 # The method names a package defines IN ITSELF -- CODE slots whose compiled body
79 # actually originates in $pkg, not aliased/imported subs and not inherited ones.
80 # Standard phasers and OO plumbing are never "layer methods" for collision
81 # purposes, so they are excluded (two layers each having a BEGIN block, or an
82 # imported croak, is not a method collision).
83 374 my %NOT_A_METHOD = map { $_ => 1 }
84 qw(BEGIN END INIT CHECK UNITCHECK DESTROY AUTOLOAD import unimport);
85  
86 129 ▶sub _own_methods {
Subroutine _own_methods: called CC=6 SCAR=17.9
87 129 my ($class, $pkg) = @_;
88  
89 129 my @names;
90 374 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
91 129 my $stash = \%{"${pkg}::"};
92 129 for my $name (keys %$stash) {
93 910 ▶ next if $name =~ /::\z/; # nested stash, not a sub
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /::\z/ 4 906 910
94 906 ▶ next if $NOT_A_METHOD{$name};
Branch: 1 branch
BranchTrueFalseTotal
if $NOT_A_METHOD{$name} 61 845 906
95 845 my $code = *{"${pkg}::${name}"}{CODE};
96 845 ▶ next unless defined $code;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $code 425 420 845
97 # Only count a sub whose compiled body originates in $pkg -- skips imports
98 # and re-exports that merely alias into the stash.
99 420 my $origin = B::svref_2object($code)->GV->STASH->NAME;
100 420 ▶ next unless $origin eq $pkg;
Branch: 1 branch
BranchTrueFalseTotal
unless $origin eq $pkg 0 420 420
101 420 push @names, $name;
102 }
103 129 return @names;
104 }
105  
106 # A layer that defines a driver-owned method is refused: the rule is stated
107 # on its own, not reported as a collision between layers.
108 35 ▶sub _assert_no_driver_owned_methods {
Subroutine _assert_no_driver_owned_methods: called CC=3 SCAR=11.0
109 35 my ($class, $layers) = @_;
110  
111 35 for my $method (sort keys %DRIVER_OWNED) {
112 my @offenders = grep {
113 35 my $layer = $_;
114 46 grep { $_ eq $method } $class->_own_methods($layer);
115 } @$layers;
116 35 ▶ next unless @offenders;
Branch: 1 branch
BranchTrueFalseTotal
unless @offenders 31 4 35
117  
118 4 DBIO::Exception->throw(
119 'storage layer ' . join(', ', @offenders) . " must not define $method: "
120 . 'the driver storage owns it, and a layer adds to a deploy through the '
121 . 'extension list the driver runs (ADR 0066 section 4)'
122 );
123 }
124 }
125  
126 31 ▶sub _assert_no_layer_collisions {
Subroutine _assert_no_layer_collisions: called CC=4 SCAR=13.9
127 31 my ($class, $layers) = @_;
128  
129 31 my %defined_in; # method name => [ packages that define it ]
130 31 for my $layer (@$layers) {
131 41 push @{ $defined_in{$_} }, $layer for $class->_own_methods($layer);
132 }
133  
134 31 my @collisions = grep { @{ $defined_in{$_} } > 1 }
135 sort keys %defined_in;
136 31 ▶ return unless @collisions;
Branch: 1 branch
BranchTrueFalseTotal
unless @collisions 28 3 31
137  
138 my $detail = join '; ', map {
139 3 "$_ (defined in " . join(', ', @{ $defined_in{$_} }) . ')'
140 } @collisions;
141  
142 3 DBIO::Exception->throw(
143 "storage layer method collision: $detail -- two or more storage layers "
144 . 'define the same method; rename or merge them so only one layer owns it, '
145 . 'or override the base method in a single layer and let the others chain '
146 . 'via next::method'
147 );
148 }
149  
150 374 1;
151  
152 __END__
« blib/lib/DBIO/DeploymentHandler/HandlesVersioning.pm ↑ Summary blib/lib/DBIO/Storage/DateTimeFormat.pm »