-
Notifications
You must be signed in to change notification settings - Fork 92
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
Add stub for form options #419
base: 2.0.x
Are you sure you want to change the base?
Changes from all commits
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,33 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Type\Symfony; | ||
|
||
use PHPStan\Rules\Methods\CallMethodsRule; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Testing\RuleTestCase; | ||
|
||
/** | ||
* @extends RuleTestCase<CallMethodsRule> | ||
*/ | ||
class CallMethodsRuleTest extends RuleTestCase | ||
{ | ||
|
||
protected function getRule(): Rule | ||
{ | ||
return self::getContainer()->getByType(CallMethodsRule::class); | ||
} | ||
|
||
public function testExtension(): void | ||
{ | ||
$this->analyse([__DIR__ . '/data/form_options.php'], []); | ||
} | ||
|
||
public static function getAdditionalConfigFiles(): array | ||
{ | ||
return [ | ||
__DIR__ . '/../../../extension.neon', | ||
__DIR__ . '/../../../vendor/phpstan/phpstan-strict-rules/rules.neon', | ||
]; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace GenericFormOptionsType; | ||
|
||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; | ||
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 | ||
{ | ||
} | ||
|
||
/** | ||
* @extends AbstractType<DataClass, array{required: string, optional: int}> | ||
*/ | ||
class DataClassType extends AbstractType | ||
{ | ||
|
||
public function buildForm(FormBuilderInterface $builder, array $options): void | ||
{ | ||
assertType('string', $options['required']); | ||
assertType('int', $options['optional']); | ||
|
||
$builder | ||
->add('foo', NumberType::class) | ||
->add('bar', TextType::class) | ||
; | ||
} | ||
|
||
public function configureOptions(OptionsResolver $resolver): void | ||
{ | ||
$resolver | ||
->setDefaults([ | ||
'data_class' => DataClass::class, | ||
'optional' => 0, | ||
]) | ||
->setRequired('required') | ||
->setAllowedTypes('required', 'string') | ||
->setAllowedTypes('optional', 'int') | ||
; | ||
} | ||
|
||
} | ||
|
||
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('Symfony\Component\Form\FormInterface<GenericFormOptionsType\DataClass>', $form); | ||
} | ||
|
||
public function doSomethingWithOption(): void | ||
{ | ||
$form = $this->formFactory->create(DataClassType::class, new DataClass(), ['required' => 'foo']); | ||
assertType('Symfony\Component\Form\FormInterface<GenericFormOptionsType\DataClass>', $form); | ||
} | ||
|
||
public function doSomethingWithInvalidOption(): void | ||
{ | ||
$form = $this->formFactory->create(DataClassType::class, new DataClass(), ['required' => 42]); | ||
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'd like to get an error here saying I pass an integer when a string is expected because DataClassType is
I thought it would have been reported by the CallMethodsRuleTest. What do I miss @ondrejmirtes ? 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. Please recreate the scenario in a minimal way on phpstan.org/try so I don't have to hold all the interfaces and Symfony classes in my head. 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. Sure. But I discovered that in Symfony, that the required options in the Factory::create methods and the certains options in the buildForm $options are not exactly the same. So this would give something like I dunno if it's possible to have something better than
|
||
assertType('Symfony\Component\Form\FormInterface<GenericFormOptionsType\DataClass>', $form); | ||
} | ||
|
||
} | ||
|
||
class FormController extends AbstractController | ||
{ | ||
|
||
public function doSomething(): void | ||
{ | ||
$form = $this->createForm(DataClassType::class, new DataClass()); | ||
assertType('Symfony\Component\Form\FormInterface<GenericFormOptionsType\DataClass>', $form); | ||
} | ||
|
||
public function doSomethingWithOption(): void | ||
{ | ||
$form = $this->createForm(DataClassType::class, new DataClass(), ['required' => 'foo']); | ||
assertType('Symfony\Component\Form\FormInterface<GenericFormOptionsType\DataClass>', $form); | ||
} | ||
|
||
public function doSomethingWithInvalidOption(): void | ||
{ | ||
$form = $this->createForm(DataClassType::class, new DataClass(), ['required' => 42]); | ||
assertType('Symfony\Component\Form\FormInterface<GenericFormOptionsType\DataClass>', $form); | ||
} | ||
|
||
} |
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.
This has to be optional, otherwise it's a BC break. See phpstan/phpstan-src#3457