Passed
Push — master ( 67cfc6...1f1489 )
by Edson
01:18
created

InheritanceTag::replace()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
namespace Sketch\Tpl;
4
5
class InheritanceTag extends Tag
0 ignored issues
show
Bug introduced by
The type Sketch\Tpl\Tag was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
{
7
    private $blocks = [];
8
    private $patternBlock = '/{\s?block \'?"?([\w]+)"?\'?\s?}(.*?){\s?\/block\s?}/is';
9
    private $patternExtends = '/{\s?extends \'?"?(.*?)"?\'?\s?}/is';
10
11
    public function __construct()
12
    {
13
        parent::__construct();
14
        $this->extends();
15
    }
16
17
    private function extends(): void
18
    {
19
        if (preg_match($this->patternExtends, self::$content, $match)) {
20
            self::$content = Content::getContent($match[1], self::$config);
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...
21
            $this->replace();
22
        }
23
    }
24
25
    private function replace(): void
26
    {
27
        foreach ($this->blocks as $key => $value) {
28
            $pattern = '/{\s?block \'?"?' . $key . '"?\'?\s?}(.*?){\s?\/block\s?}/is';
29
            self::$content = preg_replace($pattern, $value, self::$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...
30
        }
31
32
        self::$content = preg_replace('/{\s?block \'?"?[\w]+"?\'?\s?}/is', '', self::$content);
33
        self::$content = preg_replace('/{\s?\/block\s?}/is', '', self::$content);
34
    }
35
36
    public function handle(): void
37
    {
38
        if (preg_match_all($this->patternBlock, self::$content, $matches, PREG_SET_ORDER)) {
39
            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...
40
                $this->blocks[$matches[$i][1]] = $matches[$i][2];
41
                self::$content = str_replace($this->blocks[$matches[$i][1]], '', self::$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...
42
            };
43
        }
44
    }
45
}
46