Skip to content

Commit

Permalink
Fix AccessoryLiteralStringType::setOffsetValueType()
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Aug 14, 2024
1 parent c19155c commit 93c5226
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/Type/Accessory/AccessoryLiteralStringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,11 @@ public function getOffsetValueType(Type $offsetType): Type

public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $unionValues = true): Type
{
return $this;
if ($valueType->isLiteralString()->yes()) {
return $this;
}

return new StringType();
}

public function setExistingOffsetValueType(Type $offsetType, Type $valueType): Type
Expand Down
13 changes: 13 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-3981.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,17 @@ public function doFoo(string $s, string $nonEmptyString): void
assertType('non-empty-string', $s);
}

/**
* @param literal-string $literalString
* @param literal-string $anotherLiteralString
*/
public function doBar(string $literalString, string $generalString): void
{
$literalString[0] = 'a';
assertType('literal-string&non-empty-string', $literalString);

$literalString[1] = $generalString;
assertType('non-empty-string', $literalString);
}

}

0 comments on commit 93c5226

Please sign in to comment.