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

VariableTag   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 41
ccs 20
cts 20
cp 1
rs 10
c 0
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A handle() 0 19 3
A getVar() 0 13 2
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