Completed
Push — master ( fe8048...f27190 )
by Edson
01:22
created

RepeatTag::handle()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 4
nop 1
dl 0
loc 13
ccs 9
cts 9
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Sketch\Tpl;
4
5
class RepeatTag extends Tag
6
{
7 10
    public function __construct()
8
    {
9 10
        parent::__construct(
10 10
            '/{(\s?)+repeat (\s?)+([\d]+)(\s?)+(:?)(\s?)+([\w]+)?(\s?)+}(.*?){(\s?)+\/repeat(\s?)+}/is'
11
        );
12 10
    }
13
14 8
    public function handle(array $match): string
15
    {
16 8
        static $count = 0;
17
18 8
        $index = !empty($match[5]) && !empty($match[7])
19 4
            ? $match[7]
20 8
            : 'index' . ++$count;
21
22 8
        $replace  = "<?php for (\$$index = 0; \$$index < $match[3]; \$$index++) { ?>";
23 8
        $replace .= $match[9];
24 8
        $replace .= "<?php } ?>";
25
26 8
        return $replace;
27
    }
28
}
29