8000 Prevent adding empty CompositeExpression objects by IchHabRecht · Pull Request #2389 · doctrine/dbal · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Prevent adding empty CompositeExpression objects #2389

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

Conversation

IchHabRecht
Copy link

Fixes #2388

@IchHabRecht IchHabRecht force-pushed the preventsAddingEmptyCompositeExpressionObjects branch from 647e812 to 5afcd39 Compare May 12, 2016 10:16
$emptyExpr = new CompositeExpression(CompositeExpression::TYPE_AND, []);
$expr = new CompositeExpression(CompositeExpression::TYPE_AND, [$emptyExpr]);

$this->assertEquals(0, count($expr));
Copy link
Member

Choose a reason for hiding this comment

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

I don't quite get it. The object is mutable, so you can add any more parts as you want as long as you do not cast it as string. Then you might want to check for number of parts >= 2 and eventually throw an exception. But your test is not revealing any issue. Also I am not sure if this is not considered part of the internal API /cc @Ocramius

Copy link
Author

Choose a reason for hiding this comment

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

Sorry, but I don't get your comment at all. I can try to explain the issue a bit more, but maybe there is any way to get in contact with you in any chat?

The problem which I ran into:

  • reading the (old) code, it should be possible to pass an object of type CompositeExpression (object2) as a new part in another CompositeExpression object (object1)
  • object2 should only be added it it contains parts on its own
  • so creating object2 with empty parts and passing it to object1 shouldn't change parts of object1

Currently this is broken and an empty object2 is added to the parts of object1. IMHO this is not intended. I created the patch and a unit test which fails before and is fixed with the patch.

Copy link
Member

Choose a reason for hiding this comment

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

Hehe sorry if I was not clear enough. What I want to point out is that if passing an empty instance of CompositeExpression it should not be rejected at all IMO because it is passed as an object by reference and you can still add parts to that later on. Example:

$expression1 = new CompositeExpression(CompositeExpression::TYPE_AND, []);
$expression2 = new CompositeExpression(CompositeExpression::TYPE_AND, [$expression1]);

$expression1->add(array('u.group_id = 1')); // now $expression1 is not empty anymore

This is what I was referring to as "the API is mutable".

Copy link
Author

Choose a reason for hiding this comment

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

Ir that would be a way how the api can be used, why was there a check

($part instanceof self && $part->count() > 0)

in the code? I think it shouldn't be possible to add constraints after adding the object to the CompositeExpression

Copy link
Member

Choose a reason for hiding this comment

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

I don't have a strong opinion on this TBH and I don't know what the original intention was /cc @Ocramius @guilhermeblanco

Copy link
Member

Choose a reason for hiding this comment

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

It would be much easier to prevent an empty operands list to make it into a CompositeExpression, but I suppose that we can't change the API now.

Copy link

Choose a reason for hiding this comment

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

@Ocramius just to be sure: what you're saying is that the fix here is correct, but as it affects public API, it can't be fixed?

Copy link
Member

Choose a reason for hiding this comment

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

No, it was just wishful thinking: if we didn't allow mutation of expressions, this problem would not exist in first place.

Anyway, the fix doesn't seem correct to me: I think the correct way of dealing with the problem is to remove unnecessary composite expression building within __toString, which is called at the very end of the entire procedure. This way, even if state changed in-between, we'd still get consistent behavior.

Base automatically changed from master to 4.0.x January 22, 2021 07:43
@morozov
Copy link
Member
morozov commented Jul 26, 2021

Fixed in #3864.

@morozov morozov closed this Jul 26, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 27, 2022
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.

5 participants
0