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