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

Migrate remaining exceptions to PHP 8 syntax #10402

Merged
merged 1 commit into from
Jan 18, 2023
Merged
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
113 changes: 19 additions & 94 deletions lib/Doctrine/ORM/ORMInvalidArgumentException.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,67 +24,39 @@
*/
class ORMInvalidArgumentException extends InvalidArgumentException
{
/**
* @param object $entity
*
* @return ORMInvalidArgumentException
*/
public static function scheduleInsertForManagedEntity($entity)
public static function scheduleInsertForManagedEntity(object $entity): self
{
return new self('A managed+dirty entity ' . self::objToStr($entity) . ' can not be scheduled for insertion.');
}

/**
* @param object $entity
*
* @return ORMInvalidArgumentException
*/
public static function scheduleInsertForRemovedEntity($entity)
public static function scheduleInsertForRemovedEntity(object $entity): self
{
return new self('Removed entity ' . self::objToStr($entity) . ' can not be scheduled for insertion.');
}

/**
* @param object $entity
*
* @return ORMInvalidArgumentException
*/
public static function scheduleInsertTwice($entity)
public static function scheduleInsertTwice(object $entity): self
{
return new self('Entity ' . self::objToStr($entity) . ' can not be scheduled for insertion twice.');
}

/**
* @param string $className
* @param object $entity
*
* @return ORMInvalidArgumentException
*/
public static function entityWithoutIdentity($className, $entity)
public static function entityWithoutIdentity(string $className, object $entity): self
{
return new self(
"The given entity of type '" . $className . "' (" . self::objToStr($entity) . ') has no identity/no ' .
'id values set. It cannot be added to the identity map.',
);
}

/**
* @param object $entity
*
* @return ORMInvalidArgumentException
*/
public static function readOnlyRequiresManagedEntity($entity)
public static function readOnlyRequiresManagedEntity(object $entity): self
{
return new self('Only managed entities can be marked or checked as read only. But ' . self::objToStr($entity) . ' is not');
}

/**
* @param array[][]|object[][] $newEntitiesWithAssociations non-empty an array
* of [array $associationMapping, object $entity] pairs
*
* @return ORMInvalidArgumentException
*/
public static function newEntitiesFoundThroughRelationships($newEntitiesWithAssociations)
public static function newEntitiesFoundThroughRelationships(array $newEntitiesWithAssociations): self
{
$errorMessages = array_map(
static function (array $newEntityWithAssociation): string {
Expand All @@ -106,96 +78,54 @@ static function (array $newEntityWithAssociation): string {
);
}

/**
* @param object $entry
* @psalm-param AssociationMapping $associationMapping
*
* @return ORMInvalidArgumentException
*/
public static function newEntityFoundThroughRelationship(array $associationMapping, $entry)
/** @psalm-param AssociationMapping $associationMapping */
public static function newEntityFoundThroughRelationship(array $associationMapping, object $entry): self
{
return new self(self::newEntityFoundThroughRelationshipMessage($associationMapping, $entry));
}

/**
* @param object $entry
* @psalm-param array<string, string> $assoc
*
* @return ORMInvalidArgumentException
*/
public static function detachedEntityFoundThroughRelationship(array $assoc, $entry)
/** @psalm-param array<string, string> $assoc */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably something we should fix on a lower branch: The method has a pretty strong opinion on which keys should be defined on that array. It might be worth either documenting the shape or splitting $assoc into three (?) parameters.

public static function detachedEntityFoundThroughRelationship(array $assoc, object $entry): self
{
return new self('A detached entity of type ' . $assoc['targetEntity'] . ' (' . self::objToStr($entry) . ') '
. " was found through the relationship '" . $assoc['sourceEntity'] . '#' . $assoc['fieldName'] . "' "
. 'during cascading a persist operation.');
}

/**
* @param object $entity
*
* @return ORMInvalidArgumentException
*/
public static function entityNotManaged($entity)
public static function entityNotManaged(object $entity): self
{
return new self('Entity ' . self::objToStr($entity) . ' is not managed. An entity is managed if its fetched ' .
'from the database or registered as new through EntityManager#persist');
}

/**
* @param object $entity
* @param string $operation
*
* @return ORMInvalidArgumentException
*/
public static function entityHasNoIdentity($entity, $operation)
public static function entityHasNoIdentity(object $entity, string $operation): self
{
return new self('Entity has no identity, therefore ' . $operation . ' cannot be performed. ' . self::objToStr($entity));
}

/**
* @param object $entity
* @param string $operation
*
* @return ORMInvalidArgumentException
*/
public static function entityIsRemoved($entity, $operation)
public static function entityIsRemoved(object $entity, string $operation): self
{
return new self('Entity is removed, therefore ' . $operation . ' cannot be performed. ' . self::objToStr($entity));
}

/**
* @param object $entity
* @param string $operation
*
* @return ORMInvalidArgumentException
*/
public static function detachedEntityCannot($entity, $operation)
public static function detachedEntityCannot(object $entity, string $operation): self
{
return new self('Detached entity ' . self::objToStr($entity) . ' cannot be ' . $operation);
}

/**
* @param string $context
* @param mixed $given
* @param int $parameterIndex
*
* @return ORMInvalidArgumentException
*/
public static function invalidObject($context, $given, $parameterIndex = 1)
public static function invalidObject(string $context, mixed $given, int $parameterIndex = 1): self
{
return new self($context . ' expects parameter ' . $parameterIndex .
' to be an entity object, ' . gettype($given) . ' given.');
}

/** @return ORMInvalidArgumentException */
public static function invalidCompositeIdentifier()
public static function invalidCompositeIdentifier(): self
{
return new self('Binding an entity with a composite primary key to a query is not supported. ' .
'You should split the parameter into the explicit fields and bind them separately.');
}

/** @return ORMInvalidArgumentException */
public static function invalidIdentifierBindingEntity(string $class)
public static function invalidIdentifierBindingEntity(string $class): self
{
return new self(sprintf(
<<<'EXCEPTION'
Expand All @@ -207,13 +137,8 @@ public static function invalidIdentifierBindingEntity(string $class)
));
}

/**
* @param mixed[] $assoc
* @param mixed $actualValue
*
* @return self
*/
public static function invalidAssociation(ClassMetadata $targetClass, $assoc, $actualValue)
/** @param mixed[] $assoc */
public static function invalidAssociation(ClassMetadata $targetClass, array $assoc, mixed $actualValue): self
{
$expectedType = $targetClass->getName();

Expand Down
44 changes: 14 additions & 30 deletions lib/Doctrine/ORM/OptimisticLockException.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,56 +15,40 @@
*/
class OptimisticLockException extends Exception implements ORMException
{
/**
* @param string $msg
* @param object|string|null $entity
*/
public function __construct($msg, private $entity, Throwable|null $previous = null)
{
public function __construct(
string $msg,
private object|string|null $entity,
Throwable|null $previous = null,
) {
parent::__construct($msg, 0, $previous);
}

/**
* Gets the entity that caused the exception.
*
* @return object|string|null
*/
public function getEntity()
public function getEntity(): object|string|null
{
return $this->entity;
}

/**
* @param object|class-string $entity
*
* @return OptimisticLockException
*/
public static function lockFailed($entity)
/** @param object|class-string $entity */
public static function lockFailed(object|string $entity): self
{
return new self('The optimistic lock on an entity failed.', $entity);
}

/**
* @param object $entity
* @param int|string|DateTimeInterface $expectedLockVersion
* @param int|string|DateTimeInterface $actualLockVersion
*
* @return OptimisticLockException
*/
public static function lockFailedVersionMismatch($entity, $expectedLockVersion, $actualLockVersion)
{
public static function lockFailedVersionMismatch(
object $entity,
int|string|DateTimeInterface $expectedLockVersion,
int|string|DateTimeInterface $actualLockVersion,
): self {
$expectedLockVersion = $expectedLockVersion instanceof DateTimeInterface ? $expectedLockVersion->getTimestamp() : $expectedLockVersion;
$actualLockVersion = $actualLockVersion instanceof DateTimeInterface ? $actualLockVersion->getTimestamp() : $actualLockVersion;

return new self('The optimistic lock failed, version ' . $expectedLockVersion . ' was expected, but is actually ' . $actualLockVersion, $entity);
}

/**
* @param string $entityName
*
* @return OptimisticLockException
*/
public static function notVersioned($entityName)
public static function notVersioned(string $entityName): self
{
return new self('Cannot obtain optimistic lock on unversioned entity ' . $entityName, null);
}
Expand Down