Passed
Push — master ( 3053a1...49b8bf )
by Thomas
14:13
created

DebugBarUtils::formatListQuery()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 5
c 1
b 0
f 1
nc 2
nop 2
dl 0
loc 8
rs 10
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
    public static function formatListQuery(DataList $list, bool $inline = true): string
33
    {
34
        $parameters = [];
35
        $formatted = self::formatSql($list->sql($parameters));
36
        if ($inline) {
37
            $formatted = DB::inline_parameters($formatted, $parameters);
38
        }
39
        return $formatted;
40
    }
41
42
    public static function isCli(): bool
43
    {
44
        return in_array(PHP_SAPI ?: '', ['cli', 'phpdbg']) || !http_response_code();
45
    }
46
}
47