Passed
Push — 2.x ( e8dd26...ba8025 )
by Quentin
07:25
created

BladeCompiler::compile()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 12.4085

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 12
c 1
b 0
f 0
nc 5
nop 2
dl 0
loc 18
ccs 4
cts 12
cp 0.3333
crap 12.4085
rs 9.5555
1
<?php
2
3
namespace A17\Twill\Services\Blocks;
4
5
use Exception;
6
use Throwable;
7
use Illuminate\View\Factory;
8
use Illuminate\Support\Facades\Blade;
9
use Symfony\Component\Debug\Exception\FatalThrowableError;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Debug\...ion\FatalThrowableError 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...
10
11
class BladeCompiler
12
{
13
    /**
14
     * @param $data
15
     * @return mixed
16
     */
17 5
    protected static function absorbApplicationEnvironment($data)
18
    {
19 5
        $data['__env'] = app(Factory::class);
20
21 5
        return $data;
22
    }
23
24
    /**
25
     * @param string $php
26
     * @param array $data
27
     * @throws \Symfony\Component\Debug\Exception\FatalThrowableError
28
     */
29 5
    protected static function compile(string $php, array $data)
30
    {
31 5
        $obLevel = self::initializeOutputBuffering();
32
33
        try {
34 5
            extract(self::absorbApplicationEnvironment($data), EXTR_SKIP);
35
36 5
            eval('?' . '>' . $php);
0 ignored issues
show
introduced by
The use of eval() is discouraged.
Loading history...
37
        } catch (Exception $e) {
38
            while (ob_get_level() > $obLevel) {
39
                ob_end_clean();
40
            }
41
            throw $e;
42
        } catch (Throwable $e) {
43
            while (ob_get_level() > $obLevel) {
44
                ob_end_clean();
45
            }
46
            throw new FatalThrowableError($e);
47
        }
48 5
    }
49
50
    /**
51
     * @return false|string
52
     */
53 5
    protected static function getRendered()
54
    {
55 5
        return ob_get_clean();
56
    }
57
58
    /**
59
     * @return int
60
     */
61 5
    protected static function initializeOutputBuffering()
62
    {
63 5
        $obLevel = ob_get_level();
64
65 5
        ob_start();
66
67 5
        return $obLevel;
68
    }
69
70
    /**
71
     * @param $string
72
     * @param $data
73
     * @return false|string
74
     * @throws \Symfony\Component\Debug\Exception\FatalThrowableError
75
     */
76 5
    public static function render($string, $data)
77
    {
78 5
        self::compile(Blade::compileString($string), $data);
79
80 5
        return self::getRendered();
81
    }
82
}
83