Passed
Pull Request — master (#334)
by Antoine
09:38
created

WithKeyword::build()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 10
c 1
b 0
f 0
nc 5
nop 2
dl 0
loc 21
rs 9.9332
1
<?php
2
/**
3
 * `UNION` keyword builder.
4
 */
5
6
declare(strict_types=1);
7
8
namespace PhpMyAdmin\SqlParser\Components;
9
10
use PhpMyAdmin\SqlParser\Component;
11
use PhpMyAdmin\SqlParser\Parser;
12
use RuntimeException;
13
14
/**
15
 * `WITH` keyword builder.
16
 */
17
class WithKeyword extends Component
18
{
19
    /** @var string */
20
    public $name;
21
22
    /** @var ArrayObj[] */
23
    public $columns = [];
24
25
    /** @var Parser */
26
    public $statement;
27
28
    public function __construct(string $name)
29
    {
30
        $this->name = $name;
31
    }
32
33
    /**
34
     * @param WithKeyword $component
35
     * @param mixed[]     $options
36
     *
37
     * @return string
38
     */
39
    public static function build($component, array $options = [])
40
    {
41
        if (! $component instanceof WithKeyword) {
0 ignored issues
show
introduced by
$component is always a sub-type of PhpMyAdmin\SqlParser\Components\WithKeyword.
Loading history...
42
            throw new RuntimeException('Can not build a component that is not a WithKeyword');
43
        }
44
45
        $str = $component->name;
46
47
        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...
48
            $str .= ArrayObj::build($component->columns);
49
        }
50
51
        $str .= ' AS (';
52
53
        foreach ($component->statement->statements as $statement) {
54
            $str .= $statement->build();
55
        }
56
57
        $str .= ')';
58
59
        return $str;
60
    }
61
}
62