Skip to content

Commit

Permalink
Leverage the Stringable interface (doctrine#9535)
Browse files Browse the repository at this point in the history
  • Loading branch information
derrabus authored Feb 21, 2022
1 parent 597460d commit 87b894e
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 12 deletions.
13 changes: 5 additions & 8 deletions lib/Doctrine/ORM/ORMInvalidArgumentException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

use Doctrine\ORM\Mapping\ClassMetadata;
use InvalidArgumentException;
use Stringable;

use function array_map;
use function count;
use function get_debug_type;
use function gettype;
use function implode;
use function method_exists;
use function reset;
use function spl_object_id;
use function sprintf;
Expand Down Expand Up @@ -223,27 +223,24 @@ public static function invalidAssociation(ClassMetadata $targetClass, $assoc, $a

/**
* Helper method to show an object as string.
*
* @param object $obj
*/
private static function objToStr($obj): string
private static function objToStr(object $obj): string
{
return method_exists($obj, '__toString') ? (string) $obj : get_debug_type($obj) . '@' . spl_object_id($obj);
return $obj instanceof Stringable ? (string) $obj : get_debug_type($obj) . '@' . spl_object_id($obj);
}

/**
* @param object $entity
* @psalm-param array<string,string> $associationMapping
*/
private static function newEntityFoundThroughRelationshipMessage(array $associationMapping, $entity): string
private static function newEntityFoundThroughRelationshipMessage(array $associationMapping, object $entity): string
{
return 'A new entity was found through the relationship \''
. $associationMapping['sourceEntity'] . '#' . $associationMapping['fieldName'] . '\' that was not'
. ' configured to cascade persist operations for entity: ' . self::objToStr($entity) . '.'
. ' To solve this issue: Either explicitly call EntityManager#persist()'
. ' on this unknown entity or configure cascade persist'
. ' this association in the mapping for example @ManyToOne(..,cascade={"persist"}).'
. (method_exists($entity, '__toString')
. ($entity instanceof Stringable
? ''
: ' If you cannot find out which entity causes the problem implement \''
. $associationMapping['targetEntity'] . '#__toString()\' to get a clue.'
Expand Down
4 changes: 2 additions & 2 deletions lib/Doctrine/ORM/UnitOfWork.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
use Exception;
use InvalidArgumentException;
use RuntimeException;
use Stringable;
use Throwable;
use UnexpectedValueException;

Expand All @@ -60,7 +61,6 @@
use function in_array;
use function is_array;
use function is_object;
use function method_exists;
use function reset;
use function spl_object_id;
use function sprintf;
Expand Down Expand Up @@ -2836,7 +2836,7 @@ public function initializeObject(object $obj): void
*/
private static function objToStr(object $obj): string
{
return method_exists($obj, '__toString') ? (string) $obj : get_debug_type($obj) . '@' . spl_object_id($obj);
return $obj instanceof Stringable ? (string) $obj : get_debug_type($obj) . '@' . spl_object_id($obj);
}

/**
Expand Down
5 changes: 3 additions & 2 deletions tests/Doctrine/Tests/ORM/ORMInvalidArgumentExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Doctrine\ORM\ORMInvalidArgumentException;
use PHPUnit\Framework\TestCase;
use stdClass;
use Stringable;

use function spl_object_id;
use function uniqid;
Expand Down Expand Up @@ -46,7 +47,7 @@ public function newEntitiesFoundThroughRelationshipsErrorMessages(): array
$stringEntity3 = uniqid('entity3', true);
$entity1 = new stdClass();
$entity2 = new stdClass();
$entity3 = $this->getMockBuilder(stdClass::class)->setMethods(['__toString'])->getMock();
$entity3 = $this->createMock(Stringable::class);
$association1 = [
'sourceEntity' => 'foo1',
'fieldName' => 'bar1',
Expand All @@ -63,7 +64,7 @@ public function newEntitiesFoundThroughRelationshipsErrorMessages(): array
'targetEntity' => 'baz3',
];

$entity3->expects(self::any())->method('__toString')->willReturn($stringEntity3);
$entity3->method('__toString')->willReturn($stringEntity3);

return [
'one entity found' => [
Expand Down

0 comments on commit 87b894e

Please sign in to comment.