Passed
Push — master ( 8b4ad6...d22084 )
by Edson
01:33
created

IfTag::isStringValue()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 1
nc 4
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 3
rs 10
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 39 and the first side effect is on line 9.

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
/**
6
 * Class IfTag
7
 * @package Sketch\Tpl
8
 */
9
class IfTag extends Tag
0 ignored issues
show
Bug introduced by
Possible parse error: class missing opening or closing brace
Loading history...
10
{
11
    use ElseView;
12
    use ElseifView;
13
14
    /**
15
     * @var string
16
     */
17
    private $block = '';
0 ignored issues
show
introduced by
The private property $block is not used, and could be removed.
Loading history...
18
    /**
19
     * @var string
20
     */
21
    private $firstCondition = '';
22
    /**
23
     * @var string
24
     */
25
    private $secondCondition = '';
26
    /**
27
     * @var string
28
     */
29
    private $operator = '';
30
    /**
31
     * @var string
32
     */
33
    private $ifContent = '';
34
    /**
35
     * @var
36
     */
37
    private $replace;
38
39 10
    public function __construct()
40
    {
41 10
        parent::__construct('/{\s?if (.*?)\s?([><!=])(=)?(=)?\s?(.*?)\s?}(.*?){\s?\/if\s?}/is');
42 10
    }
43
44 8
    public function handle(array $match): string
45
    {
46 8
        $this->setIfOperator();
47 8
        $this->setIfFirstCondition();
48 8
        $this->setIfSecondCondition();
49 8
        $this->setIfContents();
50
51 8
        $this->if();
52 8
        $this->elseif();
53 8
        $this->else();
54 8
        $this->endif();
55
56 8
        $this->elseif = [];
57
58 8
        return $this->replace;
59
    }
60
61 8
    private function setIfOperator() : void
62
    {
63 8
        $this->operator = implode('', array_slice($this->match, 2, 3));
64 8
    }
65
66 8
    private function setIfFirstCondition() : void
67
    {
68 8
        $this->firstCondition = $this->setCondition($this->match[1]);
69 8
    }
70
71 8
    private function setIfSecondCondition() : void
72
    {
73 8
        $this->secondCondition = $this->setCondition($this->match[5]);
74 8
    }
75
76
    /**
77
     * @param $condition
78
     * @return string
79
     */
80 8
    private function setCondition($condition) : string
81
    {
82 8
        if ($this->isVariable($condition)) {
83 8
            $explode = explode('.', $condition);
84
85 8
            $condition = '$'.$explode[0];
86
87 8
            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...
88 2
                $condition .= "->".$explode[$i];
89
            }
90
        }
91
92 8
        return $condition;
93
    }
94
95 8
    private function setIfContents()
96
    {
97 8
        $this->setElseContent();
98 8
        $this->setElseifContent();
99 8
    }
100
101 8
    private function if(): void
102
    {
103 8
        $this->replace  = "<?php if({$this->firstCondition} {$this->operator} {$this->secondCondition}): ?>";
104 8
        $this->replace .= trim($this->ifContent);
105 8
    }
106
107 8
    private function endif(): void
108
    {
109 8
        $this->replace .= "<?php endif; ?>";
110 8
    }
111
112
    /**
113
     * @param $term
114
     * @return bool
115
     */
116 8
    private function isVariable($term)
117
    {
118 8
        return (is_numeric($term)
119 8
        || $this->isStringValue((string) $term)
120 8
        || $this->isReservedKey($term)) ? false : true;
121
    }
122
123
    /**
124
     * @param $key
125
     * @return bool
126
     */
127 8
    private function isReservedKey($key)
128
    {
129 8
        return ($key == "null" || $key == "true" || $key == "false") ? true : false;
130
    }
131
132
    /**
133
     * @param string $string
134
     * @return bool
135
     */
136 8
    private function isStringValue(string $string)
137
    {
138 8
        return (substr($string, 0, 1) == '"' || substr($string, 0, 1) == "'") ? true : false;
139
    }
140
}
141