Skip to content

Commit

Permalink
Migrate remaining exceptions to PHP 8 syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
greg0ire committed Jan 14, 2023
1 parent 3cd65b1 commit 41370fe
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 124 deletions.
113 changes: 19 additions & 94 deletions lib/Doctrine/ORM/ORMInvalidArgumentException.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,67 +22,39 @@
*/
class ORMInvalidArgumentException extends InvalidArgumentException
{
/**
* @param object $entity
*
* @return ORMInvalidArgumentException
*/
public static function scheduleInsertForManagedEntity($entity)
public static function scheduleInsertForManagedEntity(object $entity): ORMInvalidArgumentException
{
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): ORMInvalidArgumentException
{
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): ORMInvalidArgumentException
{
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): ORMInvalidArgumentException
{
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): ORMInvalidArgumentException
{
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): ORMInvalidArgumentException
{
$errorMessages = array_map(
static function (array $newEntityWithAssociation): string {
Expand All @@ -104,96 +76,54 @@ static function (array $newEntityWithAssociation): string {
);
}

/**
* @param object $entry
* @psalm-param array<string, string> $associationMapping
*
* @return ORMInvalidArgumentException
*/
public static function newEntityFoundThroughRelationship(array $associationMapping, $entry)
/** @psalm-param array<string, string> $associationMapping */
public static function newEntityFoundThroughRelationship(array $associationMapping, object $entry): ORMInvalidArgumentException
{
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 */
public static function detachedEntityFoundThroughRelationship(array $assoc, object $entry): ORMInvalidArgumentException
{
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): ORMInvalidArgumentException
{
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): ORMInvalidArgumentException
{
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): ORMInvalidArgumentException
{
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): ORMInvalidArgumentException
{
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): ORMInvalidArgumentException
{
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(): ORMInvalidArgumentException
{
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): ORMInvalidArgumentException
{
return new self(sprintf(
<<<'EXCEPTION'
Expand All @@ -205,13 +135,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): ORMInvalidArgumentException
{
$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): OptimisticLockException
{
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,
): OptimisticLockException {
$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): OptimisticLockException
{
return new self('Cannot obtain optimistic lock on unversioned entity ' . $entityName, null);
}
Expand Down

0 comments on commit 41370fe

Please sign in to comment.