Passed
Push — master ( 67cfc6...1f1489 )
by Edson
01:18
created

VariableTag::getVariable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 0
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Sketch\Tpl;
4
5
6
class VariableTag extends Tag
0 ignored issues
show
Bug introduced by
The type Sketch\Tpl\Tag was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
{
8
    private $pattern = '/{\s?([\w]+.?[\w]+.?[\w]+)\s?\|?\s?([\w]+)?\s?}/is';
9
    private $match   = [];
10
    private $matches = [];
11
    private $replace = '';
12
    private $variable = '';
13
14
    public function handle(): void
15
    {
16
        if (preg_match_all($this->pattern, self::$content, $this->matches, PREG_SET_ORDER)) {
17
            for ($i = 0; $i < count($this->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...
18
                $this->match = $this->matches[$i];
19
                $this->getVariable();
20
                $this->replace = '<?php echo('.$this->variable.'); ?>';
21
                $this->filter('upper');
22
                self::$content = str_replace($this->match[0], $this->replace, self::$content);
0 ignored issues
show
Bug Best Practice introduced by
The property content does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
23
            };
24
        }
25
    }
26
27
    private function getVariable(): void
28
    {
29
        $explode = explode('.', $this->match[1]);
30
31
        $variable = $explode[0];
32
33
        $variable = '$'.$variable;
34
35
        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...
36
            $variable .= "->".$explode[$k];
37
        }
38
39
        $this->variable = $variable;
40
    }
41
42
    private function filter(string $name): void
43
    {
44
        $this->$name();
45
    }
46
47
    private function upper(): void
48
    {
49
        if (isset($this->match[2]) && $this->match[2] == 'capitalize') {
50
            $this->replace = '<?php echo(ucwords(strtolower('.$this->variable.'))); ?>';
51
        }
52
    }
53
}
54