Skip to content
Closed
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
11 changes: 1 addition & 10 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -3343,16 +3343,7 @@ public function specifyExpressionType(Expr $expr, Type $type, Type $nativeType,
if ($dimType->isInteger()->yes() || $dimType->isString()->yes()) {
$exprVarType = $scope->getType($expr->var);
if (!$exprVarType instanceof MixedType && !$exprVarType->isArray()->no()) {
$types = [
new ArrayType(new MixedType(), new MixedType()),
new ObjectType(ArrayAccess::class),
new NullType(),
];
if ($dimType->isInteger()->yes()) {
$types[] = new StringType();
}
$offsetValueType = TypeCombinator::intersect($exprVarType, TypeCombinator::union(...$types));

Comment on lines -3346 to -3355
Copy link
Contributor Author

@staabm staabm Jan 18, 2026

Choose a reason for hiding this comment

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

the actual change is here.

I am not sure why this no longer makes a difference.. maybe related to Type->setExistingOffset() and friends

$offsetValueType = $exprVarType;
if ($dimType instanceof ConstantIntegerType || $dimType instanceof ConstantStringType) {
$offsetValueType = TypeCombinator::intersect(
$offsetValueType,
Expand Down
6 changes: 3 additions & 3 deletions tests/PHPStan/Analyser/nsrt/composer-array-bug.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,17 @@ public function doFoo(): void
}
}

assertType("non-empty-array&hasOffsetValue('authors', mixed)", $this->config);
assertType("non-empty-array<mixed>&hasOffsetValue('authors', mixed)", $this->config);
assertType("mixed", $this->config['authors']);

if (empty($this->config['authors'])) {
unset($this->config['authors']);
assertType("array<mixed~'authors', mixed>", $this->config);
} else {
assertType("non-empty-array&hasOffsetValue('authors', mixed~(0|0.0|''|'0'|array{}|false|null))", $this->config);
assertType("non-empty-array<mixed>&hasOffsetValue('authors', mixed~(0|0.0|''|'0'|array{}|false|null))", $this->config);
}

assertType("array", $this->config);
assertType("array<mixed>", $this->config);
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/nsrt/superglobals.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function canBePartlyOverwritten(): void
public function canBeNarrowed(): void
{
if (isset($GLOBALS['foo'])) {
assertType("non-empty-array&hasOffsetValue('foo', mixed~null)", $GLOBALS);
assertType("non-empty-array<mixed>&hasOffsetValue('foo', mixed~null)", $GLOBALS);
assertNativeType("non-empty-array<mixed>&hasOffset('foo')", $GLOBALS); // https://github.com/phpstan/phpstan/issues/8395
} else {
assertType('array<mixed>', $GLOBALS);
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Rules/Functions/data/bug-7156.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function foobar2(mixed $data): void
throw new \RuntimeException();
}

assertType("non-empty-array&hasOffsetValue('value', string)", $data);
assertType("non-empty-array<mixed, mixed>&hasOffsetValue('value', string)", $data);

foo($data);
}
Loading