Completed
Push — master ( 110bba...069124 )
by Edson
01:59
created

VariableTag::getVar()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 0
dl 0
loc 13
ccs 7
cts 7
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Sketch\Tpl;
4
5
/**
6
 * Class VariableTag
7
 * @package Sketch\Tpl
8
 */
9
class VariableTag extends Tag
10
{
11 10
    public function __construct()
12
    {
13 10
        parent::__construct('/{{(\s?)+([\w\.]+)(\s?)+\|?(\s?)+([\w|]+)?(\s?)+}}/is');
14 10
    }
15
16 10
    public function handle(array $match): string
17
    {
18 10
        $filters = explode('|', $match[5]);
19
20 10
        $res = "<?= ";
21
        
22 10
        foreach ($filters as $filter) {
23 10
            $res .= "$filter(";
24
        }
25
26 10
        $res .= $this->getVar();
27
28 10
        foreach ($filters as $filter) {
29 10
            $res .= ")";
30
        }
31
32 10
        $res .= " ?>";
33
34 10
        return $res;
35
    }
36
37 10
    private function getVar(): string
38
    {
39 10
        $explode = explode('.', $this->match[2]);
40
41 10
        $variable = $explode[0];
42
43 10
        $variable = '$'.$variable;
44
45 10
        for ($k = 1; $k < count($explode); $k++) {
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...
46 4
            $variable .= "->".$explode[$k];
47
        }
48
49 10
        return $variable;
50
    }
51
}
52