Skip to content

Commit 4546dcf

Browse files
committed
Split up RenameOperation
Signed-off-by: Kamil Tekiela <[email protected]>
1 parent 2a796f9 commit 4546dcf

File tree

7 files changed

+183
-154
lines changed

7 files changed

+183
-154
lines changed

phpstan-baseline.neon

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,26 @@ parameters:
245245
count: 1
246246
path: src/Components/Lists/ParameterDefinitions.php
247247

248+
-
249+
message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Components\\\\RenameOperation\\:\\:\\$new \\(PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\) does not accept PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\|null\\.$#"
250+
count: 1
251+
path: src/Components/Lists/RenameOperations.php
252+
253+
-
254+
message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Components\\\\RenameOperation\\:\\:\\$new \\(PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\) in empty\\(\\) is not falsy\\.$#"
255+
count: 1
256+
path: src/Components/Lists/RenameOperations.php
257+
258+
-
259+
message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Components\\\\RenameOperation\\:\\:\\$old \\(PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\) does not accept PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\|null\\.$#"
260+
count: 1
261+
path: src/Components/Lists/RenameOperations.php
262+
263+
-
264+
message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Components\\\\RenameOperation\\:\\:\\$old \\(PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\) in empty\\(\\) is not falsy\\.$#"
265+
count: 2
266+
path: src/Components/Lists/RenameOperations.php
267+
248268
-
249269
message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Components\\\\LockExpression\\:\\:\\$table \\(PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\) does not accept PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\|null\\.$#"
250270
count: 1
@@ -362,22 +382,12 @@ parameters:
362382

363383
-
364384
message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Components\\\\RenameOperation\\:\\:\\$new \\(PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\) does not accept PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\|null\\.$#"
365-
count: 2
366-
path: src/Components/RenameOperation.php
367-
368-
-
369-
message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Components\\\\RenameOperation\\:\\:\\$new \\(PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\) in empty\\(\\) is not falsy\\.$#"
370385
count: 1
371386
path: src/Components/RenameOperation.php
372387

373388
-
374389
message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Components\\\\RenameOperation\\:\\:\\$old \\(PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\) does not accept PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\|null\\.$#"
375-
count: 2
376-
path: src/Components/RenameOperation.php
377-
378-
-
379-
message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Components\\\\RenameOperation\\:\\:\\$old \\(PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\) in empty\\(\\) is not falsy\\.$#"
380-
count: 2
390+
count: 1
381391
path: src/Components/RenameOperation.php
382392

383393
-
@@ -606,7 +616,7 @@ parameters:
606616
path: src/Statements/PurgeStatement.php
607617

608618
-
609-
message: "#^Parameter \\#1 \\$component of static method PhpMyAdmin\\\\SqlParser\\\\Components\\\\RenameOperation\\:\\:buildAll\\(\\) expects array\\<PhpMyAdmin\\\\SqlParser\\\\Components\\\\RenameOperation\\>, array\\<PhpMyAdmin\\\\SqlParser\\\\Components\\\\RenameOperation\\>\\|null given\\.$#"
619+
message: "#^Parameter \\#1 \\$component of static method PhpMyAdmin\\\\SqlParser\\\\Components\\\\Lists\\\\RenameOperations\\:\\:buildAll\\(\\) expects array\\<PhpMyAdmin\\\\SqlParser\\\\Components\\\\RenameOperation\\>, array\\<PhpMyAdmin\\\\SqlParser\\\\Components\\\\RenameOperation\\>\\|null given\\.$#"
610620
count: 1
611621
path: src/Statements/RenameStatement.php
612622

psalm-baseline.xml

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,29 @@
297297
<code>$options</code>
298298
</UnusedParam>
299299
</file>
300+
<file src="src/Components/Lists/RenameOperations.php">
301+
<PossiblyNullPropertyAssignmentValue>
302+
<code><![CDATA[Expression::parse(
303+
$parser,
304+
$list,
305+
[
306+
'breakOnAlias' => true,
307+
'parseField' => 'table',
308+
],
309+
)]]></code>
310+
<code><![CDATA[Expression::parse(
311+
$parser,
312+
$list,
313+
[
314+
'breakOnAlias' => true,
315+
'parseField' => 'table',
316+
],
317+
)]]></code>
318+
</PossiblyNullPropertyAssignmentValue>
319+
<UnusedParam>
320+
<code>$options</code>
321+
</UnusedParam>
322+
</file>
300323
<file src="src/Components/LockExpression.php">
301324
<MissingConstructor>
302325
<code>$table</code>
@@ -467,26 +490,7 @@
467490
<PossiblyNullPropertyAssignmentValue>
468491
<code>$new</code>
469492
<code>$old</code>
470-
<code><![CDATA[Expression::parse(
471-
$parser,
472-
$list,
473-
[
474-
'breakOnAlias' => true,
475-
'parseField' => 'table',
476-
],
477-
)]]></code>
478-
<code><![CDATA[Expression::parse(
479-
$parser,
480-
$list,
481-
[
482-
'breakOnAlias' => true,
483-
'parseField' => 'table',
484-
],
485-
)]]></code>
486493
</PossiblyNullPropertyAssignmentValue>
487-
<UnusedParam>
488-
<code>$options</code>
489-
</UnusedParam>
490494
</file>
491495
<file src="src/Components/SetOperation.php">
492496
<PossiblyNullPropertyAssignmentValue>
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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\RenameOperation;
9+
use PhpMyAdmin\SqlParser\Parser;
10+
use PhpMyAdmin\SqlParser\TokensList;
11+
use PhpMyAdmin\SqlParser\TokenType;
12+
13+
use function implode;
14+
15+
/**
16+
* `RENAME TABLE` keyword parser.
17+
*/
18+
final class RenameOperations
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 RenameOperation[]
26+
*/
27+
public static function parse(Parser $parser, TokensList $list, array $options = []): array
28+
{
29+
$ret = [];
30+
31+
$expr = new RenameOperation();
32+
33+
/**
34+
* The state of the parser.
35+
*
36+
* Below are the states of the parser.
37+
*
38+
* 0 ---------------------[ old name ]--------------------> 1
39+
*
40+
* 1 ------------------------[ TO ]-----------------------> 2
41+
*
42+
* 2 ---------------------[ new name ]--------------------> 3
43+
*
44+
* 3 ------------------------[ , ]------------------------> 0
45+
* 3 -----------------------[ else ]----------------------> (END)
46+
*
47+
* @var int
48+
*/
49+
$state = 0;
50+
51+
for (; $list->idx < $list->count; ++$list->idx) {
52+
/**
53+
* Token parsed at this moment.
54+
*/
55+
$token = $list->tokens[$list->idx];
56+
57+
// End of statement.
58+
if ($token->type === TokenType::Delimiter) {
59+
break;
60+
}
61+
62+
// Skipping whitespaces and comments.
63+
if (($token->type === TokenType::Whitespace) || ($token->type === TokenType::Comment)) {
64+
continue;
65+
}
66+
67+
if ($state === 0) {
68+
$expr->old = Expression::parse(
69+
$parser,
70+
$list,
71+
[
72+
'breakOnAlias' => true,
73+
'parseField' => 'table',
74+
],
75+
);
76+
if (empty($expr->old)) {
77+
$parser->error('The old name of the table was expected.', $token);
78+
}
79+
80+
$state = 1;
81+
} elseif ($state === 1) {
82+
if ($token->type !== TokenType::Keyword || $token->keyword !== 'TO') {
83+
$parser->error('Keyword "TO" was expected.', $token);
84+
break;
85+
}
86+
87+
$state = 2;
88+
} elseif ($state === 2) {
89+
$expr->new = Expression::parse(
90+
$parser,
91+
$list,
92+
[
93+
'breakOnAlias' => true,
94+
'parseField' => 'table',
95+
],
96+
);
97+
if (empty($expr->new)) {
98+
$parser->error('The new name of the table was expected.', $token);
99+
}
100+
101+
$state = 3;
102+
} elseif ($state === 3) {
103+
if (($token->type !== TokenType::Operator) || ($token->value !== ',')) {
104+
break;
105+
}
106+
107+
$ret[] = $expr;
108+
$expr = new RenameOperation();
109+
$state = 0;
110+
}
111+
}
112+
113+
if ($state !== 3) {
114+
$parser->error('A rename operation was expected.', $list->tokens[$list->idx - 1]);
115+
}
116+
117+
// Last iteration was not saved.
118+
if (! empty($expr->old)) {
119+
$ret[] = $expr;
120+
}
121+
122+
--$list->idx;
123+
124+
return $ret;
125+
}
126+
127+
/** @param RenameOperation[] $component the component to be built */
128+
public static function buildAll(array $component): string
129+
{
130+
return implode(', ', $component);
131+
}
132+
}

src/Components/RenameOperation.php

Lines changed: 0 additions & 118 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
* `RENAME TABLE` keyword parser.
@@ -40,124 +35,11 @@ public function __construct(Expression|null $old = null, Expression|null $new =
4035
$this->new = $new;
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 RenameOperation[]
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 ---------------------[ old name ]--------------------> 1
62-
*
63-
* 1 ------------------------[ TO ]-----------------------> 2
64-
*
65-
* 2 ---------------------[ new name ]--------------------> 3
66-
*
67-
* 3 ------------------------[ , ]------------------------> 0
68-
* 3 -----------------------[ else ]----------------------> (END)
69-
*
70-
* @var int
71-
*/
72-
$state = 0;
73-
74-
for (; $list->idx < $list->count; ++$list->idx) {
75-
/**
76-
* Token parsed at this moment.
77-
*/
78-
$token = $list->tokens[$list->idx];
79-
80-
// End of statement.
81-
if ($token->type === TokenType::Delimiter) {
82-
break;
83-
}
84-
85-
// Skipping whitespaces and comments.
86-
if (($token->type === TokenType::Whitespace) || ($token->type === TokenType::Comment)) {
87-
continue;
88-
}
89-
90-
if ($state === 0) {
91-
$expr->old = Expression::parse(
92-
$parser,
93-
$list,
94-
[
95-
'breakOnAlias' => true,
96-
'parseField' => 'table',
97-
],
98-
);
99-
if (empty($expr->old)) {
100-
$parser->error('The old name of the table was expected.', $token);
101-
}
102-
103-
$state = 1;
104-
} elseif ($state === 1) {
105-
if ($token->type !== TokenType::Keyword || $token->keyword !== 'TO') {
106-
$parser->error('Keyword "TO" was expected.', $token);
107-
break;
108-
}
109-
110-
$state = 2;
111-
} elseif ($state === 2) {
112-
$expr->new = Expression::parse(
113-
$parser,
114-
$list,
115-
[
116-
'breakOnAlias' => true,
117-
'parseField' => 'table',
118-
],
119-
);
120-
if (empty($expr->new)) {
121-
$parser->error('The new name of the table was expected.', $token);
122-
}
123-
124-
$state = 3;
125-
} elseif ($state === 3) {
126-
if (($token->type !== TokenType::Operator) || ($token->value !== ',')) {
127-
break;
128-
}
129-
130-
$ret[] = $expr;
131-
$expr = new static();
132-
$state = 0;
133-
}
134-
}
135-
136-
if ($state !== 3) {
137-
$parser->error('A rename operation was expected.', $list->tokens[$list->idx - 1]);
138-
}
139-
140-
// Last iteration was not saved.
141-
if (! empty($expr->old)) {
142-
$ret[] = $expr;
143-
}
144-
145-
--$list->idx;
146-
147-
return $ret;
148-
}
149-
15038
public function build(): string
15139
{
15240
return $this->old . ' TO ' . $this->new;
15341
}
15442

155-
/** @param RenameOperation[] $component the component to be built */
156-
public static function buildAll(array $component): string
157-
{
158-
return implode(', ', $component);
159-
}
160-
16143
public function __toString(): string
16244
{
16345
return $this->build();

src/Parser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ class Parser
298298
'field' => 'procedure',
299299
],
300300
'RENAME' => [
301-
'class' => Components\RenameOperation::class,
301+
'class' => Components\Lists\RenameOperations::class,
302302
'field' => 'renames',
303303
],
304304
'REPAIR' => [

0 commit comments

Comments
 (0)