Passed
Push — trunk ( 9dfcf5...f40966 )
by Christian
12:13 queued 13s
created

DoctrineExtension::escapeFunction()   C

Complexity

Conditions 12
Paths 8

Size

Total Lines 42
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 23
c 0
b 0
f 0
nc 8
nop 1
dl 0
loc 42
rs 6.9666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php declare(strict_types=1);
2
3
namespace Shopware\Core\Profiling\Twig;
4
5
use Doctrine\SqlFormatter\HtmlHighlighter;
6
use Doctrine\SqlFormatter\NullHighlighter;
7
use Doctrine\SqlFormatter\SqlFormatter;
8
use Shopware\Core\Framework\Log\Package;
9
use Symfony\Component\VarDumper\Cloner\Data;
10
use Twig\Extension\AbstractExtension;
11
use Twig\TwigFilter;
12
13
#[Package('core
14
This class contains the needed functions in order to do the query highlighting')]
15
class DoctrineExtension extends AbstractExtension
16
{
17
    private SqlFormatter $sqlFormatter;
18
19
    /**
20
     * Define our functions
21
     *
22
     * @return list<TwigFilter>
23
     */
24
    public function getFilters(): array
25
    {
26
        return [
27
            new TwigFilter('doctrine_prettify_sql', $this->prettifySql(...), ['is_safe' => ['html']]),
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected ')' on line 27 at column 74
Loading history...
28
            new TwigFilter('doctrine_format_sql', $this->formatSql(...), ['is_safe' => ['html']]),
29
            new TwigFilter('doctrine_replace_query_parameters', $this->replaceQueryParameters(...)),
30
        ];
31
    }
32
33
    /**
34
     * Escape parameters of a SQL query
35
     * DON'T USE THIS FUNCTION OUTSIDE ITS INTENDED SCOPE
36
     *
37
     * @internal
38
     */
39
    public static function escapeFunction(mixed $parameter): string
40
    {
41
        $result = $parameter;
42
43
        switch (true) {
44
            // Check if result is non-unicode string using PCRE_UTF8 modifier
45
            case \is_string($result) && !preg_match('//u', $result):
46
                $result = '0x' . strtoupper(bin2hex($result));
47
48
                break;
49
50
            case \is_string($result):
51
                $result = '\'' . addslashes($result) . '\'';
52
53
                break;
54
55
            case \is_array($result):
56
                foreach ($result as &$value) {
57
                    $value = static::escapeFunction($value);
58
                }
59
60
                $result = implode(', ', $result) ?: 'NULL';
61
62
                break;
63
64
            case \is_object($result) && method_exists($result, '__toString'):
65
                $result = addslashes((string) $result->__toString());
66
67
                break;
68
69
            case $result === null:
70
                $result = 'NULL';
71
72
                break;
73
74
            case \is_bool($result):
75
                $result = $result ? '1' : '0';
76
77
                break;
78
        }
79
80
        return (string) $result;
81
    }
82
83
    /**
84
     * Return a query with the parameters replaced
85
     *
86
     * @param array<mixed>|Data $parameters
87
     */
88
    public function replaceQueryParameters(string $query, array|Data $parameters = []): string
89
    {
90
        if ($parameters instanceof Data) {
91
            $parameters = $parameters->getValue(true);
92
            /** @var array<mixed> $parameters */
93
        }
94
95
        $i = 0;
96
97
        if (!\array_key_exists(0, $parameters) && \array_key_exists(1, $parameters)) {
98
            $i = 1;
99
        }
100
101
        return (string) preg_replace_callback(
102
            '/\?|((?<!:):[a-z0-9_]+)/i',
103
            static function ($matches) use ($parameters, &$i) {
104
                $key = substr($matches[0], 1);
105
106
                if (!\array_key_exists($i, $parameters) && !\array_key_exists($key, $parameters)) {
107
                    return $matches[0];
108
                }
109
110
                $value = \array_key_exists($i, $parameters) ? $parameters[$i] : $parameters[$key];
111
                $result = DoctrineExtension::escapeFunction($value);
112
                ++$i;
113
114
                return $result;
115
            },
116
            $query
117
        );
118
    }
119
120
    public function prettifySql(string $sql): string
121
    {
122
        $this->setUpSqlFormatter();
123
124
        return $this->sqlFormatter->highlight($sql);
125
    }
126
127
    public function formatSql(string $sql, bool $highlight): string
128
    {
129
        $this->setUpSqlFormatter($highlight);
130
131
        return $this->sqlFormatter->format($sql);
132
    }
133
134
    /**
135
     * Get the name of the extension
136
     */
137
    public function getName(): string
138
    {
139
        return 'doctrine_extension';
140
    }
141
142
    private function setUpSqlFormatter(bool $highlight = true, bool $legacy = false): void
143
    {
144
        $this->sqlFormatter = new SqlFormatter($highlight ? new HtmlHighlighter([
145
            HtmlHighlighter::HIGHLIGHT_PRE => 'class="highlight highlight-sql"',
146
            HtmlHighlighter::HIGHLIGHT_QUOTE => 'class="string"',
147
            HtmlHighlighter::HIGHLIGHT_BACKTICK_QUOTE => 'class="string"',
148
            HtmlHighlighter::HIGHLIGHT_RESERVED => 'class="keyword"',
149
            HtmlHighlighter::HIGHLIGHT_BOUNDARY => 'class="symbol"',
150
            HtmlHighlighter::HIGHLIGHT_NUMBER => 'class="number"',
151
            HtmlHighlighter::HIGHLIGHT_WORD => 'class="word"',
152
            HtmlHighlighter::HIGHLIGHT_ERROR => 'class="error"',
153
            HtmlHighlighter::HIGHLIGHT_COMMENT => 'class="comment"',
154
            HtmlHighlighter::HIGHLIGHT_VARIABLE => 'class="variable"',
155
        ], !$legacy) : new NullHighlighter());
156
    }
157
}
158