8000 Fix `CompositeExpression#add()` optimization, which was leading to broken `CompositeExpression` instances by Grzesie2k · Pull Request #2785 · doctrine/dbal · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix CompositeExpression#add() optimization, which was leading to broken CompositeExpression instances #2785

8000
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 1 commit into from
Aug 3, 2017
Merged

Fix CompositeExpression#add() optimization, which was leading to broken CompositeExpression instances #2785

merged 1 commit into from
Aug 3, 2017

Conversation

Grzesie2k
Copy link

Current implementation of CompositeExpression->add() has invalid rule for empty instances of self class (never executed).


$expr->add(new CompositeExpression(CompositeExpression::TYPE_OR, []));

$this->assertEquals(2, 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.

This scenario doesn't hit the code path you changed

Copy link
Author
@Grzesie2k Grzesie2k Jul 21, 2017

Choose a reason for hiding this comment

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

@Ocramius Unfortunately, you are wrong. It was failing test.

Copy link
Member

Choose a reason for hiding this comment

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

Could you move it to a separate test method, and verify both paths then? Specifically:

  • When an empty compositeexpression is added
  • When a non-empty compositeexpression is added

Copy link
Author

Choose a reason for hiding this comment

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

@@ -91,7 +91,7 @@ public function addMultiple(array $parts = [])
*/
public function add($part)
{
if ( ! empty($part) || ($part instanceof self && $part->count() > 0)) {
if ( ! empty($part) && ! ($part instanceof self && $part->count() === 0)) {
Copy link
Member

Choose a reason for hiding this comment

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

Can you refactor this conditional to remove some negations? It is extremely hard to read.

Copy link
Author

Choose a reason for hiding this comment

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

Copy link
Member
@Ocramius Ocramius left a comment

Choose a reason for hiding this comment

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

Looking good - just nitpicks on the assertions now

{
$expr = new CompositeExpression(CompositeExpression::TYPE_OR, array('u.group_id = 1'));

$this->assertEquals(1, 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.

could you use assertCount here?

@Ocramius Ocramius added this to the 2.5.13 milestone Jul 22, 2017
@Ocramius Ocramius self-assigned this Jul 22, 2017
@@ -91,7 +91,7 @@ public function addMultiple(array $parts = [])
*/
public function add($part)
{
if ( ! empty($part) || ($part instanceof self && $part->count() > 0)) {
if (!empty($part) && count($part)) {
Copy link
Member

Choose a reason for hiding this comment

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

What if $part is not empty but not countable? E.g. $expr->add('id = 1'). It will fail on PHP 7.2.

See https://wiki.php.net/rfc/counting_non_countables

Copy link
Member

Choose a reason for hiding this comment

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

Ouch, missed that :-\

< 8000 span data-view-component="true"> Copy link
Member
@morozov morozov Jul 22, 2017

Choose a reason for hiding this comment

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

I believe we still should check for instanceof self and only check count in this case. Otherwise, we don't know if empty collection will cast to an empty string (can be any custom class). Something like:

if (empty($part)) {
    return;
}

if ($part instanceof self && !count($part)) {
    return;
}

// proceed

@Ocramius Ocramius removed this from the 2.5.13 milestone Jul 22, 2017
@Ocramius Ocramius removed their assignment Jul 22, 2017
@Ocramius Ocramius added the WIP label Jul 22, 2017
Copy link
Member
@morozov morozov left a comment

Choose a reason for hiding this comment

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

Looks good to me.

@Ocramius Ocramius changed the title Fix CompositeExpression->add method optimization Fix CompositeExpression#add() optimization, which was leading to bbroken CompositeExpression instances Aug 3, 2017
@Ocramius Ocramius added this to the 2.6.2 milestone Aug 3, 2017
@Ocramius Ocramius assigned Ocramius and unassigned Grzesie2k Aug 3, 2017
@Ocramius Ocramius merged commit 436179f into doctrine:master Aug 3, 2017
@Ocramius
Copy link
Member
Ocramius commented Aug 3, 2017

Merged, thanks @Grzesie2k!

Backported to 2.6 via 324f3ac

@Grzesie2k Grzesie2k deleted the fix/composite-expression-add branch August 4, 2017 09:05
@Ocramius Ocramius changed the title Fix CompositeExpression#add() optimization, which was leading to bbroken CompositeExpression instances Fix CompositeExpression#add() optimization, which was leading to broken CompositeExpression instances Aug 28, 2017
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 15, 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.

3 participants
0