-
Notifications
You must be signed in to change notification settings - Fork 548
array_keys losses array-shape on HasOffsetType
#1084
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
Conversation
|
did some debugging but I cannot wrap my head arround the TypeCombinator yet. I think I can only leave this here as is, with my initial notes for someone else to pick up. maybe @herndlm or @arnaud-lb have an idea on how this could/should work? |
|
I think this line is completely wrong: TypeCombinator::intersect(new ArrayType(new IntegerType(), $keyType), ...TypeUtils::getAccessoryTypes($valueType));It makes sense for stuff like For other cases it just mixes unrelated things together. Because Please try something like this instead: diff --git a/src/Type/Php/ArrayKeysFunctionDynamicReturnTypeExtension.php b/src/Type/Php/ArrayKeysFunctionDynamicReturnTypeExtension.php
index e0f0d451c..665b91543 100644
--- a/src/Type/Php/ArrayKeysFunctionDynamicReturnTypeExtension.php
+++ b/src/Type/Php/ArrayKeysFunctionDynamicReturnTypeExtension.php
@@ -5,6 +5,7 @@ namespace PHPStan\Type\Php;
use PhpParser\Node\Expr\FuncCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\FunctionReflection;
+use PHPStan\Type\Accessory\NonEmptyArrayType;
use PHPStan\Type\ArrayType;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
@@ -33,7 +34,11 @@ class ArrayKeysFunctionDynamicReturnTypeExtension implements DynamicFunctionRetu
return $valueType->getKeysArray();
}
$keyType = $valueType->getIterableKeyType();
- return TypeCombinator::intersect(new ArrayType(new IntegerType(), $keyType), ...TypeUtils::getAccessoryTypes($valueType));
+ $array = new ArrayType(new IntegerType(), $keyType);
+ if ($valueType->isIterableAtLeastOnce()->yes()) {
+ $array = TypeCombinator::intersect($array, new NonEmptyArrayType());
+ }
+ return $array;
}
}
|
|
Thanks ondrej for the suggestion. I applied it to the source and locally it works.. lets see what CI results will look like. I think it needs another double check in these places, which use a similar logic:
what do you think? |
|
Try to break these places and then try to fix them :) |
ddd20b4 to
95d480b
Compare
|
I couldn't come up with a case for |
|
Thank you. |
the return-type extension is taking this path
phpstan-src/src/Type/Php/ArrayKeysFunctionDynamicReturnTypeExtension.php
Line 36 in 984fbcf
and tries to intersect a
Array<int, __benevolent<int|string>>with aHasOffsetTypeofConstantStringTypewhich results in a
NeverTypephpstan-src/src/Type/TypeCombinator.php
Lines 841 to 843 in 984fbcf
closes phpstan/phpstan#6859