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

IfTag::isVariable()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 3
nc 6
nop 1
dl 0
loc 5
rs 9.2
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 19 and the first side effect is on line 5.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
namespace Sketch\Tpl;
4
5
class IfTag 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...
Bug introduced by
Possible parse error: class missing opening or closing brace
Loading history...
6
{
7
    use ElseView;
8
    use ElseifView;
9
10
    private $pattern = '/{\s?if (.*?)\s?([><!=])(=)?(=)?\s?(.*?)\s?}(.*?){\s?\/if\s?}/is';
11
    private $block = '';
12
    private $firstCondition = '';
13
    private $secondCondition = '';
14
    private $operator = '';
15
    private $ifContent = '';
16
    private $replace;
17
    private $match;
18
19
    public function handle(): void
20
    {
21
        if (preg_match_all($this->pattern, self::$content, $matches, PREG_SET_ORDER)) {
22
            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...
23
                $this->match = $matches[$i];
24
25
                $this->setIfBlock();
26
                $this->setIfOperator();
27
                $this->setIfFirstCondition();
28
                $this->setIfSecondCondition();
29
                $this->setIfContents();
30
31
                $this->if();
32
                $this->elseif();
33
                $this->else();
34
                $this->endif();
35
36
                $this->elseif = [];
37
38
                self::$content = str_replace($this->block, $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...
39
            }
40
            new IfTag();
41
        }
42
    }
43
44
    private function setIfBlock() : void
45
    {
46
        $this->block = $this->match[0];
47
    }
48
49
    private function setIfOperator() : void
50
    {
51
        $this->operator = implode('', array_slice($this->match, 2, 3));
52
    }
53
54
    private function setIfFirstCondition() : void
55
    {
56
        $this->firstCondition = $this->setCondition($this->match[1]);
57
    }
58
59
    private function setIfSecondCondition() : void
60
    {
61
        $this->secondCondition = $this->setCondition($this->match[5]);
62
    }
63
64
    private function setCondition($condition) : string
65
    {
66
        if ($this->isVariable($condition)) {
67
            $explode = explode('.', $condition);
68
69
            $condition = '$'.$explode[0];
70
71
            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...
72
                $condition .= "->".$explode[$i];
73
            }
74
        }
75
76
        return $condition;
77
    }
78
79
    private function setIfContents()
80
    {
81
        $this->setElseContent();
82
        $this->setElseifContent();
83
    }
84
85
    private function if(): void
86
    {
87
        $this->replace  = "<?php if({$this->firstCondition} {$this->operator} {$this->secondCondition}): ?>";
88
        $this->replace .= trim($this->ifContent);
89
    }
90
91
    private function endif(): void
92
    {
93
        $this->replace .= "<?php endif; ?>";
94
    }
95
96
    private function isVariable($term)
97
    {
98
        return (is_numeric($term)
99
        || $this->isStringValue((string) $term)
100
        || $this->isReservedKey($term)) ? false : true;
101
    }
102
103
    private function isReservedKey($key)
104
    {
105
        return ($key == "null" || $key == "true" || $key == "false") ? true : false;
106
    }
107
108
    private function isStringValue(string $string)
109
    {
110
        return (substr($string, 0, 1) == '"' || substr($string, 0, 1) == "'") ? true : false;
111
    }
112
}
113