Skip to content

Discussion: better (?) typing suggestion for AbstractController::createForm #355

@zerkms

Description

@zerkms

Current definition is the following:

/**
* @template TData
* @template TFormType of FormTypeInterface<TData>
*
* @psalm-param class-string<TFormType> $type
*
* @psalm-return FormInterface<TData>
*/
protected function createForm(string $type, $data = null, array $options = []): FormInterface {}

I suggest to change it to:

    /**
     * @template TData
     * @template TFormType of FormTypeInterface<TData>
     *
     * @psalm-param class-string<TFormType> $type
     * @psalm-param TData $data
     *
     * @psalm-return FormInterface<null|TData>
     */
    protected function createForm(string $type, $data = null, array $options = []): FormInterface {}

What has changed:

  1. TData $data parameter added
  2. null|TData generic parameter changed for the return type

Why: this would allow check the $this->createForm(FormType::class, ['rubbish']) compatibility: if the FormType does not extend AbstractType<array{0: 'rubbish'}> then it will be marked as incompatible.

Thoughts?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions