-
-
Notifications
You must be signed in to change notification settings - Fork 56
refactor: allow multiple node mapping patterns to be used and their arguments to be validated in variadic functions #350
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
fixtures/MartinGeorgiev/Doctrine/Function/TestVariadicFunction.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Fixtures\MartinGeorgiev\Doctrine\Function; | ||
|
|
||
| use Doctrine\ORM\Query\AST\Literal; | ||
| use Doctrine\ORM\Query\AST\Node; | ||
| use MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\BaseVariadicFunction; | ||
| use MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\Exception\InvalidArgumentForVariadicFunctionException; | ||
| use MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\Traits\BooleanValidationTrait; | ||
|
|
||
| /** | ||
| * A concrete implementation of BaseVariadicFunction for testing purposes. | ||
| */ | ||
| class TestVariadicFunction extends BaseVariadicFunction | ||
| { | ||
| use BooleanValidationTrait; | ||
|
|
||
| protected function getNodeMappingPattern(): array | ||
| { | ||
| return ['StringPrimary']; | ||
| } | ||
|
|
||
| protected function getFunctionName(): string | ||
| { | ||
| return 'test_function'; | ||
| } | ||
|
|
||
| protected function getMinArgumentCount(): int | ||
| { | ||
| return 2; | ||
| } | ||
|
|
||
| protected function getMaxArgumentCount(): int | ||
| { | ||
| return 10; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Consider declaring
customizeFunction()asfinalcustomizeFunction()is now fully implemented in the base class and should never be overridden by descendants – doing so would break the new centralised prototype logic you have just introduced. Marking itfinaleliminates that risk and makes the intended extension‑points (getFunctionName(),getMinArgumentCount(),getMaxArgumentCount()) explicit.📝 Committable suggestion
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not true. The
Arrclass has a different prototype, and it overrides it accordingly for that.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Length of output: 910
@martin-georgiev You're absolutely right. Looking at the
Arrclass implementation, I can confirm it needs to overridecustomizeFunction()to use a different prototype format:The
Arrclass uses square brackets (ARRAY[%s]) instead of the default parentheses pattern. MakingcustomizeFunction()final would prevent this type of legitimate customization, which is needed for PostgreSQL array constructor syntax.Thanks for the correction.
🧬 Code Graph Analysis Results
Relevant snippets for understanding
src/MartinGeorgiev/Doctrine/ORM/Query/AST/Functions/BaseVariadicFunction.phpand the user comment:File:
src/MartinGeorgiev/Doctrine/ORM/Query/AST/Functions/Arr.phpLines 17-40:
Arrextends or relates to variadic functions and overrides the function prototype to use square brackets (ARRAY[%s]) instead of parentheses.'ARRAY'.PHP_INT_MAX).'StringPrimary'.File:
src/MartinGeorgiev/Doctrine/ORM/Query/AST/Functions/BaseVariadicFunction.phpLines 1-110 (entire file provided)
BaseFunctionto support variadic functions with flexible argument counts.getFunctionName(): stringgetNodeMappingPattern(): arraygetMinArgumentCount(): intgetMaxArgumentCount(): intcustomizeFunction()to set the function prototype asfunctionName(%s)by default.feedParserWithNodes(Parser $parser)which tries multiple node mapping patterns to parse arguments.feedParserWithNodesForNodeMappingPattern(Parser $parser, string $nodeMappingPattern)which parses the arguments according to the pattern and validates argument count.validateArguments(Node ...$arguments).getSql(SqlWalker $sqlWalker)generates SQL by dispatching nodes and formatting with the function prototype.DoctrineLexerandDoctrineOrmutilities to handle lexer differences between Doctrine versions.File:
src/MartinGeorgiev/Doctrine/ORM/Query/AST/Functions/BaseFunction.phpLines 20-105 (summary):
feedParserWithNodesparses nodes according to a fixed mapping pattern.customizeFunction()is empty by default but overridden in subclasses.File:
src/MartinGeorgiev/Utils/DoctrineOrm.phpLines 12-18:
TokenTypeclass.BaseVariadicFunctionandBaseFunctionto handle lexer differences.File:
src/MartinGeorgiev/Utils/DoctrineLexer.phpLines 12-58 (summary):
File:
src/MartinGeorgiev/Doctrine/ORM/Query/AST/Functions/Exception/InvalidArgumentForVariadicFunctionException.phpLines 7-56 (summary):
Summary of relevance:
Arrclass having a different prototype and overriding accordingly.Arr.phpshows that it overridescustomizeFunction()to set a prototype with square brackets (ARRAY[%s]), differing from the default parentheses inBaseVariadicFunction.BaseVariadicFunctionclass sets the default prototype with parentheses and handles variadic argument parsing and validation.BaseFunctionuses parentheses by default and a fixed node mapping pattern.DoctrineOrmandDoctrineLexerhandle version differences in Doctrine ORM and Lexer.BaseVariadicFunctionto throw errors on invalid argument counts.These snippets provide the necessary context to understand the difference in function prototype handling between
BaseVariadicFunctionand theArrclass, which overrides the prototype accordingly.✏️ Learnings added