Repository navigation
Allow null $data in form factory stubs - #529
Merged
Merged
Conversation
The form factory methods and AbstractController::createForm() default $data to null and already return FormInterface<null|TData> for it, but declared it as TData. Passing or omitting null therefore inferred TData = DataClass|null, which a FormTypeInterface<DataClass> form type does not satisfy, so PHPStan's check of inferred template types against their bounds (bleeding edge, phpstan/phpstan-src#7251) reported valid calls. Amp-Thread-ID: https://ampcode.com/threads/T-01a22307-fb39-7133-a55b-a5cc3cda81e7
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
FormFactoryInterface::create(),createNamed(),createBuilder(),createNamedBuilder()andAbstractController::createForm()default$datatonulland already returnFormInterface<null|TData>/FormBuilderInterface<null|TData>for it, but declared@param TData $data.So
create(DataClassType::class)orcreate(DataClassType::class, null)inferredTData = DataClass|null. With phpstan/phpstan-src#7251, which checks inferred template types against bounds that mention other templates (bleeding edge), that gives a false positive on valid code:This changes
$datatoTData|null. Inferred types don't change:create(DataClassType::class)is stillFormInterface<DataClass|null>, and passing aDataClassstill givesFormInterface<DataClass>. I added a test for an explicitnull.Checked with phpstan-src#7251 and bleeding edge on
tests/Type/Symfony/data/form_data_type.php: the old stubs report 5 of these errors, the new ones report none. Passing data of the wrong type (create(DataClassType::class, new stdClass())) is still reported.