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 all 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
15 changes: 14 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,19 @@ final public static function getIdHashByIdentifier(array $identifier): string

return implode(
' ',
$identifier,
array_map(
static function ($value) {
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']]);
}
}
Loading