blib/lib/DTL/Fast/Tag/For.pm

filter: Statementstmt 94.5% 122 / 129 Branchbran 80.4% 37 / 46 Conditioncond 25.0% 7 / 28 MC/DCmcdc 7.1% 1 / 14 Subroutinesub 100% 16 / 16 Podpod 0.0% 0 / 10 totaltotal 75.3% 183 / 243 SCAR 38.1
CC
37
Cov
82%
render
22.5
render_array
13.1
render_hash
6.1
CRAP 45.3
« blib/lib/DTL/Fast/Expression/Operator/Binary/Eq.pm ↑ Summary blib/lib/DTL/Fast/Cache/File.pm »
1 package DTL::Fast::Tag::For;
2 10 ▶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 10 ▶use parent 'DTL::Fast::Tag';
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 10 $DTL::Fast::TAG_HANDLERS{'for'} = __PACKAGE__;
6  
7 10 ▶use DTL::Fast::Variable;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 10 ▶use Scalar::Util qw(blessed reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 #@Override
11 59 ▶sub get_close_tag{ return 'endfor';}
Subroutine get_close_tag: called CC=1 SCAR=0.0
Pod undocumented
12  
13 #@Override
14 sub parse_parameters
15 {
16 40 ▶ my( $self ) = @_;
Subroutine parse_parameters: called CC=5 SCAR=16.4
Pod undocumented
17  
18 40 my(@target_names, $source_name, $reversed);
19 40 ▶ if( $self->{'parameter'} =~ /^\s*(.+)\s+in\s+(.+?)\s*(reversed)?\s*$/si )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'parameter'} =~ /^\s*(.+)\s+in\s+(.+?)\s*(reversed)?\s*$/is) { } 40 0 40
20 {
21 40 $source_name = $2;
22 40 $reversed = $3;
23 @target_names = map{
24 40 ▶ die $self->get_parse_error("iterator variable can't be traversable: $_") if /\./;
Branch: 1 branch
BranchTrueFalseTotal
if /\./ 0 51 51
25 51 $_;
26 } split( /\s*,\s*/, $1 );
27 }
28 else
29 {
30 0 die $self->get_parse_error("do not understand condition: $self->{'parameter'}");
31 }
32  
33 40 $self->{'renderers'} = [];
34 40 $self->add_renderer();
35  
36 40 $self->{'targets'} = [@target_names];
37  
38 40 $self->{'source'} = DTL::Fast::Variable->new($source_name);
39  
40 40 ▶ if( $reversed )
Branch: 1 branch
BranchTrueFalseTotal
if ($reversed) 1 39 40
41 {
42 1 $self->{'source'}->add_filter('reverse');
43 }
44  
45 40 ▶ if( not scalar @{$self->{'targets'}} )
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @{$self->{"targets"};}) 0 40 40
46 {
47 0 die $self->get_parse_error("there is no target variables defined for iteration");
48 }
49  
50 40 return $self;
51 }
52  
53 #@Override
54 sub add_chunk
55 {
56 287 ▶ my( $self, $chunk ) = @_;
Subroutine add_chunk: called CC=1 SCAR=0.0
Pod undocumented
57  
58 287 $self->{'renderers'}->[-1]->add_chunk($chunk);
59 287 return $self;
60 }
61  
62 #@Override
63 sub parse_tag_chunk
64 {
65 63 ▶ my( $self, $tag_name, $tag_param, $chunk_lines ) = @_;
Subroutine parse_tag_chunk: called CC=3 SCAR=11.0
Pod undocumented
66  
67 63 my $result = undef;
68  
69 63 ▶ if( $tag_name eq 'empty' )
Branch: 1 branch
BranchTrueFalseTotal
if ($tag_name eq 'empty') { } 4 59 63
70 {
71 4 ▶ if( scalar @{$self->{'renderers'}} == 2 )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$self->{'renderers'};} == 2) { } 1 3 4
72 {
73 1 die $self->get_block_parse_error("there can be only one {% empty %} block, ingoring");
74 }
75 else
76 {
77 3 $self->add_renderer;
78 }
79 3 $DTL::Fast::Template::CURRENT_TEMPLATE_LINE += $chunk_lines;
80 }
81 else
82 {
83 59 $result = $self->SUPER::parse_tag_chunk($tag_name, $tag_param, $chunk_lines);
84 }
85  
86 62 return $result;
87 }
88  
89 #@Override
90 sub render
91 {
92 45 ▶ my( $self, $context ) = @_;
Subroutine render: called CC=10 SCAR=31.1
Pod undocumented
93  
94 45 my $result = '';
95  
96 45 my $source_data = $self->{'source'}->render($context);
97 45 my $source_ref = ref $source_data;
98 45 my $source_type = reftype $source_data;
99  
100 45 ▶ if( # iterating array
Branch: 2 branches
BranchTrueFalseTotal
if ($source_ref eq 'ARRAY' or UNIVERSAL::can($source_data, 'as_array') and $source_data = $source_data->as_array($context)) { } 43 2 45
elsif ($source_ref eq 'HASH' or UNIVERSAL::can($source_data, 'as_hash') and $source_data = $source_data->as_hash($context)) { } 2 0 2
Condition50%3/6
$source_ref eq "ARRAY" or UNIVERSAL::can($source_data, "as_array") and $source_data = $source_data->as_array($context)
l!l&&r!l&&!r
4302
UNIVERSAL::can($source_data, "as_array") and $source_data = $source_data->as_array($context)
!ll&&!rl&&r
200
MC/DC33%1/3
$source_ref eq "ARRAY" or UNIVERSAL::can($source_data, "as_array") and $source_data = $source_data->as_array($context)
$source_ref eq "ARRAY" UNIVERSAL::can($source_data, "as_array") $source_data = $source_data->as_array($context)
Truth table50%2/4
A or B and C
A= $source_ref eq "ARRAY"B= UNIVERSAL::can($source_data, "as_array")C= $source_data = $source_data->as_array($context)
ABCresult
1XX1
0111
00X0
0100
101 $source_ref eq 'ARRAY'
102 or (
103 UNIVERSAL::can($source_data, 'as_array')
104 and ($source_data = $source_data->as_array($context))
105 )
106 )
107 {
108 43 $result = $self->render_array(
109 $context
110 , $source_data
111 );
112 }
113 ▶ elsif( # iterating hash
Condition16%1/6
$source_ref eq "HASH" or UNIVERSAL::can($source_data, "as_hash") and $source_data = $source_data->as_hash($context)
l!l&&r!l&&!r
200
UNIVERSAL::can($source_data, "as_hash") and $source_data = $source_data->as_hash($context)
!ll&&!rl&&r
000
MC/DC0%0/3
$source_ref eq "HASH" or UNIVERSAL::can($source_data, "as_hash") and $source_data = $source_data->as_hash($context)
$source_ref eq "HASH" UNIVERSAL::can($source_data, "as_hash") $source_data = $source_data->as_hash($context)
Truth table25%1/4
A or B and C
A= $source_ref eq "HASH"B= UNIVERSAL::can($source_data, "as_hash")C= $source_data = $source_data->as_hash($context)
ABCresult
1XX1
0111
00X0
0100
114 $source_ref eq 'HASH'
115 or (
116 UNIVERSAL::can($source_data, 'as_hash')
117 and ($source_data = $source_data->as_hash($context))
118 )
119 )
120 {
121 2 $result = $self->render_hash(
122 $context
123 , $source_data
124 );
125 }
126 else
127 {
128 die sprintf('Do not know how to iterate %s (%s, %s)'
129 0 ▶ , $self->{'source'}->{'original'} // 'undef'
Condition0%0/6
$self->{"source"}{"original"} // "undef"
l!l
00
$source_data // "undef"
l!l
00
$source_ref // "SCALAR"
l!l
00
MC/DC0%0/1
$self->{"source"}{"original"} // "undef"
$self->{"source"}{"original"}
MC/DC0%0/1
$source_data // "undef"
$source_data
MC/DC0%0/1
$source_ref // "SCALAR"
$source_ref
Truth table0%0/2
A
A= $self->{"source"}{"original"}
Aresult
11
00
Truth table0%0/2
A
A= $source_data
Aresult
11
00
Truth table0%0/2
A
A= $source_ref
Aresult
11
00
130 , $source_data // 'undef'
131 , $source_ref // 'SCALAR'
132 );
133 }
134  
135 45 return $result;
136 }
137  
138 sub render_array
139 {
140 43 ▶ my( $self, $context, $source_data ) = @_;
Subroutine render_array: called CC=11 SCAR=25.8
Pod undocumented
141  
142 43 my $result = '';
143  
144 43 ▶ if( scalar @$source_data )
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @$source_data) { } 42 1 43
elsif (scalar @{$self->{'renderers'};} == 2) { } 1 0 1
145 {
146 42 $context->push_scope();
147  
148 42 my $source_size = scalar @$source_data;
149 42 my $forloop = $self->get_forloop($context, $source_size);
150  
151 42 $context->{'ns'}->[-1]->{'forloop'} = $forloop;
152  
153 42 my $variables_number = scalar @{$self->{'targets'}};
154  
155 42 foreach my $value (@$source_data)
156 {
157 191 my $value_type = ref $value;
158 191 ▶ if( $variables_number == 1 )
Branch: 1 branch
BranchTrueFalseTotal
if ($variables_number == 1) { } 172 19 191
159 {
160 172 $context->{'ns'}->[-1]->{$self->{'targets'}->[0]} = $value;
161 }
162 else
163 {
164 19 ▶ if(
Branch: 1 branch
BranchTrueFalseTotal
if ($value_type eq 'ARRAY' or UNIVERSAL::can($value, 'as_array') and $value = $value->as_array($context)) { } 19 0 19
Condition16%1/6
$value_type eq "ARRAY" or UNIVERSAL::can($value, "as_array") and $value = $value->as_array($context)
l!l&&r!l&&!r
1900
UNIVERSAL::can($value, "as_array") and $value = $value->as_array($context)
!ll&&!rl&&r
000
MC/DC0%0/3
$value_type eq "ARRAY" or UNIVERSAL::can($value, "as_array") and $value = $value->as_array($context)
$value_type eq "ARRAY" UNIVERSAL::can($value, "as_array") $value = $value->as_array($context)
Truth table25%1/4
A or B and C
A= $value_type eq "ARRAY"B= UNIVERSAL::can($value, "as_array")C= $value = $value->as_array($context)
ABCresult
1XX1
0111
00X0
0100
165 $value_type eq 'ARRAY'
166 or (
167 UNIVERSAL::can($value, 'as_array')
168 and ($value = $value->as_array($context))
169 )
170 )
171 {
172 19 ▶ if( scalar @$value >= $variables_number )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$value >= $variables_number) { } 19 0 19
173 {
174 19 ▶ for( my $i = 0; $i < $variables_number; $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $variables_number) 47 19 66
175 {
176 47 $context->{'ns'}->[-1]->{$self->{'targets'}->[$i]} = $value->[$i];
177 }
178 }
179 else
180 {
181 die sprintf(
182 'Sub-array (%s) contains less items than variables number (%s)'
183 , join(', ', @$value)
184 0 , join(', ', @{$self->{'targets'}})
185 );
186 }
187 }
188 else
189 {
190 0 die "Multi-var iteration argument $value ($value_type) is not an ARRAY and has no as_array method";
191 }
192 }
193 191 ▶ $result .= $self->{'renderers'}->[0]->render($context) // '';
Condition50%1/2
$self->{"renderers"}[0]->render($context) // ""
l!l
1910
MC/DC0%0/1
$self->{"renderers"}[0]->render($context) // ""
$self->{"renderers"}[0]->render($context)
Truth table50%1/2
A
A= $self->{"renderers"}[0]->render($context)
Aresult
11
00
194  
195 191 $self->step_forloop($forloop);
196 }
197  
198 42 $context->pop_scope();
199 }
200 1 elsif( scalar @{$self->{'renderers'}} == 2 ) # there is an empty block
201 {
202 1 $result = $self->{'renderers'}->[1]->render($context);
203 }
204  
205 43 return $result;
206 }
207  
208 sub render_hash
209 {
210 2 ▶ my( $self, $context, $source_data ) = @_;
Subroutine render_hash: called CC=6 SCAR=18.1
Pod undocumented
211  
212 2 my $result = '';
213  
214 2 my @keys = keys %$source_data;
215 2 my $source_size = scalar @keys;
216 2 ▶ if( $source_size )
Branch: 2 branches
BranchTrueFalseTotal
if ($source_size) { } 1 1 2
elsif (scalar @{$self->{'renderers'};} == 2) { } 1 0 1
217 {
218 1 ▶ if( scalar @{$self->{'targets'}} == 2 )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$self->{'targets'};} == 2) { } 1 0 1
219 {
220 1 $context->push_scope();
221 1 my $forloop = $self->get_forloop($context, $source_size);
222 1 $context->{'ns'}->[-1]->{'forloop'} = $forloop;
223  
224 1 foreach my $key (@keys)
225 {
226 4 my $val = $source_data->{$key};
227 4 $context->{'ns'}->[-1]->{$self->{'targets'}->[0]} = $key;
228 4 $context->{'ns'}->[-1]->{$self->{'targets'}->[1]} = $val;
229 4 ▶ $result .= $self->{'renderers'}->[0]->render($context) // '';
Condition50%1/2
$self->{"renderers"}[0]->render($context) // ""
l!l
40
MC/DC0%0/1
$self->{"renderers"}[0]->render($context) // ""
$self->{"renderers"}[0]->render($context)
Truth table50%1/2
A
A= $self->{"renderers"}[0]->render($context)
Aresult
11
00
230  
231 4 $self->step_forloop($forloop);
232 }
233  
234 1 $context->pop_scope();
235 }
236 else
237 {
238 0 die $self->get_render_error("hash can be only iterated with 2 target variables");
239 }
240 }
241 1 elsif( scalar @{$self->{'renderers'}} == 2 ) # there is an empty block
242 {
243 1 $result = $self->{'renderers'}->[1]->render($context);
244 }
245  
246 2 return $result;
247 }
248  
249 sub add_renderer
250 {
251 43 ▶ my( $self ) = @_;
Subroutine add_renderer: called CC=1 SCAR=0.0
Pod undocumented
252 43 push @{$self->{'renderers'}}, DTL::Fast::Renderer->new();
253 43 return $self;
254 }
255  
256 sub get_forloop
257 {
258 43 ▶ my( $self, $context, $source_size ) = @_;
Subroutine get_forloop: called CC=2 SCAR=6.9
Pod undocumented
259  
260 return {
261 43 ▶ 'parentloop' => $context->{'ns'}->[-1]->{'forloop'}
Branch: 1 branch
BranchTrueFalseTotal
$source_size == 1 ? : 3 40 43
262 , 'counter' => 1
263 , 'counter0' => 0
264 , 'revcounter' => $source_size
265 , 'revcounter0' => $source_size-1
266 , 'first' => 1
267 , 'last' => $source_size == 1 ? 1: 0
268 , 'length' => $source_size
269 , 'odd' => 1
270 , 'odd0' => 0
271 , 'even' => 0
272 , 'even0' => 1
273 };
274 }
275  
276 sub step_forloop
277 {
278 195 ▶ my( $self, $forloop ) = @_;
Subroutine step_forloop: called CC=6 SCAR=17.9
Pod undocumented
279  
280 195 $forloop->{'counter'}++;
281 195 $forloop->{'counter0'}++;
282 195 $forloop->{'revcounter'}--;
283 195 $forloop->{'revcounter0'}--;
284 195 ▶ $forloop->{'odd'} = $forloop->{'odd'} ? 0: 1;
Branch: 1 branch
BranchTrueFalseTotal
$forloop->{'odd'} ? : 114 81 195
285 195 ▶ $forloop->{'odd0'} = $forloop->{'odd0'} ? 0: 1;
Branch: 1 branch
BranchTrueFalseTotal
$forloop->{'odd0'} ? : 81 114 195
286 195 ▶ $forloop->{'even'} = $forloop->{'even'} ? 0: 1;
Branch: 1 branch
BranchTrueFalseTotal
$forloop->{'even'} ? : 81 114 195
287 195 ▶ $forloop->{'even0'} = $forloop->{'even0'} ? 0: 1;
Branch: 1 branch
BranchTrueFalseTotal
$forloop->{'even0'} ? : 114 81 195
288 195 $forloop->{'first'} = 0;
289 195 ▶ if( $forloop->{'counter'} == $forloop->{'length'} )
Branch: 1 branch
BranchTrueFalseTotal
if ($forloop->{"counter"} == $forloop->{"length"}) 40 155 195
290 {
291 40 $forloop->{'last'} = 1;
292 }
293 195 return $self;
294 }
295  
296 10 1;
« blib/lib/DTL/Fast/Expression/Operator/Binary/Eq.pm ↑ Summary blib/lib/DTL/Fast/Cache/File.pm »