Total Complexity | 9 |
Total Lines | 48 |
Duplicated Lines | 0 % |
Coverage | 0% |
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 | 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++) { |
||
|
|||
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 |
||
53 | } |
||
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: