From 1aaddff78978060ea3e66fe4054fb4c26f7e0c55 Mon Sep 17 00:00:00 2001 From: pfilsx Date: Thu, 22 May 2025 14:09:19 +0300 Subject: [PATCH 1/2] update for 2.18 orm --- Makefile | 5 ++++- composer.json | 4 ++-- src/ORM/Query/AST/ArrayExpression.php | 2 +- .../AST/Functions/AbstractAggregateWithFilterFunction.php | 2 +- src/ORM/Query/AST/Functions/ArrayAgg.php | 2 +- src/ORM/Query/AST/Functions/Cast.php | 2 +- src/ORM/Query/AST/Functions/Extract.php | 2 +- src/ORM/Query/AST/Functions/JsonGetArrayElement.php | 4 ++-- src/ORM/Query/AST/Functions/JsonGetArrayElementAsText.php | 4 ++-- 9 files changed, 15 insertions(+), 12 deletions(-) diff --git a/Makefile b/Makefile index f1479a4..cea513a 100644 --- a/Makefile +++ b/Makefile @@ -25,4 +25,7 @@ test-deprecations: @docker-compose exec -ti php vendor/bin/phpunit --display-deprecations test-with-coverage: - @docker-compose exec -e XDEBUG_MODE=coverage -ti php vendor/bin/phpunit --coverage-html var \ No newline at end of file + @docker-compose exec -e XDEBUG_MODE=coverage -ti php vendor/bin/phpunit --coverage-html var + +sh: + @docker-compose exec -ti php /bin/sh \ No newline at end of file diff --git a/composer.json b/composer.json index 011f08d..ca4a7a2 100644 --- a/composer.json +++ b/composer.json @@ -36,13 +36,13 @@ }, "require-dev": { "friendsofphp/php-cs-fixer": "^3.13", - "doctrine/orm": "^2.13.0", + "doctrine/orm": "^2.18", "symfony/serializer": ">=5.4", "symfony/property-info": ">=5.4", "phpunit/phpunit": "^10.0", "symfony/property-access": ">=5.4" }, "conflict": { - "doctrine/orm": ">=2.18.0" + "doctrine/orm": "<2.18.0" } } diff --git a/src/ORM/Query/AST/ArrayExpression.php b/src/ORM/Query/AST/ArrayExpression.php index d54947e..38a8711 100644 --- a/src/ORM/Query/AST/ArrayExpression.php +++ b/src/ORM/Query/AST/ArrayExpression.php @@ -34,7 +34,7 @@ public static function parse(Parser $parser): self \assert($lexer->lookahead !== null); $nodes = []; - switch ($lexer->lookahead['type']) { + switch ($lexer->lookahead->type) { case Lexer::T_INPUT_PARAMETER: $nodes[] = $parser->InputParameter(); diff --git a/src/ORM/Query/AST/Functions/AbstractAggregateWithFilterFunction.php b/src/ORM/Query/AST/Functions/AbstractAggregateWithFilterFunction.php index ce967cf..dca658c 100644 --- a/src/ORM/Query/AST/Functions/AbstractAggregateWithFilterFunction.php +++ b/src/ORM/Query/AST/Functions/AbstractAggregateWithFilterFunction.php @@ -25,7 +25,7 @@ public function parse(Parser $parser): void return; } - $lookaheadValue = $lexer->lookahead['value'] ?? null; + $lookaheadValue = $lexer->lookahead?->value; if (!\is_string($lookaheadValue) || \mb_strtoupper($lookaheadValue) !== self::FILTER_IDENTIFIER) { return; diff --git a/src/ORM/Query/AST/Functions/ArrayAgg.php b/src/ORM/Query/AST/Functions/ArrayAgg.php index da49cf4..a2015c2 100644 --- a/src/ORM/Query/AST/Functions/ArrayAgg.php +++ b/src/ORM/Query/AST/Functions/ArrayAgg.php @@ -49,7 +49,7 @@ public function parseFunction(Parser $parser): void $parser->match(Lexer::T_COMMA); $parser->match(Lexer::T_STRING); - $this->returnType = $lexer->token['value']; + $this->returnType = $lexer->token?->value; } $parser->match(Lexer::T_CLOSE_PARENTHESIS); diff --git a/src/ORM/Query/AST/Functions/Cast.php b/src/ORM/Query/AST/Functions/Cast.php index dc6b99a..e1446da 100644 --- a/src/ORM/Query/AST/Functions/Cast.php +++ b/src/ORM/Query/AST/Functions/Cast.php @@ -32,7 +32,7 @@ public function parse(Parser $parser): void $parser->match(Lexer::T_AS); $parser->match(Lexer::T_IDENTIFIER); - $type = $parser->getLexer()->token['value'] ?? null; + $type = $parser->getLexer()->token?->value; if (!\is_string($type)) { return; diff --git a/src/ORM/Query/AST/Functions/Extract.php b/src/ORM/Query/AST/Functions/Extract.php index c5facd6..885c059 100644 --- a/src/ORM/Query/AST/Functions/Extract.php +++ b/src/ORM/Query/AST/Functions/Extract.php @@ -30,7 +30,7 @@ public function parse(Parser $parser): void $parser->match(Lexer::T_OPEN_PARENTHESIS); $parser->match(Lexer::T_IDENTIFIER); - $this->expr = $parser->getLexer()->token['value']; + $this->expr = $parser->getLexer()->token?->value; $parser->match(Lexer::T_FROM); $this->from = $parser->StringPrimary(); diff --git a/src/ORM/Query/AST/Functions/JsonGetArrayElement.php b/src/ORM/Query/AST/Functions/JsonGetArrayElement.php index 2ee588a..fb35179 100644 --- a/src/ORM/Query/AST/Functions/JsonGetArrayElement.php +++ b/src/ORM/Query/AST/Functions/JsonGetArrayElement.php @@ -25,14 +25,14 @@ public function parse(Parser $parser): void $parser->match(Lexer::T_COMMA); $parser->match(Lexer::T_INTEGER); - $this->path[] = new Literal(Literal::NUMERIC, $parser->getLexer()->token['value']); + $this->path[] = new Literal(Literal::NUMERIC, $parser->getLexer()->token?->value); if (!$parser->getLexer()->isNextToken(Lexer::T_CLOSE_PARENTHESIS)) { while ($parser->getLexer()->isNextToken(Lexer::T_COMMA)) { $parser->match(Lexer::T_COMMA); $parser->match(Lexer::T_INTEGER); - $this->path[] = new Literal(Literal::NUMERIC, $parser->getLexer()->token['value']); + $this->path[] = new Literal(Literal::NUMERIC, $parser->getLexer()->token?->value); } } $parser->match(Lexer::T_CLOSE_PARENTHESIS); diff --git a/src/ORM/Query/AST/Functions/JsonGetArrayElementAsText.php b/src/ORM/Query/AST/Functions/JsonGetArrayElementAsText.php index 0fd727d..9c39ab6 100644 --- a/src/ORM/Query/AST/Functions/JsonGetArrayElementAsText.php +++ b/src/ORM/Query/AST/Functions/JsonGetArrayElementAsText.php @@ -26,14 +26,14 @@ public function parse(Parser $parser): void $parser->match(Lexer::T_COMMA); $parser->match(Lexer::T_INTEGER); - $this->path[] = new Literal(Literal::NUMERIC, $parser->getLexer()->token['value']); + $this->path[] = new Literal(Literal::NUMERIC, $parser->getLexer()->token?->value); if (!$parser->getLexer()->isNextToken(Lexer::T_CLOSE_PARENTHESIS)) { while ($parser->getLexer()->isNextToken(Lexer::T_COMMA)) { $parser->match(Lexer::T_COMMA); $parser->match(Lexer::T_INTEGER); - $this->path[] = new Literal(Literal::NUMERIC, $parser->getLexer()->token['value']); + $this->path[] = new Literal(Literal::NUMERIC, $parser->getLexer()->token?->value); } } From 5f2e99c880c86566e078d768c36401302ce8ff4b Mon Sep 17 00:00:00 2001 From: pfilsx Date: Fri, 23 May 2025 16:16:45 +0300 Subject: [PATCH 2/2] add both lexer token types support --- composer.json | 5 +--- src/ORM/Query/AST/ArrayExpression.php | 9 ++++++- .../AbstractAggregateWithFilterFunction.php | 5 +++- src/ORM/Query/AST/Functions/ArrayAgg.php | 5 +++- src/ORM/Query/AST/Functions/Cast.php | 5 +++- src/ORM/Query/AST/Functions/Extract.php | 4 ++- .../AST/Functions/JsonGetArrayElement.php | 9 ++++--- .../Functions/JsonGetArrayElementAsText.php | 9 ++++--- src/ORM/Trait/VariadicTokenTrait.php | 26 +++++++++++++++++++ 9 files changed, 62 insertions(+), 15 deletions(-) create mode 100644 src/ORM/Trait/VariadicTokenTrait.php diff --git a/composer.json b/composer.json index ca4a7a2..5ebd33f 100644 --- a/composer.json +++ b/composer.json @@ -36,13 +36,10 @@ }, "require-dev": { "friendsofphp/php-cs-fixer": "^3.13", - "doctrine/orm": "^2.18", + "doctrine/orm": "^2.13", "symfony/serializer": ">=5.4", "symfony/property-info": ">=5.4", "phpunit/phpunit": "^10.0", "symfony/property-access": ">=5.4" - }, - "conflict": { - "doctrine/orm": "<2.18.0" } } diff --git a/src/ORM/Query/AST/ArrayExpression.php b/src/ORM/Query/AST/ArrayExpression.php index 38a8711..71a94fa 100644 --- a/src/ORM/Query/AST/ArrayExpression.php +++ b/src/ORM/Query/AST/ArrayExpression.php @@ -4,6 +4,7 @@ namespace Pfilsx\PostgreSQLDoctrine\ORM\Query\AST; +use Doctrine\Common\Lexer\Token; use Doctrine\ORM\Query\AST\Node; use Doctrine\ORM\Query\Lexer; use Doctrine\ORM\Query\Parser; @@ -34,7 +35,13 @@ public static function parse(Parser $parser): self \assert($lexer->lookahead !== null); $nodes = []; - switch ($lexer->lookahead->type) { + if (\class_exists(Token::class) && $lexer->lookahead instanceof Token) { + $type = $lexer->lookahead->type; + } else { + $type = $lexer->lookahead['type'] ?? null; + } + + switch ($type) { case Lexer::T_INPUT_PARAMETER: $nodes[] = $parser->InputParameter(); diff --git a/src/ORM/Query/AST/Functions/AbstractAggregateWithFilterFunction.php b/src/ORM/Query/AST/Functions/AbstractAggregateWithFilterFunction.php index dca658c..d8fc90e 100644 --- a/src/ORM/Query/AST/Functions/AbstractAggregateWithFilterFunction.php +++ b/src/ORM/Query/AST/Functions/AbstractAggregateWithFilterFunction.php @@ -9,9 +9,12 @@ use Doctrine\ORM\Query\Parser; use Doctrine\ORM\Query\SqlWalker; use Pfilsx\PostgreSQLDoctrine\ORM\Query\AST\FilterExpression; +use Pfilsx\PostgreSQLDoctrine\ORM\Trait\VariadicTokenTrait; abstract class AbstractAggregateWithFilterFunction extends FunctionNode { + use VariadicTokenTrait; + private const FILTER_IDENTIFIER = 'FILTER'; private ?FilterExpression $filterExpression = null; @@ -25,7 +28,7 @@ public function parse(Parser $parser): void return; } - $lookaheadValue = $lexer->lookahead?->value; + $lookaheadValue = $this->getTokenField($lexer->lookahead, 'value'); if (!\is_string($lookaheadValue) || \mb_strtoupper($lookaheadValue) !== self::FILTER_IDENTIFIER) { return; diff --git a/src/ORM/Query/AST/Functions/ArrayAgg.php b/src/ORM/Query/AST/Functions/ArrayAgg.php index a2015c2..2a3b6bb 100644 --- a/src/ORM/Query/AST/Functions/ArrayAgg.php +++ b/src/ORM/Query/AST/Functions/ArrayAgg.php @@ -11,6 +11,7 @@ use Doctrine\ORM\Query\Lexer; use Doctrine\ORM\Query\Parser; use Doctrine\ORM\Query\SqlWalker; +use Pfilsx\PostgreSQLDoctrine\ORM\Trait\VariadicTokenTrait; /** * Implementation of PostgreSql ARRAY_AGG() function. @@ -26,6 +27,8 @@ */ final class ArrayAgg extends AbstractAggregateWithFilterFunction implements TypedExpression { + use VariadicTokenTrait; + private bool $distinct = false; private Node $expr; @@ -49,7 +52,7 @@ public function parseFunction(Parser $parser): void $parser->match(Lexer::T_COMMA); $parser->match(Lexer::T_STRING); - $this->returnType = $lexer->token?->value; + $this->returnType = $this->getTokenField($lexer->token, 'value'); } $parser->match(Lexer::T_CLOSE_PARENTHESIS); diff --git a/src/ORM/Query/AST/Functions/Cast.php b/src/ORM/Query/AST/Functions/Cast.php index e1446da..e81e5db 100644 --- a/src/ORM/Query/AST/Functions/Cast.php +++ b/src/ORM/Query/AST/Functions/Cast.php @@ -9,6 +9,7 @@ use Doctrine\ORM\Query\Lexer; use Doctrine\ORM\Query\Parser; use Doctrine\ORM\Query\SqlWalker; +use Pfilsx\PostgreSQLDoctrine\ORM\Trait\VariadicTokenTrait; /** * Implementation of PostgreSql CAST() function. @@ -19,6 +20,8 @@ */ final class Cast extends FunctionNode { + use VariadicTokenTrait; + public Node $source; public string $type; @@ -32,7 +35,7 @@ public function parse(Parser $parser): void $parser->match(Lexer::T_AS); $parser->match(Lexer::T_IDENTIFIER); - $type = $parser->getLexer()->token?->value; + $type = $this->getTokenField($parser->getLexer()->token, 'value'); if (!\is_string($type)) { return; diff --git a/src/ORM/Query/AST/Functions/Extract.php b/src/ORM/Query/AST/Functions/Extract.php index 885c059..9ae39d8 100644 --- a/src/ORM/Query/AST/Functions/Extract.php +++ b/src/ORM/Query/AST/Functions/Extract.php @@ -9,6 +9,7 @@ use Doctrine\ORM\Query\Lexer; use Doctrine\ORM\Query\Parser; use Doctrine\ORM\Query\SqlWalker; +use Pfilsx\PostgreSQLDoctrine\ORM\Trait\VariadicTokenTrait; /** * Implementation of PostgreSql EXTRACT function. @@ -20,6 +21,7 @@ */ class Extract extends FunctionNode { + use VariadicTokenTrait; private string $expr; private Node $from; @@ -30,7 +32,7 @@ public function parse(Parser $parser): void $parser->match(Lexer::T_OPEN_PARENTHESIS); $parser->match(Lexer::T_IDENTIFIER); - $this->expr = $parser->getLexer()->token?->value; + $this->expr = $this->getTokenField($parser->getLexer()->token, 'value'); $parser->match(Lexer::T_FROM); $this->from = $parser->StringPrimary(); diff --git a/src/ORM/Query/AST/Functions/JsonGetArrayElement.php b/src/ORM/Query/AST/Functions/JsonGetArrayElement.php index fb35179..9619e8b 100644 --- a/src/ORM/Query/AST/Functions/JsonGetArrayElement.php +++ b/src/ORM/Query/AST/Functions/JsonGetArrayElement.php @@ -7,6 +7,7 @@ use Doctrine\ORM\Query\AST\Literal; use Doctrine\ORM\Query\Lexer; use Doctrine\ORM\Query\Parser; +use Pfilsx\PostgreSQLDoctrine\ORM\Trait\VariadicTokenTrait; /** * Implementation of PostgreSql JSON(B) array field retrieval by index. @@ -15,8 +16,10 @@ * * @example JSON_GET_ARRAY_ELEMENT(entity.field, 1) */ -final class JsonGetArrayElement extends JsonGetField +class JsonGetArrayElement extends JsonGetField { + use VariadicTokenTrait; + public function parse(Parser $parser): void { $parser->match(Lexer::T_IDENTIFIER); @@ -25,14 +28,14 @@ public function parse(Parser $parser): void $parser->match(Lexer::T_COMMA); $parser->match(Lexer::T_INTEGER); - $this->path[] = new Literal(Literal::NUMERIC, $parser->getLexer()->token?->value); + $this->path[] = new Literal(Literal::NUMERIC, $this->getTokenField($parser->getLexer()->token, 'value')); if (!$parser->getLexer()->isNextToken(Lexer::T_CLOSE_PARENTHESIS)) { while ($parser->getLexer()->isNextToken(Lexer::T_COMMA)) { $parser->match(Lexer::T_COMMA); $parser->match(Lexer::T_INTEGER); - $this->path[] = new Literal(Literal::NUMERIC, $parser->getLexer()->token?->value); + $this->path[] = new Literal(Literal::NUMERIC, $this->getTokenField($parser->getLexer()->token, 'value')); } } $parser->match(Lexer::T_CLOSE_PARENTHESIS); diff --git a/src/ORM/Query/AST/Functions/JsonGetArrayElementAsText.php b/src/ORM/Query/AST/Functions/JsonGetArrayElementAsText.php index 9c39ab6..094e135 100644 --- a/src/ORM/Query/AST/Functions/JsonGetArrayElementAsText.php +++ b/src/ORM/Query/AST/Functions/JsonGetArrayElementAsText.php @@ -7,6 +7,7 @@ use Doctrine\ORM\Query\AST\Literal; use Doctrine\ORM\Query\Lexer; use Doctrine\ORM\Query\Parser; +use Pfilsx\PostgreSQLDoctrine\ORM\Trait\VariadicTokenTrait; /** * Implementation of PostgreSql JSON(B) array field retrieval by index. @@ -15,8 +16,10 @@ * * @example JSON_GET_ARRAY_ELEMENT_AS_TEXT(entity.field, 1) */ -final class JsonGetArrayElementAsText extends JsonGetFieldAsText +class JsonGetArrayElementAsText extends JsonGetFieldAsText { + use VariadicTokenTrait; + public function parse(Parser $parser): void { $parser->match(Lexer::T_IDENTIFIER); @@ -26,14 +29,14 @@ public function parse(Parser $parser): void $parser->match(Lexer::T_COMMA); $parser->match(Lexer::T_INTEGER); - $this->path[] = new Literal(Literal::NUMERIC, $parser->getLexer()->token?->value); + $this->path[] = new Literal(Literal::NUMERIC, $this->getTokenField($parser->getLexer()->token, 'value')); if (!$parser->getLexer()->isNextToken(Lexer::T_CLOSE_PARENTHESIS)) { while ($parser->getLexer()->isNextToken(Lexer::T_COMMA)) { $parser->match(Lexer::T_COMMA); $parser->match(Lexer::T_INTEGER); - $this->path[] = new Literal(Literal::NUMERIC, $parser->getLexer()->token?->value); + $this->path[] = new Literal(Literal::NUMERIC, $this->getTokenField($parser->getLexer()->token, 'value')); } } diff --git a/src/ORM/Trait/VariadicTokenTrait.php b/src/ORM/Trait/VariadicTokenTrait.php new file mode 100644 index 0000000..f607236 --- /dev/null +++ b/src/ORM/Trait/VariadicTokenTrait.php @@ -0,0 +1,26 @@ +$field; + } + + return $token[$field] ?? null; + } +}