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 Liquid/Tag/For.pm »
1 package Template::Liquid::Context;
2 26 our $VERSION = '1.0.23';
3 26 require Template::Liquid::Utility;
4 26 require Template::Liquid::Error;
5 26 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 26 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 26 ▶use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 370 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
10 370 my ($class, %args) = @_;
11 return bless {scopes => [$args{assigns}],
12 template => $args{template}, # Required
13 370 errors => []
14 }, $class;
15 }
16  
17 127 ▶sub push {
Subroutine push: called CC=3 SCAR=11.0
Pod undocumented
18 127 my ($s, $context) = @_;
19 return
20 raise Template::Liquid::Error {type => 'Stack',
21 template => $s->{template},
22 message => 'Cannot push new scope!'
23 }
24 127 ▶ if scalar @{$s->{'scopes'}} == 100;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{$s->{"scopes"};} == 100 0 127 127
25 127 ▶ return push @{$s->{'scopes'}}, (defined $context ? $context : {});
Branch: 1 branch
BranchTrueFalseTotal
defined $context ? : 20 107 127
26 }
27  
28 127 ▶sub pop {
Subroutine pop: called CC=2 SCAR=7.0
Pod undocumented
29 127 my ($s) = @_;
30 return
31 raise Template::Liquid::Error {type => 'Stack',
32 template => $s->{template},
33 message => 'Cannot pop scope!'
34 }
35 127 ▶ if scalar @{$s->{'scopes'}} == 1;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{$s->{"scopes"};} == 1 0 127 127
36 127 return pop @{$s->{'scopes'}};
37 }
38  
39 107 ▶sub stack {
Subroutine stack: called CC=1 SCAR=0.0
Pod undocumented
40 107 my ($s, $block) = @_;
41 107 my $old_scope = $s->{scopes}[-1];
42 107 $s->push();
43 107 $s->merge($old_scope);
44 107 my $result = $block->($s);
45 107 $s->pop;
46 107 return $result;
47 }
48  
49 107 ▶sub merge {
Subroutine merge: called CC=1 SCAR=0.0
Pod undocumented
50 107 my ($s, $new) = @_;
51 107 return $s->{'scopes'}->[0] = __merge(reverse $s->{scopes}[-1], $new);
52 }
53  
54 0 ▶sub _merge { # Deeply merges data structures
Subroutine _merge: not called CC=8 SCAR=42.8
55 0 my ($source, $target) = @_;
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 26 my $merge_precedent;
71  
72 107 ▶sub __merge { # unless right is more interesting, this is a left-
Subroutine __merge: called CC=4 SCAR=15.4
73 107 my $return = $_[1]; # precedent merge function
74 $merge_precedent ||= {
75 18 ▶ SCALAR => {SCALAR => sub { defined $_[0] ? $_[0] : $_[1] },
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 17 1 18
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 65 ▶ SCALAR => sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
81 65 ▶ [@{$_[0]}, defined $_[1] ? $_[1] : ()];
Branch: 1 branch
BranchTrueFalseTotal
defined $_[1] ? : 0 65 65
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 107 ▶ };
Condition100%2/2
$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]); } }}
l!l
998
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 107 for my $key (keys %{$_[0]}) {
92 my ($left_ref, $right_ref)
93 124 ▶ = map { ref($_->{$key}) =~ m[^(HASH|ARRAY)$]o ? $1 : 'SCALAR' }
Branch: 1 branch
BranchTrueFalseTotal
ref($_->{$key}) =~ /^(HASH|ARRAY)$/o ? : 106 142 248
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 124 ->($_[0]->{$key}, $_[1]->{$key});
99 }
100 107 return $return;
101 }
102  
103 2101 ▶sub get {
Subroutine get: called CC=33 SCAR=35.0
Pod undocumented
104 2101 my ($s, $var) = @_;
105 2101 ▶ return if !defined $var;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $var 6 2095 2101
106 2095 ▶ return $2 if $var =~ m[^(["'])(.+)\1$]o;
Branch: 1 branch
BranchTrueFalseTotal
if $var =~ /^(["'])(.+)\1$/o 311 1784 2095
107 1784 my @path = split $Template::Liquid::Utility::VariableAttributeSeparator,
108 $var;
109 1784 my $cursor = \$s->{scopes}[-1];
110 return $var
111 1784 ▶ 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]} 731 1053 1784
Condition100%3/3
$var =~ /^[-\+]?(\d*\.)?\d+$/o and not exists $$$cursor{$path[0]}
!ll&&!rl&&r
10512731
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 1053 ▶ return if $var eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $var eq "" 12 1041 1053
113 1041 ▶ return '' if $var eq '""';
Branch: 1 branch
BranchTrueFalseTotal
if $var eq "\"\"" 5 1036 1041
114 1036 ▶ return "" if $var eq "''";
Branch: 1 branch
BranchTrueFalseTotal
if $var eq "''" 3 1033 1036
115 1033 ▶ return if $var eq 'null';
Branch: 1 branch
BranchTrueFalseTotal
if $var eq "null" 1 1032 1033
116 1032 ▶ return if $var eq 'nil';
Branch: 1 branch
BranchTrueFalseTotal
if $var eq "nil" 1 1031 1032
117 1031 ▶ return if $var eq 'blank';
Branch: 1 branch
BranchTrueFalseTotal
if $var eq "blank" 1 1030 1031
118 1030 ▶ return if $var eq 'empty';
Branch: 1 branch
BranchTrueFalseTotal
if $var eq "empty" 1 1029 1030
119 1029 ▶ return !1 if $var eq 'false';
Branch: 1 branch
BranchTrueFalseTotal
if $var eq "false" 3 1026 1029
120 1026 ▶ return !!1 if $var eq 'true';
Branch: 1 branch
BranchTrueFalseTotal
if $var eq "true" 4 1022 1026
121  
122 1022 ▶ if ($var =~ m[^\((\S+)\s*\.\.\s*(\S+)\)$]o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($var =~ /^\((\S+)\s*\.\.\s*(\S+)\)$/o) 30 992 1022
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 992 ▶ if ($var =~ m'^(.+)\[(\d+)\]$'o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($var =~ /^(.+)\[(\d+)\]$/o) 4 988 992
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 988 ▶ if ($var =~ m'^(.+)\[(.+)\]$'o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($var =~ /^(.+)\[(.+)\]$/o) 1 987 988
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 987 ▶STEP: while (@path) {
Branch: 1 branch
BranchTrueFalseTotal
if (@path) 1178 0 1178
145 1178 my $crumb = shift @path;
146 1178 my $reftype = ref $$cursor;
147 1178 ▶ if (Scalar::Util::blessed($$cursor) && $$cursor->can($crumb)) {
Branch: 3 branches
BranchTrueFalseTotal
if (builtin::blessed($$cursor) and $$cursor->can($crumb)) { } 2 1176 1178
elsif ($reftype eq 'HASH') { } 1150 26 1176
elsif ($reftype eq 'ARRAY') { } 20 6 26
Condition66%2/3
builtin::blessed($$cursor) and $$cursor->can($crumb)
!ll&&!rl&&r
117602
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 1150 ▶ if (exists $$cursor->{$crumb}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$cursor->{$crumb}) 942 208 1150
156 942 ▶ return $$cursor->{$crumb} if !@path;
Branch: 1 branch
BranchTrueFalseTotal
unless @path 757 185 942
157 185 $cursor = \$$cursor->{$crumb};
158 185 next STEP;
159 }
160 208 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 6 return ();
173 }
174 }
175  
176 862 ▶sub set {
Subroutine set: called CC=18 SCAR=31.6
Pod undocumented
177 862 my ($s, $var, $val) = @_;
178 862 my $var_reftype = ref $val;
179 862 my @path = split $Template::Liquid::Utility::VariableAttributeSeparator,
180 $var;
181 862 my $cursor = \$s->{scopes}[-1];
182 862 ▶ $cursor = \$$cursor->{shift @path} if (exists $$cursor->{$path[0]});
Branch: 1 branch
BranchTrueFalseTotal
if exists $$$cursor{$path[0]} 606 256 862
183 862 ▶STEP: while (@path) {
Branch: 1 branch
BranchTrueFalseTotal
if (@path) 286 587 873
184 286 my $crumb = shift @path;
185 286 my $reftype = ref $$cursor;
186 286 ▶ if ($reftype eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if ($reftype eq 'HASH') { } 280 6 286
elsif ($reftype eq 'ARRAY') { } 4 2 6
187 280 ▶ if (!@path) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @path) { } 272 8 280
188 272 ▶ if (exists $$cursor->{$crumb}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$cursor->{$crumb}) 14 258 272
189  
190 # TODO: If the reftype is different, mention it
191 }
192 272 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 587 return $$cursor = $val;
238 }
239 26 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 Liquid/Tag/For.pm »