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 1 commit
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
6 changes: 6 additions & 0 deletions src/UnitOfWork.php
Original file line number Diff line number Diff line change
Expand Up @@ -1561,11 +1561,17 @@
' ',
array_map(
static function ($value) {
if ($value instanceof BackedEnum) {

Check failure on line 1564 in src/UnitOfWork.php

View workflow job for this annotation

GitHub Actions / coding-standards / Coding Standards (8.2)

Expected 1 line after "if", found 0.
return $value->value;
}
greg0ire marked this conversation as resolved.
Show resolved Hide resolved
if (!is_scalar($value) || !$value instanceof Stringable) {

Check failure on line 1567 in src/UnitOfWork.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (default, phpstan.neon)

Instanceof between bool|float|int|string and Stringable will always evaluate to false.

Check failure on line 1567 in src/UnitOfWork.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (default, phpstan.neon)

Result of || is always true.

Check failure on line 1567 in src/UnitOfWork.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (3.7, phpstan-dbal3.neon)

Instanceof between bool|float|int|string and Stringable will always evaluate to false.

Check failure on line 1567 in src/UnitOfWork.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (3.7, phpstan-dbal3.neon)

Result of || is always true.

Check failure on line 1567 in src/UnitOfWork.php

View workflow job for this annotation

GitHub Actions / coding-standards / Coding Standards (8.2)

Expected 1 space(s) after NOT operator; 0 found

Check failure on line 1567 in src/UnitOfWork.php

View workflow job for this annotation

GitHub Actions / coding-standards / Coding Standards (8.2)

Function is_scalar() should not be referenced via a fallback global name, but via a use statement.

Check failure on line 1567 in src/UnitOfWork.php

View workflow job for this annotation

GitHub Actions / coding-standards / Coding Standards (8.2)

Expected 1 space(s) after NOT operator; 0 found
throw new UnexpectedValueException(sprintf(
'Unexpected identifier value: Expecting scalar, got %s.',
gettype($value),

Check failure on line 1570 in src/UnitOfWork.php

View workflow job for this annotation

GitHub Actions / coding-standards / Coding Standards (8.2)

Function gettype() should not be referenced via a fallback global name, but via a use statement.
symfonyaml marked this conversation as resolved.
Show resolved Hide resolved
));
}

return $value;

Check failure on line 1574 in src/UnitOfWork.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (default, phpstan.neon)

Unreachable statement - code above always terminates.

Check failure on line 1574 in src/UnitOfWork.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (3.7, phpstan-dbal3.neon)

Unreachable statement - code above always terminates.
},
$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, got array.');
$this->_em->find(CmsUser::class, ['id' => ['array']]);
}
}

Check failure on line 26 in tests/Tests/ORM/Functional/IdentifierFunctionalTest.php

View workflow job for this annotation

GitHub Actions / coding-standards / Coding Standards (8.2)

Expected 1 newline at end of file; 0 found
Loading