Passed
Push — master ( 973a91...34c593 )
by Edson
01:12
created

InheritanceTag::extends()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 5
ccs 1
cts 1
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Sketch\Tpl;
4
5
/**
6
 * Class InheritanceTag
7
 * @package Sketch\Tpl
8
 */
9
class InheritanceTag extends Tag
10
{
11
    /**
12
     * @var array
13
     */
14
    private $blocks = [];
15
    
16
    /**
17
     * @var string
18
     */
19
    private $patternExtends = '/{\s?extends \'?"?(.*?)"?\'?\s?}/is';
20
21
    /**
22
     * InheritanceTag constructor.
23
     * @throws \Exception
24
     */
25
    public function __construct()
26
    {
27
        parent::__construct('/{\s?block \'?"?([\w]+)"?\'?\s?}(.*?){\s?\/block\s?}/is');
28 4
        $this->extends();
29
    }
30 4
31 4
    /**
32 4
     * @throws \Exception
33
     */
34
    private function extends(): void
35
    {
36
        if (preg_match($this->patternExtends, self::$content, $match)) {
37 4
            self::$content = Content::getContent($match[1], self::$config);
38
            $this->replaceTag();
39 4
        }
40 2
    }
41 2
42
    private function replaceTag(): void
43 4
    {
44
        foreach ($this->blocks as $key => $value) {
45 2
            $pattern = '/{\s?block \'?"?' . $key . '"?\'?\s?}(.*?){\s?\/block\s?}/is';
46
            self::$content = preg_replace($pattern, $value, self::$content);
47 2
        }
48 2
49 2
        self::$content = preg_replace('/{\s?block \'?"?[\w]+"?\'?\s?}/is', '', self::$content);
50
        self::$content = preg_replace('/{\s?\/block\s?}/is', '', self::$content);
51
    }
52 2
53 2
    public function handle(): string
54 2
    {
55
        $this->blocks[$this->match[1]] = $this->match[2];
56 4
        self::$content = str_replace($this->blocks[$this->match[1]], '', self::$content);
57
        return '';
58 4
    }
59
}
60