Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Idea how to infer most custom types without descriptors #617

Draft
wants to merge 1 commit into
base: 1.5.x
Choose a base branch
from
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 @@
$type = new MixedType();
}
} catch (DescriptorNotRegisteredException $e) {
if (TypeRegistry::has($typeName)) {

Check failure on line 2014 in src/Type/Doctrine/Query/QueryResultTypeWalker.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.3)

Static call to instance method Doctrine\DBAL\Types\TypeRegistry::has().

Check failure on line 2014 in src/Type/Doctrine/Query/QueryResultTypeWalker.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4)

Static call to instance method Doctrine\DBAL\Types\TypeRegistry::has().

Check failure on line 2014 in src/Type/Doctrine/Query/QueryResultTypeWalker.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0)

Static call to instance method Doctrine\DBAL\Types\TypeRegistry::has().

Check failure on line 2014 in src/Type/Doctrine/Query/QueryResultTypeWalker.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1)

Static call to instance method Doctrine\DBAL\Types\TypeRegistry::has().

Check failure on line 2014 in src/Type/Doctrine/Query/QueryResultTypeWalker.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2)

Static call to instance method Doctrine\DBAL\Types\TypeRegistry::has().

Check failure on line 2014 in src/Type/Doctrine/Query/QueryResultTypeWalker.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3)

Static call to instance method Doctrine\DBAL\Types\TypeRegistry::has().

Check failure on line 2014 in src/Type/Doctrine/Query/QueryResultTypeWalker.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4)

Static call to instance method Doctrine\DBAL\Types\TypeRegistry::has().

Check failure on line 2014 in src/Type/Doctrine/Query/QueryResultTypeWalker.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, composer require --dev doctrine/orm:^3.0 doctrine/dbal:^4.0 carbonphp/carbon-doctri...

Static call to instance method Doctrine\DBAL\Types\TypeRegistry::has().
return $this->autodetectDbalTypePhpValue(TypeRegistry::get($typeName));

Check failure on line 2015 in src/Type/Doctrine/Query/QueryResultTypeWalker.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.3)

Static call to instance method Doctrine\DBAL\Types\TypeRegistry::get().

Check failure on line 2015 in src/Type/Doctrine/Query/QueryResultTypeWalker.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4)

Static call to instance method Doctrine\DBAL\Types\TypeRegistry::get().

Check failure on line 2015 in src/Type/Doctrine/Query/QueryResultTypeWalker.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0)

Static call to instance method Doctrine\DBAL\Types\TypeRegistry::get().

Check failure on line 2015 in src/Type/Doctrine/Query/QueryResultTypeWalker.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1)

Static call to instance method Doctrine\DBAL\Types\TypeRegistry::get().

Check failure on line 2015 in src/Type/Doctrine/Query/QueryResultTypeWalker.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2)

Static call to instance method Doctrine\DBAL\Types\TypeRegistry::get().

Check failure on line 2015 in src/Type/Doctrine/Query/QueryResultTypeWalker.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3)

Static call to instance method Doctrine\DBAL\Types\TypeRegistry::get().

Check failure on line 2015 in src/Type/Doctrine/Query/QueryResultTypeWalker.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4)

Static call to instance method Doctrine\DBAL\Types\TypeRegistry::get().

Check failure on line 2015 in src/Type/Doctrine/Query/QueryResultTypeWalker.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, composer require --dev doctrine/orm:^3.0 doctrine/dbal:^4.0 carbonphp/carbon-doctri...

Static call to instance method Doctrine\DBAL\Types\TypeRegistry::get().
}

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

return $type;
return $type;
}

/** @param ?class-string<BackedEnum> $enumType */
Expand Down Expand Up @@ -2188,4 +2195,18 @@
});
}

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

Check failure on line 2200 in src/Type/Doctrine/Query/QueryResultTypeWalker.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.3)

Access to an undefined property PHPStan\Type\Doctrine\Query\QueryResultTypeWalker::$reflectionProvider.

Check failure on line 2200 in src/Type/Doctrine/Query/QueryResultTypeWalker.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4)

Access to an undefined property PHPStan\Type\Doctrine\Query\QueryResultTypeWalker::$reflectionProvider.

Check failure on line 2200 in src/Type/Doctrine/Query/QueryResultTypeWalker.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0)

Access to an undefined property PHPStan\Type\Doctrine\Query\QueryResultTypeWalker::$reflectionProvider.

Check failure on line 2200 in src/Type/Doctrine/Query/QueryResultTypeWalker.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1)

Access to an undefined property PHPStan\Type\Doctrine\Query\QueryResultTypeWalker::$reflectionProvider.

Check failure on line 2200 in src/Type/Doctrine/Query/QueryResultTypeWalker.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2)

Access to an undefined property PHPStan\Type\Doctrine\Query\QueryResultTypeWalker::$reflectionProvider.

Check failure on line 2200 in src/Type/Doctrine/Query/QueryResultTypeWalker.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3)

Access to an undefined property PHPStan\Type\Doctrine\Query\QueryResultTypeWalker::$reflectionProvider.

Check failure on line 2200 in src/Type/Doctrine/Query/QueryResultTypeWalker.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4)

Access to an undefined property PHPStan\Type\Doctrine\Query\QueryResultTypeWalker::$reflectionProvider.

Check failure on line 2200 in src/Type/Doctrine/Query/QueryResultTypeWalker.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, composer require --dev doctrine/orm:^3.0 doctrine/dbal:^4.0 carbonphp/carbon-doctri...

Access to an undefined property PHPStan\Type\Doctrine\Query\QueryResultTypeWalker::$reflectionProvider.
->getClass(get_class($dbalType))
->getNativeMethod('convertToPHPValue');

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

return TypeCombinator::removeNull($type);
}

}
Loading