Skip to content
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

isset() narrows string-key in int-keyed-array to numeric-string #3472

Merged
merged 2 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 3 additions & 41 deletions src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use PHPStan\Reflection\ParametersAcceptorWithPhpDocs;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Reflection\ResolvedFunctionVariant;
use PHPStan\Rules\Arrays\AllowedArrayKeysTypes;
use PHPStan\ShouldNotHappenException;
use PHPStan\TrinaryLogic;
use PHPStan\Type\Accessory\AccessoryArrayListType;
Expand Down Expand Up @@ -820,47 +821,8 @@ public function specifyTypesInCondition(
);
} else {
$varType = $scope->getType($var->var);
if ($varType->isArray()->yes() && !$varType->isIterableAtLeastOnce()->no()) {
$varIterableKeyType = $varType->getIterableKeyType();

if ($varIterableKeyType->isConstantScalarValue()->yes()) {
$narrowedKey = TypeCombinator::union(
$varIterableKeyType,
TypeCombinator::remove($varIterableKeyType->toString(), new ConstantStringType('')),
);

if (!$varType->hasOffsetValueType(new ConstantIntegerType(0))->no()) {
$narrowedKey = TypeCombinator::union(
$narrowedKey,
new ConstantBooleanType(false),
);
}

if (!$varType->hasOffsetValueType(new ConstantIntegerType(1))->no()) {
$narrowedKey = TypeCombinator::union(
$narrowedKey,
new ConstantBooleanType(true),
);
}

if (!$varType->hasOffsetValueType(new ConstantStringType(''))->no()) {
$narrowedKey = TypeCombinator::addNull($narrowedKey);
}

if (!$varIterableKeyType->isNumericString()->no() || !$varIterableKeyType->isInteger()->no()) {
$narrowedKey = TypeCombinator::union($narrowedKey, new FloatType());
}
} else {
$narrowedKey = new MixedType(
false,
new UnionType([
new ArrayType(new MixedType(), new MixedType()),
new ObjectWithoutClassType(),
new ResourceType(),
]),
);
}

$narrowedKey = AllowedArrayKeysTypes::narrowOffsetKeyType($varType, $dimType);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as a first step I think its easier to have a static method for this.

after we get more similar with the array_key_exists impl we will see whether it makes sense to move this method into the ConstantScalarType-interface, as we discussed in dresden

if ($narrowedKey !== null) {
$types = $types->unionWith(
$this->create(
$var->dim,
Expand Down
59 changes: 59 additions & 0 deletions src/Rules/Arrays/AllowedArrayKeysTypes.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,20 @@

namespace PHPStan\Rules\Arrays;

use PHPStan\Type\ArrayType;
use PHPStan\Type\BooleanType;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\FloatType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\MixedType;
use PHPStan\Type\NullType;
use PHPStan\Type\ObjectWithoutClassType;
use PHPStan\Type\ResourceType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\UnionType;

final class AllowedArrayKeysTypes
Expand All @@ -24,4 +32,55 @@ public static function getType(): Type
]);
}

public static function narrowOffsetKeyType(Type $varType, Type $keyType): ?Type
{
if (!$varType->isArray()->yes() || $varType->isIterableAtLeastOnce()->no()) {
return null;
}

$varIterableKeyType = $varType->getIterableKeyType();

if ($varIterableKeyType->isConstantScalarValue()->yes()) {
$narrowedKey = TypeCombinator::union(
$varIterableKeyType,
TypeCombinator::remove($varIterableKeyType->toString(), new ConstantStringType('')),
);

if (!$varType->hasOffsetValueType(new ConstantIntegerType(0))->no()) {
$narrowedKey = TypeCombinator::union(
$narrowedKey,
new ConstantBooleanType(false),
);
}

if (!$varType->hasOffsetValueType(new ConstantIntegerType(1))->no()) {
$narrowedKey = TypeCombinator::union(
$narrowedKey,
new ConstantBooleanType(true),
);
}

if (!$varType->hasOffsetValueType(new ConstantStringType(''))->no()) {
$narrowedKey = TypeCombinator::addNull($narrowedKey);
}

if (!$varIterableKeyType->isNumericString()->no() || !$varIterableKeyType->isInteger()->no()) {
$narrowedKey = TypeCombinator::union($narrowedKey, new FloatType());
}

return $narrowedKey;
} elseif ($varIterableKeyType->isInteger()->yes() && $keyType->isString()->yes()) {
return TypeCombinator::intersect($varIterableKeyType->toString(), $keyType);
}

return new MixedType(
false,
new UnionType([
new ArrayType(new MixedType(), new MixedType()),
new ObjectWithoutClassType(),
new ResourceType(),
]),
);
}

}
32 changes: 27 additions & 5 deletions tests/PHPStan/Analyser/nsrt/bug-11716.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ public function parse(string $glue): string
}

/**
* @param array<int, string> $arr
* @param array<int, string> $intKeyedArr
* @param array<string, string> $stringKeyedArr
*/
function narrowKey($mixed, string $s, int $i, array $generalArr, array $arr): void {
function narrowKey($mixed, string $s, int $i, array $generalArr, array $intKeyedArr, array $stringKeyedArr): void {
if (isset($generalArr[$mixed])) {
assertType('mixed~(array|object|resource)', $mixed);
} else {
Expand All @@ -59,21 +60,42 @@ function narrowKey($mixed, string $s, int $i, array $generalArr, array $arr): vo
}
assertType('string', $s);

if (isset($arr[$mixed])) {
if (isset($intKeyedArr[$mixed])) {
assertType('mixed~(array|object|resource)', $mixed);
} else {
assertType('mixed', $mixed);
}
assertType('mixed', $mixed);

if (isset($arr[$i])) {
if (isset($intKeyedArr[$i])) {
assertType('int', $i);
} else {
assertType('int', $i);
}
assertType('int', $i);

if (isset($arr[$s])) {
if (isset($intKeyedArr[$s])) {
assertType("numeric-string", $s);
} else {
assertType('string', $s);
}
assertType('string', $s);

if (isset($stringKeyedArr[$mixed])) {
assertType('mixed~(array|object|resource)', $mixed);
} else {
assertType('mixed', $mixed);
}
assertType('mixed', $mixed);

if (isset($stringKeyedArr[$i])) {
assertType('int', $i);
} else {
assertType('int', $i);
}
assertType('int', $i);

if (isset($stringKeyedArr[$s])) {
assertType('string', $s);
} else {
assertType('string', $s);
Expand Down
Loading