Skip to content

Commit

Permalink
Merge pull request #8064 from weirdan/null-is-cast-to-string-in-array…
Browse files Browse the repository at this point in the history
…-offsets

Coerce null to empty string in array keys
  • Loading branch information
orklah committed Jun 7, 2022
2 parents d55988a + 4e59398 commit 53c3c9b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ public static function getArrayAccessTypeGivenOffset(

if ($in_assignment) {
$offset_type->removeType('null');
$offset_type->addType(new TLiteralInt(0));
$offset_type->addType(new TLiteralString(''));
}
}

Expand All @@ -528,7 +528,7 @@ public static function getArrayAccessTypeGivenOffset(
$offset_type->removeType('null');

if (!$offset_type->ignore_nullable_issues) {
$offset_type->addType(new TLiteralInt(0));
$offset_type->addType(new TLiteralString(''));
}
}
}
Expand Down
14 changes: 7 additions & 7 deletions tests/ArrayAssignmentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1074,26 +1074,26 @@ function bar(array $value): void {
if (is_int($value["a"])) {}
}'
],
'coercePossiblyNullKeyToZero' => [
'coercePossiblyNullKeyToEmptyString' => [
'<?php
function int_or_null(): ?int {
return rand(0, 1) !== 0 ? 42 : null;
function string_or_null(): ?string {
return rand(0, 1) !== 0 ? "aaa" : null;
}
/**
* @return array<array-key, null>
* @return array<string, null>
*/
function foo(): array {
$array = [];
/** @psalm-suppress PossiblyNullArrayOffset */
$array[int_or_null()] = null;
$array[string_or_null()] = null;
return $array;
}'
],
'coerceNullKeyToZero' => [
'coerceNullKeyToEmptyString' => [
'<?php
/**
* @return array<int, null>
* @return array<string, null>
*/
function foo(): array {
$array = [];
Expand Down

0 comments on commit 53c3c9b

Please sign in to comment.