diff --git a/attribute-errors.neon b/attribute-errors.neon new file mode 100644 index 00000000..26463f90 --- /dev/null +++ b/attribute-errors.neon @@ -0,0 +1,6 @@ +parameters: + ignoreErrors: + - + message: "#^Call to an undefined method ReflectionClass::getAttributes\\(\\)\\.$#" + count: 1 + path: src/Type/Doctrine/ObjectMetadataResolver.php diff --git a/ignore-by-php-version.neon.php b/ignore-by-php-version.neon.php new file mode 100644 index 00000000..70da741e --- /dev/null +++ b/ignore-by-php-version.neon.php @@ -0,0 +1,15 @@ +load(__DIR__ . '/attribute-errors.neon')); +} + +$config['parameters']['phpVersion'] = PHP_VERSION_ID; + +return $config; diff --git a/phpstan.neon b/phpstan.neon index a8cde690..ba1e8811 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,6 +1,7 @@ includes: - extension.neon - rules.neon + - ignore-by-php-version.neon.php - vendor/phpstan/phpstan-strict-rules/rules.neon - vendor/phpstan/phpstan-phpunit/extension.neon - vendor/phpstan/phpstan-phpunit/rules.neon diff --git a/src/Type/Doctrine/ObjectMetadataResolver.php b/src/Type/Doctrine/ObjectMetadataResolver.php index 79cd1a56..e1406239 100644 --- a/src/Type/Doctrine/ObjectMetadataResolver.php +++ b/src/Type/Doctrine/ObjectMetadataResolver.php @@ -2,6 +2,7 @@ namespace PHPStan\Type\Doctrine; +use Doctrine\ORM\Mapping\Entity; use Doctrine\Persistence\ObjectManager; use PHPStan\Reflection\ReflectionProvider; use function is_file; @@ -97,6 +98,17 @@ public function getResolvedRepositoryClass(): string public function getRepositoryClass(string $className): string { + if (PHP_MAJOR_VERSION >= 8 && $this->reflectionProvider->hasClass($className)) { + $classReflection = $this->reflectionProvider->getClass($className)->getNativeReflection(); + $attribute = $classReflection->getAttributes(Entity::class)[0] ?? null; + if ($attribute !== null) { + $attributeInstance = $attribute->newInstance(); + if ($attributeInstance->repositoryClass !== null) { + return $attributeInstance->repositoryClass; + } + } + } + $objectManager = $this->getObjectManager(); if ($objectManager === null) { return $this->getResolvedRepositoryClass();