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 #11285

Open
wants to merge 7 commits into
base: 3.4.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/UnitOfWork.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
use function in_array;
use function is_array;
use function is_object;
use function is_scalar;
use function reset;
use function spl_object_id;
use function sprintf;
Expand Down Expand Up @@ -1565,7 +1566,23 @@ final public static function getIdHashByIdentifier(array $identifier): string

return implode(
' ',
$identifier,
array_map(
static function ($value) {
if ($value instanceof BackedEnum) {
return $value->value;
}
Copy link
Member

Choose a reason for hiding this comment

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

This looks redundant… isn't it already handled on line 1561?

Copy link
Author

Choose a reason for hiding this comment

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

The code looks redundant yes, but the check is not ?

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 get it… after line 1565, can no longer contain any BackedEnum, right? So why add this check?

Copy link
Author

Choose a reason for hiding this comment

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

Ok yes got it. I removed this check thank you !


if (! is_scalar($value) && ! ($value instanceof Stringable)) {
throw new UnexpectedValueException(sprintf(
'Unexpected identifier value: Expecting scalar or Stringable, got %s.',
get_debug_type($value),
));
}

return $value;
},
$identifier,
),
);
}

Expand Down
26 changes: 26 additions & 0 deletions tests/Tests/ORM/Functional/IdentifierFunctionalTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional;

use Doctrine\Tests\Models\CMS\CmsUser;
use Doctrine\Tests\OrmFunctionalTestCase;
use UnexpectedValueException;

class IdentifierFunctionalTest extends OrmFunctionalTestCase
{
protected function setUp(): void
{
$this->useModelSet('cms');

parent::setUp();
}

public function testIdentifierArrayValue(): void
{
$this->expectException(UnexpectedValueException::class);
$this->expectExceptionMessage('Unexpected identifier value: Expecting scalar or Stringable, got array.');
$this->_em->find(CmsUser::class, ['id' => ['array']]);
}
}