blib/lib/DTL/Fast/Context.pm

filter: Statementstmt 96.1% 75 / 78 Branchbran 88.2% 30 / 34 Conditioncond 50.0% 17 / 34 MC/DCmcdc 21.0% 4 / 19 Subroutinesub 100% 12 / 12 Podpod 0.0% 0 / 8 totaltotal 74.5% 138 / 185 SCAR 36.7
CC
34
Cov
84%
traverse
13.7
set
13.4
get
6.0
CRAP 39.1
« blib/lib/DTL/Fast/Cache/File.pm ↑ Summary blib/lib/DTL/Fast/Cache/Serialized.pm »
1 package DTL::Fast::Context;
2 99 ▶use strict; use utf8; use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 99 ▶use Scalar::Util qw(reftype blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 sub new
7 {
8 617 ▶ my( $proto, $context ) = @_;
Subroutine new: called CC=3 SCAR=11.0
Pod undocumented
9 617 ▶ $context //= {};
Condition100%2/2
$context //= {}
l!l
6143
MC/DC100%1/1
$context //= {}
$context
Truth table100%2/2
A
A= $context
Aresult
11
00
10  
11 617 ▶ die "Context should be a HASH reference"
Branch: 1 branch
BranchTrueFalseTotal
if ref $context ne "HASH" 0 617 617
12 if ref $context ne 'HASH';
13  
14 617 return bless {
15 'ns' => [$context]
16 }, $proto;
17 }
18  
19 sub get
20 {
21 1209 ▶ my( $self, $variable_path, $source_object ) = @_;
Subroutine get: called CC=6 SCAR=18.0
Pod undocumented
22  
23 1209 ▶ if( ref $variable_path ne 'ARRAY' ) # suppose that raw variable invoked, period separated
Branch: 1 branch
BranchTrueFalseTotal
if (ref $variable_path ne 'ARRAY') { } 956 253 1209
24 {
25 956 $variable_path = [split /\.+/x, $variable_path];
26 }
27 else
28 {
29 253 $variable_path = [@$variable_path]; # cloning for re-use
30 }
31  
32 1209 my $variable_name = shift @$variable_path;
33  
34 # faster version
35 1209 my $namespace = $self->{'ns'}->[-1];
36 my $variable = exists $namespace->{$variable_name} ?
37 1209 ▶ $namespace->{$variable_name}
Branch: 1 branch
BranchTrueFalseTotal
exists $namespace->{$variable_name} ? : 1206 3 1209
38 : undef;
39  
40 1209 ▶ while( ref $variable eq 'CODE' )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $variable eq "CODE") 0 1209 1209
41 {
42 0 $variable = $variable->();
43 }
44  
45 1209 ▶ $variable = $self->traverse($variable, $variable_path, $source_object)
Branch: 1 branch
BranchTrueFalseTotal
if defined $variable and scalar @$variable_path 254 955 1209
Condition100%2/2
defined $variable and scalar @$variable_path
!ll
101199
MC/DC100%1/1
defined $variable and scalar @$variable_path
defined $variable
Truth table100%2/2
A
A= defined $variable
Aresult
00
11
46 if
47 defined $variable
48 and scalar @$variable_path;
49  
50 1205 return $variable;
51 }
52  
53 # tracing variable path
54 sub traverse
55 {
56 284 ▶ my( $self, $variable, $path, $source_object ) = @_;
Subroutine traverse: called CC=13 SCAR=26.2
Pod undocumented
57  
58 284 my $variable_original = $variable;
59
60 284 foreach my $step (@$path)
61 {
62 339 my $current_type = reftype $variable;
63 339 ▶ if(
Branch: 4 branches
BranchTrueFalseTotal
if (builtin::blessed($variable) and $variable->can($step)) { } 3 336 339
elsif (not defined $current_type) { } 2 334 336
elsif ($current_type eq 'HASH') { } 214 120 334
elsif ($current_type eq 'ARRAY' and $step =~ /^\-?\d+$/x) { } 118 2 120
Condition66%2/3
builtin::blessed($variable) and $variable->can($step)
!ll&&!rl&&r
33603
MC/DC50%1/2
builtin::blessed($variable) and $variable->can($step)
builtin::blessed($variable) $variable->can($step)
Truth table66%2/3
A and B
A= builtin::blessed($variable)B= $variable->can($step)
ABresult
0X0
100
111
64 blessed($variable)
65 and $variable->can($step) )
66 {
67 3 $variable = $variable->$step();
68 }
69 elsif( not defined $current_type )
70 {
71 2 ▶ die $self->get_error(
Condition50%1/2
$step // "undef"
l!l
20
MC/DC0%0/1
$step // "undef"
$step
Truth table50%1/2
A
A= $step
Aresult
11
00
72 $source_object
73 , sprintf( "non-reference value encountered on step `%s` while traversing context path", $step // 'undef' )
74 , 'Traversing path' => join( '.', @$path )
75 , 'Traversed variable' => $self->dump_with_indent($variable_original)
76 );
77 }
78 elsif( $current_type eq 'HASH' )
79 {
80 214 $variable = $variable->{$step};
81 }
82 ▶ elsif(
Condition66%2/3
$current_type eq "ARRAY" and $step =~ /^\-?\d+$/x
!ll&&!rl&&r
02118
MC/DC50%1/2
$current_type eq "ARRAY" and $step =~ /^\-?\d+$/x
$current_type eq "ARRAY" $step =~ /^\-?\d+$/x
Truth table66%2/3
A and B
A= $current_type eq "ARRAY"B= $step =~ /^\-?\d+$/x
ABresult
0X0
100
111
83 $current_type eq 'ARRAY'
84 and $step =~ /^\-?\d+$/x
85 )
86 {
87 118 $variable = $variable->[$step];
88 }
89 else
90 {
91 2 ▶ die $self->get_error(
Condition50%3/6
ref $variable || 'SCALAR'
l!l
20
builtin::reftype($variable) // "not blessed"
l!l
20
$step // "undef"
l!l
20
MC/DC0%0/1
ref $variable || 'SCALAR'
ref $variable
MC/DC0%0/1
builtin::reftype($variable) // "not blessed"
builtin::reftype($variable)
MC/DC0%0/1
$step // "undef"
$step
Truth table50%1/2
A
A= ref $variable
Aresult
11
00
Truth table50%1/2
A
A= builtin::reftype($variable)
Aresult
11
00
Truth table50%1/2
A
A= $step
Aresult
11
00
92 $source_object
93 , sprintf(
94 "don't know how continue traversing %s (%s) with step `%s`"
95 , ref $variable || 'SCALAR'
96 , reftype $variable // 'not blessed'
97 , $step // 'undef'
98 )
99 , 'Traversing path' => join( '.', @$path )
100 , 'Traversable variable' => $self->dump_with_indent($variable_original)
101 );
102 }
103 }
104  
105 280 ▶ while( ref $variable eq 'CODE' )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $variable eq "CODE") 4 280 284
106 {
107 4 $variable = $variable->();
108 }
109  
110 280 return $variable;
111 }
112  
113 sub dump_with_indent
114 {
115 4 ▶ my ($self, $object ) = @_;
Subroutine dump_with_indent: called CC=1 SCAR=0.0
Pod undocumented
116 4 require Data::Dumper;
117 4 my $result = Data::Dumper->Dump([$object]);
118 # $result =~ s/^/ /mg; # now done in Entity::compile_error_messages
119 4 return $result;
120 }
121  
122 sub set
123 {
124 1501 ▶ my( $self, @sets ) = @_;
Subroutine set: called CC=11 SCAR=25.9
Pod undocumented
125  
126 1501 ▶ while( scalar @sets > 1 )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @sets > 1) 2486 1500 3986
127 {
128 2486 my $key = shift @sets;
129 2486 my $val = shift @sets;
130 2486 ▶ if( $key =~ /\./x ) # traversed set
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /\./x) { } 4 2482 2486
131 {
132 4 my @key = split /\.+/x, $key;
133 4 my $variable_name = pop @key;
134 4 my $variable = $self->get([@key]);
135  
136 4 ▶ die sprintf('Unable to set variable %s because parent %s is not defined.'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $variable 1 3 4
Condition50%2/4
$key // "undef"
l!l
10
join(".", @key) // "undef"
l!l
10
MC/DC0%0/1
$key // "undef"
$key
MC/DC0%0/1
join(".", @key) // "undef"
join(".", @key)
Truth table50%1/2
A
A= $key
Aresult
11
00
Truth table50%1/2
A
A= join(".", @key)
Aresult
11
00
137 , $key // 'undef'
138 , join('.', @key) // 'undef'
139 ) if not defined $variable;
140  
141 3 my $variable_type = ref $variable;
142 3 ▶ if( $variable_type eq 'HASH' )
Branch: 2 branches
BranchTrueFalseTotal
if ($variable_type eq 'HASH') { } 1 2 3
elsif ($variable_type eq 'ARRAY' and $variable_name =~ /^\-?\d+$/x) { } 2 0 2
143 {
144 1 $variable->{$variable_name} = $val;
145 }
146 ▶ elsif(
Condition33%1/3
$variable_type eq "ARRAY" and $variable_name =~ /^\-?\d+$/x
!ll&&!rl&&r
002
MC/DC0%0/2
$variable_type eq "ARRAY" and $variable_name =~ /^\-?\d+$/x
$variable_type eq "ARRAY" $variable_name =~ /^\-?\d+$/x
Truth table33%1/3
A and B
A= $variable_type eq "ARRAY"B= $variable_name =~ /^\-?\d+$/x
ABresult
0X0
100
111
147 $variable_type eq 'ARRAY'
148 and $variable_name =~ /^\-?\d+$/x
149 )
150 {
151 2 $variable->[$variable_name] = $val;
152 }
153 else
154 {
155 0 ▶ die sprintf("Don't know how to set variable %s for parent node of type %s"
Condition0%0/4
$variable_name // "undef"
l!l
00
$variable_type // "undef"
l!l
00
MC/DC0%0/1
$variable_name // "undef"
$variable_name
MC/DC0%0/1
$variable_type // "undef"
$variable_type
Truth table0%0/2
A
A= $variable_name
Aresult
11
00
Truth table0%0/2
A
A= $variable_type
Aresult
11
00
156 , $variable_name // 'undef'
157 , $variable_type // 'undef'
158 );
159 }
160 }
161 else
162 {
163 2482 $self->{'ns'}->[-1]->{$key} = $val;
164 }
165 }
166 1500 return $self;
167 }
168  
169 sub get_error
170 {
171 4 ▶ my ($self, $source_object, $message, @messages) = @_;
Subroutine get_error: called CC=3 SCAR=12.3
Pod undocumented
172
173 4 my $result;
174 4 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($source_object) and $source_object->can('get_render_error')) { } 4 0 4
Condition33%1/3
builtin::blessed($source_object) and $source_object->can("get_render_error")
!ll&&!rl&&r
004
MC/DC0%0/2
builtin::blessed($source_object) and $source_object->can("get_render_error")
builtin::blessed($source_object) $source_object->can("get_render_error")
Truth table33%1/3
A and B
A= builtin::blessed($source_object)B= $source_object->can("get_render_error")
ABresult
0X0
100
111
175 blessed $source_object
176 and $source_object->can('get_render_error')
177 ){
178 4 $result = $source_object->get_render_error(
179 $self
180 , $message
181 , @messages
182 );
183 } else {
184 0 $result = sprintf <<'_EOT_'
185 Rendering error: %s
186 %s
187 _EOT_
188 , $message
189 , join '', @messages
190 ;
191 }
192 4 return $result;
193 }
194  
195 sub push_scope
196 {
197 2432 ▶ my( $self ) = @_;
Subroutine push_scope: called CC=2 SCAR=7.0
Pod undocumented
198 2432 ▶ push @{$self->{'ns'}}, {%{$self->{'ns'}->[-1] // {}}};
Condition50%1/2
$self->{"ns"}[-1] // {}
l!l
24320
MC/DC0%0/1
$self->{"ns"}[-1] // {}
$self->{"ns"}[-1]
Truth table50%1/2
A
A= $self->{"ns"}[-1]
Aresult
11
00
199 2432 return $self;
200 }
201  
202 sub pop_scope
203 {
204 2399 ▶ my( $self ) = @_;
Subroutine pop_scope: called CC=2 SCAR=6.9
Pod undocumented
205 die "It's a last context layer available."
206 2399 ▶ if scalar @{$self->{'ns'}} == 1;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{$self->{"ns"};} == 1 1 2398 2399
207 2398 pop @{$self->{'ns'}};
208 2398 return $self;
209 }
210  
211 99 1;
« blib/lib/DTL/Fast/Cache/File.pm ↑ Summary blib/lib/DTL/Fast/Cache/Serialized.pm »