Skip to content

Commit acad9a3

Browse files
committed
chore(form): add better type information using phpdoc
Signed-off-by: azjezz <[email protected]>
1 parent 9e63e78 commit acad9a3

17 files changed

+139
-14
lines changed

AbstractType.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
use Symfony\Component\OptionsResolver\OptionsResolver;
1717

1818
/**
19+
* @template T
20+
*
21+
* @implements FormTypeInterface<T>
22+
*
1923
* @author Bernhard Schussek <[email protected]>
2024
*/
2125
abstract class AbstractType implements FormTypeInterface
@@ -36,20 +40,28 @@ public function configureOptions(OptionsResolver $resolver)
3640
}
3741

3842
/**
43+
* @param FormBuilderInterface<T> $builder
44+
*
3945
* @return void
4046
*/
4147
public function buildForm(FormBuilderInterface $builder, array $options)
4248
{
4349
}
4450

4551
/**
52+
* @param FormView<T> $form
53+
* @param FormInterface<T> $view
54+
*
4655
* @return void
4756
*/
4857
public function buildView(FormView $view, FormInterface $form, array $options)
4958
{
5059
}
5160

5261
/**
62+
* @param FormView<T> $form
63+
* @param FormInterface<T> $view
64+
*
5365
* @return void
5466
*/
5567
public function finishView(FormView $view, FormInterface $form, array $options)

AbstractTypeExtension.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
use Symfony\Component\OptionsResolver\OptionsResolver;
1515

1616
/**
17+
* @template T
18+
*
19+
* @implements FormTypeExtensionInterface<T>
20+
*
1721
* @author Bernhard Schussek <[email protected]>
1822
*/
1923
abstract class AbstractTypeExtension implements FormTypeExtensionInterface
@@ -22,14 +26,25 @@ public function configureOptions(OptionsResolver $resolver): void
2226
{
2327
}
2428

29+
/**
30+
* @param FormBuilderInterface<T> $builder
31+
*/
2532
public function buildForm(FormBuilderInterface $builder, array $options): void
2633
{
2734
}
2835

36+
/**
37+
* @param FormView<T> $form
38+
* @param FormInterface<T> $view
39+
*/
2940
public function buildView(FormView $view, FormInterface $form, array $options): void
3041
{
3142
}
3243

44+
/**
45+
* @param FormView<T> $form
46+
* @param FormInterface<T> $view
47+
*/
3348
public function finishView(FormView $view, FormInterface $form, array $options): void
3449
{
3550
}

Event/PostSetDataEvent.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
*
2020
* It can be used to modify a form depending on the populated data (adding or
2121
* removing fields dynamically).
22+
*
23+
* @template T
24+
*
25+
* @extends FormEvent<T>
2226
*/
2327
final class PostSetDataEvent extends FormEvent
2428
{

Event/PostSubmitEvent.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
* once the model and view data have been denormalized.
2020
*
2121
* It can be used to fetch data after denormalization.
22+
*
23+
* @template T
24+
*
25+
* @extends FormEvent<T>
2226
*/
2327
final class PostSubmitEvent extends FormEvent
2428
{

Event/PreSetDataEvent.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
* This event is dispatched at the beginning of the Form::setData() method.
1818
*
1919
* It can be used to modify the data given during pre-population.
20+
*
21+
* @template T
22+
*
23+
* @extends FormEvent<T>
2024
*/
2125
final class PreSetDataEvent extends FormEvent
2226
{

Event/PreSubmitEvent.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
* It can be used to:
2020
* - Change data from the request, before submitting the data to the form.
2121
* - Add or remove form fields, before submitting the data to the form.
22+
*
23+
* @extends FormEvent<array<string, mixed>>
2224
*/
2325
final class PreSubmitEvent extends FormEvent
2426
{

Event/SubmitEvent.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
* transforms back the normalized data to the model and view data.
1919
*
2020
* It can be used to change data from the normalized representation of the data.
21+
*
22+
* @template T
23+
*
24+
* @extends FormEvent<T>
2125
*/
2226
final class SubmitEvent extends FormEvent
2327
{

Form.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,10 @@
6868
* @author Fabien Potencier <[email protected]>
6969
* @author Bernhard Schussek <[email protected]>
7070
*
71+
* @template T
72+
*
7173
* @implements \IteratorAggregate<string, FormInterface>
74+
* @implements FormInterface<T>
7275
*/
7376
#[AssertForm]
7477
#[Traverse(false)]

FormBuilderInterface.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
/**
1515
* @author Bernhard Schussek <[email protected]>
1616
*
17+
* @template T
18+
*
19+
* @extends FormConfigBuilderInterface<T>
1720
* @extends \Traversable<string, FormBuilderInterface>
1821
*/
1922
interface FormBuilderInterface extends \Traversable, \Countable, FormConfigBuilderInterface
@@ -32,9 +35,9 @@ public function add(string|self $child, ?string $type = null, array $options = [
3235
/**
3336
* Creates a form builder.
3437
*
35-
* @param string $name The name of the form or the name of the property
36-
* @param string|null $type The type of the form or null if name is a property
37-
* @param array<string, mixed> $options
38+
* @param string $name The name of the form or the name of the property
39+
* @param class-string<FormTypeInterface>|null $type The type of the form or null if name is a property
40+
* @param array<string, mixed> $options
3841
*/
3942
public function create(string $name, ?string $type = null, array $options = []): self;
4043

@@ -64,6 +67,8 @@ public function all(): array;
6467

6568
/**
6669
* Creates the form.
70+
*
71+
* @return FormInterface<T>
6772
*/
6873
public function getForm(): FormInterface;
6974
}

FormConfigBuilderInterface.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,26 @@
1313

1414
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
1515
use Symfony\Component\PropertyAccess\PropertyPathInterface;
16+
use Symfony\Component\Form\Event\PreSetDataEvent;
17+
use Symfony\Component\Form\Event\PreSubmitEvent;
18+
use Symfony\Component\Form\Event\PostSetDataEvent;
19+
use Symfony\Component\Form\Event\SubmitEvent;
20+
use Symfony\Component\Form\Event\PostSubmitEvent;
1621

1722
/**
23+
* @template T
24+
*
25+
* @extends FormConfigInterface<T>
26+
*
1827
* @author Bernhard Schussek <[email protected]>
1928
*/
2029
interface FormConfigBuilderInterface extends FormConfigInterface
2130
{
2231
/**
2332
* Adds an event listener to an event on this form.
2433
*
34+
* @param FormEvents::* $eventName
35+
* @param (callable(PreSetDataEvent<T>): void)|(callable(PostSetDataEvent<T>): void)|(callable(PreSubmitEvent): void)|(callable(SubmitEvent<T>): void)|(callable(PostSubmitEvent<T>): void) $listener
2536
* @param int $priority The priority of the listener. Listeners
2637
* with a higher priority are called before
2738
* listeners with a lower priority.
@@ -247,6 +258,8 @@ public function setAutoInitialize(bool $initialize): static;
247258

248259
/**
249260
* Builds and returns the form configuration.
261+
*
262+
* @return FormConfigInterface<T>
250263
*/
251264
public function getFormConfig(): FormConfigInterface;
252265

0 commit comments

Comments
 (0)