Skip to content

Commit

Permalink
Solve phpstan deprecation
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet authored and ondrejmirtes committed Apr 5, 2023
1 parent 03741b4 commit c110e57
Showing 1 changed file with 30 additions and 18 deletions.
48 changes: 30 additions & 18 deletions src/Type/Doctrine/Query/QueryResultDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\Accessory\AccessoryArrayListType;
use PHPStan\Type\ArrayType;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\Doctrine\ObjectMetadataResolver;
use PHPStan\Type\DynamicMethodReturnTypeExtension;
Expand Down Expand Up @@ -209,19 +208,22 @@ static function (Type $type, callable $traverse) use ($objectManager): Type {

private function getScalarHydratedReturnType(Type $queryResultType): Type
{
if (!$queryResultType instanceof ArrayType) {
if (!$queryResultType->isArray()->yes()) {
return new ArrayType(new MixedType(), new MixedType());
}

$itemType = $queryResultType->getItemType();
$hasNoObject = (new ObjectWithoutClassType())->isSuperTypeOf($itemType)->no();
$hasNoArray = $itemType->isArray()->no();
foreach ($queryResultType->getArrays() as $arrayType) {
$itemType = $arrayType->getItemType();

if ($hasNoArray && $hasNoObject) {
return $queryResultType;
if (
!(new ObjectWithoutClassType())->isSuperTypeOf($itemType)->no()
|| !$itemType->isArray()->no()
) {
return new ArrayType(new MixedType(), new MixedType());
}
}

return new ArrayType(new MixedType(), new MixedType());
return $queryResultType;
}

private function getSimpleObjectHydratedReturnType(Type $queryResultType): Type
Expand All @@ -236,31 +238,41 @@ private function getSimpleObjectHydratedReturnType(Type $queryResultType): Type
private function getSingleScalarHydratedReturnType(Type $queryResultType): Type
{
$queryResultType = $this->getScalarHydratedReturnType($queryResultType);
if (!$queryResultType instanceof ConstantArrayType) {
if (!$queryResultType->isConstantArray()->yes()) {
return new MixedType();
}

$values = $queryResultType->getValueTypes();
if (count($values) !== 1) {
return new MixedType();
$types = [];
foreach ($queryResultType->getConstantArrays() as $constantArrayType) {
$values = $constantArrayType->getValueTypes();
if (count($values) !== 1) {
return new MixedType();
}

$types[] = $constantArrayType->getFirstIterableValueType();
}

return $queryResultType->getFirstIterableValueType();
return TypeCombinator::union(...$types);
}

private function getScalarColumnHydratedReturnType(Type $queryResultType): Type
{
$queryResultType = $this->getScalarHydratedReturnType($queryResultType);
if (!$queryResultType instanceof ConstantArrayType) {
if (!$queryResultType->isConstantArray()->yes()) {
return new MixedType();
}

$values = $queryResultType->getValueTypes();
if (count($values) !== 1) {
return new MixedType();
$types = [];
foreach ($queryResultType->getConstantArrays() as $constantArrayType) {
$values = $constantArrayType->getValueTypes();
if (count($values) !== 1) {
return new MixedType();
}

$types[] = $constantArrayType->getFirstIterableValueType();
}

return $queryResultType->getFirstIterableValueType();
return TypeCombinator::union(...$types);
}

private function originalReturnType(MethodReflection $methodReflection): Type
Expand Down

0 comments on commit c110e57

Please sign in to comment.