Passed
Push — master ( ab7df5...238a89 )
by Edson
01:47
created

Inheritance::extends()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
crap 6
1
<?php
2
3
namespace Bonfim\Component\View;
4
5
class Inheritance
6
{
7
    use ParseTpl;
8
9
    private $blocks = [];
10
    private $content = '';
11
    private $patternBlock   = '/{\s?block \'?"?([\w]+)"?\'?\s?}(.*?){\s?\/block\s?}/is';
12
    private $patternExtends = '/{\s?extends \'?"?(.*?)"?\'?\s?}/is';
13
14
    public function __construct(string $content, array $config)
15
    {
16
        $this->config = $config;
17
        $this->content = $content;
18
        $this->block();
19
        $this->extends();
20
    }
21
22
    public function __toString(): string
23
    {
24
        return $this->content;
25
    }
26
27
    private function block(): void
28
    {
29
        if (preg_match_all($this->patternBlock, $this->content, $matches, PREG_SET_ORDER)) {
30
            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...
31
                $this->blocks[$matches[$i][1]] = $matches[$i][2];
32
                $this->content = str_replace($this->blocks[$matches[$i][1]], '', $this->content);
33
            };
34
        }
35
    }
36
37
    private function extends(): void
38
    {
39
        if (preg_match($this->patternExtends, $this->content, $match)) {
40
            $this->content = $this->getContent($match[1]);
41
            $this->replace();
42
        }
43
    }
44
45
    private function replace(): void
46
    {
47
        foreach ($this->blocks as $key => $value) {
48
            $this->content = preg_replace('/{\s?block \'?"?'.$key.'"?\'?\s?}(.*?){\s?\/block\s?}/is', $value, $this->content);
49
        }
50
51
        $this->content = preg_replace('/{\s?block \'?"?[\w]+"?\'?\s?}/is', '', $this->content);
52
        $this->content = preg_replace('/{\s?\/block\s?}/is', '', $this->content);
53
    }
54
}
55