Skip to content

Commit 99e20df

Browse files
committed
Push Statement::parse() down to SelectStatement
This removes SelectStatement specific code from Statement::parse() method. Signed-off-by: Maurício Meneghini Fauth <[email protected]>
1 parent ac1004c commit 99e20df

File tree

4 files changed

+340
-53
lines changed

4 files changed

+340
-53
lines changed

phpstan-baseline.neon

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,121 @@ parameters:
685685
count: 1
686686
path: src/Statements/RenameStatement.php
687687

688+
-
689+
message: "#^Access to an undefined property PhpMyAdmin\\\\SqlParser\\\\Statements\\\\SelectStatement\\:\\:\\$call\\.$#"
690+
count: 1
691+
path: src/Statements/SelectStatement.php
692+
693+
-
694+
message: "#^Access to an undefined property PhpMyAdmin\\\\SqlParser\\\\Statements\\\\SelectStatement\\:\\:\\$fields\\.$#"
695+
count: 1
696+
path: src/Statements/SelectStatement.php
697+
698+
-
699+
message: "#^Access to an undefined property PhpMyAdmin\\\\SqlParser\\\\Statements\\\\SelectStatement\\:\\:\\$index_hints\\.$#"
700+
count: 1
701+
path: src/Statements/SelectStatement.php
702+
703+
-
704+
message: "#^Access to an undefined property PhpMyAdmin\\\\SqlParser\\\\Statements\\\\SelectStatement\\:\\:\\$renames\\.$#"
705+
count: 1
706+
path: src/Statements/SelectStatement.php
707+
708+
-
709+
message: "#^Access to an undefined property PhpMyAdmin\\\\SqlParser\\\\Statements\\\\SelectStatement\\:\\:\\$set\\.$#"
710+
count: 1
711+
path: src/Statements/SelectStatement.php
712+
713+
-
714+
message: "#^Access to an undefined property PhpMyAdmin\\\\SqlParser\\\\Statements\\\\SelectStatement\\:\\:\\$table\\.$#"
715+
count: 1
716+
path: src/Statements/SelectStatement.php
717+
718+
-
719+
message: "#^Access to an undefined property PhpMyAdmin\\\\SqlParser\\\\Statements\\\\SelectStatement\\:\\:\\$tables\\.$#"
720+
count: 1
721+
path: src/Statements/SelectStatement.php
722+
723+
-
724+
message: "#^Access to an undefined property PhpMyAdmin\\\\SqlParser\\\\Statements\\\\SelectStatement\\:\\:\\$values\\.$#"
725+
count: 1
726+
path: src/Statements/SelectStatement.php
727+
728+
-
729+
message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Statement\\:\\:\\$options \\(PhpMyAdmin\\\\SqlParser\\\\Components\\\\OptionsArray\\|null\\) does not accept array\\<PhpMyAdmin\\\\SqlParser\\\\Component\\>\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\ArrayObj\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\FunctionCall\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\IntoKeyword\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\Limit\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\OptionsArray\\|null\\.$#"
730+
count: 1
731+
path: src/Statements/SelectStatement.php
732+
733+
-
734+
message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Statements\\\\SelectStatement\\:\\:\\$endOptions \\(PhpMyAdmin\\\\SqlParser\\\\Components\\\\OptionsArray\\|null\\) does not accept array\\<PhpMyAdmin\\\\SqlParser\\\\Component\\>\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\ArrayObj\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\FunctionCall\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\IntoKeyword\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\Limit\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\OptionsArray\\|null\\.$#"
735+
count: 1
736+
path: src/Statements/SelectStatement.php
737+
738+
-
739+
message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Statements\\\\SelectStatement\\:\\:\\$expr \\(array\\<PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\>\\) does not accept array\\<PhpMyAdmin\\\\SqlParser\\\\Component\\>\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\ArrayObj\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\FunctionCall\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\IntoKeyword\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\Limit\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\OptionsArray\\|null\\.$#"
740+
count: 1
741+
path: src/Statements/SelectStatement.php
742+
743+
-
744+
message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Statements\\\\SelectStatement\\:\\:\\$from \\(array\\<PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\>\\) does not accept array\\<PhpMyAdmin\\\\SqlParser\\\\Component\\>\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\ArrayObj\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\FunctionCall\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\IntoKeyword\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\Limit\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\OptionsArray\\|null\\.$#"
745+
count: 1
746+
path: src/Statements/SelectStatement.php
747+
748+
-
749+
message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Statements\\\\SelectStatement\\:\\:\\$group \\(array\\<PhpMyAdmin\\\\SqlParser\\\\Components\\\\GroupKeyword\\>\\|null\\) does not accept array\\<PhpMyAdmin\\\\SqlParser\\\\Component\\>\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\ArrayObj\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\FunctionCall\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\IntoKeyword\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\Limit\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\OptionsArray\\|null\\.$#"
750+
count: 1
751+
path: src/Statements/SelectStatement.php
752+
753+
-
754+
message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Statements\\\\SelectStatement\\:\\:\\$groupOptions \\(PhpMyAdmin\\\\SqlParser\\\\Components\\\\OptionsArray\\|null\\) does not accept array\\<PhpMyAdmin\\\\SqlParser\\\\Component\\>\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\ArrayObj\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\FunctionCall\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\IntoKeyword\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\Limit\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\OptionsArray\\|null\\.$#"
755+
count: 1
756+
path: src/Statements/SelectStatement.php
757+
758+
-
759+
message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Statements\\\\SelectStatement\\:\\:\\$having \\(array\\<PhpMyAdmin\\\\SqlParser\\\\Components\\\\Condition\\>\\|null\\) does not accept array\\<PhpMyAdmin\\\\SqlParser\\\\Component\\>\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\ArrayObj\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\FunctionCall\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\IntoKeyword\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\Limit\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\OptionsArray\\|null\\.$#"
760+
count: 1
761+
path: src/Statements/SelectStatement.php
762+
763+
-
764+
message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Statements\\\\SelectStatement\\:\\:\\$into \\(PhpMyAdmin\\\\SqlParser\\\\Components\\\\IntoKeyword\\|null\\) does not accept array\\<PhpMyAdmin\\\\SqlParser\\\\Component\\>\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\ArrayObj\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\FunctionCall\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\IntoKeyword\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\Limit\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\OptionsArray\\|null\\.$#"
765+
count: 1
766+
path: src/Statements/SelectStatement.php
767+
768+
-
769+
message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Statements\\\\SelectStatement\\:\\:\\$join \\(array\\<PhpMyAdmin\\\\SqlParser\\\\Components\\\\JoinKeyword\\>\\|null\\) does not accept array\\<PhpMyAdmin\\\\SqlParser\\\\Component\\>\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\ArrayObj\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\FunctionCall\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\IntoKeyword\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\Limit\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\OptionsArray\\|null\\.$#"
770+
count: 1
771+
path: src/Statements/SelectStatement.php
772+
773+
-
774+
message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Statements\\\\SelectStatement\\:\\:\\$limit \\(PhpMyAdmin\\\\SqlParser\\\\Components\\\\Limit\\|null\\) does not accept array\\<PhpMyAdmin\\\\SqlParser\\\\Component\\>\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\ArrayObj\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\FunctionCall\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\IntoKeyword\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\Limit\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\OptionsArray\\|null\\.$#"
775+
count: 1
776+
path: src/Statements/SelectStatement.php
777+
778+
-
779+
message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Statements\\\\SelectStatement\\:\\:\\$order \\(array\\<PhpMyAdmin\\\\SqlParser\\\\Components\\\\OrderKeyword\\>\\|null\\) does not accept array\\<PhpMyAdmin\\\\SqlParser\\\\Component\\>\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\ArrayObj\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\FunctionCall\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\IntoKeyword\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\Limit\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\OptionsArray\\|null\\.$#"
780+
count: 1
781+
path: src/Statements/SelectStatement.php
782+
783+
-
784+
message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Statements\\\\SelectStatement\\:\\:\\$partition \\(PhpMyAdmin\\\\SqlParser\\\\Components\\\\ArrayObj\\|null\\) does not accept array\\<PhpMyAdmin\\\\SqlParser\\\\Component\\>\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\ArrayObj\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\FunctionCall\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\IntoKeyword\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\Limit\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\OptionsArray\\|null\\.$#"
785+
count: 1
786+
path: src/Statements/SelectStatement.php
787+
788+
-
789+
message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Statements\\\\SelectStatement\\:\\:\\$procedure \\(PhpMyAdmin\\\\SqlParser\\\\Components\\\\FunctionCall\\|null\\) does not accept array\\<PhpMyAdmin\\\\SqlParser\\\\Component\\>\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\ArrayObj\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\FunctionCall\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\IntoKeyword\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\Limit\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\OptionsArray\\|null\\.$#"
790+
count: 1
791+
path: src/Statements/SelectStatement.php
792+
793+
-
794+
message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Statements\\\\SelectStatement\\:\\:\\$union \\(array\\<PhpMyAdmin\\\\SqlParser\\\\Statements\\\\SelectStatement\\>\\) does not accept array\\<PhpMyAdmin\\\\SqlParser\\\\Component\\>\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\ArrayObj\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\FunctionCall\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\IntoKeyword\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\Limit\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\OptionsArray\\|null\\.$#"
795+
count: 1
796+
path: src/Statements/SelectStatement.php
797+
798+
-
799+
message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Statements\\\\SelectStatement\\:\\:\\$where \\(array\\<PhpMyAdmin\\\\SqlParser\\\\Components\\\\Condition\\>\\|null\\) does not accept array\\<PhpMyAdmin\\\\SqlParser\\\\Component\\>\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\ArrayObj\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\FunctionCall\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\IntoKeyword\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\Limit\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\OptionsArray\\|null\\.$#"
800+
count: 1
801+
path: src/Statements/SelectStatement.php
802+
688803
-
689804
message: "#^Cannot call method build\\(\\) on PhpMyAdmin\\\\SqlParser\\\\Components\\\\OptionsArray\\|null\\.$#"
690805
count: 1

psalm-baseline.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,6 +1065,20 @@
10651065
</PossiblyNullOperand>
10661066
</file>
10671067
<file src="src/Statements/SelectStatement.php">
1068+
<DocblockTypeContradiction>
1069+
<code><![CDATA[empty(static::$clauses[$token->value])]]></code>
1070+
</DocblockTypeContradiction>
1071+
<InvalidArgument>
1072+
<code><![CDATA[$parsedClauses[$token->value]]]></code>
1073+
</InvalidArgument>
1074+
<InvalidArrayOffset>
1075+
<code><![CDATA[$parsedClauses[$token->value]]]></code>
1076+
<code><![CDATA[$parsedClauses[$token->value]]]></code>
1077+
<code><![CDATA[Parser::KEYWORD_PARSERS[$token->value]]]></code>
1078+
<code>Parser::KEYWORD_PARSERS[$tokenValue]</code>
1079+
<code><![CDATA[Parser::STATEMENT_PARSERS[$token->value]]]></code>
1080+
<code><![CDATA[static::$clauses[$token->value]]]></code>
1081+
</InvalidArrayOffset>
10681082
<PossiblyNullArrayOffset>
10691083
<code>$tables[$thisDb]</code>
10701084
</PossiblyNullArrayOffset>
@@ -1074,8 +1088,12 @@
10741088
<code>$indexHints</code>
10751089
<code>$partition</code>
10761090
</PossiblyUnusedProperty>
1091+
<RedundantCondition>
1092+
<code><![CDATA[! empty($parsedClauses[$token->value])]]></code>
1093+
</RedundantCondition>
10771094
<RiskyTruthyFalsyComparison>
10781095
<code><![CDATA[empty($this->join)]]></code>
1096+
<code><![CDATA[empty(Parser::KEYWORD_PARSERS[$tokenValue]['options'])]]></code>
10791097
</RiskyTruthyFalsyComparison>
10801098
</file>
10811099
<file src="src/Statements/SetStatement.php">

src/Statement.php

Lines changed: 7 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66

77
use AllowDynamicProperties;
88
use PhpMyAdmin\SqlParser\Components\OptionsArray;
9+
use PhpMyAdmin\SqlParser\Exceptions\ParserException;
910
use PhpMyAdmin\SqlParser\Parsers\OptionsArrays;
11+
use PhpMyAdmin\SqlParser\Statements\SetStatement;
1012
use Stringable;
1113

1214
use function array_flip;
@@ -159,7 +161,7 @@ public function build(): string
159161
* @param Parser $parser the instance that requests parsing
160162
* @param TokensList $list the list of tokens to be parsed
161163
*
162-
* @throws Exceptions\ParserException
164+
* @throws ParserException
163165
*/
164166
public function parse(Parser $parser, TokensList $list): void
165167
{
@@ -221,28 +223,6 @@ public function parse(Parser $parser, TokensList $list): void
221223

222224
$lastIdx = $list->idx;
223225

224-
// ON DUPLICATE KEY UPDATE ...
225-
// has to be parsed in parent statement (INSERT or REPLACE)
226-
// so look for it and break
227-
if ($this instanceof Statements\SelectStatement && $token->value === 'ON') {
228-
++$list->idx; // Skip ON
229-
230-
// look for ON DUPLICATE KEY UPDATE
231-
$first = $list->getNextOfType(TokenType::Keyword);
232-
$second = $list->getNextOfType(TokenType::Keyword);
233-
$third = $list->getNextOfType(TokenType::Keyword);
234-
235-
if (
236-
$first && $second && $third
237-
&& $first->value === 'DUPLICATE'
238-
&& $second->value === 'KEY'
239-
&& $third->value === 'UPDATE'
240-
) {
241-
$list->idx = $lastIdx;
242-
break;
243-
}
244-
}
245-
246226
$list->idx = $lastIdx;
247227

248228
/**
@@ -311,41 +291,15 @@ public function parse(Parser $parser, TokensList $list): void
311291
$parsedOptions = true;
312292
}
313293
} elseif ($class === null) {
314-
if ($this instanceof Statements\SelectStatement && $token->value === 'WITH ROLLUP') {
315-
// Handle group options in Select statement
316-
$this->groupOptions = OptionsArrays::parse(
317-
$parser,
318-
$list,
319-
Statements\SelectStatement::STATEMENT_GROUP_OPTIONS,
320-
);
321-
} elseif (
322-
$this instanceof Statements\SelectStatement
323-
&& ($token->value === 'FOR UPDATE'
324-
|| $token->value === 'LOCK IN SHARE MODE')
325-
) {
326-
// Handle special end options in Select statement
327-
$this->endOptions = OptionsArrays::parse(
328-
$parser,
329-
$list,
330-
Statements\SelectStatement::STATEMENT_END_OPTIONS,
331-
);
332-
} elseif (
333-
$this instanceof Statements\SetStatement
334-
&& ($token->value === 'COLLATE'
335-
|| $token->value === 'DEFAULT')
336-
) {
337-
// Handle special end options in SET statement
338-
$this->endOptions = OptionsArrays::parse(
339-
$parser,
340-
$list,
341-
Statements\SetStatement::STATEMENT_END_OPTIONS,
342-
);
343-
} else {
294+
if (! ($this instanceof SetStatement) || ($token->value !== 'COLLATE' && $token->value !== 'DEFAULT')) {
344295
// There is no parser for this keyword and isn't the beginning
345296
// of a statement (so no options) either.
346297
$parser->error('Unrecognized keyword.', $token);
347298
continue;
348299
}
300+
301+
// Handle special end options in SET statement
302+
$this->endOptions = OptionsArrays::parse($parser, $list, SetStatement::STATEMENT_END_OPTIONS);
349303
}
350304

351305
$this->before($parser, $list, $token);

0 commit comments

Comments
 (0)