Passed
Push — master ( 6c3f09...c54108 )
by William
03:43
created

WithKeyword::build()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 25
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 5
eloc 12
nc 6
nop 2
dl 0
loc 25
ccs 13
cts 13
cp 1
crap 5
rs 9.5555
c 2
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpMyAdmin\SqlParser\Components;
6
7
use PhpMyAdmin\SqlParser\Component;
8
use PhpMyAdmin\SqlParser\Parser;
9
use PhpMyAdmin\SqlParser\TokensList;
10
use PhpMyAdmin\SqlParser\Translator;
11
use RuntimeException;
12
13
/**
14
 * `WITH` keyword builder.
15
 */
16
final class WithKeyword implements Component
17
{
18
    /** @var string */
19
    public $name;
20
21
    /** @var ArrayObj[] */
22
    public $columns = [];
23
24
    /** @var Parser|null */
25
    public $statement;
26
27 48
    public function __construct(string $name)
28
    {
29 48
        $this->name = $name;
30
    }
31
32
    /**
33
     * Parses the tokens contained in the given list in the context of the given parser.
34
     *
35
     * @param Parser               $parser  the parser that serves as context
36
     * @param TokensList           $list    the list of tokens that are being parsed
37
     * @param array<string, mixed> $options parameters for parsing
38
     *
39
     * @return mixed
40
     *
41
     * @throws RuntimeException not implemented yet.
42
     */
43
    public static function parse(Parser $parser, TokensList $list, array $options = [])
44
    {
45
        throw new RuntimeException(Translator::gettext('Not implemented yet.'));
46
    }
47
48
    /**
49
     * @param WithKeyword          $component
50
     * @param array<string, mixed> $options
51
     */
52 14
    public static function build($component, array $options = []): string
53
    {
54 14
        if (! $component instanceof WithKeyword) {
0 ignored issues
show
introduced by
$component is always a sub-type of PhpMyAdmin\SqlParser\Components\WithKeyword.
Loading history...
55 2
            throw new RuntimeException('Can not build a component that is not a WithKeyword');
56
        }
57
58 12
        if (! isset($component->statement)) {
59 2
            throw new RuntimeException('No statement inside WITH');
60
        }
61
62 10
        $str = $component->name;
63
64 10
        if ($component->columns) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $component->columns of type PhpMyAdmin\SqlParser\Components\ArrayObj[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
65 6
            $str .= ArrayObj::build($component->columns);
66
        }
67
68 10
        $str .= ' AS (';
69
70 10
        foreach ($component->statement->statements as $statement) {
71 10
            $str .= $statement->build();
72
        }
73
74 10
        $str .= ')';
75
76 10
        return $str;
77
    }
78
79
    public function __toString(): string
80
    {
81
        return static::build($this);
82
    }
83
}
84