WithKeyword::__toString()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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