-
-
Notifications
You must be signed in to change notification settings - Fork 52
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
Improve type inference for element attributes #245
Conversation
There was a problem hiding this 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:
laminas-form/test/FactoryTest.php
Lines 99 to 105 in 943ece1
'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 <[email protected]>
bf98515
to
a12b893
Compare
Signed-off-by: George Steel <[email protected]>
…te setting value options via attributes Signed-off-by: George Steel <[email protected]>
There was a problem hiding this 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 💪
There was a problem hiding this 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!
'Providing multi-checkbox value options via attributes is deprecated and will be removed in ' | ||
. 'version 4.0 of this library', |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 <[email protected]>
Thanks @Slamdunk - I've adjusted the tests to use 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 <[email protected]>
Signed-off-by: George Steel <[email protected]>
Signed-off-by: George Steel <[email protected]>
What a refreshing patch, thank you @gsteel |
@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. |
Only allowed in major versions. Do you find a problem? |
@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. |
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:
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 |
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 consideringElementInterface::getAttribute(string $name): mixed