From 64a24514c0a55b2933615f704b99311983d00959 Mon Sep 17 00:00:00 2001 From: "Alexander M. Turek" Date: Thu, 31 Mar 2022 17:50:32 +0200 Subject: [PATCH] Fix types on ResultSetMapping --- lib/Doctrine/ORM/Query/ResultSetMapping.php | 27 +++++++------- .../ORM/Query/ResultSetMappingBuilder.php | 23 ++++++++++-- lib/Doctrine/ORM/Query/SqlWalker.php | 2 +- phpstan-baseline.neon | 5 --- psalm-baseline.xml | 36 ++----------------- 5 files changed, 38 insertions(+), 55 deletions(-) diff --git a/lib/Doctrine/ORM/Query/ResultSetMapping.php b/lib/Doctrine/ORM/Query/ResultSetMapping.php index 66ea0c0aae..a258d84021 100644 --- a/lib/Doctrine/ORM/Query/ResultSetMapping.php +++ b/lib/Doctrine/ORM/Query/ResultSetMapping.php @@ -73,7 +73,7 @@ class ResultSetMapping * Maps column names in the result set to the alias/field name to use in the mapped result. * * @ignore - * @psalm-var array + * @psalm-var array */ public $scalarMappings = []; @@ -169,6 +169,7 @@ class ResultSetMapping * results or joined entity results within this ResultSetMapping. * @param string|null $resultAlias The result alias with which the entity result should be * placed in the result structure. + * @psalm-param class-string $class * * @return $this * @@ -315,6 +316,7 @@ public function isFieldResult($columnName) * the field $fieldName is defined on a subclass, specify that here. * If not specified, the field is assumed to belong to the class * designated by $alias. + * @psalm-param class-string|null $declaringClass * * @return $this * @@ -344,6 +346,7 @@ public function addFieldResult($alias, $columnName, $fieldName, $declaringClass * @param string $parentAlias The alias of the entity result that is the parent of this joined result. * @param string $relation The association field that connects the parent entity result * with the joined entity result. + * @psalm-param class-string $class * * @return $this * @@ -361,9 +364,9 @@ public function addJoinedEntityResult($class, $alias, $parentAlias, $relation) /** * Adds a scalar result mapping. * - * @param string $columnName The name of the column in the SQL result set. - * @param string $alias The result alias with which the scalar result should be placed in the result structure. - * @param string $type The column type + * @param string $columnName The name of the column in the SQL result set. + * @param string|int $alias The result alias with which the scalar result should be placed in the result structure. + * @param string $type The column type * * @return $this * @@ -384,8 +387,8 @@ public function addScalarResult($columnName, $alias, $type = 'string') /** * Adds a metadata parameter mappings. * - * @param mixed $parameter The parameter name in the SQL result set. - * @param string $attribute The metadata attribute. + * @param string|int $parameter The parameter name in the SQL result set. + * @param string $attribute The metadata attribute. * * @return void */ @@ -426,7 +429,7 @@ public function getClassName($alias) * * @param string $columnName The name of the column in the SQL result set. * - * @return string + * @return string|int */ public function getScalarAlias($columnName) { @@ -549,11 +552,11 @@ public function isMixedResult() /** * Adds a meta column (foreign key or discriminator column) to the result set. * - * @param string $alias The result alias with which the meta result should be placed in the result structure. - * @param string $columnName The name of the column in the SQL result set. - * @param string $fieldName The name of the field on the declaring class. - * @param bool $isIdentifierColumn - * @param string $type The column type + * @param string $alias The result alias with which the meta result should be placed in the result structure. + * @param string $columnName The name of the column in the SQL result set. + * @param string $fieldName The name of the field on the declaring class. + * @param bool $isIdentifierColumn + * @param string|null $type The column type * * @return $this * diff --git a/lib/Doctrine/ORM/Query/ResultSetMappingBuilder.php b/lib/Doctrine/ORM/Query/ResultSetMappingBuilder.php index 3021de1207..e762d255ae 100644 --- a/lib/Doctrine/ORM/Query/ResultSetMappingBuilder.php +++ b/lib/Doctrine/ORM/Query/ResultSetMappingBuilder.php @@ -11,7 +11,9 @@ use Doctrine\ORM\Mapping\MappingException; use Doctrine\ORM\Utility\PersisterHelper; use InvalidArgumentException; +use LogicException; +use function assert; use function explode; use function in_array; use function sprintf; @@ -57,11 +59,13 @@ class ResultSetMappingBuilder extends ResultSetMapping * Default column renaming mode. * * @var int + * @psalm-var self::COLUMN_RENAMING_* */ private $defaultRenameMode; /** * @param int $defaultRenameMode + * @psalm-param self::COLUMN_RENAMING_* $defaultRenameMode */ public function __construct(EntityManagerInterface $em, $defaultRenameMode = self::COLUMN_RENAMING_NONE) { @@ -76,7 +80,9 @@ public function __construct(EntityManagerInterface $em, $defaultRenameMode = sel * @param string $alias The unique alias to use for the root entity. * @param string[] $renamedColumns Columns that have been renamed (tableColumnName => queryColumnName). * @param int|null $renameMode One of the COLUMN_RENAMING_* constants or array for BC reasons (CUSTOM). + * @psalm-param class-string $class * @psalm-param array $renamedColumns + * @psalm-param self::COLUMN_RENAMING_*|null $renameMode * * @return void */ @@ -99,7 +105,9 @@ public function addRootEntityFromClassMetadata($class, $alias, $renamedColumns = * with the joined entity result. * @param string[] $renamedColumns Columns that have been renamed (tableColumnName => queryColumnName). * @param int|null $renameMode One of the COLUMN_RENAMING_* constants or array for BC reasons (CUSTOM). + * @psalm-param class-string $class * @psalm-param array $renamedColumns + * @psalm-param self::COLUMN_RENAMING_*|null $renameMode * * @return void */ @@ -186,6 +194,8 @@ private function isInheritanceSupported(ClassMetadataInfo $classMetadata): bool * Gets column alias for a given column. * * @psalm-param array $customRenameColumns + * + * @psalm-assert self::COLUMN_RENAMING_* $mode */ private function getColumnAlias(string $columnName, int $mode, array $customRenameColumns): string { @@ -273,8 +283,10 @@ public function addNamedNativeQueryMapping(ClassMetadataInfo $class, array $quer public function addNamedNativeQueryResultClassMapping(ClassMetadataInfo $class, $resultClassName) { $classMetadata = $this->em->getClassMetadata($resultClassName); - $shortName = $classMetadata->reflClass->getShortName(); - $alias = strtolower($shortName[0]) . '0'; + assert($classMetadata->reflClass !== null); + + $shortName = $classMetadata->reflClass->getShortName(); + $alias = strtolower($shortName[0]) . '0'; $this->addEntityResult($class->name, $alias); @@ -316,14 +328,19 @@ public function addNamedNativeQueryResultClassMapping(ClassMetadataInfo $class, */ public function addNamedNativeQueryResultSetMapping(ClassMetadataInfo $class, $resultSetMappingName) { + if ($class->reflClass === null) { + throw new LogicException('Given class metadata has now class reflector.'); + } + $counter = 0; $resultMapping = $class->getSqlResultSetMapping($resultSetMappingName); $rootShortName = $class->reflClass->getShortName(); $rootAlias = strtolower($rootShortName[0]) . $counter; if (isset($resultMapping['entities'])) { - foreach ($resultMapping['entities'] as $key => $entityMapping) { + foreach ($resultMapping['entities'] as $entityMapping) { $classMetadata = $this->em->getClassMetadata($entityMapping['entityClass']); + assert($classMetadata->reflClass !== null); if ($class->reflClass->name === $classMetadata->reflClass->name) { $this->addEntityResult($classMetadata->name, $rootAlias); diff --git a/lib/Doctrine/ORM/Query/SqlWalker.php b/lib/Doctrine/ORM/Query/SqlWalker.php index c93fa6f37f..87dffa24c5 100644 --- a/lib/Doctrine/ORM/Query/SqlWalker.php +++ b/lib/Doctrine/ORM/Query/SqlWalker.php @@ -111,7 +111,7 @@ class SqlWalker implements TreeWalker /** * Map from result variable names to their SQL column alias names. * - * @psalm-var array> + * @psalm-var array> */ private $scalarResultAliasMap = []; diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index cd8c7cf0c0..82a5fc3aef 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -960,11 +960,6 @@ parameters: count: 1 path: lib/Doctrine/ORM/Query/SqlWalker.php - - - message: "#^Parameter \\#2 \\$alias of method Doctrine\\\\ORM\\\\Query\\\\ResultSetMapping\\:\\:addScalarResult\\(\\) expects string, int given\\.$#" - count: 1 - path: lib/Doctrine/ORM/Query/SqlWalker.php - - message: "#^Result of && is always false\\.$#" count: 2 diff --git a/psalm-baseline.xml b/psalm-baseline.xml index f0f3b7c646..d1dc14ecfc 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -2367,29 +2367,10 @@ Comparison::EQ - - - $this->aliasMap - $this->aliasMap - $this->declaringClasses - - - - $class - $class + $class - $renameMode - $renameMode - - $classMetadata->reflClass->name - - - getShortName - getShortName - getShortName - @@ -2411,20 +2392,7 @@ walkConditionalPrimary - - $this->scalarResultAliasMap - $this->scalarResultAliasMap - $this->scalarResultAliasMap - $this->scalarResultAliasMap - $this->scalarResultAliasMap - $this->scalarResultAliasMap - $this->scalarResultAliasMap - - - $resultAlias - $resultAlias - $resultAlias - $resultAlias + $this->queryComponents[$expr]['token']['value'] $this->queryComponents[$factor]['token']['value'] $this->queryComponents[$term]['token']['value']