Total Complexity | 9 |
Total Lines | 48 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 0 |
1 | <?php |
||
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 | 2 | public function __construct(string $content, array $config) |
|
15 | { |
||
16 | 2 | $this->config = $config; |
|
17 | 2 | $this->content = $content; |
|
18 | 2 | $this->block(); |
|
19 | 2 | $this->extends(); |
|
20 | 2 | } |
|
21 | |||
22 | 2 | public function __toString(): string |
|
23 | { |
||
24 | 2 | return $this->content; |
|
25 | } |
||
26 | |||
27 | 2 | private function block(): void |
|
28 | { |
||
29 | 2 | if (preg_match_all($this->patternBlock, $this->content, $matches, PREG_SET_ORDER)) { |
|
30 | 2 | for ($i = 0; $i < count($matches); $i++) { |
|
|
|||
31 | 2 | $this->blocks[$matches[$i][1]] = $matches[$i][2]; |
|
32 | 2 | $this->content = str_replace($this->blocks[$matches[$i][1]], '', $this->content); |
|
33 | }; |
||
34 | } |
||
35 | 2 | } |
|
36 | |||
37 | 2 | private function extends(): void |
|
38 | { |
||
39 | 2 | if (preg_match($this->patternExtends, $this->content, $match)) { |
|
40 | 2 | $this->content = $this->getContent($match[1]); |
|
41 | 2 | $this->replace(); |
|
42 | } |
||
43 | 2 | } |
|
44 | |||
45 | 2 | private function replace(): void |
|
53 | 2 | } |
|
54 | } |
||
55 |
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: