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

WithKeyword   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Test Coverage

Coverage 86.67%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 16
dl 0
loc 42
ccs 13
cts 15
cp 0.8667
rs 10
c 2
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A __toString() 0 3 1
A build() 0 21 4
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