DebugBarUtils   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 2
Metric Value
eloc 18
c 3
b 0
f 2
dl 0
loc 47
rs 10
wmc 11

3 Methods

Rating   Name   Duplication   Size   Complexity  
A formatSql() 0 14 5
A formatListQuery() 0 11 3
A isCli() 0 3 3
1
<?php
2
3
namespace LeKoala\DebugBar;
4
5
use SilverStripe\ORM\DataList;
6
use SilverStripe\ORM\DB;
7
8
class DebugBarUtils
9
{
10
11
    /**
12
     * Format a sql query string using available formatters.
13
     * If no formatters are available, simply return the string as is.
14
     * Highlighting is used if not in cli context
15
     */
16
    public static function formatSql(string $query): string
17
    {
18
        if (class_exists(\Doctrine\SqlFormatter\SqlFormatter::class)) {
19
            return (new \Doctrine\SqlFormatter\SqlFormatter())->format($query);
20
        }
21
        if (class_exists(\SqlFormatter::class)) {
0 ignored issues
show
Bug introduced by
The type SqlFormatter 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...
22
            return \SqlFormatter::format($query, !self::isCli());
23
        }
24
        if (class_exists(\SilverStripe\View\Parsers\SQLFormatter::class)) {
25
            $method = self::isCli() ? 'formatPlain' : 'formatHTML';
26
            $formatter = new \SilverStripe\View\Parsers\SQLFormatter;
27
            return $formatter->$method($query);
28
        }
29
        return $query;
30
    }
31
32
    /**
33
     * Show the underlying sql query for a list
34
     * @param DataList $list
35
     * @param bool $inline Inlines paramters
36
     * @param bool $noQuotes Remove ANSI Quotes
37
     * @return string
38
     */
39
    public static function formatListQuery(DataList $list, bool $inline = true, bool $noQuotes = false): string
40
    {
41
        $parameters = [];
42
        $formatted = self::formatSql($list->sql($parameters));
43
        if ($inline) {
44
            $formatted = DB::inline_parameters($formatted, $parameters);
45
        }
46
        if ($noQuotes) {
47
            $formatted = str_replace('&quot;', '', $formatted);
48
        }
49
        return $formatted;
50
    }
51
52
    public static function isCli(): bool
53
    {
54
        return in_array(PHP_SAPI ?: '', ['cli', 'phpdbg']) || !http_response_code();
55
    }
56
}
57