PatchingCompiler   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Test Coverage

Coverage 90%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 10
c 1
b 0
f 0
dl 0
loc 39
ccs 9
cts 10
cp 0.9
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A raw() 0 3 1
A write() 0 5 1
A patch() 0 10 2
1
<?php
2
declare(strict_types=1);
3
4
namespace Shoot\Shoot\Twig;
5
6
use Twig\Compiler;
7
8
/**
9
 * This compiler patches a few crucial lines in some core Twig classes. It allows Shoot to be used with extend, embed,
10
 * and blocks. Out of all hacks to make that work, this one seemed to be the most straightforward.
11
 *
12
 * @internal
13
 */
14
final class PatchingCompiler extends Compiler
15
{
16
    /**
17
     * @param mixed $string
18
     *
19
     * @return Compiler
20
     */
21 6
    public function raw($string)
22
    {
23 6
        return parent::raw($this->patch($string));
24
    }
25
26
    /**
27
     * @param mixed[] ...$strings
28
     *
29
     * @return Compiler
30
     */
31 6
    public function write(...$strings)
32
    {
33 6
        $strings = array_map([$this, 'patch'], $strings);
34
35 6
        return parent::write(...$strings);
36
    }
37
38
    /**
39
     * @param mixed $string
40
     *
41
     * @return mixed
42
     */
43 6
    private function patch($string)
44
    {
45 6
        static $patterns = [
46
            '/(->display\()\$context(, array_merge\(\$this->blocks, \$blocks\)\);\n)/',
47
            '/(\$this->displayBlock\(\'[^\']+\', )\$context(, \$blocks\);\n)/',
48
        ];
49
50 6
        return !is_string($string)
51
            ? $string
52 6
            : preg_replace($patterns, '$1array_merge($context, $originalContext ?? [])$2', $string);
53
    }
54
}
55