Passed
Pull Request — master (#334)
by Antoine
10:44 queued 43s
created

WithKeyword   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
dl 0
loc 40
rs 10
c 1
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A build() 0 21 4
A __construct() 0 3 1
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 $statement */
20
    public $name;
21
    /** @var ArrayObj[] $statement */
22
    public $columns = [];
23
    /** @var Parser $statement */
24
    public $statement;
25
26
    public function __construct(string $name)
27
    {
28
        $this->name = $name;
29
    }
30
31
    /**
32
     * @param WithKeyword $component
33
     *
34
     * @return string
35
     */
36
    public static function build($component, array $options = [])
37
    {
38
        if (! $component instanceof WithKeyword) {
0 ignored issues
show
introduced by
$component is always a sub-type of PhpMyAdmin\SqlParser\Components\WithKeyword.
Loading history...
39
            throw new RuntimeException('Can not build a component that is not a WithKeyword');
40
        }
41
42
        $str = $component->name;
43
44
        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...
45
            $str .= ArrayObj::build($component->columns);
46
        }
47
48
        $str .= ' AS (';
49
50
        foreach ($component->statement->statements as $statement) {
51
            $str .= $statement->build();
52
        }
53
54
        $str .= ')';
55
56
        return $str;
57
    }
58
}
59