Skip to content

feat: Support specifying type of data_class in forms #337

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

Merged
merged 10 commits into from
Apr 14, 2023
7 changes: 7 additions & 0 deletions extension.neon
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ parameters:
consoleApplicationLoader: null
featureToggles:
skipCheckGenericClasses:
- Symfony\Component\Form\AbstractType
- Symfony\Component\Form\FormInterface
- Symfony\Component\Form\FormTypeExtensionInterface
- Symfony\Component\Form\FormTypeInterface
- Symfony\Component\OptionsResolver\Options
- Symfony\Component\Security\Core\Authorization\Voter\Voter
- Symfony\Component\Security\Core\User\PasswordUpgraderInterface
Expand All @@ -34,13 +38,15 @@ parameters:
- stubs/Symfony/Component/EventDispatcher/EventDispatcherInterface.stub
- stubs/Symfony/Component/EventDispatcher/EventSubscriberInterface.stub
- stubs/Symfony/Component/EventDispatcher/GenericEvent.stub
- stubs/Symfony/Component/Form/AbstractType.stub
- stubs/Symfony/Component/Form/ChoiceList/Loader/ChoiceLoaderInterface.stub
- stubs/Symfony/Component/Form/Exception/ExceptionInterface.stub
- stubs/Symfony/Component/Form/Exception/RuntimeException.stub
- stubs/Symfony/Component/Form/Exception/TransformationFailedException.stub
- stubs/Symfony/Component/Form/DataTransformerInterface.stub
- stubs/Symfony/Component/Form/FormBuilderInterface.stub
- stubs/Symfony/Component/Form/FormInterface.stub
- stubs/Symfony/Component/Form/FormFactoryInterface.stub
- stubs/Symfony/Component/Form/FormTypeExtensionInterface.stub
- stubs/Symfony/Component/Form/FormTypeInterface.stub
- stubs/Symfony/Component/Form/FormView.stub
Expand All @@ -50,6 +56,7 @@ parameters:
- stubs/Symfony/Component/HttpFoundation/Session.stub
- stubs/Symfony/Component/Messenger/StampInterface.stub
- stubs/Symfony/Component/Messenger/Envelope.stub
- stubs/Symfony/Component/OptionsResolver/Exception/InvalidOptionsException.stub
- stubs/Symfony/Component/OptionsResolver/Options.stub
- stubs/Symfony/Component/Process/Process.stub
- stubs/Symfony/Component/PropertyAccess/PropertyPathInterface.stub
Expand Down
12 changes: 12 additions & 0 deletions stubs/Symfony/Component/Form/AbstractType.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Symfony\Component\Form;

/**
* @template TData
Copy link
Member

Choose a reason for hiding this comment

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

Every time a new class is made generic, the class name should be added here:

featureToggles:
skipCheckGenericClasses:
- Symfony\Component\OptionsResolver\Options
- Symfony\Component\Security\Core\Authorization\Voter\Voter
- Symfony\Component\Security\Core\User\PasswordUpgraderInterface
(to make sure users are not bothered by "Function foo() has parameter $hw with generic class HelloWorld but does not specify its types: T" errors.

This list will be made empty with the next major version.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added in da2cb98

*
* @implements FormTypeInterface<TData>
*/
abstract class AbstractType implements FormTypeInterface
{
}
36 changes: 36 additions & 0 deletions stubs/Symfony/Component/Form/FormFactoryInterface.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Symfony\Component\Form;

use Symfony\Component\Form\Extension\Core\Type\FormType;

interface FormFactoryInterface
{
/**
* @template TFormType of FormTypeInterface<TData>
* @template TData
*
* @param class-string<TFormType> $type
* @param TData $data
* @param array<string, mixed> $options
*
* @phpstan-return ($data is null ? FormInterface<null|TData> : FormInterface<TData>)
Copy link
Member

Choose a reason for hiding this comment

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

These complex generics should be tested with TypeInferenceTestCase. To make sure they behave like you expect them to.

*
* @throws \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
*/
public function create(string $type = FormType::class, $data = null, array $options = []): FormInterface;

/**
* @template TFormType of FormTypeInterface<TData>
* @template TData
*
* @param class-string<TFormType> $type
* @param TData $data
* @param array<string, mixed> $options
*
* @phpstan-return ($data is null ? FormInterface<null|TData> : FormInterface<TData>)
*
* @throws \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
*/
public function createNamed(string $name, string $type = FormType::class, $data = null, array $options = []): FormInterface;
}
16 changes: 14 additions & 2 deletions stubs/Symfony/Component/Form/FormInterface.stub
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,22 @@
namespace Symfony\Component\Form;

/**
* @extends \ArrayAccess<string, \Symfony\Component\Form\FormInterface>
* @extends \Traversable<string, \Symfony\Component\Form\FormInterface>
* @template TData
*
* @extends \ArrayAccess<string, \Symfony\Component\Form\FormInterface<mixed>>
* @extends \Traversable<string, \Symfony\Component\Form\FormInterface<mixed>>
*/
interface FormInterface extends \ArrayAccess, \Traversable, \Countable
{
/**
* @param TData $modelData
*
* @return $this
*/
public function setData($modelData): FormInterface;

/**
* @return TData
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

I see. So it means psalm should improve his typing ^^

*/
public function getData();
}
5 changes: 5 additions & 0 deletions stubs/Symfony/Component/Form/FormTypeExtensionInterface.stub
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Symfony\Component\Form;

/**
* @template TData
*/
interface FormTypeExtensionInterface
{
/**
Expand All @@ -10,11 +13,13 @@ interface FormTypeExtensionInterface
public function buildForm(FormBuilderInterface $builder, array $options): void;

/**
* @phpstan-param FormInterface<TData> $form
* @param array<string, mixed> $options
*/
public function buildView(FormView $view, FormInterface $form, array $options): void;

/**
* @phpstan-param FormInterface<TData> $form
* @param array<string, mixed> $options
*/
public function finishView(FormView $view, FormInterface $form, array $options): void;
Expand Down
5 changes: 5 additions & 0 deletions stubs/Symfony/Component/Form/FormTypeInterface.stub
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Symfony\Component\Form;

/**
* @template TData
*/
interface FormTypeInterface
{
/**
Expand All @@ -10,11 +13,13 @@ interface FormTypeInterface
public function buildForm(FormBuilderInterface $builder, array $options): void;

/**
* @phpstan-param FormInterface<TData> $form
* @param array<string, mixed> $options
*/
public function buildView(FormView $view, FormInterface $form, array $options): void;

/**
* @phpstan-param FormInterface<TData> $form
* @param array<string, mixed> $options
*/
public function finishView(FormView $view, FormInterface $form, array $options): void;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Symfony\Component\OptionsResolver\Exception;

class InvalidOptionsException extends \InvalidArgumentException
{
}
1 change: 1 addition & 0 deletions tests/Type/Symfony/ExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public function dataFileAsserts(): iterable

yield from $this->gatherAssertTypes(__DIR__ . '/data/FormInterface_getErrors.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/cache.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/form_data_type.php');
}

/**
Expand Down
72 changes: 72 additions & 0 deletions tests/Type/Symfony/data/form_data_type.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php declare(strict_types = 1);

namespace GenericFormDataType;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\NumberType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use function PHPStan\Testing\assertType;

class DataClass
{

/** @var int */
public $foo;

/** @var string */
public $bar;

}

/**
* @extends AbstractType<DataClass>
*/
class DataClassType extends AbstractType
{

public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('foo', NumberType::class)
->add('bar', TextType::class)
;
}

public function configureOptions(OptionsResolver $resolver): void
{
$resolver
->setDefaults([
'data_class' => DataClass::class,
])
;
}

}

class FormFactoryAwareClass
{

/** @var FormFactoryInterface */
private $formFactory;

public function __construct(FormFactoryInterface $formFactory)
{
$this->formFactory = $formFactory;
}

public function doSomething(): void
{
$form = $this->formFactory->create(DataClassType::class, new DataClass());
assertType('GenericFormDataType\DataClass', $form->getData());
}

public function doSomethingNullable(): void
{
$form = $this->formFactory->create(DataClassType::class);
assertType('GenericFormDataType\DataClass|null', $form->getData());
}

}