fix: Undefined array input type - #819
Conversation
|
@michael-georgiadis I'm not quite sure I understand the issue here. Where are things breaking? |
|
@oojacoboo So, up until now the library handled the undefined compounds on the non-doc-block based type hints. An But for this case /** @param list<int> $array */
private array|Undefined|null $array;if the code block below does not exist: if ($innerType instanceof Compound && UndefinedTypeMapper::containsUndefined($innerType)) {
$innerType = NullableTypeMapperAdapter::getNonNullableType(
UndefinedTypeMapper::replaceUndefinedWith($innerType),
) ?? $innerType;
} The type coming in in this if statement if (
$innerType instanceof Array_
|| $innerType instanceof Iterable_
|| $innerType instanceof Mixed_
|| $innerType instanceof Callable_
|| ($innerType instanceof Object_
&& $docBlockType instanceof Collection
&& (string) $innerType->getFqsen() === (string) $docBlockType->getFqsen()
)
)was a So the fix here is basically unwrapping the compound with previously written code and making sure that the innerType is handled as an Array instead and prints the type correctly. Scalars also work by passing the else statement, but there are no conflicting docblocks so they survive |
|
This is not me being passive aggressive btw, but you can checkout the branch and try the integration tests with the fixtures but reverting the actual code-change and you will see that it blows up |
|
So, I guess I'm uncertain why you'd want to docblock type differently from the actual type signature. |
|
@oojacoboo I am unsure what you mean by that. I will try my best to clear the uncertainty tho. The docblock and the actualy type signature for arrays carry different information (as ofc you know). So, the only thing that |
/** @param list<int> $array */
private array|Undefined|null $array;I'm saying this is an incorrect phpdoc type, as it doesn't include /** @param list<int>|Undefined|null $array */
private array|Undefined|null $array; |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #819 +/- ##
============================================
- Coverage 95.72% 91.72% -4.01%
- Complexity 1773 2025 +252
============================================
Files 154 198 +44
Lines 4586 5436 +850
============================================
+ Hits 4390 4986 +596
- Misses 196 450 +254 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
@oojacoboo fair point. Tried it with the proper docblock typing (as you can see in the latest commit on the fixtures) and the issue still remains if I revert my code. |
There was a problem hiding this comment.
Can you add a test here that checks that a passed null value into the input type is respected as an update? So, the default value would be something other than null, and it's updated to be nullified.
There was a problem hiding this comment.
@oojacoboo, Sure thing. I added the test case
There was a problem hiding this comment.
@michael-georgiadis sorry to be pedantic about this. But can you please assert the value before the mutation and after, so we can be sure that the change is respected. I don't actually recall if the previous mutations maintain state, or if each mutation within a test is stateless.
|
@michael-georgiadis Thanks for the PR and working through it with me! |
|
@oojacoboo, no worries. Do we know when the release is coming up? |
Description
Optional-via-
Undefinedworks for scalars but explodes on lists:Fix: drop Undefined and take the non-null core before the container check, reusing existing helpers (UndefinedTypeMapper + a now-public static getNonNullableType).
Covers array/list. Native iterable isn't handled (PHP expands it to array|Traversable) and since it's rare for an input, I left it alone