Skip to content

Infer type for builder getData #371

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 1 commit into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions extension.neon
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ parameters:
featureToggles:
skipCheckGenericClasses:
- Symfony\Component\Form\AbstractType
- Symfony\Component\Form\FormBuilderInterface
- Symfony\Component\Form\FormConfigBuilderInterface
- Symfony\Component\Form\FormConfigInterface
- Symfony\Component\Form\FormInterface
- Symfony\Component\Form\FormTypeExtensionInterface
- Symfony\Component\Form\FormTypeInterface
Expand Down Expand Up @@ -47,6 +50,8 @@ parameters:
- stubs/Symfony/Component/Form/Exception/TransformationFailedException.stub
- stubs/Symfony/Component/Form/DataTransformerInterface.stub
- stubs/Symfony/Component/Form/FormBuilderInterface.stub
- stubs/Symfony/Component/Form/FormConfigBuilderInterface.stub
- stubs/Symfony/Component/Form/FormConfigInterface.stub
- stubs/Symfony/Component/Form/FormInterface.stub
- stubs/Symfony/Component/Form/FormFactoryInterface.stub
- stubs/Symfony/Component/Form/FormTypeExtensionInterface.stub
Expand Down
1 change: 1 addition & 0 deletions stubs/Symfony/Component/Form/AbstractType.stub
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ abstract class AbstractType implements FormTypeInterface
{

/**
* @param FormBuilderInterface<TData|null> $builder
* @param array<string, mixed> $options
*/
public function buildForm(FormBuilderInterface $builder, array $options): void;
Expand Down
12 changes: 10 additions & 2 deletions stubs/Symfony/Component/Form/FormBuilderInterface.stub
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,17 @@
namespace Symfony\Component\Form;

/**
* @extends \Traversable<int, \Symfony\Component\Form\FormBuilderInterface>
* @template TData
*
* @extends \Traversable<int, \Symfony\Component\Form\FormBuilderInterface<mixed>>
* @extends FormConfigBuilderInterface<TData>
*/
interface FormBuilderInterface extends \Traversable
interface FormBuilderInterface extends \Traversable, \Countable, FormConfigBuilderInterface
{

/**
* @return FormInterface<TData|null>
*/
public function getForm(): FormInterface;

}
13 changes: 13 additions & 0 deletions stubs/Symfony/Component/Form/FormConfigBuilderInterface.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Symfony\Component\Form;

/**
* @template TData
*
* @extends FormConfigInterface<TData>
*/
interface FormConfigBuilderInterface extends FormConfigInterface
{

}
16 changes: 16 additions & 0 deletions stubs/Symfony/Component/Form/FormConfigInterface.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Symfony\Component\Form;

/**
* @template TData
*/
interface FormConfigInterface
{

/**
* @return TData
*/
public function getData(): mixed;

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace Symfony\Component\Form;
interface FormTypeExtensionInterface
{
/**
* @param FormBuilderInterface<TData|null> $builder
* @param array<string, mixed> $options
*/
public function buildForm(FormBuilderInterface $builder, array $options): void;
Expand Down
1 change: 1 addition & 0 deletions stubs/Symfony/Component/Form/FormTypeInterface.stub
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace Symfony\Component\Form;
interface FormTypeInterface
{
/**
* @param FormBuilderInterface<TData|null> $builder
* @param array<string, mixed> $options
*/
public function buildForm(FormBuilderInterface $builder, array $options): void;
Expand Down
3 changes: 3 additions & 0 deletions tests/Type/Symfony/data/form_data_type.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ class DataClassType extends AbstractType

public function buildForm(FormBuilderInterface $builder, array $options): void
{
assertType('GenericFormDataType\DataClass|null', $builder->getData());
assertType('GenericFormDataType\DataClass|null', $builder->getForm()->getData());
Comment on lines +32 to +33
Copy link
Contributor Author

Choose a reason for hiding this comment

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


$builder
->add('foo', NumberType::class)
->add('bar', TextType::class)
Expand Down