Passed
Push — master ( f8e1da...c49986 )
by Edson
01:13
created

IfTpl::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 20 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 IfTpl
0 ignored issues
show
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 $content;
17
    private $replace;
18
    private $match;
19
20
    public function __construct(string $content)
21
    {
22
        $this->content = $content;
23
        $this->handle();
24
    }
25
26
    public function __toString(): string
27
    {
28
        return $this->content;
29
    }
30
31
    private function handle() : void
32
    {
33
        if (preg_match_all($this->pattern, $this->content, $matches, PREG_SET_ORDER)) {
34
            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...
35
                $this->match = $matches[$i];
36
37
                $this->setIfBlock();
38
                $this->setIfOperator();
39
                $this->setIfFirstCondition();
40
                $this->setIfSecondCondition();
41
                $this->setIfContents();
42
43
                $this->if();
44
                $this->elseif();
45
                $this->else();
46
                $this->endif();
47
48
                $this->elseif = [];
49
50
                $this->content = str_replace($this->block, $this->replace, $this->content);
51
            }
52
        }
53
    }
54
55
    private function setIfBlock() : void
56
    {
57
        $this->block = $this->match[0];
58
    }
59
60
    private function setIfOperator() : void
61
    {
62
        $this->operator = implode('', array_slice($this->match, 2, 3));
63
    }
64
65
    private function setIfFirstCondition() : void
66
    {
67
        $this->firstCondition = $this->setCondition($this->match[1]);
68
    }
69
70
    private function setIfSecondCondition() : void
71
    {
72
        $this->secondCondition = $this->setCondition($this->match[5]);
73
    }
74
75
    private function setCondition($condition) : string
76
    {
77
        if ($this->isVariable($condition)) {
78
            $explode = explode('.', $condition);
79
80
            $condition = '$'.$explode[0];
81
82
            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...
83
                $condition .= "->".$explode[$i];
84
            }
85
        }
86
87
        return $condition;
88
    }
89
90
    private function setIfContents()
91
    {
92
        $this->setElseContent();
93
        $this->setElseifContent();
94
    }
95
96
    private function if(): void
97
    {
98
        $this->replace  = "<?php if({$this->firstCondition} {$this->operator} {$this->secondCondition}): ?>";
99
        $this->replace .= trim($this->ifContent);
100
    }
101
102
    private function endif(): void
103
    {
104
        $this->replace .= "<?php endif; ?>";
105
    }
106
107
    private function isVariable($term)
108
    {
109
        return (is_numeric($term)
110
        || $this->isStringValue((string) $term)
111
        || $this->isReservedKey($term)) ? false : true;
112
    }
113
114
    private function isReservedKey($key)
115
    {
116
        return ($key == "null" || $key == "true" || $key == "false") ? true : false;
117
    }
118
119
    private function isStringValue(string $string)
120
    {
121
        return (substr($string, 0, 1) == '"' || substr($string, 0, 1) == "'") ? true : false;
122
    }
123
}
124