diff --git a/lib/Doctrine/ORM/ORMInvalidArgumentException.php b/lib/Doctrine/ORM/ORMInvalidArgumentException.php index 31b8c99f1bb..ab0e6d412e9 100644 --- a/lib/Doctrine/ORM/ORMInvalidArgumentException.php +++ b/lib/Doctrine/ORM/ORMInvalidArgumentException.php @@ -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; @@ -223,19 +223,16 @@ 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 $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' @@ -243,7 +240,7 @@ private static function newEntityFoundThroughRelationshipMessage(array $associat . ' 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.' diff --git a/lib/Doctrine/ORM/UnitOfWork.php b/lib/Doctrine/ORM/UnitOfWork.php index b513639c342..13fe99c0360 100644 --- a/lib/Doctrine/ORM/UnitOfWork.php +++ b/lib/Doctrine/ORM/UnitOfWork.php @@ -40,6 +40,7 @@ use Exception; use InvalidArgumentException; use RuntimeException; +use Stringable; use Throwable; use UnexpectedValueException; @@ -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; @@ -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); } /** diff --git a/tests/Doctrine/Tests/ORM/ORMInvalidArgumentExceptionTest.php b/tests/Doctrine/Tests/ORM/ORMInvalidArgumentExceptionTest.php index ea3e0129712..bd65e60edcb 100644 --- a/tests/Doctrine/Tests/ORM/ORMInvalidArgumentExceptionTest.php +++ b/tests/Doctrine/Tests/ORM/ORMInvalidArgumentExceptionTest.php @@ -7,6 +7,7 @@ use Doctrine\ORM\ORMInvalidArgumentException; use PHPUnit\Framework\TestCase; use stdClass; +use Stringable; use function spl_object_id; use function uniqid; @@ -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', @@ -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' => [