Passed
Pull Request — master (#535)
by
unknown
02:58
created

WithKeyword::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
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 RuntimeException;
10
11
/**
12
 * `WITH` keyword builder.
13
 */
14
final class WithKeyword implements Component
15
{
16
    /** @var string */
17
    public $name;
18
19
    /** @var ArrayObj[] */
20
    public $columns = [];
21
22
    /** @var Parser|null */
23
    public $statement;
24
25 50
    public function __construct(string $name)
26
    {
27 50
        $this->name = $name;
28
    }
29
30 12
    public function build(): string
31
    {
32 12
        if (! isset($this->statement)) {
33 2
            throw new RuntimeException('No statement inside WITH');
34
        }
35
36 10
        $str = $this->name;
37
38 10
        if ($this->columns) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->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...
39 6
            $str .= ArrayObj::buildAll($this->columns);
40
        }
41
42 10
        $str .= ' AS (';
43
44 10
        foreach ($this->statement->statements as $statement) {
45 10
            $str .= $statement->build();
46
        }
47
48 10
        $str .= ')';
49
50 10
        return $str;
51
    }
52
53
    public function __toString(): string
54
    {
55
        return $this->build();
56
    }
57
}
58