Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion src/Type/Doctrine/Query/QueryResultTypeWalker.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
namespace PHPStan\Type\Doctrine\Query;

use BackedEnum;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\StringType as DbalStringType;
use Doctrine\DBAL\Types\Type as DbalType;
use Doctrine\DBAL\Types\TypeRegistry;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Query;
Expand All @@ -16,6 +18,7 @@
use PDO;
use PHPStan\Doctrine\Driver\DriverDetector;
use PHPStan\Php\PhpVersion;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\ShouldNotHappenException;
use PHPStan\TrinaryLogic;
use PHPStan\Type\Accessory\AccessoryNumericStringType;
Expand Down Expand Up @@ -2008,6 +2011,10 @@ private function resolveDoctrineType(string $typeName, ?string $enumType = null,
$type = new MixedType();
}
} catch (DescriptorNotRegisteredException $e) {
if (TypeRegistry::has($typeName)) {
return $this->autodetectDbalTypePhpValue(TypeRegistry::get($typeName));
}

if ($enumType !== null) {
$type = new ObjectType($enumType);
} else {
Expand All @@ -2019,7 +2026,7 @@ private function resolveDoctrineType(string $typeName, ?string $enumType = null,
$type = TypeCombinator::addNull($type);
}

return $type;
return $type;
}

/** @param ?class-string<BackedEnum> $enumType */
Expand Down Expand Up @@ -2188,4 +2195,18 @@ private function simpleFloatify(Type $type): Type
});
}

private function autodetectDbalTypePhpValue(DbalType $dbalType): Type
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basically the same thing as ReflectionDescriptor does. So the deafult behaviour could be the same as registering ReflectionDescriptor for all registered types.

{
$method = $this->reflectionProvider
->getClass(get_class($dbalType))
->getNativeMethod('convertToPHPValue');

$type = ParametersAcceptorSelector::selectFromTypes([
new MixedType(),
new ObjectType(AbstractPlatform::class),
], $method->getVariants(), false)->getReturnType();

return TypeCombinator::removeNull($type);
}

}