diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index f73868f2..b6e8875d 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -1230,12 +1230,6 @@ parameters: count: 1 path: src/Statements/ExplainStatement.php - - - message: '#^Cannot access property \$value on PhpMyAdmin\\SqlParser\\Token\|null\.$#' - identifier: property.nonObject - count: 1 - path: src/Statements/ExplainStatement.php - - message: '#^Class PhpMyAdmin\\SqlParser\\Statements\\ExplainStatement has an uninitialized property \$statementAlias\. Give it default value or assign it in the constructor\.$#' identifier: property.uninitialized @@ -1260,12 +1254,6 @@ parameters: count: 1 path: src/Statements/ExplainStatement.php - - - message: '#^Property PhpMyAdmin\\SqlParser\\Statements\\ExplainStatement\:\:\$connectionId \(int\|null\) does not accept bool\|float\|int\|string\.$#' - identifier: assign.propertyType - count: 1 - path: src/Statements/ExplainStatement.php - - message: '#^Property PhpMyAdmin\\SqlParser\\Statements\\ExplainStatement\:\:\$explainedDatabase \(string\|null\) does not accept bool\|float\|int\|string\.$#' identifier: assign.propertyType diff --git a/psalm-baseline.xml b/psalm-baseline.xml index b05f0578..0565e46f 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -704,13 +704,11 @@ keyword]]> - value]]> - value]]> value]]> value]]> diff --git a/src/Statements/ExplainStatement.php b/src/Statements/ExplainStatement.php index 7976b456..b93085e5 100644 --- a/src/Statements/ExplainStatement.php +++ b/src/Statements/ExplainStatement.php @@ -12,6 +12,7 @@ use PhpMyAdmin\SqlParser\TokenType; use function array_slice; +use function is_int; /** * `EXPLAIN` statement. @@ -161,6 +162,11 @@ public function parse(Parser $parser, TokensList $list): void $list->idx++; // Ignore the current token $list->getNext(); // CONNECTION $nextToken = $list->getNext(); // Identifier + if ($nextToken === null || $nextToken->type !== TokenType::Number || ! is_int($nextToken->value)) { + $parser->error('Unexpected token.', $nextToken); + break; + } + $this->connectionId = $nextToken->value; break; } diff --git a/tests/Parser/ExplainStatementTest.php b/tests/Parser/ExplainStatementTest.php index 26552a4a..f2b46484 100644 --- a/tests/Parser/ExplainStatementTest.php +++ b/tests/Parser/ExplainStatementTest.php @@ -34,10 +34,14 @@ public static function explainProvider(): array ['parser/parseExplain12'], ['parser/parseExplain13'], ['parser/parseExplain14'], + ['parser/parseExplain15'], ['parser/parseExplainErr'], ['parser/parseExplainErr1'], ['parser/parseExplainErr2'], ['parser/parseExplainErr3'], + ['parser/parseExplainErr4'], + ['parser/parseExplainErr5'], + ['parser/parseExplainErr6'], ]; } } diff --git a/tests/data/parser/parseExplain15.in b/tests/data/parser/parseExplain15.in new file mode 100644 index 00000000..01c2d81c --- /dev/null +++ b/tests/data/parser/parseExplain15.in @@ -0,0 +1 @@ +EXPLAIN FOR CONNECTION 458 \ No newline at end of file diff --git a/tests/data/parser/parseExplain15.out b/tests/data/parser/parseExplain15.out new file mode 100644 index 00000000..171e2a8d --- /dev/null +++ b/tests/data/parser/parseExplain15.out @@ -0,0 +1,146 @@ +{ + "query": "EXPLAIN FOR CONNECTION 458", + "lexer": { + "@type": "PhpMyAdmin\\SqlParser\\Lexer", + "strict": false, + "errors": [], + "str": "EXPLAIN FOR CONNECTION 458", + "len": 26, + "last": 26, + "list": { + "@type": "PhpMyAdmin\\SqlParser\\TokensList", + "count": 8, + "idx": 8, + "tokens": [ + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "EXPLAIN", + "value": "EXPLAIN", + "keyword": "EXPLAIN", + "type": { + "@type": "PhpMyAdmin\\SqlParser\\TokenType", + "name": "Keyword", + "value": 1 + }, + "flags": 3, + "position": 0 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": " ", + "value": " ", + "keyword": null, + "type": { + "@type": "PhpMyAdmin\\SqlParser\\TokenType", + "name": "Whitespace", + "value": 3 + }, + "flags": 0, + "position": 7 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "FOR", + "value": "FOR", + "keyword": "FOR", + "type": { + "@type": "@3" + }, + "flags": 3, + "position": 8 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": " ", + "value": " ", + "keyword": null, + "type": { + "@type": "@5" + }, + "flags": 0, + "position": 11 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "CONNECTION", + "value": "CONNECTION", + "keyword": "CONNECTION", + "type": { + "@type": "@3" + }, + "flags": 1, + "position": 12 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": " ", + "value": " ", + "keyword": null, + "type": { + "@type": "@5" + }, + "flags": 0, + "position": 22 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "458", + "value": 458, + "keyword": null, + "type": { + "@type": "PhpMyAdmin\\SqlParser\\TokenType", + "name": "Number", + "value": 6 + }, + "flags": 0, + "position": 23 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "", + "value": "", + "keyword": null, + "type": { + "@type": "PhpMyAdmin\\SqlParser\\TokenType", + "name": "Delimiter", + "value": 9 + }, + "flags": 0, + "position": null + } + ] + }, + "delimiter": ";", + "delimiterLen": 1 + }, + "parser": { + "@type": "PhpMyAdmin\\SqlParser\\Parser", + "strict": false, + "errors": [], + "list": { + "@type": "@1" + }, + "statements": [ + { + "@type": "PhpMyAdmin\\SqlParser\\Statements\\ExplainStatement", + "bodyParser": null, + "statementAlias": "EXPLAIN", + "connectionId": 458, + "explainedDatabase": null, + "explainedTable": null, + "explainedColumn": null, + "options": { + "@type": "PhpMyAdmin\\SqlParser\\Components\\OptionsArray", + "options": [] + }, + "first": 0, + "last": 7 + } + ], + "brackets": 0 + }, + "errors": { + "lexer": [], + "parser": [] + } +} \ No newline at end of file diff --git a/tests/data/parser/parseExplainErr4.in b/tests/data/parser/parseExplainErr4.in new file mode 100644 index 00000000..593bb894 --- /dev/null +++ b/tests/data/parser/parseExplainErr4.in @@ -0,0 +1 @@ +EXPLAIN FOR CONNECTION \ No newline at end of file diff --git a/tests/data/parser/parseExplainErr4.out b/tests/data/parser/parseExplainErr4.out new file mode 100644 index 00000000..857cab81 --- /dev/null +++ b/tests/data/parser/parseExplainErr4.out @@ -0,0 +1,130 @@ +{ + "query": "EXPLAIN FOR CONNECTION", + "lexer": { + "@type": "PhpMyAdmin\\SqlParser\\Lexer", + "strict": false, + "errors": [], + "str": "EXPLAIN FOR CONNECTION", + "len": 22, + "last": 22, + "list": { + "@type": "PhpMyAdmin\\SqlParser\\TokensList", + "count": 6, + "idx": 7, + "tokens": [ + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "EXPLAIN", + "value": "EXPLAIN", + "keyword": "EXPLAIN", + "type": { + "@type": "PhpMyAdmin\\SqlParser\\TokenType", + "name": "Keyword", + "value": 1 + }, + "flags": 3, + "position": 0 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": " ", + "value": " ", + "keyword": null, + "type": { + "@type": "PhpMyAdmin\\SqlParser\\TokenType", + "name": "Whitespace", + "value": 3 + }, + "flags": 0, + "position": 7 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "FOR", + "value": "FOR", + "keyword": "FOR", + "type": { + "@type": "@3" + }, + "flags": 3, + "position": 8 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": " ", + "value": " ", + "keyword": null, + "type": { + "@type": "@5" + }, + "flags": 0, + "position": 11 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "CONNECTION", + "value": "CONNECTION", + "keyword": "CONNECTION", + "type": { + "@type": "@3" + }, + "flags": 1, + "position": 12 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "", + "value": "", + "keyword": null, + "type": { + "@type": "PhpMyAdmin\\SqlParser\\TokenType", + "name": "Delimiter", + "value": 9 + }, + "flags": 0, + "position": null + } + ] + }, + "delimiter": ";", + "delimiterLen": 1 + }, + "parser": { + "@type": "PhpMyAdmin\\SqlParser\\Parser", + "strict": false, + "errors": [], + "list": { + "@type": "@1" + }, + "statements": [ + { + "@type": "PhpMyAdmin\\SqlParser\\Statements\\ExplainStatement", + "bodyParser": null, + "statementAlias": "EXPLAIN", + "connectionId": null, + "explainedDatabase": null, + "explainedTable": null, + "explainedColumn": null, + "options": { + "@type": "PhpMyAdmin\\SqlParser\\Components\\OptionsArray", + "options": [] + }, + "first": 0, + "last": 6 + } + ], + "brackets": 0 + }, + "errors": { + "lexer": [], + "parser": [ + [ + "Unexpected token.", + { + "@type": "@9" + }, + 0 + ] + ] + } +} \ No newline at end of file diff --git a/tests/data/parser/parseExplainErr5.in b/tests/data/parser/parseExplainErr5.in new file mode 100644 index 00000000..9f232022 --- /dev/null +++ b/tests/data/parser/parseExplainErr5.in @@ -0,0 +1 @@ +EXPLAIN FOR CONNECTION abc \ No newline at end of file diff --git a/tests/data/parser/parseExplainErr5.out b/tests/data/parser/parseExplainErr5.out new file mode 100644 index 00000000..f9f38c5f --- /dev/null +++ b/tests/data/parser/parseExplainErr5.out @@ -0,0 +1,154 @@ +{ + "query": "EXPLAIN FOR CONNECTION abc", + "lexer": { + "@type": "PhpMyAdmin\\SqlParser\\Lexer", + "strict": false, + "errors": [], + "str": "EXPLAIN FOR CONNECTION abc", + "len": 26, + "last": 26, + "list": { + "@type": "PhpMyAdmin\\SqlParser\\TokensList", + "count": 8, + "idx": 8, + "tokens": [ + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "EXPLAIN", + "value": "EXPLAIN", + "keyword": "EXPLAIN", + "type": { + "@type": "PhpMyAdmin\\SqlParser\\TokenType", + "name": "Keyword", + "value": 1 + }, + "flags": 3, + "position": 0 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": " ", + "value": " ", + "keyword": null, + "type": { + "@type": "PhpMyAdmin\\SqlParser\\TokenType", + "name": "Whitespace", + "value": 3 + }, + "flags": 0, + "position": 7 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "FOR", + "value": "FOR", + "keyword": "FOR", + "type": { + "@type": "@3" + }, + "flags": 3, + "position": 8 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": " ", + "value": " ", + "keyword": null, + "type": { + "@type": "@5" + }, + "flags": 0, + "position": 11 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "CONNECTION", + "value": "CONNECTION", + "keyword": "CONNECTION", + "type": { + "@type": "@3" + }, + "flags": 1, + "position": 12 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": " ", + "value": " ", + "keyword": null, + "type": { + "@type": "@5" + }, + "flags": 0, + "position": 22 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "abc", + "value": "abc", + "keyword": null, + "type": { + "@type": "PhpMyAdmin\\SqlParser\\TokenType", + "name": "None", + "value": 0 + }, + "flags": 0, + "position": 23 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "", + "value": "", + "keyword": null, + "type": { + "@type": "PhpMyAdmin\\SqlParser\\TokenType", + "name": "Delimiter", + "value": 9 + }, + "flags": 0, + "position": null + } + ] + }, + "delimiter": ";", + "delimiterLen": 1 + }, + "parser": { + "@type": "PhpMyAdmin\\SqlParser\\Parser", + "strict": false, + "errors": [], + "list": { + "@type": "@1" + }, + "statements": [ + { + "@type": "PhpMyAdmin\\SqlParser\\Statements\\ExplainStatement", + "bodyParser": null, + "statementAlias": "EXPLAIN", + "connectionId": null, + "explainedDatabase": null, + "explainedTable": null, + "explainedColumn": null, + "options": { + "@type": "PhpMyAdmin\\SqlParser\\Components\\OptionsArray", + "options": [] + }, + "first": 0, + "last": 7 + } + ], + "brackets": 0 + }, + "errors": { + "lexer": [], + "parser": [ + [ + "Unexpected token.", + { + "@type": "@10" + }, + 0 + ] + ] + } +} \ No newline at end of file diff --git a/tests/data/parser/parseExplainErr6.in b/tests/data/parser/parseExplainErr6.in new file mode 100644 index 00000000..bc4c292a --- /dev/null +++ b/tests/data/parser/parseExplainErr6.in @@ -0,0 +1 @@ +EXPLAIN FOR CONNECTION 1.5 \ No newline at end of file diff --git a/tests/data/parser/parseExplainErr6.out b/tests/data/parser/parseExplainErr6.out new file mode 100644 index 00000000..7628c7d3 --- /dev/null +++ b/tests/data/parser/parseExplainErr6.out @@ -0,0 +1,154 @@ +{ + "query": "EXPLAIN FOR CONNECTION 1.5", + "lexer": { + "@type": "PhpMyAdmin\\SqlParser\\Lexer", + "strict": false, + "errors": [], + "str": "EXPLAIN FOR CONNECTION 1.5", + "len": 26, + "last": 26, + "list": { + "@type": "PhpMyAdmin\\SqlParser\\TokensList", + "count": 8, + "idx": 8, + "tokens": [ + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "EXPLAIN", + "value": "EXPLAIN", + "keyword": "EXPLAIN", + "type": { + "@type": "PhpMyAdmin\\SqlParser\\TokenType", + "name": "Keyword", + "value": 1 + }, + "flags": 3, + "position": 0 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": " ", + "value": " ", + "keyword": null, + "type": { + "@type": "PhpMyAdmin\\SqlParser\\TokenType", + "name": "Whitespace", + "value": 3 + }, + "flags": 0, + "position": 7 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "FOR", + "value": "FOR", + "keyword": "FOR", + "type": { + "@type": "@3" + }, + "flags": 3, + "position": 8 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": " ", + "value": " ", + "keyword": null, + "type": { + "@type": "@5" + }, + "flags": 0, + "position": 11 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "CONNECTION", + "value": "CONNECTION", + "keyword": "CONNECTION", + "type": { + "@type": "@3" + }, + "flags": 1, + "position": 12 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": " ", + "value": " ", + "keyword": null, + "type": { + "@type": "@5" + }, + "flags": 0, + "position": 22 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "1.5", + "value": 1.5, + "keyword": null, + "type": { + "@type": "PhpMyAdmin\\SqlParser\\TokenType", + "name": "Number", + "value": 6 + }, + "flags": 2, + "position": 23 + }, + { + "@type": "PhpMyAdmin\\SqlParser\\Token", + "token": "", + "value": "", + "keyword": null, + "type": { + "@type": "PhpMyAdmin\\SqlParser\\TokenType", + "name": "Delimiter", + "value": 9 + }, + "flags": 0, + "position": null + } + ] + }, + "delimiter": ";", + "delimiterLen": 1 + }, + "parser": { + "@type": "PhpMyAdmin\\SqlParser\\Parser", + "strict": false, + "errors": [], + "list": { + "@type": "@1" + }, + "statements": [ + { + "@type": "PhpMyAdmin\\SqlParser\\Statements\\ExplainStatement", + "bodyParser": null, + "statementAlias": "EXPLAIN", + "connectionId": null, + "explainedDatabase": null, + "explainedTable": null, + "explainedColumn": null, + "options": { + "@type": "PhpMyAdmin\\SqlParser\\Components\\OptionsArray", + "options": [] + }, + "first": 0, + "last": 7 + } + ], + "brackets": 0 + }, + "errors": { + "lexer": [], + "parser": [ + [ + "Unexpected token.", + { + "@type": "@10" + }, + 0 + ] + ] + } +} \ No newline at end of file