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

ForeachTpl::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
crap 1
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 15 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\Component\View;
4
5
class ForeachTpl
0 ignored issues
show
Bug introduced by
Possible parse error: class missing opening or closing brace
Loading history...
6
{
7
    private $pattern = '/{\s?foreach (.*?) as ([\w]+)\s?}(.*?){\s?\/foreach\s?}/is';
8
    private $content;
9
    private $block = '';
10
    private $array = '';
11
    private $callback = '';
12
    private $foreachContent = '';
13
    private $match;
14
15 2
    public function __construct(string $content)
16
    {
17 2
        $this->content = $content;
18 2
        $this->foreach();
19 2
    }
20
21 2
    public function __toString(): string
22
    {
23 2
        return $this->content;
24
    }
25
26 2
    private function foreach() : void
27
    {
28 2
        if (preg_match_all($this->pattern, $this->content, $matches, PREG_SET_ORDER)) {
29 2
            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...
30 2
                $this->match = $matches[$i];
31 2
                $this->setForeachBlock();
32 2
                $this->setForeachArray();
33 2
                $this->setForeachCallback();
34 2
                $this->setForeachContent();
35
36 2
                $content  = "<?php foreach({$this->array} as {$this->callback}): ?>";
37 2
                $content .= $this->foreachContent;
38 2
                $content .= "<?php endforeach; ?>";
39
40 2
                $this->content = str_replace($this->block, $content, $this->content);
41
            };
42
        }
43 2
    }
44
45 2
    private function setForeachBlock() : void
46
    {
47 2
        $this->block = $this->match[0];
48 2
    }
49
50 2
    private function setForeachArray() : void
51
    {
52 2
        $explode = explode('.', $this->match[1]);
53
54 2
        $variableArray = '$'.$explode[0];
55
56 2
        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...
57 2
            $variableArray .= "['".$explode[$i]."']";
58
        }
59
60 2
        $this->array = $variableArray;
61 2
    }
62
63 2
    private function setForeachCallback() : void
64
    {
65 2
        $this->callback = '$'.$this->match[2];
66 2
    }
67
68 2
    private function setForeachContent() : void
69
    {
70 2
        $this->foreachContent = trim($this->match[3]);
71 2
    }
72
}
73