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

VariableTpl   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 56
ccs 29
cts 29
cp 1
rs 10
c 0
b 0
f 0
wmc 11

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getVariable() 0 13 2
A upper() 0 4 3
A filter() 0 3 1
A __construct() 0 4 1
A __toString() 0 3 1
A variable() 0 9 3
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