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

Add error handling when misuse of find() with array values #11237

Closed

Conversation

symfonyaml
Copy link

Q A
Branch? 2.18.x
Bug fix? yes
New feature? no
Issues Fix #11236

Issue

When we (inexpertly) use $em->find(...) with array values in identifer, it returns a PHP warning..
See all details and how to reproduce it in the issue : #11236.

Solution

In this PR

  • Add some DX with an exception explaining the problem, to avoid PHP warning
  • Add tests

@derrabus
Copy link
Member

derrabus commented Feb 8, 2024

The current behavior is not a bug. For DX improvements, please target our next feature release which is 3.1.

Comment on lines +1784 to +1786
if (array_filter($identifier, 'is_array')) {
throw new UnexpectedValueException('Unexpected identifier value: Expecting scalar, got array.');
}
Copy link
Member

Choose a reason for hiding this comment

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

I think, we can handle this case in the mapper function below. And we could throw that exception if we encounter anything that is not a scalar or stringable.

Copy link
Author

Choose a reason for hiding this comment

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

Done in new PR targeting 3.1.x branch #11285

Copy link
Author

Choose a reason for hiding this comment

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

@derrabus Unfortunately we cannot do that because it breaks the find by custom id object feature :

$ vendor/bin/phpunit

1) Doctrine\Tests\ORM\Functional\CustomIdObjectTypeTest::testFindByCustomIdObject
UnexpectedValueException: Unexpected identifier value: Expecting scalar, got object.

2) Doctrine\Tests\ORM\Functional\CustomIdObjectTypeTest::testFetchJoinCustomIdObject
UnexpectedValueException: Unexpected identifier value: Expecting scalar, got object.

3) Doctrine\Tests\ORM\Functional\CustomIdObjectTypeTest::testFetchJoinWhereCustomIdObject
UnexpectedValueException: Unexpected identifier value: Expecting scalar, got object.

4) Doctrine\Tests\ORM\Functional\PaginationTest::testCustomIdTypeWithoutOutputWalker
UnexpectedValueException: Unexpected identifier value: Expecting scalar, got object.

# etc...

Copy link
Member

Choose a reason for hiding this comment

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

But those custom IDs need to be stringable, otherwise the implode below would fail. I don't understand the issue here.

Copy link
Author

Choose a reason for hiding this comment

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

In this PR I'm just checking that the "array" type is invalid.
So the only check we can do inside the mapper function is to check that $value is an array

if (is_array($value)) {
    throw new UnexpectedValueException(...);
}

This bit of code is not dealing with other types like objects.

Do you want me to move the is_array($value) condition in the mapper function then ?

Copy link
Member

Choose a reason for hiding this comment

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

In this PR I'm just checking that the "array" type is invalid.

Which is a bit arbitrary. If we add validation here, let's do it properly and catch everything that we don't accept.

This bit of code is not dealing with other types like objects.

But it should.

Copy link
Author

Choose a reason for hiding this comment

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

So you mean we should do something like

if (! is_scalar($value) && ! ($value instanceof Stringable)) ...

?

Copy link
Author

@symfonyaml symfonyaml Feb 22, 2024

Choose a reason for hiding this comment

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

@derrabus I commited in the new PR targeting 3.1.x, I hope that's what we want 🤞

Copy link
Member

Choose a reason for hiding this comment

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

Thank you!

@symfonyaml
Copy link
Author

The current behavior is not a bug. For DX improvements, please target our next feature release which is 3.1.

Created new PR targeting 3.1.x branch #11285

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

Successfully merging this pull request may close these issues.

2 participants