Skip to content

Commit 9df9241

Browse files
committed
minor #47412 [Form] Add generics to DataTransformerInterface (VincentLanglet)
This PR was squashed before being merged into the 6.2 branch. Discussion ---------- [Form] Add generics to DataTransformerInterface | Q | A | ------------- | --- | Branch? | 6.2 | Bug fix? | no | New feature? | not really | Deprecations? | no | Tickets | Fix #... <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead --> | License | MIT | Doc PR | symfony/symfony-docs#... <!-- required for new features --> Now that some generics are added to the Symfony codebase, I think we could add more. For people using phpstan/psalm this will be helpful: - It avoid duplicating the generics in both psalm-plugin/phpstan-plugin - It avoid duplicating code between symfony and plugins - When phpstan/psalm report an error it's less confusing to look for the generic in the symfony file rather the plugin one. You can look at the current generic definition for psalm https://github.com/psalm/psalm-plugin-symfony/blob/master/src/Stubs/common/Component/Form/DataTransformerInterface.stubphp for phpstan https://github.com/phpstan/phpstan-symfony/blob/1.2.x/stubs/Symfony/Component/Form/DataTransformerInterface.stub Commits ------- dff26d39bf [Form] Add generics to DataTransformerInterface
2 parents fbcb5ab + 8778f5e commit 9df9241

24 files changed

+60
-8
lines changed

DataTransformerInterface.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
* Transforms a value between different representations.
1818
*
1919
* @author Bernhard Schussek <[email protected]>
20+
*
21+
* @template TValue
22+
* @template TTransformedValue
2023
*/
2124
interface DataTransformerInterface
2225
{
@@ -53,9 +56,9 @@ interface DataTransformerInterface
5356
* of the first data transformer outputs NULL, the second must be able to
5457
* process that value.
5558
*
56-
* @param mixed $value The value in the original representation
59+
* @param TValue|null $value The value in the original representation
5760
*
58-
* @return mixed
61+
* @return TTransformedValue|null
5962
*
6063
* @throws TransformationFailedException when the transformation fails
6164
*/
@@ -82,9 +85,9 @@ public function transform(mixed $value);
8285
* By convention, reverseTransform() should return NULL if an empty string
8386
* is passed.
8487
*
85-
* @param mixed $value The value in the transformed representation
88+
* @param TTransformedValue|null $value The value in the transformed representation
8689
*
87-
* @return mixed
90+
* @return TValue|null
8891
*
8992
* @throws TransformationFailedException when the transformation fails
9093
*/

Extension/Core/DataTransformer/ArrayToPartsTransformer.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
/**
1818
* @author Bernhard Schussek <[email protected]>
19+
*
20+
* @implements DataTransformerInterface<array, array>
1921
*/
2022
class ArrayToPartsTransformer implements DataTransformerInterface
2123
{

Extension/Core/DataTransformer/BaseDateTimeTransformer.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
use Symfony\Component\Form\DataTransformerInterface;
1515
use Symfony\Component\Form\Exception\InvalidArgumentException;
1616

17+
/**
18+
* @template TTransformedValue
19+
* @implements DataTransformerInterface<\DateTimeInterface, TTransformedValue>
20+
*/
1721
abstract class BaseDateTimeTransformer implements DataTransformerInterface
1822
{
1923
protected static $formats = [

Extension/Core/DataTransformer/BooleanToStringTransformer.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
*
2121
* @author Bernhard Schussek <[email protected]>
2222
* @author Florian Eckerstorfer <[email protected]>
23+
*
24+
* @implements DataTransformerInterface<bool, string>
2325
*/
2426
class BooleanToStringTransformer implements DataTransformerInterface
2527
{

Extension/Core/DataTransformer/ChoiceToValueTransformer.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
/**
1919
* @author Bernhard Schussek <[email protected]>
20+
*
21+
* @implements DataTransformerInterface<string, string>
2022
*/
2123
class ChoiceToValueTransformer implements DataTransformerInterface
2224
{

Extension/Core/DataTransformer/ChoicesToValuesTransformer.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
/**
1919
* @author Bernhard Schussek <[email protected]>
20+
*
21+
* @implements DataTransformerInterface<array, array>
2022
*/
2123
class ChoicesToValuesTransformer implements DataTransformerInterface
2224
{

Extension/Core/DataTransformer/DateIntervalToArrayTransformer.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
* Transforms between a normalized date interval and an interval string/array.
2020
*
2121
* @author Steffen Roßkamp <[email protected]>
22+
*
23+
* @implements DataTransformerInterface<\DateInterval, array>
2224
*/
2325
class DateIntervalToArrayTransformer implements DataTransformerInterface
2426
{

Extension/Core/DataTransformer/DateIntervalToStringTransformer.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
* Transforms between a date string and a DateInterval object.
2020
*
2121
* @author Steffen Roßkamp <[email protected]>
22+
*
23+
* @implements DataTransformerInterface<\DateInterval, string>
2224
*/
2325
class DateIntervalToStringTransformer implements DataTransformerInterface
2426
{

Extension/Core/DataTransformer/DateTimeImmutableToDateTimeTransformer.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
* Transforms between a DateTimeImmutable object and a DateTime object.
1919
*
2020
* @author Valentin Udaltsov <[email protected]>
21+
*
22+
* @implements DataTransformerInterface<\DateTimeImmutable, \DateTime>
2123
*/
2224
final class DateTimeImmutableToDateTimeTransformer implements DataTransformerInterface
2325
{

Extension/Core/DataTransformer/DateTimeToArrayTransformer.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
*
1919
* @author Bernhard Schussek <[email protected]>
2020
* @author Florian Eckerstorfer <[email protected]>
21+
*
22+
* @extends BaseDateTimeTransformer<array>
2123
*/
2224
class DateTimeToArrayTransformer extends BaseDateTimeTransformer
2325
{

0 commit comments

Comments
 (0)