Skip to content

Commit adb86c4

Browse files
committed
Revert "Fix array_count_values key type for general string input"
This reverts commit 4c7aa46.
1 parent 920e47f commit adb86c4

File tree

4 files changed

+2
-43
lines changed

4 files changed

+2
-43
lines changed

CLAUDE.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -323,10 +323,6 @@ Many built-in PHP functions need `DynamicFunctionReturnTypeExtension` implementa
323323
- Extensions live in `src/Type/Php/` and are registered in `conf/services.neon`
324324
- Each reads the argument types from `Scope::getType()` and returns a more precise `Type`
325325

326-
### toArrayKey() and numeric string coercion
327-
328-
`Type::toArrayKey()` converts a value type to its array key representation. `StringType::toArrayKey()` returns `string`, but PHP casts numeric strings (like `'123'`) to integer keys. `ConstantStringType::toArrayKey()` correctly handles this (e.g. `'1'``int(1)`), and `AccessoryNumericStringType::toArrayKey()` returns `int|numeric-string`. However, a general `string` where `isNumericString()` returns `maybe` needs special handling in extensions that create arrays from values — the key type should be `int|string` (array-key), not just `string`. This applies to `array_count_values`, `array_flip`, and similar functions where input values become output keys.
329-
330326
### Function signature corrections (`src/Reflection/SignatureMap/`)
331327

332328
PHPStan maintains its own signature map for built-in PHP functions in `functionMap.php` and delta files. Fixes involve:

src/Type/Php/ArrayCountValuesDynamicReturnTypeExtension.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,8 @@ public function getTypeFromFunctionCall(
5454
continue;
5555
}
5656

57-
$keyType = $itemType->toArrayKey();
58-
59-
// PHP casts numeric strings to integer keys, so a general string
60-
// that might be numeric should produce int|string keys
61-
if ($itemType->isString()->yes() && $itemType->isNumericString()->maybe()) {
62-
$keyType = TypeCombinator::union($keyType, new IntegerType());
63-
}
64-
6557
$outputTypes[] = new IntersectionType([
66-
new ArrayType($keyType, IntegerRangeType::fromInterval(1, null)),
58+
new ArrayType($itemType->toArrayKey(), IntegerRangeType::fromInterval(1, null)),
6759
new NonEmptyArrayType(),
6860
]);
6961
}

tests/PHPStan/Analyser/nsrt/array-count-values.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ function returnsStringOrObjectArray(): array
2525
}
2626

2727
// Objects are ignored by array_count_values, with a warning emitted.
28-
assertType('non-empty-array<int|string, int<1, max>>', array_count_values(returnsStringOrObjectArray()));
28+
assertType('non-empty-array<string, int<1, max>>', array_count_values(returnsStringOrObjectArray()));
2929

3030
class StringableObject
3131
{

tests/PHPStan/Analyser/nsrt/bug-13996.php

Lines changed: 0 additions & 29 deletions
This file was deleted.

0 commit comments

Comments
 (0)