-
Notifications
You must be signed in to change notification settings - Fork 93
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
Changes from all commits
2f97c6a
7ac48f5
a18b756
88da905
da2cb98
dcf9b4f
b66084e
1a263bc
bad7dab
24e94dd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\Form; | ||
|
||
/** | ||
* @template TData | ||
* | ||
* @implements FormTypeInterface<TData> | ||
*/ | ||
abstract class AbstractType implements FormTypeInterface | ||
{ | ||
} |
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>) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't look where is the truth, but the Psalm template returns There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it will be null only if we omit a initial value of the form. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\OptionsResolver\Exception; | ||
|
||
class InvalidOptionsException extends \InvalidArgumentException | ||
{ | ||
} |
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()); | ||
} | ||
|
||
} |
There was a problem hiding this comment.
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:
phpstan-symfony/extension.neon
Lines 14 to 18 in 1da7bf4
This list will be made empty with the next major version.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added in da2cb98