Passed
Push — master ( f8e1da...c49986 )
by Edson
01:13
created

VariableTpl   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 57
rs 10
c 0
b 0
f 0
wmc 11

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __toString() 0 3 1
A variable() 0 10 3
A upper() 0 4 3
A getVariable() 0 13 2
A filter() 0 3 1
1
<?php
2
3
namespace Sketch\Tpl;
4
5
use Exception;
6
7
class VariableTpl
8
{
9
    private $pattern = '/{\s?([\w]+.?[\w]+.?[\w]+)\s?\|?\s?([\w]+)?\s?}/is';
10
    private $match   = [];
11
    private $matches = [];
12
    private $replace = '';
13
    private $variable = '';
14
15
    public function __construct(string $content)
16
    {
17
        $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
        $this->variable();
19
    }
20
21
    public function __toString(): string
22
    {
23
        return $this->content;
24
    }
25
26
    private function variable(): void
27
    {
28
        if (preg_match_all($this->pattern, $this->content, $this->matches, PREG_SET_ORDER)) {
29
            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
                $this->match = $this->matches[$i];
31
                $this->getVariable();
32
                /*$this->replace = "<?php if (is_object({$this->variable})) {$this->variable} = (array) {$this->variable}; ?>";*/
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
33
                $this->replace = '<?php echo('.$this->variable.'); ?>';
34
                $this->filter('upper');
35
                $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...
36
            };
37
        }
38
    }
39
40
    private function getVariable(): void
41
    {
42
        $explode = explode('.', $this->match[1]);
43
44
        $variable = $explode[0];
45
46
        $variable = '$'.$variable;
47
48
        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...
49
            $variable .= "->".$explode[$k];
50
        }
51
52
        $this->variable = $variable;
53
    }
54
55
    private function filter(string $name): void
56
    {
57
        $this->$name();
58
    }
59
60
    private function upper(): void
61
    {
62
        if (isset($this->match[2]) && $this->match[2] == 'capitalize') {
63
            $this->replace = '<?php echo(ucwords(strtolower('.$this->variable.'))); ?>';
64
        }
65
    }
66
}
67