Passed
Push — master ( 577b1c...f24633 )
by Maurício
03:03 queued 12s
created

WithKeyword   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Test Coverage

Coverage 76.47%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 17
dl 0
loc 61
ccs 13
cts 17
cp 0.7647
rs 10
c 3
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A parse() 0 3 1
A __construct() 0 3 1
A build() 0 21 4
A __toString() 0 3 1
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
     */
51 12
    public static function build($component): string
52
    {
53 12
        if (! isset($component->statement)) {
54 2
            throw new RuntimeException('No statement inside WITH');
55
        }
56
57 10
        $str = $component->name;
58
59 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...
60 6
            $str .= ArrayObj::build($component->columns);
61
        }
62
63 10
        $str .= ' AS (';
64
65 10
        foreach ($component->statement->statements as $statement) {
66 10
            $str .= $statement->build();
67
        }
68
69 10
        $str .= ')';
70
71 10
        return $str;
72
    }
73
74
    public function __toString(): string
75
    {
76
        return static::build($this);
77
    }
78
}
79