Completed
Push — master ( 09ae22...bc0d3f )
by Edson
01:06
created

RepeatTag   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 30
rs 10
c 0
b 0
f 0
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
B handle() 0 21 7
A __construct() 0 4 1
1
<?php
2
3
namespace Sketch\Tpl;
4
5
class RepeatTag extends Tag
6
{
7
    public function __construct()
8
    {
9
        parent::__construct(
10
            '/{(\s?)+repeat (\s?)+([\d]+)(\s?)+(:?)(\s?)+([\w]+)?(\s?)+}(.*?){(\s?)+\/repeat(\s?)+}/is'
11
        );
12
    }
13
14
    public function handle(array $match): string
15
    {
16
        static $count = 0;
17
18
        if (!empty($match[5]) && empty($match[7])) {
19
            throw new \Exception("Invalid tag"); // @codeCoverageIgnore
20
        }
21
22
        if (empty($match[5]) && !empty($match[7])) {
23
            throw new \Exception("Invalid tag"); // @codeCoverageIgnore
24
        }
25
26
        $index = !empty($match[5]) && !empty($match[7])
27
            ? $match[7]
28
            : 'index' . ++$count;
29
30
        $replace  = "<?php for (\$$index = 0; \$$index < $match[3]; \$$index++) { ?>";
31
        $replace .= $match[9];
32
        $replace .= "<?php } ?>";
33
34
        return $replace;
35
    }
36
}
37