Added FunctionTypeSpecifyingExtension for array_is_list#770
Added FunctionTypeSpecifyingExtension for array_is_list#770canvural wants to merge 1 commit intophpstan:masterfrom
array_is_list#770Conversation
205ad66 to
46a8b88
Compare
| if (array_is_list($foo)) { | ||
| assertType('array<int, mixed>', $foo); | ||
| } else { |
| if ($functionName === 'array_is_list' && count($node->getArgs()) === 1) { | ||
| $arrayValue = $scope->getType($node->getArgs()[0]->value); | ||
|
|
||
| if (!$arrayValue instanceof ArrayType) { | ||
| return null; | ||
| } | ||
|
|
||
| if ((new StringType())->isSuperTypeOf($arrayValue->getKeyType())->yes()) { | ||
| return false; | ||
| } | ||
|
|
||
| return null; | ||
| } |
There was a problem hiding this comment.
I am wondering whether these changes are required?
isn't the type-specifying extension enough to get it working?
| use PHPStan\Type\FunctionTypeSpecifyingExtension; | ||
| use PHPStan\Type\IntegerType; | ||
|
|
||
| class ArrayIsListFunctionTypeSpecifyingExtension implements FunctionTypeSpecifyingExtension, TypeSpecifierAwareExtension |
There was a problem hiding this comment.
I think there should be a php-version check somewhere in this extension, since array_is_list is only available with php 8.1+
|
Hi, thanks! I've reused your tests and implemented it the way I think is right (inspired by the |
|
Oh great! So the changes in the extension was enough to get the |
|
Yes, these errors are reported when the type returned from the extension against the current expression type form an impossible intersection. If you return empty SpecifiedTypes when the argument isn't an array, there's nothing to report :) |
This PR adds a new
FunctionTypeSpecifyingExtensionfor PHP 8.1'sarray_is_listfunction.I'm not sure if the tests cover every case that it should cover. If you request I can add more.