Remove array_filter mixed handling which breaks type specification #1012
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This adapts
ArrayFilterFunctionReturnTypeReturnTypeExtensionto not handle a mixed argument anymore with aBenevolentUnionType.This fixes truthy type specification with
is_array && array_filterif the input is mixed, but I guess this is just a side-effect and there might be a better fix..Context / use case: phpstan/phpstan-webmozart-assert#101
Without the modifications here I'd get
1) PHPStan\Analyser\TypeSpecifierTest::testCondition with data set #90 (PhpParser\Node\Expr\BinaryOp\BooleanAnd Object (...), array('array<string, mixed>', 'array<string, mixed>'), array()) if (is_array($foo) && array_filter($foo, 'is_string', ARRAY_FILTER_USE_KEY) === $foo) Failed asserting that two arrays are identical. --- Expected +++ Actual @@ @@ Array &0 ( - '$foo' => 'array<string, mixed>' - 'array_filter($foo, 'is_string', ARRAY_FILTER_USE_KEY)' => 'array<string, mixed>' + '$foo' => 'array' + 'array_filter($foo, 'is_string', ARRAY_FILTER_USE_KEY)' => '(array|null)' )BUT I'm also aware that the changes here might be negatively affecting
array_filterbehaviour pre PHP 8, see https://3v4l.org/sOWbb. Is there a better / more pragmatical way to improve this? On the other hand, is PHPStan actively supporting PHP quirks with warnings and such?The main problem / limitation that I have is apparently how
TypeSpecifierhandles the BooleanAnd expression.IsArrayFunctionTypeSpecifyingExtensionin https://github.com/phpstan/phpstan-src/blob/1.4.6/src/Analyser/TypeSpecifier.php#L608 and makes PHPStan aware that my input var is of type ArrayType, which is goodAny other ideas how to improve that? It looks like NodeScopeResolver is handling this a bit different and not loosing the information so to say. Or I'm misunderstanding it somehow :)