Skip to content

Commit a0d0cb5

Browse files
committed
Split up OrderKeyword
Signed-off-by: Kamil Tekiela <[email protected]>
1 parent 2eb3bde commit a0d0cb5

File tree

7 files changed

+126
-101
lines changed

7 files changed

+126
-101
lines changed

phpstan-baseline.neon

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,16 @@ parameters:
220220
count: 1
221221
path: src/Components/Lists/JoinKeywords.php
222222

223+
-
224+
message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Components\\\\OrderKeyword\\:\\:\\$expr \\(PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\) does not accept PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\|null\\.$#"
225+
count: 1
226+
path: src/Components/Lists/OrderKeywords.php
227+
228+
-
229+
message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Components\\\\OrderKeyword\\:\\:\\$expr \\(PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\) in empty\\(\\) is not falsy\\.$#"
230+
count: 2
231+
path: src/Components/Lists/OrderKeywords.php
232+
223233
-
224234
message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Components\\\\LockExpression\\:\\:\\$table \\(PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\) does not accept PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\|null\\.$#"
225235
count: 1
@@ -277,12 +287,7 @@ parameters:
277287

278288
-
279289
message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Components\\\\OrderKeyword\\:\\:\\$expr \\(PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\) does not accept PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\|null\\.$#"
280-
count: 2
281-
path: src/Components/OrderKeyword.php
282-
283-
-
284-
message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Components\\\\OrderKeyword\\:\\:\\$expr \\(PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\) in empty\\(\\) is not falsy\\.$#"
285-
count: 2
290+
count: 1
286291
path: src/Components/OrderKeyword.php
287292

288293
-

psalm-baseline.xml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,14 @@
275275
<code>$state</code>
276276
</UnusedVariable>
277277
</file>
278+
<file src="src/Components/Lists/OrderKeywords.php">
279+
<PossiblyNullPropertyAssignmentValue>
280+
<code>Expression::parse($parser, $list)</code>
281+
</PossiblyNullPropertyAssignmentValue>
282+
<UnusedParam>
283+
<code>$options</code>
284+
</UnusedParam>
285+
</file>
278286
<file src="src/Components/LockExpression.php">
279287
<MissingConstructor>
280288
<code>$table</code>
@@ -372,11 +380,7 @@
372380
<file src="src/Components/OrderKeyword.php">
373381
<PossiblyNullPropertyAssignmentValue>
374382
<code>$expr</code>
375-
<code>Expression::parse($parser, $list)</code>
376383
</PossiblyNullPropertyAssignmentValue>
377-
<UnusedParam>
378-
<code>$options</code>
379-
</UnusedParam>
380384
</file>
381385
<file src="src/Components/ParameterDefinition.php">
382386
<MixedAssignment>
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpMyAdmin\SqlParser\Components\Lists;
6+
7+
use PhpMyAdmin\SqlParser\Components\Expression;
8+
use PhpMyAdmin\SqlParser\Components\OrderKeyword;
9+
use PhpMyAdmin\SqlParser\Parser;
10+
use PhpMyAdmin\SqlParser\TokensList;
11+
use PhpMyAdmin\SqlParser\TokenType;
12+
13+
use function implode;
14+
15+
/**
16+
* `ORDER BY` keyword parser.
17+
*/
18+
final class OrderKeywords
19+
{
20+
/**
21+
* @param Parser $parser the parser that serves as context
22+
* @param TokensList $list the list of tokens that are being parsed
23+
* @param array<string, mixed> $options parameters for parsing
24+
*
25+
* @return OrderKeyword[]
26+
*/
27+
public static function parse(Parser $parser, TokensList $list, array $options = []): array
28+
{
29+
$ret = [];
30+
31+
$expr = new OrderKeyword();
32+
33+
/**
34+
* The state of the parser.
35+
*
36+
* Below are the states of the parser.
37+
*
38+
* 0 --------------------[ expression ]-------------------> 1
39+
*
40+
* 1 ------------------------[ , ]------------------------> 0
41+
* 1 -------------------[ ASC / DESC ]--------------------> 1
42+
*
43+
* @var int
44+
*/
45+
$state = 0;
46+
47+
for (; $list->idx < $list->count; ++$list->idx) {
48+
/**
49+
* Token parsed at this moment.
50+
*/
51+
$token = $list->tokens[$list->idx];
52+
53+
// End of statement.
54+
if ($token->type === TokenType::Delimiter) {
55+
break;
56+
}
57+
58+
// Skipping whitespaces and comments.
59+
if (($token->type === TokenType::Whitespace) || ($token->type === TokenType::Comment)) {
60+
continue;
61+
}
62+
63+
if ($state === 0) {
64+
$expr->expr = Expression::parse($parser, $list);
65+
$state = 1;
66+
} elseif ($state === 1) {
67+
if (
68+
($token->type === TokenType::Keyword)
69+
&& (($token->keyword === 'ASC') || ($token->keyword === 'DESC'))
70+
) {
71+
$expr->type = $token->keyword;
72+
} elseif (($token->type === TokenType::Operator) && ($token->value === ',')) {
73+
if (! empty($expr->expr)) {
74+
$ret[] = $expr;
75+
}
76+
77+
$expr = new OrderKeyword();
78+
$state = 0;
79+
} else {
80+
break;
81+
}
82+
}
83+
}
84+
85+
// Last iteration was not processed.
86+
if (! empty($expr->expr)) {
87+
$ret[] = $expr;
88+
}
89+
90+
--$list->idx;
91+
92+
return $ret;
93+
}
94+
95+
/** @param OrderKeyword[] $component the component to be built */
96+
public static function buildAll(array $component): string
97+
{
98+
return implode(', ', $component);
99+
}
100+
}

src/Components/OrderKeyword.php

Lines changed: 0 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@
55
namespace PhpMyAdmin\SqlParser\Components;
66

77
use PhpMyAdmin\SqlParser\Component;
8-
use PhpMyAdmin\SqlParser\Parser;
9-
use PhpMyAdmin\SqlParser\TokensList;
10-
use PhpMyAdmin\SqlParser\TokenType;
11-
12-
use function implode;
138

149
/**
1510
* `ORDER BY` keyword parser.
@@ -40,92 +35,11 @@ public function __construct(Expression|null $expr = null, string $type = 'ASC')
4035
$this->type = $type;
4136
}
4237

43-
/**
44-
* @param Parser $parser the parser that serves as context
45-
* @param TokensList $list the list of tokens that are being parsed
46-
* @param array<string, mixed> $options parameters for parsing
47-
*
48-
* @return OrderKeyword[]
49-
*/
50-
public static function parse(Parser $parser, TokensList $list, array $options = []): array
51-
{
52-
$ret = [];
53-
54-
$expr = new static();
55-
56-
/**
57-
* The state of the parser.
58-
*
59-
* Below are the states of the parser.
60-
*
61-
* 0 --------------------[ expression ]-------------------> 1
62-
*
63-
* 1 ------------------------[ , ]------------------------> 0
64-
* 1 -------------------[ ASC / DESC ]--------------------> 1
65-
*
66-
* @var int
67-
*/
68-
$state = 0;
69-
70-
for (; $list->idx < $list->count; ++$list->idx) {
71-
/**
72-
* Token parsed at this moment.
73-
*/
74-
$token = $list->tokens[$list->idx];
75-
76-
// End of statement.
77-
if ($token->type === TokenType::Delimiter) {
78-
break;
79-
}
80-
81-
// Skipping whitespaces and comments.
82-
if (($token->type === TokenType::Whitespace) || ($token->type === TokenType::Comment)) {
83-
continue;
84-
}
85-
86-
if ($state === 0) {
87-
$expr->expr = Expression::parse($parser, $list);
88-
$state = 1;
89-
} elseif ($state === 1) {
90-
if (
91-
($token->type === TokenType::Keyword)
92-
&& (($token->keyword === 'ASC') || ($token->keyword === 'DESC'))
93-
) {
94-
$expr->type = $token->keyword;
95-
} elseif (($token->type === TokenType::Operator) && ($token->value === ',')) {
96-
if (! empty($expr->expr)) {
97-
$ret[] = $expr;
98-
}
99-
100-
$expr = new static();
101-
$state = 0;
102-
} else {
103-
break;
104-
}
105-
}
106-
}
107-
108-
// Last iteration was not processed.
109-
if (! empty($expr->expr)) {
110-
$ret[] = $expr;
111-
}
112-
113-
--$list->idx;
114-
115-
return $ret;
116-
}
117-
11838
public function build(): string
11939
{
12040
return $this->expr . ' ' . $this->type;
12141
}
12242

123-
/** @param OrderKeyword[] $component the component to be built */
124-
public static function buildAll(array $component): string
125-
{
126-
return implode(', ', $component);
127-
}
128-
12943
public function __toString(): string
13044
{
13145
return $this->build();

src/Parser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ class Parser
286286
'options' => ['parseField' => 'table'],
287287
],
288288
'ORDER BY' => [
289-
'class' => Components\OrderKeyword::class,
289+
'class' => Components\Lists\OrderKeywords::class,
290290
'field' => 'order',
291291
],
292292
'PARTITION' => [

src/Statements/DeleteStatement.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use PhpMyAdmin\SqlParser\Components\Lists\Conditions;
1313
use PhpMyAdmin\SqlParser\Components\Lists\ExpressionArray;
1414
use PhpMyAdmin\SqlParser\Components\Lists\JoinKeywords;
15+
use PhpMyAdmin\SqlParser\Components\Lists\OrderKeywords;
1516
use PhpMyAdmin\SqlParser\Components\OptionsArray;
1617
use PhpMyAdmin\SqlParser\Components\OrderKeyword;
1718
use PhpMyAdmin\SqlParser\Parser;
@@ -180,7 +181,7 @@ public function build(): string
180181
}
181182

182183
if ($this->order !== null && $this->order !== []) {
183-
$ret .= ' ORDER BY ' . OrderKeyword::buildAll($this->order);
184+
$ret .= ' ORDER BY ' . OrderKeywords::buildAll($this->order);
184185
}
185186

186187
if ($this->limit !== null && strlen((string) $this->limit) > 0) {
@@ -290,7 +291,7 @@ public function parse(Parser $parser, TokensList $list): void
290291
break;
291292
case 'ORDER BY':
292293
++$list->idx; // Skip 'ORDER BY'
293-
$this->order = OrderKeyword::parse($parser, $list);
294+
$this->order = OrderKeywords::parse($parser, $list);
294295
$state = 5;
295296
break;
296297
case 'LIMIT':
@@ -328,7 +329,7 @@ public function parse(Parser $parser, TokensList $list): void
328329
switch ($token->keyword) {
329330
case 'ORDER BY':
330331
++$list->idx; // Skip 'ORDER BY'
331-
$this->order = OrderKeyword::parse($parser, $list);
332+
$this->order = OrderKeywords::parse($parser, $list);
332333
$state = 5;
333334
break;
334335
case 'LIMIT':

tests/Components/OrderKeywordTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace PhpMyAdmin\SqlParser\Tests\Components;
66

77
use PhpMyAdmin\SqlParser\Components\Expression;
8+
use PhpMyAdmin\SqlParser\Components\Lists\OrderKeywords;
89
use PhpMyAdmin\SqlParser\Components\OrderKeyword;
910
use PhpMyAdmin\SqlParser\Tests\TestCase;
1011

@@ -13,7 +14,7 @@ class OrderKeywordTest extends TestCase
1314
public function testBuildAll(): void
1415
{
1516
$this->assertEquals(
16-
OrderKeyword::buildAll(
17+
OrderKeywords::buildAll(
1718
[
1819
new OrderKeyword(new Expression('a'), 'ASC'),
1920
new OrderKeyword(new Expression('b'), 'DESC'),

0 commit comments

Comments
 (0)