Skip to content

Commit bc850bf

Browse files
committed
Split up GroupKeyword
Signed-off-by: Kamil Tekiela <[email protected]>
1 parent 70c02dc commit bc850bf

File tree

6 files changed

+122
-97
lines changed

6 files changed

+122
-97
lines changed

phpstan-baseline.neon

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,7 @@ parameters:
9292

9393
-
9494
message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Components\\\\GroupKeyword\\:\\:\\$expr \\(PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\) does not accept PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\|null\\.$#"
95-
count: 2
96-
path: src/Components/GroupKeyword.php
97-
98-
-
99-
message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Components\\\\GroupKeyword\\:\\:\\$expr \\(PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\) in empty\\(\\) is not falsy\\.$#"
100-
count: 2
95+
count: 1
10196
path: src/Components/GroupKeyword.php
10297

10398
-
@@ -210,6 +205,16 @@ parameters:
210205
count: 1
211206
path: src/Components/Lists/ExpressionArray.php
212207

208+
-
209+
message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Components\\\\GroupKeyword\\:\\:\\$expr \\(PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\) does not accept PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\|null\\.$#"
210+
count: 1
211+
path: src/Components/Lists/GroupKeywords.php
212+
213+
-
214+
message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Components\\\\GroupKeyword\\:\\:\\$expr \\(PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\) in empty\\(\\) is not falsy\\.$#"
215+
count: 2
216+
path: src/Components/Lists/GroupKeywords.php
217+
213218
-
214219
message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Components\\\\LockExpression\\:\\:\\$table \\(PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\) does not accept PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\|null\\.$#"
215220
count: 1

psalm-baseline.xml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,14 +135,10 @@
135135
<file src="src/Components/GroupKeyword.php">
136136
<PossiblyNullPropertyAssignmentValue>
137137
<code>$expr</code>
138-
<code>Expression::parse($parser, $list)</code>
139138
</PossiblyNullPropertyAssignmentValue>
140139
<PossiblyUnusedProperty>
141140
<code>$type</code>
142141
</PossiblyUnusedProperty>
143-
<UnusedParam>
144-
<code>$options</code>
145-
</UnusedParam>
146142
</file>
147143
<file src="src/Components/IndexHint.php">
148144
<MixedAssignment>
@@ -265,6 +261,14 @@
265261
<code>buildAll</code>
266262
</PossiblyUnusedMethod>
267263
</file>
264+
<file src="src/Components/Lists/GroupKeywords.php">
265+
<PossiblyNullPropertyAssignmentValue>
266+
<code>Expression::parse($parser, $list)</code>
267+
</PossiblyNullPropertyAssignmentValue>
268+
<UnusedParam>
269+
<code>$options</code>
270+
</UnusedParam>
271+
</file>
268272
<file src="src/Components/LockExpression.php">
269273
<MissingConstructor>
270274
<code>$table</code>

src/Components/GroupKeyword.php

Lines changed: 0 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@
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;
118

12-
use function implode;
139
use function trim;
1410

1511
/**
@@ -33,92 +29,11 @@ public function __construct(Expression|null $expr = null)
3329
$this->expr = $expr;
3430
}
3531

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

116-
/** @param GroupKeyword[] $component the component to be built */
117-
public static function buildAll(array $component): string
118-
{
119-
return implode(', ', $component);
120-
}
121-
12237
public function __toString(): string
12338
{
12439
return $this->build();
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\GroupKeyword;
9+
use PhpMyAdmin\SqlParser\Parser;
10+
use PhpMyAdmin\SqlParser\TokensList;
11+
use PhpMyAdmin\SqlParser\TokenType;
12+
13+
use function implode;
14+
15+
/**
16+
* `GROUP BY` keyword parser.
17+
*/
18+
final class GroupKeywords
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 GroupKeyword[]
26+
*/
27+
public static function parse(Parser $parser, TokensList $list, array $options = []): array
28+
{
29+
$ret = [];
30+
31+
$expr = new GroupKeyword();
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 GroupKeyword();
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 GroupKeyword[] $component the component to be built */
96+
public static function buildAll(array $component): string
97+
{
98+
return implode(', ', $component);
99+
}
100+
}

src/Parser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ class Parser
200200
'options' => ['field' => 'table'],
201201
],
202202
'GROUP BY' => [
203-
'class' => Components\GroupKeyword::class,
203+
'class' => Components\Lists\GroupKeywords::class,
204204
'field' => 'group',
205205
],
206206
'HAVING' => [

tests/Components/GroupKeywordTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Generator;
88
use PhpMyAdmin\SqlParser\Components\Expression;
99
use PhpMyAdmin\SqlParser\Components\GroupKeyword;
10+
use PhpMyAdmin\SqlParser\Components\Lists\GroupKeywords;
1011
use PhpMyAdmin\SqlParser\Tests\TestCase;
1112
use PHPUnit\Framework\Attributes\DataProvider;
1213

@@ -56,7 +57,7 @@ public static function provideExpressions(): Generator
5657
public function testBuild(GroupKeyword|array $component, string $expected): void
5758
{
5859
if (is_array($component)) {
59-
$this->assertSame($expected, GroupKeyword::buildAll($component));
60+
$this->assertSame($expected, GroupKeywords::buildAll($component));
6061
} else {
6162
$this->assertSame($expected, $component->build());
6263
}

0 commit comments

Comments
 (0)