Skip to content

Commit

Permalink
Add error handling when misuse of find() with array values
Browse files Browse the repository at this point in the history
  • Loading branch information
symfonyaml committed Jan 24, 2025
1 parent 0d2cb6a commit e4d6329
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
21 changes: 14 additions & 7 deletions src/UnitOfWork.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,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 @@ -1560,15 +1561,21 @@ public function addToIdentityMap(object $entity): bool
*/
final public static function getIdHashByIdentifier(array $identifier): string
{
foreach ($identifier as $k => $value) {
if ($value instanceof BackedEnum) {
$identifier[$k] = $value->value;
}
}

return implode(
' ',
$identifier,
array_map(
static function (mixed $value): Stringable|int|float|string|bool {
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']]);
}
}

0 comments on commit e4d6329

Please sign in to comment.