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

Improve type inference for element attributes #245

Merged
merged 7 commits into from
Nov 17, 2023

Conversation

gsteel
Copy link
Member

@gsteel gsteel commented Nov 16, 2023

Q A
Documentation yes
QA yes

Description

Marks all attribute arrays and iterables as <string, mixed> which is slightly better than <array-key, mixed>

This is pretty minor stuff, but it really helps in consumer projects running psalm.

I considered that we could have gone for <string, scalar|null> but I'm fairly sure there are cases where arrays are accepted, or maybe even iterables at which point, <string, mixed> felt like a better middle ground considering ElementInterface::getAttribute(string $name): mixed

@gsteel gsteel added this to the 3.15.0 milestone Nov 16, 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.

I considered that we could have gone for <string, scalar|null> but I'm fairly sure there are cases where arrays are accepted

As far as I can test and tell, those cases are errors that come from a past (year 2012) when types weren't a thing, and we tested everything just to be sure but without a valid use case.
I'm talking about:

'attributes' => [
'type' => 'radio',
'options' => [
'foo' => 'Foo Bar',
'bar' => 'Bar Baz',
],
],

$element->setAttribute('options', ['option' => 'value']);

I'd say let's go all-in with <string, scalar|null> even for ElementInterface::getAttribute PHPDoc return type, remove the four test cases with invalid sets and release.

Marks all attribute arrays and iterables as `<string, scalar|null>`

Signed-off-by: George Steel <george@net-glue.co.uk>
@gsteel gsteel force-pushed the attributes-inference-improvement branch from bf98515 to a12b893 Compare November 17, 2023 09:18
Signed-off-by: George Steel <george@net-glue.co.uk>
…te setting value options via attributes

Signed-off-by: George Steel <george@net-glue.co.uk>
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.

Really good work, some minor improvements and we can go 💪

src/LabelAwareInterface.php Outdated Show resolved Hide resolved
src/View/Helper/AbstractHelper.php Outdated Show resolved Hide resolved
src/View/Helper/FormElement.php Outdated Show resolved Hide resolved
Copy link
Member

@Ocramius Ocramius left a comment

Choose a reason for hiding this comment

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

Very cool! One question on the patch, otherwise LGTM!

Comment on lines +108 to +109
'Providing multi-checkbox value options via attributes is deprecated and will be removed in '
. 'version 4.0 of this library',
Copy link
Member

Choose a reason for hiding this comment

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

Does building the element from an array spec still work, after removing this?

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

Was more thinking about the really wonky early-laminas "let's make an entire form from a single array" kinda thing :D

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, it does still work - The factory tests were showing this, but I did adjust this test in da4a9f0 so that the deprecated version is no longer used.

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

gsteel commented Nov 17, 2023

Thanks @Slamdunk - I've adjusted the tests to use setValueOptions where appropriate and also properly deprecated providing "value options" in the options key to setAttributes - Also added deprecated tests to cover the deprecation.

Sorry, the patch is a bit bigger now, but the change to scalar|null of course caused a lot of valid psalm issues that needed fixing

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>
@Slamdunk Slamdunk merged commit 9382d98 into laminas:3.15.x Nov 17, 2023
11 checks passed
@Slamdunk
Copy link
Contributor

What a refreshing patch, thank you @gsteel

@gsteel
Copy link
Member Author

gsteel commented Nov 17, 2023

Thanks to you too @Slamdunk and @Ocramius for the reviews :)

@gsteel gsteel deleted the attributes-inference-improvement branch November 17, 2023 10:59
@pavattt
Copy link

pavattt commented Nov 30, 2023

@Slamdunk Do you have any rules on what changes can go into a minor version and what cannot? Signature change in minor version is not something I'd expect.

@froschdesign
Copy link
Member

@pavattt

Signature change in minor version is not something I'd expect.

Only allowed in major versions. Do you find a problem?

@pavattt
Copy link

pavattt commented Nov 30, 2023

@froschdesign I've upgraded laminas/laminas-form from 3.13.1 to 3.17, and now I have a ton of issues in static analysis I have to fix.

@Ocramius
Copy link
Member

Ocramius commented Dec 1, 2023

@Slamdunk Do you have any rules on what changes can go into a minor version and what cannot? Signature change in minor version is not something I'd expect.

It's a bit of a gray area here, because SA shifts regularly (even with patch versions of psalm and phpstan).

We keep BC on anything that would otherwise require changes in the code for:

  • making it run as before
  • making it "compile" as before, where "compile" is what PHP itself understands as such

For changes such as more precise data structures and types, it is very improbable that you'll get BC on that, as the types can easily shift with tiny changes, even in other packages that declare better types.

My general suggestion when doing a composer.lock change that introduces new type errors is to have a baseline file (supported by both PHPStan and Psalm), updating that, and deciding which errors that were introduced are worth fixing immediately.

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.

5 participants