blib/lib/Types/ReadOnly.pm

filter: Statementstmt 91.4% 139 / 152 Branchbran 66.1% 41 / 62 Conditioncond 52.1% 24 / 46 MC/DCmcdc 26.0% 6 / 23 Subroutinesub 96.6% 29 / 30 Podpod n/a totaltotal 76.3% 239 / 313 CC 49 SCAR 19.3
CC
49
Cov
78%
__ANON__
17.1
_make_readonly
15.1
__ANON__
7.5
CRAP 6.9
↑ Summary
1 6 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package Types::ReadOnly;
6  
7 6 our $AUTHORITY = 'cpan:TOBYINK';
8 6 our $VERSION = '0.003';
9  
10 6 ▶use Type::Tiny 1.006000 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use Type::Coercion ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 ▶use Types::Standard qw( Any Dict HashRef Ref );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 6 ▶use Type::Library -base, -declare => qw( ReadOnly Locked );
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 6 ▶use Scalar::Util qw( reftype blessed refaddr );
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1 ▶sub _dclone($) {
Subroutine _dclone: called CC=1 SCAR=0.0
18 1 require Storable;
19 6 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 *_dclone = \&Storable::dclone;
21 1 goto &Storable::dclone;
22 }
23  
24 6 my %skip = map { $_ => 1 } qw/CODE GLOB/;
25 16 ▶sub _make_readonly {
Subroutine _make_readonly: called CC=13 SCAR=27.1
26 16 my (undef, $dont_clone) = @_;
27 16 ▶ if (my $reftype = reftype $_[0] and not blessed($_[0]) and not &Internals::SvREADONLY($_[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $reftype = builtin::reftype($_[0]) and not builtin::blessed($_[0]) and not &Internals::SvREADONLY($_[0])) 6 10 16
Condition83%5/6
my $reftype = builtin::reftype($_[0]) and not builtin::blessed($_[0]) and not &Internals::SvREADONLY($_[0])
!ll&&!rl&&r
646
my $reftype = builtin::reftype($_[0]) and not builtin::blessed($_[0])
!ll&&!rl&&r
6010
MC/DC66%2/3
my $reftype = builtin::reftype($_[0]) and not builtin::blessed($_[0]) and not &Internals::SvREADONLY($_[0])
my $reftype = builtin::reftype($_[0]) not builtin::blessed($_[0]) not &Internals::SvREADONLY($_[0])
Truth table75%3/4
A and not(B) and not(C)
A= my $reftype = builtin::reftype($_[0])B= not builtin::blessed($_[0])C= not &Internals::SvREADONLY($_[0])
ABCresult
0XX0
10X0
1100
1111
28 6 ▶ $_[0] = _dclone($_[0]) if !$dont_clone && &Internals::SvREFCNT($_[0]) > 1 && !$skip{$reftype};
Branch: 1 branch
BranchTrueFalseTotal
if not $dont_clone and &Internals::SvREFCNT($_[0]) > 1 and not $skip{$reftype} 3 3 6
Condition66%4/6
not $dont_clone and &Internals::SvREFCNT($_[0]) > 1 and not $skip{$reftype}
!ll&&!rl&&r
303
not $dont_clone and &Internals::SvREFCNT($_[0]) > 1
!ll&&!rl&&r
033
MC/DC33%1/3
not $dont_clone and &Internals::SvREFCNT($_[0]) > 1 and not $skip{$reftype}
not $dont_clone &Internals::SvREFCNT($_[0]) > 1 not $skip{$reftype}
Truth table50%2/4
not(A) and B and not(C)
A= not $dont_cloneB= &Internals::SvREFCNT($_[0]) > 1C= not $skip{$reftype}
ABCresult
0XX0
10X0
1100
1111
29 6 &Internals::SvREADONLY($_[0], 1);
30 6 ▶ if ($reftype eq 'SCALAR' or $reftype eq 'REF') {
Branch: 3 branches
BranchTrueFalseTotal
if ($reftype eq 'SCALAR' or $reftype eq 'REF') { } 0 6 6
elsif ($reftype eq 'ARRAY') { } 3 3 6
elsif ($reftype eq 'HASH') { } 3 0 3
Condition33%1/3
$reftype eq "SCALAR" or $reftype eq "REF"
l!l&&r!l&&!r
006
MC/DC0%0/2
$reftype eq "SCALAR" or $reftype eq "REF"
$reftype eq "SCALAR" $reftype eq "REF"
Truth table33%1/3
A or B
A= $reftype eq "SCALAR"B= $reftype eq "REF"
ABresult
1X1
011
000
31 0 _make_readonly(${ $_[0] }, 1);
32 }
33 elsif ($reftype eq 'ARRAY') {
34 3 _make_readonly($_) for @{ $_[0] };
35 }
36 elsif ($reftype eq 'HASH') {
37 3 &Internals::hv_clear_placeholders($_[0]);
38 3 _make_readonly($_) for values %{ $_[0] };
39 }
40 }
41 16 Internals::SvREADONLY($_[0], 1);
42 16 return;
43 }
44  
45 __PACKAGE__->meta->add_type({
46 name => 'ReadOnly',
47 parent => Ref,
48 9 ▶ constraint => sub {
Subroutine __ANON__: called CC=5 SCAR=20.1
49 9 my $r = reftype($_);
50 9 ▶ ($r eq 'HASH' or $r eq 'ARRAY' or $r eq 'SCALAR' or $r eq 'REF') and &Internals::SvREADONLY($_);
Branch: 1 branch
BranchTrueFalseTotal
$r eq "HASH" || $r eq "ARRAY" || $r eq "SCALAR" || $r eq "REF" and &Internals::SvREADONLY($_) 9 0 9
Condition44%4/9
$r eq "HASH" or $r eq "ARRAY" or $r eq "SCALAR" or $r eq "REF"
l!l&&r!l&&!r
900
$r eq "HASH" or $r eq "ARRAY" or $r eq "SCALAR"
l!l&&r!l&&!r
900
$r eq "HASH" or $r eq "ARRAY"
l!l&&r!l&&!r
360
MC/DC0%0/4
$r eq "HASH" or $r eq "ARRAY" or $r eq "SCALAR" or $r eq "REF"
$r eq "HASH" $r eq "ARRAY" $r eq "SCALAR" $r eq "REF"
Truth table40%2/5
A or B or C or D
A= $r eq "HASH"B= $r eq "ARRAY"C= $r eq "SCALAR"D= $r eq "REF"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
51 },
52 4 ▶ constraint_generator => sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
53 4 ▶ my ($parameter) = @_ or return $Type::Tiny::parameterize_type;
Branch: 1 branch
BranchTrueFalseTotal
my($parameter) = or return $Type::Tiny::parameterize_type 4 0 4
54 4 $parameter->compiled_check; # only need this because parent constraint (i.e. ReadOnly) is automatically checked
55 },
56 44 ▶ inlined => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
57 44 my ($self, $varname) = @_;
58 return (
59 44 sprintf('do { my $r = Scalar::Util::reftype(%s); $r eq "HASH" or $r eq "ARRAY" or $r eq "SCALAR" or $r eq "REF" }', $varname),
60 sprintf('&Internals::SvREADONLY(%s)', $varname),
61 );
62 },
63 4 ▶ inline_generator => sub {
Subroutine __ANON__: called CC=3 SCAR=11.2
64 4 ▶ my ($parameter) = @_ or return $Type::Tiny::parameterize_type;
Branch: 1 branch
BranchTrueFalseTotal
my($parameter) = or return $Type::Tiny::parameterize_type 4 0 4
65 4 ▶ return unless $parameter->can_be_inlined;
Branch: 1 branch
BranchTrueFalseTotal
unless $parameter->can_be_inlined 0 4 4
66 26 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
67 26 my ($child, $varname) = @_;
68 26 my $me = $child->parent;
69 26 return ($me->inline_check($varname), $parameter->inline_check($varname));
70 4 };
71 },
72 coercion => [
73 Ref ,=> 'do { Types::ReadOnly::_make_readonly(my $ro = $_); $ro }',
74 ],
75 4 ▶ coercion_generator => sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
76 4 my ($me, $child) = @_;
77 4 my $parameter = $child->type_parameter;
78 4 my @extra;
79 4 ▶ if ($parameter->has_coercion) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parameter->has_coercion) 3 1 4
80 3 my @map = @{ $parameter->coercion->type_coercion_map };
81 3 ▶ while (@map) {
Branch: 1 branch
BranchTrueFalseTotal
if (@map) 3 3 6
82 3 my ($t, $code) = splice @map, 0, 2;
83 3 ▶ if (Types::TypeTiny::CodeLike->check($code)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Types::TypeTiny::CodeLike()->check($code)) { } 1 2 3
84 1 ▶ push @extra, $t, sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
85 1 my $coerced = $code->(@_);
86 1 Types::ReadOnly::_make_readonly($coerced);
87 1 $coerced;
88 1 };
89 }
90 else {
91 2 push @extra, $t, sprintf('do { my $coerced = %s; Types::ReadOnly::_make_readonly($coerced); $coerced }', $code);
92 }
93 }
94 }
95 bless(
96 4 { type_coercion_map => [
97 $parameter => 'do { Types::ReadOnly::_make_readonly(my $ro = $_); $ro }',
98 @extra,
99 ] },
100 'Type::Coercion'
101 );
102 },
103 6 });
104  
105 20 ▶my $_FIND_KEYS = sub {
Subroutine __ANON__: called CC=6 SCAR=18.1
106 my ($dict) = grep {
107 20 ▶ $_->is_parameterized
Condition83%5/6
$_->is_parameterized && $_->has_parent && $_->parent->strictly_equals(&Dict())
!ll&&!rl&&r
118215
$_->is_parameterized and $_->has_parent
!ll&&!rl&&r
118017
MC/DC66%2/3
$_->is_parameterized && $_->has_parent && $_->parent->strictly_equals(&Dict())
$_->is_parameterized $_->has_parent $_->parent->strictly_equals(&Dict())
Truth table75%3/4
A and B && C
A= $_->is_parameterizedB= $_->has_parentC= $_->parent->strictly_equals(&Dict())
ABCresult
0XX0
10X0
1100
1111
108 and $_->has_parent
109 and $_->parent->strictly_equals(Dict)
110 } $_[0], $_[0]->parents;
111 20 ▶ return unless $dict;
Branch: 1 branch
BranchTrueFalseTotal
unless $dict 5 15 20
112 15 ▶ return if ref($dict->parameters->[-1]) eq q(HASH);
Branch: 1 branch
BranchTrueFalseTotal
if ref $dict->parameters->[-1] eq "HASH" 0 15 15
113 15 my @keys = sort keys %{ +{ @{ $dict->parameters } } };
114 15 ▶ return unless @keys;
Branch: 1 branch
BranchTrueFalseTotal
unless @keys 0 15 15
115 15 \@keys;
116 6 };
117  
118 # Stolen from Hash::Util 0.15.
119 # In earlier versions, of Hash::Util, there is only a hashref_unlocked
120 # function, which happens to be very broken. :-/
121 3 ▶sub _hashref_locked { &Internals::SvREADONLY($_[0]) }
Subroutine _hashref_locked: called CC=1 SCAR=0.0
122  
123 __PACKAGE__->meta->add_type({
124 name => 'Locked',
125 parent => Ref['HASH'],
126 6 ▶ constraint => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
127 6 my $r = reftype($_);
128 6 &Internals::SvREADONLY($_);
129 },
130 7 ▶ constraint_generator => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
131 7 ▶ my ($parameter) = @_ or return $Type::Tiny::parameterize_type;
Branch: 1 branch
BranchTrueFalseTotal
my($parameter) = or return $Type::Tiny::parameterize_type 7 0 7
132 7 my $pchk = $parameter->compiled_check;
133 7 ▶ my $KEYS = $parameter->$_FIND_KEYS or return $pchk;
Branch: 1 branch
BranchTrueFalseTotal
my $KEYS = $parameter->$_FIND_KEYS or return $pchk 5 2 7
134 5 my $keys = join "*#*", @$KEYS;
135 3 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
136 3 my $legal = join "*#*", sort(&Hash::Util::legal_keys($_));
137 3 ▶ return if $keys ne $legal;
Branch: 1 branch
BranchTrueFalseTotal
if $keys ne $legal 2 1 3
138 1 goto $pchk;
139 5 };
140 },
141 90 ▶ inlined => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
142 90 my ($self, $varname) = @_;
143 90 my $r = Ref['HASH'];
144 return (
145 90 $r->inline_check($varname),
146 sprintf('&Internals::SvREADONLY(%s)', $varname),
147 );
148 },
149 7 ▶ inline_generator => sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
150 7 require Hash::Util;
151 7 ▶ my ($parameter) = @_ or return $Type::Tiny::parameterize_type;
Branch: 1 branch
BranchTrueFalseTotal
my($parameter) = or return $Type::Tiny::parameterize_type 7 0 7
152 7 ▶ return unless $parameter->can_be_inlined;
Branch: 1 branch
BranchTrueFalseTotal
unless $parameter->can_be_inlined 0 7 7
153 7 my $KEYS = $parameter->$_FIND_KEYS;
154 7 ▶ my $keys = join "*#*", @{ $KEYS || [] };
Condition100%2/2
$KEYS || []
l!l
52
MC/DC100%1/1
$KEYS || []
$KEYS
Truth table100%2/2
A
A= $KEYS
Aresult
11
00
155 66 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
156 66 my ($child, $varname) = @_;
157 66 my @extras;
158 66 ▶ if ($keys) {
Branch: 1 branch
BranchTrueFalseTotal
if ($keys) 34 32 66
159 34 require Hash::Util;
160 34 push @extras, sprintf('%s eq join("*#*", sort(&Hash::Util::legal_keys(%s)))', B::perlstring($keys), $varname);
161 }
162 66 (undef, @extras, $parameter->inline_check($varname));
163 7 };
164 },
165 coercion => [
166 Ref['HASH'] , => 'do { Types::ReadOnly::_make_readonly(my $ro = $_); $ro }',
167 ],
168 6 ▶ coercion_generator => sub {
Subroutine __ANON__: called CC=5 SCAR=17.4
169 6 require Hash::Util;
170 6 my ($me, $child) = @_;
171 6 my $parameter = $child->type_parameter;
172 6 my $KEYS = $parameter->$_FIND_KEYS;
173 6 ▶ my $qkeys = $KEYS ? join(q[,], '', map B::perlstring($_), @$KEYS) : '';
Branch: 1 branch
BranchTrueFalseTotal
$KEYS ? : 5 1 6
174 6 my @extra;
175 6 ▶ if ($parameter->has_coercion) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parameter->has_coercion) 2 4 6
176 2 my @map = @{ $parameter->coercion->type_coercion_map };
177 2 ▶ while (@map) {
Branch: 1 branch
BranchTrueFalseTotal
if (@map) 2 2 4
178 2 my ($t, $code) = splice @map, 0, 2;
179 2 ▶ if (Types::TypeTiny::CodeLike->check($code)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Types::TypeTiny::CodeLike()->check($code)) { } 0 2 2
180 0 ▶ push @extra, $t, sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
181 0 my $coerced = $code->(@_);
182 0 &Hash::Util::unlock_hash($coerced);
183 0 ▶ &Hash::Util::lock_hash($coerced, @{$KEYS||[]});
Condition0%0/2
$KEYS || []
l!l
00
MC/DC0%0/1
$KEYS || []
$KEYS
Truth table0%0/2
A
A= $KEYS
Aresult
11
00
184 0 $coerced;
185 0 };
186 }
187 else {
188 2 push @extra, $t, sprintf('do { my $coerced = %s; &Hash::Util::unlock_hash($coerced); &Hash::Util::lock_keys($coerced %s); $coerced }', $code, $qkeys);
189 }
190 }
191 }
192 bless(
193 6 { type_coercion_map => [
194 $parameter => sprintf('do { my $coerced = $_; &Hash::Util::unlock_hash($coerced); &Hash::Util::lock_keys($coerced %s); $coerced }', $qkeys),
195 @extra,
196 ] },
197 'Type::Coercion'
198 );
199 },
200 6 });
201  
202  
203 # This comparator allows Locked[Foo] to be seen as a child of Foo, and not
204 # just a child of Locked. It's probably not foolproof.
205 #
206 6 my $comparator;
207 3 ▶$comparator = sub {
Subroutine __ANON__: called CC=7 SCAR=28.4
208 3 my $A = shift->find_constraining_type;
209 3 my $B = shift->find_constraining_type;
210 3 my $RO = __PACKAGE__->get_type('ReadOnly');
211 3 my $L = __PACKAGE__->get_type('Locked');
212
213 3 ▶ my $Aprime = $A->find_parent(sub {
Subroutine __ANON__: called CC=4 SCAR=19.7
214 3 ▶ $_->is_parameterized and
Branch: 1 branch
BranchTrueFalseTotal
$_->is_parameterized and $_->has_parent and $_->parent->strictly_equals($L) || $_->parent->strictly_equals($RO) 3 0 3
Condition33%3/9
$_->is_parameterized && $_->has_parent && ($_->parent->strictly_equals($L) || $_->parent->strictly_equals($RO))
!ll&&!rl&&r
003
$_->is_parameterized and $_->has_parent
!ll&&!rl&&r
003
$_->parent->strictly_equals($L) or $_->parent->strictly_equals($RO)
l!l&&r!l&&!r
300
MC/DC0%0/4
$_->is_parameterized && $_->has_parent && ($_->parent->strictly_equals($L) || $_->parent->strictly_equals($RO))
$_->is_parameterized $_->has_parent $_->parent->strictly_equals($L) $_->parent->strictly_equals($RO)
Truth table20%1/5
A and B && C or D
A= $_->is_parameterizedB= $_->has_parentC= $_->parent->strictly_equals($L)D= $_->parent->strictly_equals($RO)
ABCDresult
0XXX0
10XX0
11000
111X1
11011
215 $_->has_parent and
216 $_->parent->strictly_equals($L) || $_->parent->strictly_equals($RO)
217 3 });
218
219 3 ▶ if ($Aprime) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Aprime) 3 0 3
220 3 my $param = $Aprime->type_parameter->find_constraining_type;
221 3 ▶ if ($param->is_a_type_of($B)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param->is_a_type_of($B)) 3 0 3
222 3 return Type::Tiny::CMP_SUBTYPE();
223 }
224 }
225
226 0 ▶ return Type::Tiny::CMP_UNKNOWN() if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
227
228 0 my $r = $comparator->($B, $A, 1);
229 0 ▶ return $r if ($r eq Type::Tiny::CMP_EQUIVALENT());
Branch: 1 branch
BranchTrueFalseTotal
if $r eq "0E0" 0 0 0
230 0 ▶ return -$r if ($r eq Type::Tiny::CMP_SUPERTYPE() || $r eq Type::Tiny::CMP_SUBTYPE());
Branch: 1 branch
BranchTrueFalseTotal
if $r eq -1 or $r eq 1 0 0 0
Condition0%0/3
$r eq -1 or $r eq 1
l!l&&r!l&&!r
000
MC/DC0%0/2
$r eq -1 or $r eq 1
$r eq -1 $r eq 1
Truth table0%0/3
A or B
A= $r eq -1B= $r eq 1
ABresult
1X1
011
000
231
232 0 Type::Tiny::CMP_UNKNOWN();
233 6 };
234  
235 6 push @Type::Tiny::CMP, $comparator;
236  
237 6 __PACKAGE__->meta->make_immutable;
238  
239  
240 __END__
↑ Summary