blib/lib/Template/Liquid/Context.pm

filter: Statementstmt 81.7% 121 / 148 Branchbran 80.0% 88 / 110 Conditioncond 35.0% 7 / 20 MC/DCmcdc 36.3% 4 / 11 Subroutinesub 66.6% 14 / 21 Podpod 0.0% 0 / 7 totaltotal 73.8% 234 / 317 CC 65 SCAR 32.3
CC
65
Cov
78%
_merge
72.0
get
33.2
set
23.5
CRAP 25.3
↑ Summary blib/lib/Template/Liquid/Tag/For.pm »
1 package Template::Liquid::Context;
2 25 our $VERSION = '1.0.22';
3 25 require Template::Liquid::Utility;
4 25 require Template::Liquid::Error;
5 25 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 25 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 25 ▶use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 sub new {
10 360 ▶ my ($class, %args) = @_;
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
11 return bless {scopes => [$args{assigns}],
12 template => $args{template}, # Required
13 360 errors => []
14 }, $class;
15 }
16  
17 sub push {
18 123 ▶ my ($s, $context) = @_;
Subroutine push: called CC=3 SCAR=11.0
Pod undocumented
19 return
20 raise Template::Liquid::Error {type => 'Stack',
21 template => $s->{template},
22 message => 'Cannot push new scope!'
23 }
24 123 ▶ if scalar @{$s->{'scopes'}} == 100;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{$s->{"scopes"};} == 100 0 123 123
25 123 ▶ return push @{$s->{'scopes'}}, (defined $context ? $context : {});
Branch: 1 branch
BranchTrueFalseTotal
defined $context ? : 18 105 123
26 }
27  
28 sub pop {
29 123 ▶ my ($s) = @_;
Subroutine pop: called CC=2 SCAR=7.0
Pod undocumented
30 return
31 raise Template::Liquid::Error {type => 'Stack',
32 template => $s->{template},
33 message => 'Cannot pop scope!'
34 }
35 123 ▶ if scalar @{$s->{'scopes'}} == 1;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{$s->{"scopes"};} == 1 0 123 123
36 123 return pop @{$s->{'scopes'}};
37 }
38  
39 sub stack {
40 105 ▶ my ($s, $block) = @_;
Subroutine stack: called CC=1 SCAR=0.0
Pod undocumented
41 105 my $old_scope = $s->{scopes}[-1];
42 105 $s->push();
43 105 $s->merge($old_scope);
44 105 my $result = $block->($s);
45 105 $s->pop;
46 105 return $result;
47 }
48  
49 sub merge {
50 105 ▶ my ($s, $new) = @_;
Subroutine merge: called CC=1 SCAR=0.0
Pod undocumented
51 105 return $s->{'scopes'}->[0] = __merge(reverse $s->{scopes}[-1], $new);
52 }
53  
54 sub _merge { # Deeply merges data structures
55 0 ▶ my ($source, $target) = @_;
Subroutine _merge: not called CC=8 SCAR=42.8
56 0 my $return = $target;
57 0 for (keys %$source) {
58 0 ▶ if ('ARRAY' eq ref $target->{$_} &&
Branch: 2 branches
BranchTrueFalseTotal
if ('ARRAY' eq ref $target->{$_} and 'ARRAY' eq ref $source->{$_} || !ref($source->{$_})) { } 0 0 0
elsif ('HASH' eq ref $target->{$_} and 'HASH' eq ref $source->{$_} || !ref($source->{$_})) { } 0 0 0
Condition0%0/6
"ARRAY" eq ref $target->{$_} and "ARRAY" eq ref $source->{$_} || !ref($source->{$_})
!ll&&!rl&&r
000
"ARRAY" eq ref $source->{$_} or not ref $source->{$_}
l!l&&r!l&&!r
000
MC/DC0%0/3
"ARRAY" eq ref $target->{$_} and "ARRAY" eq ref $source->{$_} || !ref($source->{$_})
"ARRAY" eq ref $target->{$_} "ARRAY" eq ref $source->{$_} not ref $source->{$_}
Truth table0%0/4
A and B or not(C)
A= "ARRAY" eq ref $target->{$_}B= "ARRAY" eq ref $source->{$_}C= not ref $source->{$_}
ABCresult
0XX0
1000
11X1
1011
59 ('ARRAY' eq ref $source->{$_} || !ref $source->{$_})) {
60 0 @{$return->{$_}} = [@{$target->{$_}}, @{$source->{$_}}];
61 }
62 ▶ elsif ('HASH' eq ref $target->{$_} &&
Condition0%0/6
"HASH" eq ref $target->{$_} and "HASH" eq ref $source->{$_} || !ref($source->{$_})
!ll&&!rl&&r
000
"HASH" eq ref $source->{$_} or not ref $source->{$_}
l!l&&r!l&&!r
000
MC/DC0%0/3
"HASH" eq ref $target->{$_} and "HASH" eq ref $source->{$_} || !ref($source->{$_})
"HASH" eq ref $target->{$_} "HASH" eq ref $source->{$_} not ref $source->{$_}
Truth table0%0/4
A and B or not(C)
A= "HASH" eq ref $target->{$_}B= "HASH" eq ref $source->{$_}C= not ref $source->{$_}
ABCresult
0XX0
1000
11X1
1011
63 ('HASH' eq ref $source->{$_} || !ref $source->{$_})) {
64 0 $return->{$_} = _merge($source->{$_}, $target->{$_});
65 }
66 0 else { $return->{$_} = $source->{$_}; }
67 }
68 0 return $return;
69 }
70 25 my $merge_precedent;
71  
72 sub __merge { # unless right is more interesting, this is a left-
73 105 ▶ my $return = $_[1]; # precedent merge function
Subroutine __merge: called CC=4 SCAR=15.4
74 $merge_precedent ||= {
75 15 ▶ SCALAR => {SCALAR => sub { defined $_[0] ? $_[0] : $_[1] },
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 14 1 15
76 0 ▶ ARRAY => sub { $_[1] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
77 0 ▶ HASH => sub { $_[1] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
78 },
79 ARRAY => {
80 SCALAR => sub {
81 64 ▶ [@{$_[0]}, defined $_[1] ? $_[1] : ()];
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
defined $_[1] ? : 0 64 64
82 },
83 0 ▶ ARRAY => sub { [@{$_[0]}] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
84 0 ▶ HASH => sub { [@{$_[0]}, values %{$_[1]}] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
85 },
86 41 ▶ HASH => {SCALAR => sub { $_[0] },
Subroutine __ANON__: called CC=1 SCAR=0.0
87 0 ▶ ARRAY => sub { $_[0] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
88 0 ▶ HASH => sub { _merge($_[0], $_[1], $_[2]) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
89 }
90 105 ▶ };
Condition100%2/2
$merge_precedent ||= {"SCALAR", {"SCALAR", sub {
l!l
978
MC/DC100%1/1
$merge_precedent ||= {"SCALAR", {"SCALAR", sub { defined $_[0] ? $_[0] : $_[1]; } , "ARRAY", sub { $_[1]; } , "HASH", sub { $_[1]; } }, "ARRAY", {"SCALAR", sub { [@{$_[0];}, defined $_[1] ? $_[1] : ()]; } , "ARRAY", sub { [@{$_[0];}]; } , "HASH", sub { [@{$_[0];}, values %{$_[1];}]; } }, "HASH", {"SCALAR", sub { $_[0]; } , "ARRAY", sub { $_[0]; } , "HASH", sub { _merge($_[0], $_[1], $_[2]); } }}
$merge_precedent
Truth table100%2/2
A
A= $merge_precedent
Aresult
11
00
91 105 for my $key (keys %{$_[0]}) {
92 my ($left_ref, $right_ref)
93 120 ▶ = map { ref($_->{$key}) =~ m[^(HASH|ARRAY)$]o ? $1 : 'SCALAR' }
Branch: 1 branch
BranchTrueFalseTotal
ref($_->{$key}) =~ /^(HASH|ARRAY)$/o ? : 105 135 240
94 ($_[0], $_[1]);
95  
96 #warn sprintf '%-12s [%6s|%-6s]', $key, $left_ref, $right_ref;
97 $return->{$key} = $merge_precedent->{$left_ref}{$right_ref}
98 120 ->($_[0]->{$key}, $_[1]->{$key});
99 }
100 105 return $return;
101 }
102  
103 sub get {
104 2443 ▶ my ($s, $var) = @_;
Subroutine get: called CC=33 SCAR=35.0
Pod undocumented
105 2443 ▶ return if !defined $var;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $var 6 2437 2443
106 2437 ▶ return $2 if $var =~ m[^(["'])(.+)\1$]o;
Branch: 1 branch
BranchTrueFalseTotal
if $var =~ /^(["'])(.+)\1$/o 362 2075 2437
107 2075 my @path = split $Template::Liquid::Utility::VariableAttributeSeparator,
108 $var;
109 2075 my $cursor = \$s->{scopes}[-1];
110 return $var
111 2075 ▶ if $var =~ m[^[-\+]?(\d*\.)?\d+$]o && !exists $$cursor->{$path[0]};
Branch: 1 branch
BranchTrueFalseTotal
if $var =~ /^[-\+]?(\d*\.)?\d+$/o and not exists $$$cursor{$path[0]} 847 1228 2075
Condition100%3/3
$var =~ /^[-\+]?(\d*\.)?\d+$/o and not exists $$$cursor{$path[0]}
!ll&&!rl&&r
12262847
MC/DC100%2/2
$var =~ /^[-\+]?(\d*\.)?\d+$/o and not exists $$$cursor{$path[0]}
$var =~ /^[-\+]?(\d*\.)?\d+$/o not exists $$$cursor{$path[0]}
Truth table100%3/3
A and not(B)
A= $var =~ /^[-\+]?(\d*\.)?\d+$/oB= not exists $$$cursor{$path[0]}
ABresult
0X0
100
111
112 1228 ▶ return if $var eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $var eq "" 12 1216 1228
113 1216 ▶ return '' if $var eq '""';
Branch: 1 branch
BranchTrueFalseTotal
if $var eq "\"\"" 5 1211 1216
114 1211 ▶ return "" if $var eq "''";
Branch: 1 branch
BranchTrueFalseTotal
if $var eq "''" 3 1208 1211
115 1208 ▶ return if $var eq 'null';
Branch: 1 branch
BranchTrueFalseTotal
if $var eq "null" 1 1207 1208
116 1207 ▶ return if $var eq 'nil';
Branch: 1 branch
BranchTrueFalseTotal
if $var eq "nil" 1 1206 1207
117 1206 ▶ return if $var eq 'blank';
Branch: 1 branch
BranchTrueFalseTotal
if $var eq "blank" 1 1205 1206
118 1205 ▶ return if $var eq 'empty';
Branch: 1 branch
BranchTrueFalseTotal
if $var eq "empty" 1 1204 1205
119 1204 ▶ return !1 if $var eq 'false';
Branch: 1 branch
BranchTrueFalseTotal
if $var eq "false" 3 1201 1204
120 1201 ▶ return !!1 if $var eq 'true';
Branch: 1 branch
BranchTrueFalseTotal
if $var eq "true" 4 1197 1201
121  
122 1197 ▶ if ($var =~ m[^\((\S+)\s*\.\.\s*(\S+)\)$]o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($var =~ /^\((\S+)\s*\.\.\s*(\S+)\)$/o) 30 1167 1197
123 30 return [$s->get($1) .. $s->get($2)]; # range
124 }
125  
126 # print STDERR "DEBUG:var=$var. about to get 1 and 2 from regex";
127 # return $s->get($1)->[$2] if $var =~ m'^(.+)\[(\d+)\]$'o; # array index myvar[2]
128 1167 ▶ if ($var =~ m'^(.+)\[(\d+)\]$'o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($var =~ /^(.+)\[(\d+)\]$/o) 4 1163 1167
129  
130 # print STDERR "DEBUG:array index. var=$var. 1=$1,2=$2";
131 4 my $arr = $s->get($1);
132 4 ▶ return $arr->[$2] if $arr;
Branch: 1 branch
BranchTrueFalseTotal
if $arr 4 0 4
133 0 return; # return if nothing
134 }
135  
136 # return $s->get($1)->{$2} if $var =~ m'^(.+)\[(.+)\]$'o;
137 1163 ▶ if ($var =~ m'^(.+)\[(.+)\]$'o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($var =~ /^(.+)\[(.+)\]$/o) 1 1162 1163
138  
139 # print STDERR "DEBUG:obj property. var=$var. 1=$1,2=$2";
140 1 my $obj = $s->get($1);
141 1 ▶ return $obj->{$2} if $obj;
Branch: 1 branch
BranchTrueFalseTotal
if $obj 1 0 1
142 0 return; # return if nothing
143 }
144 1162 ▶STEP: while (@path) {
Branch: 1 branch
BranchTrueFalseTotal
if (@path) 1359 0 1359
145 1359 my $crumb = shift @path;
146 1359 my $reftype = ref $$cursor;
147 1359 ▶ if (Scalar::Util::blessed($$cursor) && $$cursor->can($crumb)) {
Branch: 3 branches
BranchTrueFalseTotal
if (builtin::blessed($$cursor) and $$cursor->can($crumb)) { } 2 1357 1359
elsif ($reftype eq 'HASH') { } 1325 32 1357
elsif ($reftype eq 'ARRAY') { } 20 12 32
Condition66%2/3
builtin::blessed($$cursor) and $$cursor->can($crumb)
!ll&&!rl&&r
135702
MC/DC50%1/2
builtin::blessed($$cursor) and $$cursor->can($crumb)
builtin::blessed($$cursor) $$cursor->can($crumb)
Truth table66%2/3
A and B
A= builtin::blessed($$cursor)B= $$cursor->can($crumb)
ABresult
0X0
100
111
148 2 my $can = $$cursor->can($crumb);
149 2 my $val = $can->($$cursor);
150 2 ▶ return $val if !scalar @path;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @path 1 1 2
151 1 $cursor = \$val;
152 1 next STEP;
153 }
154 elsif ($reftype eq 'HASH') {
155 1325 ▶ if (exists $$cursor->{$crumb}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$cursor->{$crumb}) 1004 321 1325
156 1004 ▶ return $$cursor->{$crumb} if !@path;
Branch: 1 branch
BranchTrueFalseTotal
unless @path 813 191 1004
157 191 $cursor = \$$cursor->{$crumb};
158 191 next STEP;
159 }
160 321 return ();
161 }
162 elsif ($reftype eq 'ARRAY') {
163 20 ▶ return scalar @{$$cursor} if $crumb eq 'size';
Branch: 1 branch
BranchTrueFalseTotal
if $crumb eq "size" 2 18 20
164 18 ▶ $crumb = 0 if $crumb eq 'first';
Branch: 1 branch
BranchTrueFalseTotal
if $crumb eq "first" 2 16 18
165 18 ▶ $crumb = $#$$cursor if $crumb eq 'last';
Branch: 1 branch
BranchTrueFalseTotal
if $crumb eq "last" 2 16 18
166 18 ▶ return () if $crumb =~ m[\D]o;
Branch: 1 branch
BranchTrueFalseTotal
if $crumb =~ /\D/o 1 17 18
167 17 ▶ return () if scalar @$$cursor < $crumb;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @$$cursor < $crumb 4 13 17
168 13 ▶ return $$cursor->[$crumb] if !scalar @path;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @path 8 5 13
169 5 $cursor = \$$cursor->[$crumb];
170 5 next STEP;
171 }
172 12 return ();
173 }
174 }
175  
176 sub set {
177 854 ▶ my ($s, $var, $val) = @_;
Subroutine set: called CC=18 SCAR=31.6
Pod undocumented
178 854 my $var_reftype = ref $val;
179 854 my @path = split $Template::Liquid::Utility::VariableAttributeSeparator,
180 $var;
181 854 my $cursor = \$s->{scopes}[-1];
182 854 ▶ $cursor = \$$cursor->{shift @path} if (exists $$cursor->{$path[0]});
Branch: 1 branch
BranchTrueFalseTotal
if exists $$$cursor{$path[0]} 604 250 854
183 854 ▶STEP: while (@path) {
Branch: 1 branch
BranchTrueFalseTotal
if (@path) 280 585 865
184 280 my $crumb = shift @path;
185 280 my $reftype = ref $$cursor;
186 280 ▶ if ($reftype eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if ($reftype eq 'HASH') { } 274 6 280
elsif ($reftype eq 'ARRAY') { } 4 2 6
187 274 ▶ if (!@path) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @path) { } 266 8 274
188 266 ▶ if (exists $$cursor->{$crumb}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$cursor->{$crumb}) 14 252 266
189  
190 # TODO: If the reftype is different, mention it
191 }
192 266 return $$cursor->{$crumb} = $val;
193 }
194 else {
195 $$cursor->{$crumb} = $path[0] =~ m[\D] ? {} : []
196 8 ▶ if !exists $$cursor->{$crumb};
Branch: 2 branches
BranchTrueFalseTotal
unless exists $$cursor->{$crumb} 3 5 8
$path[0] =~ /\D/ ? : 3 0 3
197 8 $cursor = \$$cursor->{$crumb};
198 8 next STEP;
199 }
200 }
201 elsif ($reftype eq 'ARRAY') {
202 4 ▶ if ($crumb =~ m[\D]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($crumb =~ /\D/) 0 4 4
203  
204 # TODO: Let the user know
205 }
206 4 ▶ if (!@path) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @path) { } 1 3 4
207 1 ▶ if (exists $$cursor->[$crumb]) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$cursor->[$crumb]) 0 1 1
208  
209 # TODO: If the reftype is different, mention it
210 }
211 1 return $$cursor->[$crumb] = $val;
212 }
213 else {
214 3 ▶ $$cursor->[$crumb] = $path[0] =~ m[\D] ? {} : []
Branch: 2 branches
BranchTrueFalseTotal
unless exists $$cursor->[$crumb] 0 3 3
$path[0] =~ /\D/ ? : 0 0 0
215 if !exists $$cursor->[$crumb];
216 3 $cursor = \$$cursor->[$crumb];
217 3 next STEP;
218 }
219 }
220 else {
221 2 ▶ if (!@path) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @path) { } 2 0 2
222 2 ▶ if ($crumb =~ m[\D]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($crumb =~ /\D/) 2 0 2
223 2 $$cursor = {};
224 2 return $$cursor->{$crumb} = $val;
225 }
226 0 $$cursor = [];
227 0 return $$cursor->[$crumb] = $val;
228 }
229 else {
230 0 ▶ $$cursor->{$crumb} = $path[0] =~ m[\D] ? {} : []
Branch: 2 branches
BranchTrueFalseTotal
unless exists $$cursor->[$crumb] 0 0 0
$path[0] =~ /\D/ ? : 0 0 0
231 if !exists $$cursor->[$crumb];
232 0 $cursor = \$$cursor->{$crumb};
233 0 next STEP;
234 }
235 }
236 }
237 585 return $$cursor = $val;
238 }
239 25 1;
240  
241 =pod
242  
243 =encoding UTF-8
244  
245 =head1 NAME
246  
247 Template::Liquid::Context - Complex Variable Keeper
248  
249 =head1 Description
250  
251 This is really only to be used internally.
252  
253 =head1 Author
254  
255 Sanko Robinson <sanko@cpan.org> - http://sankorobinson.com/
256  
257 CPAN ID: SANKO
258  
259 =head1 License and Legal
260  
261 Copyright (C) 2009-2022 by Sanko Robinson E<lt>sanko@cpan.orgE<gt>
262  
263 This program is free software; you can redistribute it and/or modify it under
264 the terms of L<The Artistic License
265 2.0|http://www.perlfoundation.org/artistic_license_2_0>. See the F<LICENSE>
266 file included with this distribution or L<notes on the Artistic License
267 2.0|http://www.perlfoundation.org/artistic_2_0_notes> for clarification.
268  
269 When separated from the distribution, all original POD documentation is covered
270 by the L<Creative Commons Attribution-Share Alike 3.0
271 License|http://creativecommons.org/licenses/by-sa/3.0/us/legalcode>. See the
272 L<clarification of the
273 CCA-SA3.0|http://creativecommons.org/licenses/by-sa/3.0/us/>.
274  
275 =cut
↑ Summary blib/lib/Template/Liquid/Tag/For.pm »