Skip to content

Proper aggregate function detection (even deeper in AST) #581

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

Conversation

janedbal
Copy link
Contributor

Separated from #506.

This is needed for proper nullability detection (when no group by is used, single row is always returned). Previously, it was working only when SUM was in first level in select AST. But it can be anywhere inside the tree, so we need pre-walk the AST to detect it.

use Doctrine\ORM\Query\AST;
use function is_string;

class QueryAggregateFunctionDetectorTreeWalker extends Query\TreeWalkerAdapter
Copy link
Contributor Author

@janedbal janedbal Jun 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The AST traversal logic is copied from Doctrine's SqlWalker. But since they do double-dispatch which is not possible here, those places are mostly replaced with generic doWalkNode.

I also prefixed all methods with do so that this works in dbal3 and dbal4 (where TreeWalker interface lost most of its methods).

$this->constantArray([
[
new ConstantStringType('many'),
TypeCombinator::addNull(new ObjectType(Many::class)),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This wont be nullable in the old implementation, which is wrong.

@ondrejmirtes ondrejmirtes merged commit 3369068 into phpstan:1.4.x Jun 25, 2024
36 checks passed
@ondrejmirtes
Copy link
Member

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants