Skip to content

Commit

Permalink
Fix type errors in AbstractQuery and QueryBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
derrabus committed Jan 3, 2022
1 parent 1599975 commit 03d702d
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 59 deletions.
47 changes: 39 additions & 8 deletions lib/Doctrine/ORM/AbstractQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Doctrine\ORM\Query\QueryException;
use Doctrine\ORM\Query\ResultSetMapping;
use Doctrine\Persistence\Mapping\MappingException;
use LogicException;
use Psr\Cache\CacheItemPoolInterface;
use Traversable;

Expand Down Expand Up @@ -91,7 +92,7 @@ abstract class AbstractQuery
/**
* The user-specified ResultSetMapping to use.
*
* @var ResultSetMapping
* @var ResultSetMapping|null
*/
protected $_resultSetMapping;

Expand All @@ -113,6 +114,7 @@ abstract class AbstractQuery
* The hydration mode.
*
* @var string|int
* @psalm-var string|AbstractQuery::HYDRATE_*
*/
protected $_hydrationMode = self::HYDRATE_OBJECT;

Expand Down Expand Up @@ -150,6 +152,7 @@ abstract class AbstractQuery
* Second level query cache mode.
*
* @var int|null
* @psalm-var Cache::MODE_*|null
*/
protected $cacheMode;

Expand Down Expand Up @@ -251,7 +254,8 @@ public function setLifetime($lifetime)
}

/**
* @return int
* @return int|null
* @psalm-return Cache::MODE_*|null
*/
public function getCacheMode()
{
Expand All @@ -260,6 +264,7 @@ public function getCacheMode()

/**
* @param int $cacheMode
* @psalm-param Cache::MODE_* $cacheMode
*
* @return $this
*/
Expand Down Expand Up @@ -492,7 +497,7 @@ public function setResultSetMapping(Query\ResultSetMapping $rsm)
/**
* Gets the ResultSetMapping used for hydration.
*
* @return ResultSetMapping
* @return ResultSetMapping|null
*/
protected function getResultSetMapping()
{
Expand Down Expand Up @@ -829,6 +834,7 @@ public function setFetchMode($class, $assocName, $fetchMode)
*
* @param string|int $hydrationMode Doctrine processing mode to be used during hydration process.
* One of the Query::HYDRATE_* constants.
* @psalm-param string|AbstractQuery::HYDRATE_* $hydrationMode
*
* @return $this
*/
Expand All @@ -843,6 +849,7 @@ public function setHydrationMode($hydrationMode)
* Gets the hydration mode currently used by the query.
*
* @return string|int
* @psalm-return string|AbstractQuery::HYDRATE_*
*/
public function getHydrationMode()
{
Expand All @@ -855,6 +862,7 @@ public function getHydrationMode()
* Alias for execute(null, $hydrationMode = HYDRATE_OBJECT).
*
* @param string|int $hydrationMode
* @psalm-param string|AbstractQuery::HYDRATE_* $hydrationMode
*
* @return mixed
*/
Expand Down Expand Up @@ -902,7 +910,8 @@ public function getScalarResult()
/**
* Get exactly one result or null.
*
* @param string|int $hydrationMode
* @param string|int|null $hydrationMode
* @psalm-param string|AbstractQuery::HYDRATE_*|null $hydrationMode
*
* @return mixed
*
Expand Down Expand Up @@ -939,7 +948,8 @@ public function getOneOrNullResult($hydrationMode = null)
* If the result is not unique, a NonUniqueResultException is thrown.
* If there is no result, a NoResultException is thrown.
*
* @param string|int $hydrationMode
* @param string|int|null $hydrationMode
* @psalm-param string|AbstractQuery::HYDRATE_*|null $hydrationMode
*
* @return mixed
*
Expand Down Expand Up @@ -1037,6 +1047,7 @@ public function getHints()
*
* @param ArrayCollection|mixed[]|null $parameters The query parameters.
* @param string|int|null $hydrationMode The hydration mode to use.
* @psalm-param string|AbstractQuery::HYDRATE_*|null $hydrationMode The hydration mode to use.
*
* @return IterableResult
*/
Expand All @@ -1057,7 +1068,11 @@ public function iterate($parameters = null, $hydrationMode = null)
$this->setParameters($parameters);
}

$rsm = $this->getResultSetMapping();
$rsm = $this->getResultSetMapping();
if ($rsm === null) {
throw new LogicException('Uninitialized result set mapping.');
}

$stmt = $this->_doExecute();

return $this->_em->newHydrator($this->_hydrationMode)->iterate($stmt, $rsm, $this->_hints);
Expand All @@ -1070,6 +1085,7 @@ public function iterate($parameters = null, $hydrationMode = null)
* @param ArrayCollection|array|mixed[] $parameters The query parameters.
* @param string|int|null $hydrationMode The hydration mode to use.
* @psalm-param ArrayCollection<int, Parameter>|mixed[] $parameters
* @psalm-param string|AbstractQuery::HYDRATE_*|null $hydrationMode
*
* @return iterable<mixed>
*/
Expand All @@ -1087,6 +1103,9 @@ public function toIterable(iterable $parameters = [], $hydrationMode = null): it
}

$rsm = $this->getResultSetMapping();
if ($rsm === null) {
throw new LogicException('Uninitialized result set mapping.');
}

if ($rsm->isMixed && count($rsm->scalarMappings) > 0) {
throw QueryException::iterateWithMixedResultNotAllowed();
Expand All @@ -1103,6 +1122,7 @@ public function toIterable(iterable $parameters = [], $hydrationMode = null): it
* @param ArrayCollection|mixed[]|null $parameters Query parameters.
* @param string|int|null $hydrationMode Processing mode to be used during the hydration process.
* @psalm-param ArrayCollection<int, Parameter>|mixed[]|null $parameters
* @psalm-param string|AbstractQuery::HYDRATE_*|null $hydrationMode
*
* @return mixed
*/
Expand All @@ -1121,6 +1141,7 @@ public function execute($parameters = null, $hydrationMode = null)
* @param ArrayCollection|mixed[]|null $parameters
* @param string|int|null $hydrationMode
* @psalm-param ArrayCollection<int, Parameter>|mixed[]|null $parameters
* @psalm-param string|AbstractQuery::HYDRATE_*|null $hydrationMode
*
* @return mixed
*/
Expand Down Expand Up @@ -1165,7 +1186,11 @@ private function executeIgnoreQueryCache($parameters = null, $hydrationMode = nu
return $stmt;
}

$rsm = $this->getResultSetMapping();
$rsm = $this->getResultSetMapping();
if ($rsm === null) {
throw new LogicException('Uninitialized result set mapping.');
}

$data = $this->_em->newHydrator($this->_hydrationMode)->hydrateAll($stmt, $rsm, $this->_hints);

$setCacheEntry($data);
Expand Down Expand Up @@ -1197,12 +1222,17 @@ private function getHydrationCache(): CacheItemPoolInterface
* @param ArrayCollection|mixed[]|null $parameters
* @param string|int|null $hydrationMode
* @psalm-param ArrayCollection<int, Parameter>|mixed[]|null $parameters
* @psalm-param string|AbstractQuery::HYDRATE_*|null $hydrationMode
*
* @return mixed
*/
private function executeUsingQueryCache($parameters = null, $hydrationMode = null)
{
$rsm = $this->getResultSetMapping();
$rsm = $this->getResultSetMapping();
if ($rsm === null) {
throw new LogicException('Uninitialized result set mapping.');
}

$queryCache = $this->_em->getCache()->getQueryCache($this->cacheRegion);
$queryKey = new QueryCacheKey(
$this->getHash(),
Expand Down Expand Up @@ -1237,6 +1267,7 @@ private function executeUsingQueryCache($parameters = null, $hydrationMode = nul

private function getTimestampKey(): ?TimestampCacheKey
{
assert($this->_resultSetMapping !== null);
$entityName = reset($this->_resultSetMapping->aliasMap);

if (empty($entityName)) {
Expand Down
8 changes: 7 additions & 1 deletion lib/Doctrine/ORM/Cache/QueryCacheKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@ class QueryCacheKey extends CacheKey
public $lifetime;

/**
* Cache mode
*
* READ-ONLY: Public only for performance reasons, it should be considered immutable.
*
* @var int Cache mode (Doctrine\ORM\Cache::MODE_*)
* @var int
* @psalm-var Cache::MODE_*
*/
public $cacheMode;

Expand All @@ -32,6 +35,9 @@ class QueryCacheKey extends CacheKey
*/
public $timestampKey;

/**
* @psalm-param Cache::MODE_* $cacheMode
*/
public function __construct(
string $cacheId,
int $lifetime = 0,
Expand Down
7 changes: 4 additions & 3 deletions lib/Doctrine/ORM/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Doctrine\ORM\Exception\NamedQueryNotFound;
use Doctrine\ORM\Exception\ProxyClassesAlwaysRegenerating;
use Doctrine\ORM\Exception\UnknownEntityNamespace;
use Doctrine\ORM\Internal\Hydration\AbstractHydrator;
use Doctrine\ORM\Mapping\ClassMetadataFactory;
use Doctrine\ORM\Mapping\DefaultEntityListenerResolver;
use Doctrine\ORM\Mapping\DefaultNamingStrategy;
Expand Down Expand Up @@ -712,7 +713,7 @@ public function setCustomDatetimeFunctions(array $functions)
/**
* Sets the custom hydrator modes in one pass.
*
* @param array<string, class-string> $modes An array of ($modeName => $hydrator).
* @param array<string, class-string<AbstractHydrator>> $modes An array of ($modeName => $hydrator).
*
* @return void
*/
Expand All @@ -731,7 +732,7 @@ public function setCustomHydrationModes($modes)
* @param string $modeName The hydration mode name.
*
* @return string|null The hydrator class name.
* @psalm-return ?class-string
* @psalm-return class-string<AbstractHydrator>|null
*/
public function getCustomHydrationMode($modeName)
{
Expand All @@ -743,7 +744,7 @@ public function getCustomHydrationMode($modeName)
*
* @param string $modeName The hydration mode name.
* @param string $hydrator The hydrator class name.
* @psalm-param class-string $hydrator
* @psalm-param class-string<AbstractHydrator> $hydrator
*
* @return void
*/
Expand Down
2 changes: 2 additions & 0 deletions lib/Doctrine/ORM/EntityManagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ public function getUnitOfWork();
* @deprecated
*
* @param string|int $hydrationMode
* @psalm-param string|AbstractQuery::HYDRATE_* $hydrationMode
*
* @return AbstractHydrator
*/
Expand All @@ -293,6 +294,7 @@ public function getHydrator($hydrationMode);
* Create a new instance for the given hydration mode.
*
* @param string|int $hydrationMode
* @psalm-param string|AbstractQuery::HYDRATE_* $hydrationMode
*
* @return AbstractHydrator
*
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/ORM/Internal/Hydration/AbstractHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ final protected function resultSetMapping(): ResultSetMapping
* Hydrates all rows returned by the passed statement instance at once.
*
* @param Result|ResultStatement $stmt
* @param object $resultSetMapping
* @param ResultSetMapping $resultSetMapping
* @psalm-param array<string, string> $hints
*
* @return mixed[]
Expand Down
6 changes: 4 additions & 2 deletions lib/Doctrine/ORM/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Doctrine\ORM\Query\Parser;
use Doctrine\ORM\Query\ParserResult;
use Doctrine\ORM\Query\QueryException;
use Doctrine\ORM\Query\ResultSetMapping;
use Doctrine\ORM\Utility\HierarchyDiscriminatorResolver;
use Psr\Cache\CacheItemPoolInterface;

Expand Down Expand Up @@ -210,6 +211,8 @@ public function getAST()

/**
* {@inheritdoc}
*
* @return ResultSetMapping
*/
protected function getResultSetMapping()
{
Expand Down Expand Up @@ -442,8 +445,6 @@ private function resolveParameterValue(Parameter $parameter): array
$value = $originalValue;
$rsm = $this->getResultSetMapping();

assert($rsm !== null);

if ($value instanceof ClassMetadata && isset($rsm->metadataParameterMapping[$key])) {
$value = $value->getMetadataValue($rsm->metadataParameterMapping[$key]);
}
Expand Down Expand Up @@ -709,6 +710,7 @@ public function getMaxResults(): ?int
* @param ArrayCollection|mixed[]|null $parameters The query parameters.
* @param string|int $hydrationMode The hydration mode to use.
* @psalm-param ArrayCollection<int, Parameter>|array<string, mixed>|null $parameters
* @psalm-param string|AbstractQuery::HYDRATE_*|null $hydrationMode
*/
public function iterate($parameters = null, $hydrationMode = self::HYDRATE_OBJECT): IterableResult
{
Expand Down
11 changes: 9 additions & 2 deletions lib/Doctrine/ORM/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,20 +74,22 @@ class QueryBuilder
* The type of query this is. Can be select, update or delete.
*
* @var int
* @psalm-var self::SELECT|self::DELETE|self::UPDATE
*/
private $_type = self::SELECT;

/**
* The state of the query object. Can be dirty or clean.
*
* @var int
* @psalm-var self::STATE_*
*/
private $_state = self::STATE_CLEAN;

/**
* The complete DQL string for this query.
*
* @var string
* @var string|null
*/
private $_dql;

Expand Down Expand Up @@ -138,6 +140,7 @@ class QueryBuilder
* Second level query cache mode.
*
* @var int|null
* @psalm-var Cache::MODE_*|null
*/
protected $cacheMode;

Expand Down Expand Up @@ -244,7 +247,8 @@ public function setLifetime($lifetime)
}

/**
* @return int
* @return int|null
* @psalm-return Cache::MODE_*|null
*/
public function getCacheMode()
{
Expand All @@ -253,6 +257,7 @@ public function getCacheMode()

/**
* @param int $cacheMode
* @psalm-param Cache::MODE_* $cacheMode
*
* @return $this
*/
Expand All @@ -267,6 +272,7 @@ public function setCacheMode($cacheMode)
* Gets the type of the currently built query.
*
* @return int
* @psalm-return self::SELECT|self::DELETE|self::UPDATE
*/
public function getType()
{
Expand All @@ -287,6 +293,7 @@ public function getEntityManager()
* Gets the state of this query builder instance.
*
* @return int Either QueryBuilder::STATE_DIRTY or QueryBuilder::STATE_CLEAN.
* @psalm-return self::STATE_*
*/
public function getState()
{
Expand Down
Loading

0 comments on commit 03d702d

Please sign in to comment.