Passed
Push — master ( b16987...8b6d77 )
by Maurício
03:49 queued 13s
created

Expression   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 30
dl 0
loc 115
ccs 24
cts 24
cp 1
rs 10
c 2
b 0
f 0
wmc 14

3 Methods

Rating   Name   Duplication   Size   Complexity  
B build() 0 26 10
A __toString() 0 3 1
A __construct() 0 14 3
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
     * @var string|null
24
     */
25
    public $database;
26
27
    /**
28
     * The name of this table.
29
     *
30
     * @var string|null
31
     */
32
    public $table;
33
34
    /**
35
     * The name of the column.
36
     *
37
     * @var string|null
38
     */
39
    public $column;
40
41
    /**
42
     * The sub-expression.
43
     *
44
     * @var string|null
45
     */
46
    public $expr = '';
47
48
    /**
49
     * The alias of this expression.
50
     *
51
     * @var string|null
52
     */
53
    public $alias;
54
55
    /**
56
     * The name of the function.
57
     *
58
     * @var mixed
59
     */
60
    public $function;
61
62
    /**
63
     * The type of subquery.
64
     *
65
     * @var string|null
66
     */
67
    public $subquery;
68
69
    /**
70
     * Syntax:
71
     *     new Expression('expr')
72
     *     new Expression('expr', 'alias')
73
     *     new Expression('database', 'table', 'column')
74
     *     new Expression('database', 'table', 'column', 'alias')
75
     *
76
     * If the database, table or column name is not required, pass an empty
77
     * string.
78
     *
79
     * @param string|null $database The name of the database or the expression.
80
     * @param string|null $table    The name of the table or the alias of the expression.
81
     * @param string|null $column   the name of the column
82
     * @param string|null $alias    the name of the alias
83
     */
84 1118
    public function __construct(
85
        string|null $database = null,
86
        string|null $table = null,
87
        string|null $column = null,
88
        string|null $alias = null,
89
    ) {
90 1118
        if (($column === null) && ($alias === null)) {
91 1118
            $this->expr = $database; // case 1
92 1118
            $this->alias = $table; // case 2
93
        } else {
94 4
            $this->database = $database; // case 3
95 4
            $this->table = $table; // case 3
96 4
            $this->column = $column; // case 3
97 4
            $this->alias = $alias; // case 4
98
        }
99
    }
100
101 260
    public function build(): string
102
    {
103 260
        if ($this->expr !== '' && $this->expr !== null) {
104 242
            $ret = $this->expr;
105
        } else {
106 22
            $fields = [];
107 22
            if (isset($this->database) && ($this->database !== '')) {
108 2
                $fields[] = $this->database;
109
            }
110
111 22
            if (isset($this->table) && ($this->table !== '')) {
112 22
                $fields[] = $this->table;
113
            }
114
115 22
            if (isset($this->column) && ($this->column !== '')) {
116 2
                $fields[] = $this->column;
117
            }
118
119 22
            $ret = implode('.', Context::escapeAll($fields));
120
        }
121
122 260
        if (! empty($this->alias)) {
123 32
            $ret .= ' AS ' . Context::escape($this->alias);
124
        }
125
126 260
        return $ret;
127
    }
128
129 226
    public function __toString(): string
130
    {
131 226
        return $this->build();
132
    }
133
}
134