BladeSourceMapCompiler::trimEmptyLines()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Facade\Ignition\Views\Compilers;
4
5
use Illuminate\View\Compilers\BladeCompiler;
6
7
class BladeSourceMapCompiler extends BladeCompiler
8
{
9
    public function detectLineNumber(string $filename, int $exceptionLineNumber): int
10
    {
11
        $map = $this->compileString(file_get_contents($filename));
12
        $map = explode("\n", $map);
13
14
        $line = $map[$exceptionLineNumber - 1] ?? $exceptionLineNumber;
15
        $pattern = '/\|---LINE:([0-9]+)---\|/m';
16
17
        if (preg_match($pattern, $line, $matches)) {
18
            return $matches[1];
19
        }
20
21
        return $exceptionLineNumber;
22
    }
23
24
    public function compileString($value)
25
    {
26
        try {
27
            $value = $this->addEchoLineNumbers($value);
28
29
            $value = $this->addStatementLineNumbers($value);
30
31
            $value = parent::compileString($value);
32
33
            return $this->trimEmptyLines($value);
34
        } catch (\Exception $e) {
35
            return $value;
36
        }
37
    }
38
39
    protected function addEchoLineNumbers(string $value)
40
    {
41
        $pattern = sprintf('/(@)?%s\s*(.+?)\s*%s(\r?\n)?/s', $this->contentTags[0], $this->contentTags[1]);
42
43
        if (preg_match_all($pattern, $value, $matches, PREG_OFFSET_CAPTURE)) {
44 View Code Duplication
            foreach (array_reverse($matches[0]) as $match) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
45
                $position = mb_strlen(substr($value, 0, $match[1]));
46
47
                $value = $this->insertLineNumberAtPosition($position, $value);
48
            }
49
        }
50
51
        return $value;
52
    }
53
54
    protected function addStatementLineNumbers(string $value)
55
    {
56
        $shouldInsertLineNumbers = preg_match_all(
57
            '/\B@(@?\w+(?:::\w+)?)([ \t]*)(\( ( (?>[^()]+) | (?3) )* \))?/x',
58
            $value,
59
            $matches,
60
            PREG_OFFSET_CAPTURE
61
        );
62
63
        if ($shouldInsertLineNumbers) {
64 View Code Duplication
            foreach (array_reverse($matches[0]) as $match) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
65
                $position = mb_strlen(substr($value, 0, $match[1]));
66
67
                $value = $this->insertLineNumberAtPosition($position, $value);
68
            }
69
        }
70
71
        return $value;
72
    }
73
74
    protected function insertLineNumberAtPosition(int $position, string $value)
75
    {
76
        $before = mb_substr($value, 0, $position);
77
        $lineNumber = count(explode("\n", $before));
78
79
        return mb_substr($value, 0, $position)."|---LINE:{$lineNumber}---|".mb_substr($value, $position);
80
    }
81
82
    protected function trimEmptyLines(string $value)
83
    {
84
        $value = preg_replace('/^\|---LINE:([0-9]+)---\|$/m', '', $value);
85
86
        return ltrim($value, PHP_EOL);
87
    }
88
}
89