8000 [Form] Argument $forms in DataMapperInterface methods should be \Traversable · Issue #39311 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
[Form] Argument $forms in DataMapperInterface methods should be \Traversable  #39311
Closed
@andrew-demb

Description

@andrew-demb

Description

#32237 introduced type hints based on phpdocs for 5.0 major release with few enhancements, like change \Traversable in favor iterable.
One of the places is Symfony\Form\DataMapperInterface - https://github.com/symfony/symfony/pull/32237/files#diff-d2afd0e69fa41c456e85113d6455d72debedb1a163faf4fb5fc5860e932a8cad

The issue with change is with many existed places in applications (and recommended in docs - https://symfony.com/doc/5.2/form/data_mappers.html#creating-a-data-mapper) with iterator_to_array now will be incorrect, because iterable type int is not guaranteed safe call to this function.

It worked fine before, because $forms has type FormInterface[]|\Traversable, and iterator_to_array allows to pass \Traversable

Example

Example snippet (from 4.4, to avoid iterable in typehint - https://symfony.com/doc/4.4/form/data_mappers.html#creating-a-data-mapper):

    public function mapFormsToData($forms, &$viewData): void
    {
        /** @var FormInterface[] $forms */
        $forms = iterator_to_array($forms);

        // as data is passed by reference, overriding it will change it in
        // the form object as well
        // beware of type inconsistency, see caution below
        $viewData = new Color(
            $forms['red']->getData(),
            $forms['green']->getData(),
            $forms['blue']->getData()
        );
    }

May type hint be changed from iterable to \Traversable?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0