Skip to content

Commit

Permalink
Enforce native type declarations
Browse files Browse the repository at this point in the history
I tried to avoid changes in tests, so as to not unnecessarily widen the
gap between 2.x and 3.x.
  • Loading branch information
greg0ire committed Oct 22, 2023
1 parent 82f4a17 commit 8ab0af3
Show file tree
Hide file tree
Showing 53 changed files with 110 additions and 250 deletions.
11 changes: 3 additions & 8 deletions lib/Doctrine/ORM/Event/PreUpdateEventArgs.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@ class PreUpdateEventArgs extends LifecycleEventArgs
private array $entityChangeSet;

/**
* @param object $entity
* @param mixed[][] $changeSet
* @psalm-param array<string, array{mixed, mixed}|PersistentCollection> $changeSet
*/
public function __construct($entity, EntityManagerInterface $em, array &$changeSet)
public function __construct(object $entity, EntityManagerInterface $em, array &$changeSet)
{
parent::__construct($entity, $em);

Expand Down Expand Up @@ -55,10 +54,8 @@ public function hasChangedField(string $field): bool

/**
* Gets the old value of the changeset of the changed field.
*
* @return mixed
*/
public function getOldValue(string $field)
public function getOldValue(string $field): mixed

Check warning on line 58 in lib/Doctrine/ORM/Event/PreUpdateEventArgs.php

View check run for this annotation

Codecov / codecov/patch

lib/Doctrine/ORM/Event/PreUpdateEventArgs.php#L58

Added line #L58 was not covered by tests
{
$this->assertValidField($field);

Expand All @@ -67,10 +64,8 @@ public function getOldValue(string $field)

/**
* Gets the new value of the changeset of the changed field.
*
* @return mixed
*/
public function getNewValue(string $field)
public function getNewValue(string $field): mixed

Check warning on line 68 in lib/Doctrine/ORM/Event/PreUpdateEventArgs.php

View check run for this annotation

Codecov / codecov/patch

lib/Doctrine/ORM/Event/PreUpdateEventArgs.php#L68

Added line #L68 was not covered by tests
{
$this->assertValidField($field);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@

final class EntityIdentityCollisionException extends Exception implements ORMException
{
/**
* @param object $existingEntity
* @param object $newEntity
*/
public static function create($existingEntity, $newEntity, string $idHash): self
public static function create(object $existingEntity, object $newEntity, string $idHash): self
{
return new self(
sprintf(
Expand Down
20 changes: 8 additions & 12 deletions lib/Doctrine/ORM/Internal/TopologicalSort.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ final class TopologicalSort
*
* @var array<int, object>
*/
private $nodes = [];
private array $nodes = [];

/**
* DFS state for the different nodes, indexed by node object id and using one of
* this class' constants as value.
*
* @var array<int, self::*>
*/
private $states = [];
private array $states = [];

/**
* Edges between the nodes. The first-level key is the object id of the outgoing
Expand All @@ -48,38 +48,34 @@ final class TopologicalSort
*
* @var array<int, array<int, bool>>
*/
private $edges = [];
private array $edges = [];

/**
* Builds up the result during the DFS.
*
* @var list<object>
*/
private $sortResult = [];
private array $sortResult = [];

/** @param object $node */
public function addNode($node): void
public function addNode(object $node): void
{
$id = spl_object_id($node);
$this->nodes[$id] = $node;
$this->states[$id] = self::NOT_VISITED;
$this->edges[$id] = [];
}

/** @param object $node */
public function hasNode($node): bool
public function hasNode(object $node): bool
{
return isset($this->nodes[spl_object_id($node)]);
}

/**
* Adds a new edge between two nodes to the graph
*
* @param object $from
* @param object $to
* @param bool $optional This indicates whether the edge may be ignored during the topological sort if it is necessary to break cycles.
* @param bool $optional This indicates whether the edge may be ignored during the topological sort if it is necessary to break cycles.
*/
public function addEdge($from, $to, bool $optional): void
public function addEdge(object $from, object $to, bool $optional): void
{
$fromId = spl_object_id($from);
$toId = spl_object_id($to);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,14 @@
class CycleDetectedException extends RuntimeException
{
/** @var list<object> */
private $cycle;
private array $cycle;

/**
* Do we have the complete cycle collected?
*
* @var bool
*/
private $cycleCollected = false;
private bool $cycleCollected = false;

/** @param object $startNode */
public function __construct(private $startNode)
public function __construct(private object $startNode)
{
parent::__construct('A cycle has been detected, so a topological sort is not possible. The getCycle() method provides the list of nodes that form the cycle.');

Expand All @@ -34,8 +31,7 @@ public function getCycle(): array
return $this->cycle;
}

/** @param object $node */
public function addToCycle($node): void
public function addToCycle(object $node): void
{
array_unshift($this->cycle, $node);

Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/ORM/Mapping/AssociationMapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ public function offsetExists(mixed $offset): bool
return isset($this->$offset) || in_array($offset, ['isOwningSide', 'type'], true);
}

final public function offsetGet($offset): mixed
final public function offsetGet(mixed $offset): mixed
{
return match ($offset) {
'isOwningSide' => $this->isOwningSide(),
Expand Down
8 changes: 3 additions & 5 deletions lib/Doctrine/ORM/Mapping/ClassMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ class ClassMetadata implements PersistenceClassMetadata, Stringable
* @param string $name The name of the entity class the new instance is used for.
* @psalm-param class-string<T> $name
*/
public function __construct(public $name, NamingStrategy|null $namingStrategy = null, TypedFieldMapper|null $typedFieldMapper = null)
public function __construct(public string $name, NamingStrategy|null $namingStrategy = null, TypedFieldMapper|null $typedFieldMapper = null)
{
$this->rootEntityName = $name;
$this->namingStrategy = $namingStrategy ?? new DefaultNamingStrategy();
Expand Down Expand Up @@ -591,11 +591,9 @@ public function getSingleIdReflectionProperty(): ReflectionProperty|null
* For composite identifiers, the identifier values are returned as an array
* with the same order as the field order in {@link identifier}.
*
* @param object $entity
*
* @return array<string, mixed>
*/
public function getIdentifierValues($entity): array
public function getIdentifierValues(object $entity): array
{
if ($this->isIdentifierComposite) {
$id = [];
Expand Down Expand Up @@ -676,7 +674,7 @@ public function __toString(): string
*
* @return string[] The names of all the fields that should be serialized.
*/
public function __sleep()
public function __sleep(): array
{
// This metadata is always serialized/cached.
$serialized = [
Expand Down
3 changes: 1 addition & 2 deletions lib/Doctrine/ORM/ORMInvalidArgumentException.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,7 @@ public static function invalidAssociation(ClassMetadata $targetClass, Associatio
));
}

/** @param mixed $value */
public static function invalidAutoGenerateMode($value): self
public static function invalidAutoGenerateMode(mixed $value): self

Check warning on line 148 in lib/Doctrine/ORM/ORMInvalidArgumentException.php

View check run for this annotation

Codecov / codecov/patch

lib/Doctrine/ORM/ORMInvalidArgumentException.php#L148

Added line #L148 was not covered by tests
{
return new self(sprintf('Invalid auto generate mode "%s" given.', is_scalar($value) ? (string) $value : get_debug_type($value)));
}
Expand Down
14 changes: 4 additions & 10 deletions lib/Doctrine/ORM/Persisters/Entity/CachedPersisterContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,26 @@ class CachedPersisterContext
/**
* The SELECT column list SQL fragment used for querying entities by this persister.
* This SQL fragment is only generated once per request, if at all.
*
* @var string|null
*/
public $selectColumnListSql;
public string|null $selectColumnListSql = null;

/**
* The JOIN SQL fragment used to eagerly load all many-to-one and one-to-one
* associations configured as FETCH_EAGER, as well as all inverse one-to-one associations.
*
* @var string
*/
public $selectJoinSql;
public string|null $selectJoinSql = null;

/**
* Counter for creating unique SQL table and column aliases.
*
* @var int
*/
public $sqlAliasCounter = 0;
public int $sqlAliasCounter = 0;

/**
* Map from class names (FQCN) to the corresponding generated SQL table aliases.
*
* @var array<class-string, string>
*/
public $sqlTableAliases = [];
public array $sqlTableAliases = [];

public function __construct(
/**
Expand Down
3 changes: 1 addition & 2 deletions lib/Doctrine/ORM/Persisters/PersisterException.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@

class PersisterException extends Exception implements ORMException
{
/** @return PersisterException */
public static function matchingAssocationFieldRequiresObject(string $class, string $associationName)
public static function matchingAssocationFieldRequiresObject(string $class, string $associationName): PersisterException

Check warning on line 14 in lib/Doctrine/ORM/Persisters/PersisterException.php

View check run for this annotation

Codecov / codecov/patch

lib/Doctrine/ORM/Persisters/PersisterException.php#L14

Added line #L14 was not covered by tests
{
return new self(sprintf(
'Cannot match on %s::%s with a non-object value. Matching objects by id is ' .
Expand Down
4 changes: 1 addition & 3 deletions lib/Doctrine/ORM/Persisters/SqlValueVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,8 @@ public function getParamsAndTypes(): array
/**
* Returns the value from a Comparison. In case of a CONTAINS comparison,
* the value is wrapped in %-signs, because it will be used in a LIKE clause.
*
* @return mixed
*/
protected function getValueFromComparison(Comparison $comparison)
protected function getValueFromComparison(Comparison $comparison): mixed
{
$value = $comparison->getValue()->getValue();

Expand Down
8 changes: 2 additions & 6 deletions lib/Doctrine/ORM/Proxy/DefaultProxyClassNameResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,8 @@ public function resolveClassName(string $className): string
return substr($className, $pos + Proxy::MARKER_LENGTH + 2);
}

/**
* @param object $object
*
* @return class-string
*/
public static function getClass($object): string
/** @return class-string */
public static function getClass(object $object): string
{
return (new self())->resolveClassName(get_class($object));
}
Expand Down
10 changes: 3 additions & 7 deletions lib/Doctrine/ORM/Proxy/ProxyFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,17 +118,14 @@ public function __serialize(): array
/** The UnitOfWork this factory uses to retrieve persisters */
private readonly UnitOfWork $uow;

/** @var string */
private $proxyDir;

/** @var self::AUTOGENERATE_* */
private $autoGenerate;

/** The IdentifierFlattener used for manipulating identifiers */
private readonly IdentifierFlattener $identifierFlattener;

/** @var array<class-string, Closure> */
private $proxyFactories = [];
private array $proxyFactories = [];

/**
* Initializes a new instance of the <tt>ProxyFactory</tt> class that is
Expand All @@ -141,7 +138,7 @@ public function __serialize(): array
*/
public function __construct(
private readonly EntityManagerInterface $em,
string $proxyDir,
private string $proxyDir,
private readonly string $proxyNs,
bool|int $autoGenerate = self::AUTOGENERATE_NEVER,
) {
Expand All @@ -158,7 +155,6 @@ public function __construct(
}

$this->uow = $em->getUnitOfWork();
$this->proxyDir = $proxyDir;
$this->autoGenerate = (int) $autoGenerate;
$this->identifierFlattener = new IdentifierFlattener($this->uow, $em->getMetadataFactory());
}
Expand All @@ -184,7 +180,7 @@ public function getProxy(string $className, array $identifier): InternalProxy
*
* @return int Number of generated proxies.
*/
public function generateProxyClasses(array $classes, $proxyDir = null): int
public function generateProxyClasses(array $classes, string|null $proxyDir = null): int
{
$generated = 0;

Expand Down
3 changes: 1 addition & 2 deletions lib/Doctrine/ORM/Query/AST/ArithmeticExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ class ArithmeticExpression extends Node
{
public Node|string|null $simpleArithmeticExpression = null;

/** @var Subselect|null */
public $subselect;
public Subselect|null $subselect = null;

public function isSimpleArithmeticExpression(): bool
{
Expand Down
11 changes: 3 additions & 8 deletions lib/Doctrine/ORM/Query/AST/BetweenExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,10 @@

class BetweenExpression extends Node
{
/**
* @param ArithmeticExpression $expression
* @param ArithmeticExpression $leftBetweenExpression
* @param ArithmeticExpression $rightBetweenExpression
*/
public function __construct(
public $expression,
public $leftBetweenExpression,
public $rightBetweenExpression,
public ArithmeticExpression $expression,
public ArithmeticExpression $leftBetweenExpression,
public ArithmeticExpression $rightBetweenExpression,
public bool $not = false,
) {
}
Expand Down
3 changes: 1 addition & 2 deletions lib/Doctrine/ORM/Query/AST/CollectionMemberExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@
*/
class CollectionMemberExpression extends Node
{
/** @param PathExpression $collectionValuedPathExpression */
public function __construct(
public mixed $entityExpression,
public $collectionValuedPathExpression,
public PathExpression $collectionValuedPathExpression,
public bool $not = false,
) {
}
Expand Down
8 changes: 2 additions & 6 deletions lib/Doctrine/ORM/Query/AST/ComparisonExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,10 @@
*/
class ComparisonExpression extends Node
{
/**
* @param Node|string $leftExpression
* @param Node|string $rightExpression
*/
public function __construct(
public $leftExpression,
public Node|string $leftExpression,
public string $operator,
public $rightExpression,
public Node|string $rightExpression,
) {
}

Expand Down
3 changes: 1 addition & 2 deletions lib/Doctrine/ORM/Query/AST/ConditionalFactor.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@
*/
class ConditionalFactor extends Node implements Phase2OptimizableConditional
{
/** @param ConditionalPrimary $conditionalPrimary */
public function __construct(
public $conditionalPrimary,
public ConditionalPrimary $conditionalPrimary,
public bool $not = false,
) {
}
Expand Down
6 changes: 2 additions & 4 deletions lib/Doctrine/ORM/Query/AST/ConditionalPrimary.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@
*/
class ConditionalPrimary extends Node implements Phase2OptimizableConditional
{
/** @var Node|null */
public $simpleConditionalExpression;
public Node|null $simpleConditionalExpression = null;

/** @var ConditionalExpression|Phase2OptimizableConditional|null */
public $conditionalExpression;
public ConditionalExpression|Phase2OptimizableConditional|null $conditionalExpression = null;

public function isSimpleConditionalExpression(): bool
{
Expand Down
Loading

0 comments on commit 8ab0af3

Please sign in to comment.