Completed
Push — master ( 8d3e79...9daf84 )
by Edson
04:12
created

VariableTpl::variable()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 0
dl 0
loc 9
ccs 8
cts 8
cp 1
crap 3
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
namespace Bonfim\Tpl;
4
5
use Exception;
6
7
class VariableTpl
8
{
9
    private $pattern = '/{\s?([\w]+.?[\w]+)\s?\|?\s?([\w]+)?\s?}/is';
10
    private $match   = [];
11
    private $matches = [];
12
    private $replace = '';
13
    private $variable = '';
14
15 6
    public function __construct(string $content)
16
    {
17 6
        $this->content = $content;
0 ignored issues
show
Bug Best Practice introduced by
The property content does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
18 6
        $this->variable();
19 6
    }
20
21 6
    public function __toString(): string
22
    {
23 6
        return $this->content;
24
    }
25
26 6
    private function variable(): void
27
    {
28 6
        if (preg_match_all($this->pattern, $this->content, $this->matches, PREG_SET_ORDER)) {
29 6
            for ($i = 0; $i < count($this->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...
30 6
                $this->match = $this->matches[$i];
31 6
                $this->getVariable();
32 6
                $this->replace = '<?php echo('.$this->variable.'); ?>';
33 6
                $this->filter('upper');
34 6
                $this->content = str_replace($this->match[0], $this->replace, $this->content);
0 ignored issues
show
Bug Best Practice introduced by
The property content does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
35
            };
36
        }
37 6
    }
38
39 6
    private function getVariable(): void
40
    {
41 6
        $explode = explode('.', $this->match[1]);
42
43 6
        $variable = $explode[0];
44
45 6
        $variable = '$'.$variable;
46
47 6
        for ($k = 1; $k < count($explode); $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...
48 2
            $variable .= "['".$explode[$k]."']";
49
        }
50
51 6
        $this->variable = $variable;
52 6
    }
53
54 6
    private function filter(string $name): void
55
    {
56 6
        $this->$name();
57 6
    }
58
59 6
    private function upper(): void
60
    {
61 6
        if (isset($this->match[2]) && $this->match[2] == 'upper') {
62 2
            $this->replace = '<?php echo(ucwords('.$this->variable.')); ?>';
63
        }
64 6
    }
65
}
66