Completed
Push — master ( 578d3a...04a929 )
by Maurício
34s queued 14s
created

WithKeyword::build()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 10
nc 5
nop 0
dl 0
loc 21
ccs 11
cts 11
cp 1
crap 4
rs 9.9332
c 1
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