Skip to content
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

Disallow empty CompositeExpression #3868

Merged
merged 1 commit into from
Jan 31, 2020

Conversation

BenMorel
Copy link
Contributor

@BenMorel BenMorel commented Jan 31, 2020

Q A
Type improvement
BC Break no
Fixed issues #3845

Summary

This PR disallows an empty CompositeExpression, fixing #3845.

*/
public function __construct(string $type, array $parts = [])
public function __construct(string $type, $part, ...$parts)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do we want to keep the constructor public?

Making it private would require a change to the following logic:

private function appendToPredicate($currentPredicate, string $type, ...$predicates)
{
if ($currentPredicate instanceof CompositeExpression && $currentPredicate->getType() === $type) {
return $currentPredicate->with(...$predicates);
}
if ($currentPredicate !== null) {
array_unshift($predicates, $currentPredicate);
} elseif (count($predicates) === 1) {
return $predicates[0];
}
return new CompositeExpression($type, ...$predicates);
}

Copy link
Member

Choose a reason for hiding this comment

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

It feels like this logic belongs to the expression class itself. It’s already aware of its type and parts and should be able to produce a new instance of self. This way, we wouldn’t even need to expose getType() anymore.

But I’m not sure how to put it exactly. Let’s leave it public for now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What about moving these to static methods in CompositeExpression?

Here's how it would look like: BenMorel@a123732 (not included in this PR for now).

This way, we can make the constructor private, and remove the getType() method. Note that we still have to keep the TYPE_* constants public, unless we split appendToPredicate() into two distinct methods.

Copy link
Member

@morozov morozov Jan 31, 2020

Choose a reason for hiding this comment

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

What about moving these to static methods in CompositeExpression?

Yeah, this is exactly what I had in mind. Let’s take it out of scope since it still needs additional discussion. If you’re fine with that, we can merge the PR in its current state.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

All good for me. Let's merge this one, and I'll see what I can do in another PR!

@morozov morozov added this to the 4.0.0 milestone Jan 31, 2020
@morozov morozov self-assigned this Jan 31, 2020
@morozov morozov merged commit 5fec6ea into doctrine:master Jan 31, 2020
@morozov
Copy link
Member

morozov commented Jan 31, 2020

Merged. Thanks, @BenMorel!

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

Successfully merging this pull request may close these issues.

QueryBuilder produces invalid SQL from an empty CompositeExpression
2 participants