From b784e8f32f98af86bf573ddeeb7864661d1e7c7b Mon Sep 17 00:00:00 2001 From: jan-smrz <126762477+xNetSsx@users.noreply.github.com> Date: Tue, 1 Dec 2026 16:58:54 +0100 Subject: [PATCH] Support Symfony 8.2 #[AsFormType] classes as form types Co-Authored-By: Claude Opus 5.5 --- extension.neon | 13 ++ phpstan-baseline.neon | 6 + .../AsFormTypeDynamicReturnTypeExtension.php | 59 ++++++ src/Type/Symfony/Form/AsFormTypeHelper.php | 101 +++++++++++ .../Form/AsFormTypeIgnoreErrorExtension.php | 41 +++++ .../AsFormTypeIgnoreErrorExtensionTest.php | 82 +++++++++ tests/Type/Symfony/ExtensionTest.php | 5 + tests/Type/Symfony/data/form_as_form_type.php | 171 ++++++++++++++++++ 8 files changed, 478 insertions(+) create mode 100644 src/Type/Symfony/Form/AsFormTypeDynamicReturnTypeExtension.php create mode 100644 src/Type/Symfony/Form/AsFormTypeHelper.php create mode 100644 src/Type/Symfony/Form/AsFormTypeIgnoreErrorExtension.php create mode 100644 tests/Type/Symfony/AsFormTypeIgnoreErrorExtensionTest.php create mode 100644 tests/Type/Symfony/data/form_as_form_type.php diff --git a/extension.neon b/extension.neon index 04b4bdca..dae2228f 100644 --- a/extension.neon +++ b/extension.neon @@ -278,6 +278,19 @@ services: factory: PHPStan\Type\Symfony\Form\FormInterfaceDynamicReturnTypeExtension tags: [phpstan.broker.dynamicMethodReturnTypeExtension] + # #[AsFormType] classes as form types + - + class: PHPStan\Type\Symfony\Form\AsFormTypeHelper + - + factory: PHPStan\Type\Symfony\Form\AsFormTypeDynamicReturnTypeExtension(Symfony\Component\Form\FormFactoryInterface) + tags: [phpstan.broker.dynamicMethodReturnTypeExtension] + - + factory: PHPStan\Type\Symfony\Form\AsFormTypeDynamicReturnTypeExtension(Symfony\Bundle\FrameworkBundle\Controller\AbstractController) + tags: [phpstan.broker.dynamicMethodReturnTypeExtension] + - + class: PHPStan\Type\Symfony\Form\AsFormTypeIgnoreErrorExtension + tags: [phpstan.ignoreErrorExtension] + # Command::getHelper() return type - factory: PHPStan\Type\Symfony\CommandGetHelperDynamicReturnTypeExtension diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 4c2c7c96..927785e2 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -72,6 +72,12 @@ parameters: count: 1 path: tests/Symfony/RequiredAutowiringExtensionTest.php + - + message: '#^Accessing PHPStan\\Rules\\Methods\\CallMethodsRule\:\:class is not covered by backward compatibility promise\. The class might change in a minor PHPStan version\.$#' + identifier: phpstanApi.classConstant + count: 1 + path: tests/Type/Symfony/AsFormTypeIgnoreErrorExtensionTest.php + - message: '#^Accessing PHPStan\\Rules\\Comparison\\ImpossibleCheckTypeMethodCallRule\:\:class is not covered by backward compatibility promise\. The class might change in a minor PHPStan version\.$#' identifier: phpstanApi.classConstant diff --git a/src/Type/Symfony/Form/AsFormTypeDynamicReturnTypeExtension.php b/src/Type/Symfony/Form/AsFormTypeDynamicReturnTypeExtension.php new file mode 100644 index 00000000..4488c370 --- /dev/null +++ b/src/Type/Symfony/Form/AsFormTypeDynamicReturnTypeExtension.php @@ -0,0 +1,59 @@ +className = $className; + $this->helper = $helper; + } + + public function getClass(): string + { + return $this->className; + } + + public function isMethodSupported(MethodReflection $methodReflection): bool + { + return isset(AsFormTypeHelper::TYPE_ARGUMENT_POSITIONS[$methodReflection->getName()]); + } + + public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): ?Type + { + $dataClassType = $this->helper->findDataClassType($methodCall, $scope); + if ($dataClassType === null) { + return null; + } + + $returnClass = strpos($methodReflection->getName(), 'Builder') !== false + ? 'Symfony\Component\Form\FormBuilderInterface' + : 'Symfony\Component\Form\FormInterface'; + + if (!$this->helper->getDataType($methodCall, $scope)->isNull()->no()) { + $dataClassType = TypeCombinator::addNull($dataClassType); + } + + return new GenericObjectType($returnClass, [$dataClassType]); + } + +} diff --git a/src/Type/Symfony/Form/AsFormTypeHelper.php b/src/Type/Symfony/Form/AsFormTypeHelper.php new file mode 100644 index 00000000..3fc9b34a --- /dev/null +++ b/src/Type/Symfony/Form/AsFormTypeHelper.php @@ -0,0 +1,101 @@ + 0, + 'createNamed' => 1, + 'createBuilder' => 0, + 'createNamedBuilder' => 1, + 'createForm' => 0, + ]; + + private ReflectionProvider $reflectionProvider; + + public function __construct(ReflectionProvider $reflectionProvider) + { + $this->reflectionProvider = $reflectionProvider; + } + + public function findDataClassType(MethodCall $methodCall, Scope $scope): ?Type + { + $typeArg = $this->findArgument($methodCall, 'type', 0); + if ($typeArg === null) { + return null; + } + + $calledOnType = $scope->getType($methodCall->var); + if ( + !(new ObjectType('Symfony\Component\Form\FormFactoryInterface'))->isSuperTypeOf($calledOnType)->yes() + && !(new ObjectType('Symfony\Bundle\FrameworkBundle\Controller\AbstractController'))->isSuperTypeOf($calledOnType)->yes() + ) { + return null; + } + + $dataClassType = $scope->getType($typeArg->value)->getClassStringObjectType(); + $classReflections = $dataClassType->getObjectClassReflections(); + if ($classReflections === []) { + return null; + } + + foreach ($classReflections as $classReflection) { + if (!$this->hasAsFormTypeAttribute($classReflection)) { + return null; + } + } + + return $dataClassType; + } + + public function getDataType(MethodCall $methodCall, Scope $scope): Type + { + $dataArg = $this->findArgument($methodCall, 'data', 1); + + return $dataArg !== null ? $scope->getType($dataArg->value) : new NullType(); + } + + private function findArgument(MethodCall $methodCall, string $name, int $offset): ?Arg + { + if (!$methodCall->name instanceof Identifier || !isset(self::TYPE_ARGUMENT_POSITIONS[$methodCall->name->name])) { + return null; + } + + $position = self::TYPE_ARGUMENT_POSITIONS[$methodCall->name->name] + $offset; + foreach ($methodCall->getArgs() as $i => $arg) { + if ($arg->name !== null ? $arg->name->name === $name : $i === $position) { + return $arg; + } + } + + return null; + } + + private function hasAsFormTypeAttribute(ClassReflection $classReflection): bool + { + foreach ($classReflection->getAttributes() as $attribute) { + if (!$this->reflectionProvider->hasClass($attribute->getName())) { + continue; + } + + if ($this->reflectionProvider->getClass($attribute->getName())->is('Symfony\Component\Form\Attribute\AsFormType')) { + return true; + } + } + + return false; + } + +} diff --git a/src/Type/Symfony/Form/AsFormTypeIgnoreErrorExtension.php b/src/Type/Symfony/Form/AsFormTypeIgnoreErrorExtension.php new file mode 100644 index 00000000..b89b5eda --- /dev/null +++ b/src/Type/Symfony/Form/AsFormTypeIgnoreErrorExtension.php @@ -0,0 +1,41 @@ +helper = $helper; + } + + public function shouldIgnore(Error $error, Node $node, Scope $scope): bool + { + if ($error->getIdentifier() !== 'argument.type' || !$node instanceof MethodCall) { + return false; + } + + $dataClassType = $this->helper->findDataClassType($node, $scope); + if ($dataClassType === null) { + return false; + } + + if (TypeCombinator::addNull($dataClassType)->accepts($this->helper->getDataType($node, $scope), true)->no()) { + return false; + } + + return strpos($error->getMessage(), '$type of method') !== false; + } + +} diff --git a/tests/Type/Symfony/AsFormTypeIgnoreErrorExtensionTest.php b/tests/Type/Symfony/AsFormTypeIgnoreErrorExtensionTest.php new file mode 100644 index 00000000..f2add0f5 --- /dev/null +++ b/tests/Type/Symfony/AsFormTypeIgnoreErrorExtensionTest.php @@ -0,0 +1,82 @@ + + */ +class AsFormTypeIgnoreErrorExtensionTest extends RuleTestCase +{ + + protected function getRule(): Rule + { + return self::getContainer()->getByType(CallMethodsRule::class); + } + + public function testRule(): void + { + if (PHP_VERSION_ID < 80000) { + self::markTestSkipped('Test requires PHP 8.0.'); + } + + $this->analyse([__DIR__ . '/data/form_as_form_type.php'], [ + [ + 'Parameter #1 $type of method Symfony\Component\Form\FormFactoryInterface::create() expects class-string>, \'FormAsFormType\\\\ContactDto\' given.', + 140, + ], + [ + 'Parameter #3 $options of method Symfony\Component\Form\FormFactoryInterface::create() expects array, string given.', + 141, + ], + [ + 'Parameter #1 $name of method Symfony\Component\Form\FormFactoryInterface::createNamed() expects string, array given.', + 142, + ], + [ + 'Parameter #1 $type of method Symfony\Component\Form\FormFactoryInterface::create() expects class-string>, \'FormAsFormType\\\\ChildOfOpenDto\' given.', + 143, + ], + [ + 'Parameter #1 $type of method Symfony\Component\Form\FormFactoryInterface::create() expects class-string>, \'FormAsFormType\\\\OtherAttributeDto\' given.', + 144, + ], + [ + 'Parameter #1 $type of method Symfony\Component\Form\FormFactoryInterface::create() expects class-string>, \'FormAsFormType\\\\UnknownAttributeDto\' given.', + 145, + ], + [ + 'Parameter #1 $type of method Symfony\Component\Form\FormFactoryInterface::create() expects class-string>, \'FormAsFormType\\\\NotAFormType\' given.', + 146, + ], + [ + 'Parameter #1 $type of method Symfony\Component\Form\FormFactoryInterface::create() expects class-string>, \'FormAsFormType\\\\ContactDto\'|\'FormAsFormType\\\\NotAFormType\' given.', + 147, + ], + [ + 'Parameter #1 $type of method FormAsFormType\OtherFactory::create() expects class-string>, \'FormAsFormType\\\\ContactDto\' given.', + 148, + ], + [ + 'Parameter #1 $type of method Symfony\Bundle\FrameworkBundle\Controller\AbstractController::createForm() expects class-string>, \'FormAsFormType\\\\NotAFormType\' given.', + 161, + ], + [ + 'Cannot call method undefinedMethod() on FormAsFormType\ContactDto|null.', + 166, + ], + ]); + } + + public static function getAdditionalConfigFiles(): array + { + return [ + __DIR__ . '/../../../extension.neon', + ]; + } + +} diff --git a/tests/Type/Symfony/ExtensionTest.php b/tests/Type/Symfony/ExtensionTest.php index 84b28bb4..dfdd89bd 100644 --- a/tests/Type/Symfony/ExtensionTest.php +++ b/tests/Type/Symfony/ExtensionTest.php @@ -8,6 +8,7 @@ use function class_exists; use function interface_exists; use function strpos; +use const PHP_VERSION_ID; class ExtensionTest extends TypeInferenceTestCase { @@ -69,6 +70,10 @@ public function dataFileAsserts(): iterable yield from $this->gatherAssertTypes(__DIR__ . '/data/cache.php'); yield from $this->gatherAssertTypes(__DIR__ . '/data/form_data_type.php'); + if (PHP_VERSION_ID >= 80000) { + yield from $this->gatherAssertTypes(__DIR__ . '/data/form_as_form_type.php'); + } + yield from $this->gatherAssertTypes(__DIR__ . '/data/extension/with-configuration/WithConfigurationExtension.php'); yield from $this->gatherAssertTypes(__DIR__ . '/data/extension/without-configuration/WithoutConfigurationExtension.php'); yield from $this->gatherAssertTypes(__DIR__ . '/data/extension/anonymous/AnonymousExtension.php'); diff --git a/tests/Type/Symfony/data/form_as_form_type.php b/tests/Type/Symfony/data/form_as_form_type.php new file mode 100644 index 00000000..5677287b --- /dev/null +++ b/tests/Type/Symfony/data/form_as_form_type.php @@ -0,0 +1,171 @@ += 8.0 + +namespace Symfony\Component\Form\Attribute { + + use Attribute; + + #[Attribute(Attribute::TARGET_CLASS)] + class AsFormType + { + + } + +} + +namespace FormAsFormType { + + use Attribute; + use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; + use Symfony\Component\Form\AbstractType; + use Symfony\Component\Form\Attribute\AsFormType; + use Symfony\Component\Form\FormFactory; + use Symfony\Component\Form\FormFactoryInterface; + use Symfony\Component\Form\FormTypeInterface; + use function PHPStan\Testing\assertType; + + class Data + { + + } + + /** + * @extends AbstractType + */ + class DataType extends AbstractType + { + + } + + #[AsFormType] + final class ContactDto + { + + } + + #[AsFormType] + class OpenDto + { + + } + + class ChildOfOpenDto extends OpenDto + { + + } + + #[Attribute(Attribute::TARGET_CLASS)] + class CustomAsFormType extends AsFormType + { + + } + + #[CustomAsFormType] + final class CustomAttributeDto + { + + } + + #[Attribute(Attribute::TARGET_CLASS)] + class OtherAttribute + { + + } + + #[OtherAttribute] + final class OtherAttributeDto + { + + } + + #[UnknownAttribute] + final class UnknownAttributeDto + { + + } + + class NotAFormType + { + + } + + interface OtherFactory + { + + /** + * @param class-string> $type + */ + public function create(string $type): string; + + } + + class FormFactoryAwareClass + { + + /** + * @param class-string $openDtoClass + */ + public function doFoo( + FormFactoryInterface $formFactory, + FormFactory $concreteFormFactory, + OtherFactory $otherFactory, + string $openDtoClass, + ?ContactDto $nullableDto, + object $object, + mixed $mixed, + bool $bool + ): void + { + assertType('Symfony\Component\Form\FormInterface', $formFactory->create(ContactDto::class)); + assertType('Symfony\Component\Form\FormInterface', $formFactory->create(ContactDto::class, null)); + assertType('Symfony\Component\Form\FormInterface', $formFactory->create(ContactDto::class, new ContactDto())); + assertType('Symfony\Component\Form\FormInterface', $formFactory->create(ContactDto::class, $nullableDto)); + assertType('Symfony\Component\Form\FormInterface', $formFactory->create(ContactDto::class, $object)); + assertType('Symfony\Component\Form\FormInterface', $formFactory->create(ContactDto::class, $mixed)); + assertType('Symfony\Component\Form\FormInterface', $formFactory->create(type: ContactDto::class)); + assertType('Symfony\Component\Form\FormInterface', $formFactory->create(data: new ContactDto(), type: ContactDto::class)); + assertType('Symfony\Component\Form\FormInterface', $formFactory->createNamed('name', ContactDto::class)); + assertType('Symfony\Component\Form\FormInterface', $formFactory->createNamed('name', ContactDto::class, new ContactDto())); + assertType('Symfony\Component\Form\FormBuilderInterface', $formFactory->createBuilder(ContactDto::class)); + assertType('Symfony\Component\Form\FormBuilderInterface', $formFactory->createBuilder(ContactDto::class, new ContactDto())); + assertType('Symfony\Component\Form\FormBuilderInterface', $formFactory->createNamedBuilder('name', ContactDto::class)); + assertType('Symfony\Component\Form\FormBuilderInterface', $formFactory->createNamedBuilder('name', ContactDto::class, new ContactDto())); + assertType('Symfony\Component\Form\FormInterface', $concreteFormFactory->create(ContactDto::class)); + assertType('Symfony\Component\Form\FormInterface', $formFactory->create(OpenDto::class)); + assertType('Symfony\Component\Form\FormInterface', $formFactory->create(OpenDto::class, new ChildOfOpenDto())); + assertType('Symfony\Component\Form\FormInterface', $formFactory->create($openDtoClass)); + assertType('Symfony\Component\Form\FormInterface', $formFactory->create($bool ? ContactDto::class : OpenDto::class)); + assertType('Symfony\Component\Form\FormInterface', $formFactory->create(CustomAttributeDto::class)); + assertType('Symfony\Component\Form\FormInterface', $formFactory->create(DataType::class)); + assertType('Symfony\Component\Form\FormInterface', $formFactory->create(DataType::class, new Data())); + assertType('Symfony\Component\Form\FormInterface', $formFactory->create(ContactDto::class, new Data())); + assertType('Symfony\Component\Form\FormInterface', $formFactory->create(ContactDto::class, null, 'options')); + assertType('Symfony\Component\Form\FormInterface', $formFactory->createNamed([], ContactDto::class)); + assertType('Symfony\Component\Form\FormInterface', $formFactory->create(ChildOfOpenDto::class)); + assertType('Symfony\Component\Form\FormInterface', $formFactory->create(OtherAttributeDto::class)); + assertType('Symfony\Component\Form\FormInterface', $formFactory->create(UnknownAttributeDto::class)); + assertType('Symfony\Component\Form\FormInterface', $formFactory->create(NotAFormType::class)); + assertType('Symfony\Component\Form\FormInterface', $formFactory->create($bool ? ContactDto::class : NotAFormType::class)); + assertType('string', $otherFactory->create(ContactDto::class)); + } + + } + + class FormController extends AbstractController + { + + public function doFoo(): void + { + assertType('Symfony\Component\Form\FormInterface', $this->createForm(ContactDto::class)); + assertType('Symfony\Component\Form\FormInterface', $this->createForm(ContactDto::class, new ContactDto())); + assertType('Symfony\Component\Form\FormInterface', $this->createForm(DataType::class)); + assertType('Symfony\Component\Form\FormInterface', $this->createForm(NotAFormType::class)); + } + + public function doBar(FormFactoryInterface $formFactory, ?ContactDto $nullableDto): void + { + $formFactory->create(ContactDto::class, $nullableDto)->getData()->undefinedMethod(); + } + + } + +}