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

VariableTpl::variable()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 12
cts 12
cp 1
rs 8.7624
c 0
b 0
f 0
cc 6
eloc 11
nc 6
nop 0
crap 6
1
<?php
2
3
namespace Bonfim\Component\View;
4
5
use Exception;
6
7
class VariableTpl
8
{
9
    private $pattern = '/{\s?([\w]+.?[\w]+)\s?\|?\s?([\w]+)?\s?}/is';
10
11 4
    public function __construct(string $content)
12
    {
13 4
        $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...
14 4
        $this->variable();
15 4
    }
16
17 4
    public function __toString(): string
18
    {
19 4
        return $this->content;
20
    }
21
22 4
    private function variable(): void
23
    {
24 4
        if (preg_match_all($this->pattern, $this->content, $matches, PREG_SET_ORDER)) {
25 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...
26 4
                $explode = explode('.', $matches[$i][1]);
27
28 4
                $variable = $explode[0];
29
30 4
                $variable = '$'.$variable;
31
32 4
                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...
33 2
                    $variable .= "['".$explode[$k]."']";
34
                }
35
36 4
                $content = '<?php echo('.$variable.'); ?>';
37
38 4
                if (isset($matches[$i][2]) && $matches[$i][2] == 'upper') {
39 2
                    $content = '<?php echo(ucwords('.$variable.')); ?>';
40
                }
41
42 4
                $this->content = str_replace($matches[$i][0], $content, $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...
43
            };
44
        }
45 4
    }
46
}
47