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

Introduce Form Template to improve inference for processed payloads #248

Merged
merged 10 commits into from
Nov 28, 2023

Conversation

gsteel
Copy link
Member

@gsteel gsteel commented Nov 23, 2023

Q A
New Feature yes
RFC yes

Description

Introduce Form Template to improve inference for processed payloads…

This patch should allow users to provide templates for form classes with full inference of the resulting valid payload.

Added an interface method, which we could probably revert considering that Form::getData() has decent inference if you explicitly pass a flag, due to the conditional return on the interface.

There are some weird new issues around inheritance and fluent return types which I'm leaving here for now because I have no idea how to fix them, or why they were introduced by these changes. Maybe they are fixed in 5.16, so I'll update there…

Closes #238

Signed-off-by: George Steel <george@net-glue.co.uk>
Signed-off-by: George Steel <george@net-glue.co.uk>
Signed-off-by: George Steel <george@net-glue.co.uk>
Signed-off-by: George Steel <george@net-glue.co.uk>
@gsteel
Copy link
Member Author

gsteel commented Nov 23, 2023

Psalm 5.16 doesn't fix the new LessSpecificReturnStatement problems. Maybe someone can take a look and see if they have any idea what's going on there.

Copy link
Contributor

@Slamdunk Slamdunk left a comment

Choose a reason for hiding this comment

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

The overall patch looks very good to me.

My only worry is that if we don't fix the bugs/mess on PSalm here, the users could be flooded with invalid errors as well.

@Ocramius do you know how to get PSalm working, or anyone we could ask for help?

@gsteel
Copy link
Member Author

gsteel commented Nov 24, 2023

Sussed it, All/most of the fluid return types in Form are annotated with @return $this, when inherited, psalm infers the return type as FormSubclass<TFilteredValues> and $this is therefore LessSpecific. Returning self or $this<TFilteredValues>, fixes the issues. I'll update the patch with the latter, because it is more precise

Ok, I don't actually think that psalm likes $this<TFilteredValues> at all. I found that either self or self<TFilteredValues> solved the issues in very specific places, but wholesale replacement of $this yielded many more errors.

I've updated the Static Analysis test case to illustrate template preservation using fluid return types, but not for all possible methods.

Also, I thought that adding an interface method might be a BC break? Happy to revert that if is.

@Slamdunk Slamdunk added this to the 3.17.0 milestone Nov 24, 2023
…ited templates

Signed-off-by: George Steel <george@net-glue.co.uk>
@gsteel gsteel requested a review from Slamdunk November 24, 2023 09:45
@Slamdunk Slamdunk closed this Nov 24, 2023
@Slamdunk Slamdunk reopened this Nov 24, 2023
Copy link
Contributor

@Slamdunk Slamdunk left a comment

Choose a reason for hiding this comment

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

$ docker run --rm -v `pwd`:/app nyholm/roave-bc-check

[BC] ADDED: Method getValidatedPayload() was added to interface Laminas\Form\FormInterface                                                                                                                                                      
1 backwards-incompatible changes detected

I always thought it wasn't, but apparently it is a BC Break so yes please, revert it

src/Element.php Outdated Show resolved Hide resolved
Signed-off-by: George Steel <george@net-glue.co.uk>
Signed-off-by: George Steel <george@net-glue.co.uk>
@gsteel
Copy link
Member Author

gsteel commented Nov 28, 2023

@Slamdunk
Can I baseline the remaining issues please 🙏
I feel like the issues are concerned with the deep inheritance hierarchy where everything extends from Element. You'll see that the issues are not applied to a SomeUserForm when extending from Form but to ancestors such as Element and Fieldset.

I'd like to try and keep this patch alive because the additional inference provided to consumers would be really useful for a project I'm working on 😁

@Slamdunk
Copy link
Contributor

Can I baseline the remaining issues please 🙏

As long as you give me a reasonable guarantee that users' PSalm runs won't explode with unresolvable errors after this patch, I'm totally ok with this 👍

…m the baseline.

Signed-off-by: George Steel <george@net-glue.co.uk>
Signed-off-by: George Steel <george@net-glue.co.uk>
Signed-off-by: George Steel <george@net-glue.co.uk>
@gsteel
Copy link
Member Author

gsteel commented Nov 28, 2023

To verify that these psalm issues don't crop up for inheritors of Form and Fieldset etc, I've fixed a load of issues with test assets to remove them from baseline completely.

Thanks @Slamdunk

@Slamdunk Slamdunk merged commit bc4828e into laminas:3.17.x Nov 28, 2023
11 checks passed
@gsteel gsteel deleted the payload-shapes branch November 28, 2023 10:55
@Ocramius
Copy link
Member

Worth releasing: triggered a release

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

Successfully merging this pull request may close these issues.

[RFC] Improve type inference for getData()
4 participants