Passed
Pull Request — master (#13)
by Edson
01:22
created

LoopTag   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 73
ccs 29
cts 29
cp 1
rs 10
c 0
b 0
f 0
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setForeachContent() 0 3 1
A handle() 0 17 3
A setForeachBlock() 0 3 1
A setForeachArray() 0 11 2
A setForeachCallback() 0 3 1
1
<?php
2
3
namespace Sketch\Tpl;
4
5
/**
6
 * Class LoopTag
7
 * @package Sketch\Tpl
8
 */
9
class LoopTag extends Tag
10
{
11
    /**
12
     * @var string
13
     */
14
    private $pattern = '/{\s?loop (.*?) as ([\w]+)\s?}(.*?){\s?\/loop\s?}/is';
15
    /**
16
     * @var string
17
     */
18
    private $block = '';
19
    /**
20
     * @var string
21
     */
22
    private $array = '';
23
    /**
24
     * @var string
25
     */
26
    private $callback = '';
27
    /**
28
     * @var string
29
     */
30
    private $foreachContent = '';
31
    /**
32
     * @var
33
     */
34
    private $match;
35
36 4
    public function handle(): void
37
    {
38 4
        if (preg_match_all($this->pattern, self::$content, $matches, PREG_SET_ORDER)) {
39 2
            for ($i = 0; $i < count($matches); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
40 2
                $this->match = $matches[$i];
41 2
                $this->setForeachBlock();
42 2
                $this->setForeachArray();
43 2
                $this->setForeachCallback();
44 2
                $this->setForeachContent();
45
46 2
                $content  = "<?php foreach({$this->array} as {$this->callback}): ?>";
47 2
                $content .= $this->foreachContent;
48 2
                $content .= "<?php endforeach; ?>";
49
50 2
                self::$content = str_replace($this->block, $content, self::$content);
51
            };
52 2
            new LoopTag();
53
        }
54 4
    }
55
56 2
    private function setForeachBlock() : void
57
    {
58 2
        $this->block = $this->match[0];
59 2
    }
60
61 2
    private function setForeachArray() : void
62
    {
63 2
        $explode = explode('.', $this->match[1]);
64
65 2
        $variableArray = '$'.$explode[0];
66
67 2
        for ($i = 1; $i < count($explode); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
68 2
            $variableArray .= "->".$explode[$i];
69
        }
70
71 2
        $this->array = $variableArray;
72 2
    }
73
74 2
    private function setForeachCallback() : void
75
    {
76 2
        $this->callback = '$'.$this->match[2];
77 2
    }
78
79 2
    private function setForeachContent() : void
80
    {
81 2
        $this->foreachContent = trim($this->match[3]);
82 2
    }
83
}
84