Completed
Push — master ( d6f5b0...d05011 )
by Edson
01:16
created

IfTpl::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
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 8
    public function __construct(string $content)
19
    {
20 8
        $this->content = $content;
21 8
        $this->if();
22 8
    }
23
24 8
    public function __toString(): string
25
    {
26 8
        return $this->content;
27
    }
28
29 8
    private function if() : void
30
    {
31 8
        if (preg_match_all($this->pattern, $this->content, $matches, PREG_SET_ORDER)) {
32 8
            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 8
                $this->match = $matches[$i];
34
35 8
                $this->setIfBlock();
36 8
                $this->setIfOperator();
37 8
                $this->setIfFirstCondition();
38 8
                $this->setIfSecondCondition();
39 8
                $this->setIfContents();
40
41 8
                $content  = "<?php if({$this->firstCondition} {$this->operator} {$this->secondCondition}): ?>";
42 8
                $content .= trim($this->ifConten);
43
44
45 8
                if (count($this->elseif) != 0) {
46 4
                    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 4
                        $content .= "<?php elseif({$this->elseif[$k]['firstCondition']}";
48 4
                        $content .= " {$this->elseif[$k]['operator']}";
49 4
                        $content .= " {$this->elseif[$k]['secondCondition']}): ?>";
50 4
                        $content .= trim($this->elseif[$k]['content']);
51
                    }
52
                }
53
54 8
                if (!empty($this->elseContent)) {
55 4
                    $content .= "<?php else: ?>";
56 4
                    $content .= trim($this->elseContent);
57
                }
58
59 8
                $content .= "<?php endif; ?>";
60
61 8
                $this->content = str_replace($this->block, $content, $this->content);
62
            }
63 8
            $this->if();
64
        }
65 8
    }
66
67 8
    private function setIfBlock() : void
68
    {
69 8
        $this->block = $this->match[0];
70 8
    }
71
72 8
    private function setIfOperator() : void
73
    {
74 8
        $this->operator = implode('', array_slice($this->match, 2, 3));
75 8
    }
76
77 8
    private function setIfFirstCondition() : void
78
    {
79 8
        $this->firstCondition = $this->setCondition($this->match[1]);
80 8
    }
81
82 8
    private function setIfSecondCondition() : void
83
    {
84 8
        $this->secondCondition = $this->setCondition($this->match[5]);
85 8
    }
86
87 8
    private function setCondition($condition) : string
88
    {
89 8
        if ($this->isVariable($condition)) {
90 8
            $explode = explode('.', $condition);
91
92 8
            $condition = '$'.$explode[0];
93
94 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...
95 2
                $condition .= "['".$explode[$i]."']";
96
            }
97
        }
98
99 8
        return $condition;
100
    }
101
102 8
    private function setIfContents()
103
    {
104 8
        $this->setElseContent();
105 8
        $this->setElseifContent();
106 8
    }
107
108 8
    private function setElseContent()
109
    {
110 8
        $matches = array();
111 8
        if (preg_match('/(.*?){\s?else\s?}/is', $this->match[6], $matches)) {
112 4
            $this->ifConten = $matches[1];
113 4
            $this->elseContent = str_replace($matches[0], '', $this->match[6]);
114
        } else {
115 4
            $this->ifConten = $this->match[6];
116 4
            $this->elseContent = '';
117
        }
118 8
        $this->ifConten = str_replace('{elseif', '{end}{elseif', $this->ifConten);
119 8
        $this->ifConten = $this->ifConten . "{end}";
120 8
    }
121
122 8
    private function setElseifContent()
123
    {
124 8
        $pattern = '/{\s?elseif (.*?)\s?([><!=])(=)?(=)?\s?(.*?)\s?}(.*?){end}/is';
125 8
        if (preg_match_all($pattern, $this->ifConten, $matches, PREG_SET_ORDER)) {
126 4
            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...
127 4
                $this->ifConten = str_replace($matches[$i][0], '', $this->ifConten);
128 4
                $this->elseif[$i]['firstCondition'] = $this->setCondition($matches[$i][1]);
129 4
                $this->elseif[$i]['secondCondition'] = $this->setCondition($matches[$i][5]);
130 4
                $this->elseif[$i]['operator'] = implode('', array_slice($matches[$i], 2, 3));
131 4
                $this->elseif[$i]['content'] = trim($matches[$i][6]);
132
            }
133
        }
134 8
        $this->ifConten = str_replace('{end}', '', $this->ifConten);
135 8
    }
136
137 8
    private function isVariable($term)
138
    {
139 8
        return (is_numeric($term)
140 8
        || $this->isStringValue((string) $term)
141 8
        || $this->isReservedKey($term)) ? false : true;
142
    }
143
144 8
    private function isReservedKey($key)
145
    {
146 8
        return ($key == "null" || $key == "true" || $key == "false") ? true : false;
147
    }
148
149 8
    private function isStringValue(string $string)
150
    {
151 8
        return (substr($string, 0, 1) == '"' || substr($string, 0, 1) == "'") ? true : false;
152
    }
153
}
154