Skip to content

Commit ef7db63

Browse files
VincentLangletondrejmirtes
authored andcommitted
Infer type for builder getData
1 parent 34b3c43 commit ef7db63

8 files changed

+50
-2
lines changed

extension.neon

+5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ parameters:
1414
featureToggles:
1515
skipCheckGenericClasses:
1616
- Symfony\Component\Form\AbstractType
17+
- Symfony\Component\Form\FormBuilderInterface
18+
- Symfony\Component\Form\FormConfigBuilderInterface
19+
- Symfony\Component\Form\FormConfigInterface
1720
- Symfony\Component\Form\FormInterface
1821
- Symfony\Component\Form\FormTypeExtensionInterface
1922
- Symfony\Component\Form\FormTypeInterface
@@ -47,6 +50,8 @@ parameters:
4750
- stubs/Symfony/Component/Form/Exception/TransformationFailedException.stub
4851
- stubs/Symfony/Component/Form/DataTransformerInterface.stub
4952
- stubs/Symfony/Component/Form/FormBuilderInterface.stub
53+
- stubs/Symfony/Component/Form/FormConfigBuilderInterface.stub
54+
- stubs/Symfony/Component/Form/FormConfigInterface.stub
5055
- stubs/Symfony/Component/Form/FormInterface.stub
5156
- stubs/Symfony/Component/Form/FormFactoryInterface.stub
5257
- stubs/Symfony/Component/Form/FormTypeExtensionInterface.stub

stubs/Symfony/Component/Form/AbstractType.stub

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ abstract class AbstractType implements FormTypeInterface
1111
{
1212

1313
/**
14+
* @param FormBuilderInterface<TData|null> $builder
1415
* @param array<string, mixed> $options
1516
*/
1617
public function buildForm(FormBuilderInterface $builder, array $options): void;

stubs/Symfony/Component/Form/FormBuilderInterface.stub

+10-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,17 @@
33
namespace Symfony\Component\Form;
44

55
/**
6-
* @extends \Traversable<int, \Symfony\Component\Form\FormBuilderInterface>
6+
* @template TData
7+
*
8+
* @extends \Traversable<int, \Symfony\Component\Form\FormBuilderInterface<mixed>>
9+
* @extends FormConfigBuilderInterface<TData>
710
*/
8-
interface FormBuilderInterface extends \Traversable
11+
interface FormBuilderInterface extends \Traversable, \Countable, FormConfigBuilderInterface
912
{
1013

14+
/**
15+
* @return FormInterface<TData|null>
16+
*/
17+
public function getForm(): FormInterface;
18+
1119
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Symfony\Component\Form;
4+
5+
/**
6+
* @template TData
7+
*
8+
* @extends FormConfigInterface<TData>
9+
*/
10+
interface FormConfigBuilderInterface extends FormConfigInterface
11+
{
12+
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Symfony\Component\Form;
4+
5+
/**
6+
* @template TData
7+
*/
8+
interface FormConfigInterface
9+
{
10+
11+
/**
12+
* @return TData
13+
*/
14+
public function getData(): mixed;
15+
16+
}

stubs/Symfony/Component/Form/FormTypeExtensionInterface.stub

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ namespace Symfony\Component\Form;
88
interface FormTypeExtensionInterface
99
{
1010
/**
11+
* @param FormBuilderInterface<TData|null> $builder
1112
* @param array<string, mixed> $options
1213
*/
1314
public function buildForm(FormBuilderInterface $builder, array $options): void;

stubs/Symfony/Component/Form/FormTypeInterface.stub

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ namespace Symfony\Component\Form;
88
interface FormTypeInterface
99
{
1010
/**
11+
* @param FormBuilderInterface<TData|null> $builder
1112
* @param array<string, mixed> $options
1213
*/
1314
public function buildForm(FormBuilderInterface $builder, array $options): void;

tests/Type/Symfony/data/form_data_type.php

+3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ class DataClassType extends AbstractType
2929

3030
public function buildForm(FormBuilderInterface $builder, array $options): void
3131
{
32+
assertType('GenericFormDataType\DataClass|null', $builder->getData());
33+
assertType('GenericFormDataType\DataClass|null', $builder->getForm()->getData());
34+
3235
$builder
3336
->add('foo', NumberType::class)
3437
->add('bar', TextType::class)

0 commit comments

Comments
 (0)