Skip to content
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

feat: added stub for DataMapperInterface #422

Open
wants to merge 1 commit into
base: 2.0.x
Choose a base branch
from

Conversation

JefvdA
Copy link

@JefvdA JefvdA commented Jan 9, 2025

Ensure TData of FormInterface is provided
Set TData on mixed since we don't know the type of the child forms Add template for in DataMapperInterface

creds to @roerbakei ;)

refs: #417

Ensure TData of FormInterface is provided
Set TData on mixed since we don't know the type of the child forms
Add template for  in DataMapperInterface

creds to @roerbakei ;)

refs: phpstan#417
@Tomsgu
Copy link

Tomsgu commented Jan 16, 2025

This looks good to me. The only missing part is detecting if I pass a correct value when creating a form. Because currently I can pass anything I want.

$form = $this->createForm(SnippetType::class, $notAllowedObject); // type mismatch error
 /**
 * @template-implements DataMapperInterface<SnippetDto>
 */
class SnippetType extends AbstractType implements DataMapperInterface
{
    public function mapDataToForms(mixed $viewData, Traversable $forms): void
    {
        if ($viewData === null) {
            return;
        }

        // Does not make sense to do this check. But we still need to do it. Obvious error: 
        // Instanceof between SnippetDto and SnippetDto will always evaluate to true.
        // 🪪  instanceof.alwaysTrue
        
        //if (!$viewData instanceof SnippetDto) {
        //    throw new UnexpectedTypeException($viewData, SnippetDto::class);
        //}

        $forms = iterator_to_array($forms);
        // ...
    }
}

I am wondering if there is anything we can do about this.

Copy link
Member

@ondrejmirtes ondrejmirtes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not sufficient. The stub has to be registered in extension.neon - stubFiles. Also because it's introducing generics, we need to add it to skipCheckGenericClasses like it was done here

skipCheckGenericClasses:
- Symfony\Component\Form\AbstractType
- Symfony\Component\Form\FormBuilderInterface
- Symfony\Component\Form\FormConfigBuilderInterface
- Symfony\Component\Form\FormConfigInterface
- Symfony\Component\Form\FormInterface
- Symfony\Component\Form\FormTypeExtensionInterface
- Symfony\Component\Form\FormTypeInterface
- Symfony\Component\OptionsResolver\Options
- Symfony\Component\Security\Core\Authorization\Voter\Voter
- Symfony\Component\Security\Core\User\PasswordUpgraderInterface

@ondrejmirtes
Copy link
Member

There are two different PRs with this stub: #418

  1. Both have the same mistake of not registering the stub.
  2. Both are slightly different so you should agree first how it should look like.

/**
* @param \Traversable<mixed, FormInterface<mixed>> $forms
* @param TViewData|null $viewData
* @param-out TViewData $viewData
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is wrong. there is no guarantee that the reference argument is replaced by a non-null value by the end of the method (and actually, the 3 core implementations of the interface have cases where the data will be null in the output)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants