-
Notifications
You must be signed in to change notification settings - Fork 94
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 3 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 | ||
* | ||
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. Wrong indentation 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. Fixed in 88da905 |
||
* @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,15 @@ | |
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 | ||
{ | ||
|
||
/** | ||
* @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,14 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Stubs\Symfony\Component\Form; | ||
|
||
class DataClass | ||
{ | ||
|
||
/** @var int */ | ||
public $foo; | ||
|
||
/** @var string */ | ||
public $bar; | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Stubs\Symfony\Component\Form; | ||
|
||
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\OptionsResolver\OptionsResolver; | ||
|
||
/** | ||
* @extends AbstractType<DataClass> | ||
*/ | ||
class DataClassType extends AbstractType | ||
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. Not sure what these tests are supposed to do, they're not referenced from anywhere. 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. It's used from here: https://github.com/siketyan/phpstan-symfony/blob/a18b756621163b0040a3c3f5a58a2f24b3652f48/tests/Stubs/Symfony/Component/Form/FormFactoryAwareClass.php#L20. 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. That’s not how testing works here 😊 Please see https://phpstan.org/developing-extensions/testing to know how to test it. 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. How about this? dcf9b4f |
||
{ | ||
|
||
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, | ||
]) | ||
; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Stubs\Symfony\Component\Form; | ||
|
||
use Symfony\Component\Form\FormFactoryInterface; | ||
|
||
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()); | ||
$data = $form->getData(); | ||
$this->thisOnlyAcceptsDataClass($data); | ||
$this->thisOnlyAcceptsDataClassOrNull($data); | ||
} | ||
|
||
public function doSomethingNullable(): void | ||
{ | ||
$form = $this->formFactory->create(DataClassType::class); | ||
$data = $form->getData(); | ||
// $this->thisOnlyAcceptsDataClass($data); // ERROR! | ||
$this->thisOnlyAcceptsDataClassOrNull($data); | ||
} | ||
|
||
private function thisOnlyAcceptsDataClass(DataClass $data): void | ||
{ | ||
} | ||
|
||
private function thisOnlyAcceptsDataClassOrNull(?DataClass $data): void | ||
{ | ||
} | ||
|
||
} |
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