Passed
Push — master ( ab7df5...238a89 )
by Edson
01:47
created

IfTpl   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 143
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 32
dl 0
loc 143
ccs 0
cts 107
cp 0
rs 9.6
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A setIfContents() 0 4 1
A setCondition() 0 13 3
A __toString() 0 3 1
A setIfFirstCondition() 0 3 1
A isVariable() 0 3 4
A setIfOperator() 0 3 1
A __construct() 0 4 1
A setIfBlock() 0 3 1
A isStringValue() 0 3 3
A setElseifContent() 0 13 3
A setIfSecondCondition() 0 3 1
A setElseContent() 0 12 2
A isReservedKey() 0 3 4
B if() 0 33 6
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 18 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 Bonfim\Component\View;
4
5
class IfTpl
0 ignored issues
show
Bug introduced by
Possible parse error: class missing opening or closing brace
Loading history...
6
{
7
    private $pattern = '/{\s?if (.*?)\s?([><!=])(=)?(=)?\s?(.*?)\s?}(.*?){\s?\/if\s?}/is';
8
    private $block = '';
9
    private $firstCondition = '';
10
    private $secondCondition = '';
11
    private $operator = '';
12
    private $elseif = array();
13
    private $ifConten = '';
14
    private $elseContent = '';
15
    private $content;
16
    private $match;
17
18
    public function __construct(string $content)
19
    {
20
        $this->content = $content;
21
        $this->if();
22
    }
23
24
    public function __toString(): string
25
    {
26
        return $this->content;
27
    }
28
29
    private function if() : void
30
    {
31
        if (preg_match_all($this->pattern, $this->content, $matches, PREG_SET_ORDER)) {
32
            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...
33
                $this->match = $matches[$i];
34
35
                $this->setIfBlock();
36
                $this->setIfOperator();
37
                $this->setIfFirstCondition();
38
                $this->setIfSecondCondition();
39
                $this->setIfContents();
40
41
                $content  = "<?php if({$this->firstCondition} {$this->operator} {$this->secondCondition}): ?>";
42
                $content .= trim($this->ifConten);
43
44
45
                if (count($this->elseif) != 0) {
46
                    for ($k = 0; $k < count($this->elseif); $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...
47
                        $content .= "<?php elseif({$this->elseif[$k]['firstCondition']} {$this->elseif[$k]['operator']} {$this->elseif[$k]['secondCondition']}): ?>";
48
                        $content .= trim($this->elseif[$k]['content']);
49
                    }
50
                }
51
52
                if (!empty($this->elseContent)) {
53
                    $content .= "<?php else: ?>";
54
                    $content .= trim($this->elseContent);
55
                }
56
57
                $content .= "<?php endif; ?>";
58
59
                $this->content = str_replace($this->block, $content, $this->content);
60
            }
61
            $this->if();
62
        }
63
    }
64
65
    private function setIfBlock() : void
66
    {
67
        $this->block = $this->match[0];
68
    }
69
70
    private function setIfOperator() : void
71
    {
72
        $this->operator = implode('', array_slice($this->match, 2, 3));
73
    }
74
75
    private function setIfFirstCondition() : void
76
    {
77
        $this->firstCondition = $this->setCondition($this->match[1]);
78
    }
79
80
    private function setIfSecondCondition() : void
81
    {
82
        $this->secondCondition = $this->setCondition($this->match[5]);
83
    }
84
85
    private function setCondition($condition) : string
86
    {
87
        if ($this->isVariable($condition)) {
88
            $explode = explode('.', $condition);
89
90
            $condition = '$'.$explode[0];
91
92
            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...
93
                $condition .= "['".$explode[$i]."']";
94
            }
95
        }
96
97
        return $condition;
98
    }
99
100
    private function setIfContents()
101
    {
102
        $this->setElseContent();
103
        $this->setElseifContent();
104
    }
105
106
    private function setElseContent()
107
    {
108
        $matches = array();
109
        if (preg_match('/(.*?){\s?else\s?}/is', $this->match[6], $matches)) {
110
            $this->ifConten = $matches[1];
111
            $this->elseContent = str_replace($matches[0], '', $this->match[6]);
112
        } else {
113
            $this->ifConten = $this->match[6];
114
            $this->elseContent = '';
115
        }
116
        $this->ifConten = str_replace('{elseif', '{end}{elseif', $this->ifConten);
117
        $this->ifConten = $this->ifConten . "{end}";
118
    }
119
120
    private function setElseifContent()
121
    {
122
        $matches = array();
123
        if (preg_match_all('/{\s?elseif (.*?)\s?([><!=])(=)?(=)?\s?(.*?)\s?}(.*?){end}/is', $this->ifConten, $matches, PREG_SET_ORDER)) {
124
            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...
125
                $this->ifConten = str_replace($matches[$i][0], '', $this->ifConten);
126
                $this->elseif[$i]['firstCondition'] = $this->setCondition($matches[$i][1]);
127
                $this->elseif[$i]['secondCondition'] = $this->setCondition($matches[$i][5]);
128
                $this->elseif[$i]['operator'] = implode('', array_slice($matches[$i], 2, 3));
129
                $this->elseif[$i]['content'] = trim($matches[$i][6]);
130
            }
131
        }
132
        $this->ifConten = str_replace('{end}', '', $this->ifConten);
133
    }
134
135
    private function isVariable(mixed $term)
0 ignored issues
show
Bug introduced by
The type Bonfim\Component\View\mixed 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...
136
    {
137
        return (is_numeric($term) || $this->isStringValue((string) $term) || $this->isReservedKey($term)) ? false : true;
138
    }
139
140
    private function isReservedKey(mixed $key)
141
    {
142
        return ($key == "null" || $key == "true" || $key == "false") ? true : false;
0 ignored issues
show
introduced by
The condition $key == 'false' is always false.
Loading history...
introduced by
The condition $key == 'true' is always false.
Loading history...
143
    }
144
145
    private function isStringValue(string $string)
146
    {
147
        return (substr($string, 0, 1) == '"' || substr($string, 0, 1) == "'") ? true : false;
148
    }
149
}
150