Expression::__toString()   A
last analyzed

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 0
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 AllowDynamicProperties;
0 ignored issues
show
Bug introduced by
The type AllowDynamicProperties was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use PhpMyAdmin\SqlParser\Component;
9
use PhpMyAdmin\SqlParser\Context;
10
11
use function implode;
12
13
/**
14
 * Parses a reference to an expression (column, table or database name, function
15
 * call, mathematical expression, etc.).
16
 */
17
#[AllowDynamicProperties]
18
final class Expression implements Component
19
{
20
    /**
21
     * The name of this database.
22
     */
23
    public string|null $database = null;
24
25
    /**
26
     * The name of this table.
27
     */
28
    public string|null $table = null;
29
30
    /**
31
     * The name of the column.
32
     */
33
    public string|null $column = null;
34
35
    /**
36
     * The sub-expression.
37
     */
38
    public string|null $expr = '';
39
40
    /**
41
     * The alias of this expression.
42
     */
43
    public string|null $alias = null;
44
45
    /**
46
     * The name of the function.
47
     */
48
    public string|null $function = null;
49
50
    /**
51
     * The type of subquery.
52
     */
53
    public string|null $subquery = null;
54
55
    /**
56
     * Syntax:
57
     *     new Expression('expr')
58
     *     new Expression('expr', 'alias')
59
     *     new Expression('database', 'table', 'column')
60
     *     new Expression('database', 'table', 'column', 'alias')
61
     *
62
     * If the database, table or column name is not required, pass an empty
63
     * string.
64
     *
65
     * @param string|null $database The name of the database or the expression.
66
     * @param string|null $table    The name of the table or the alias of the expression.
67
     * @param string|null $column   the name of the column
68
     * @param string|null $alias    the name of the alias
69
     */
70 1168
    public function __construct(
71
        string|null $database = null,
72
        string|null $table = null,
73
        string|null $column = null,
74
        string|null $alias = null,
75
    ) {
76 1168
        if (($column === null) && ($alias === null)) {
77 1168
            $this->expr = $database; // case 1
78 1168
            $this->alias = $table; // case 2
79
        } else {
80 4
            $this->database = $database; // case 3
81 4
            $this->table = $table; // case 3
82 4
            $this->column = $column; // case 3
83 4
            $this->alias = $alias; // case 4
84
        }
85
    }
86
87 284
    public function build(): string
88
    {
89 284
        if ($this->expr !== '' && $this->expr !== null) {
90 266
            $ret = $this->expr;
91
        } else {
92 22
            $fields = [];
93 22
            if (isset($this->database) && ($this->database !== '')) {
94 2
                $fields[] = $this->database;
95
            }
96
97 22
            if (isset($this->table) && ($this->table !== '')) {
98 22
                $fields[] = $this->table;
99
            }
100
101 22
            if (isset($this->column) && ($this->column !== '')) {
102 2
                $fields[] = $this->column;
103
            }
104
105 22
            $ret = implode('.', Context::escapeAll($fields));
106
        }
107
108 284
        if (! empty($this->alias)) {
109 32
            $ret .= ' AS ' . Context::escape($this->alias);
110
        }
111
112 284
        return $ret;
113
    }
114
115 226
    public function __toString(): string
116
    {
117 226
        return $this->build();
118
    }
119
}
120