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
25 changes: 23 additions & 2 deletions src/Parser/AssignOpTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ protected function parseAssignFinally(
// Parent/interface/abstract declarations are not precise enough for a concrete typed object.
$runtimeObjectAssignClass = $leftClass;
} else {
$this->fatalError($left, "Cannot re-assign typed object `\${$var}` from `{$leftClass}` to `{$rightClass}`");
$this->reassignInferredObjectVar($left, $var, $leftClass, $rightClass);
}
} else {
$this->checkVarAssignExpr($left, $this->getVarType($var), Type::OBJECT);
Expand Down Expand Up @@ -644,7 +644,7 @@ protected function parseAssignFinally(
} elseif ($this->isInterface($rightClass) || $this->isAbstractClass($rightClass) || $this->isObjectClassStaticallyAssignableTo($leftClass, $rightClass)) {
$runtimeObjectAssignClass = $leftClass;
} else {
$this->fatalError($left, "Cannot re-assign typed object `\${$var}` from `{$leftClass}` to `{$rightClass}`");
$this->reassignInferredObjectVar($left, $var, $leftClass, $rightClass);
}
}
}
Expand Down Expand Up @@ -733,6 +733,27 @@ protected function parseAssignFinally(
return $var . ' = ' . $assignedExpr;
}

/**
* PHP local variables carry no type declaration; the class tracked for
* `$var` was inferred from an earlier assignment. Re-assigning an unrelated
* concrete class in a mutually exclusive branch (for example if/else) is
* valid PHP, so widen the inferred local to a generic dynamic object rather
* than rejecting it. Parameters, native objects, and explicitly declared
* object types keep their strict re-assignment check.
*/
protected function reassignInferredObjectVar(Expr $left, string $var, string $leftClass, string $rightClass): void
{
$canWiden = !$this->hasArgument($var)
&& !$this->isNativeObjectVar($var)
&& isset($this->context->objects[$var])
&& !isset($this->context->declaredObjects[$var]);
if (!$canWiden) {
$this->fatalError($left, "Cannot re-assign typed object `\${$var}` from `{$leftClass}` to `{$rightClass}`");
}
unset($this->context->objects[$var], $this->context->stableObjects[$var], $this->context->declaredObjects[$var]);
$this->addLocalVar($var, Type::OBJECT);
}

protected function parseAssignPropertyHook(
Expr\PropertyFetch $left,
Expr $right,
Expand Down
29 changes: 29 additions & 0 deletions tests/compiler/reflection/inferred-object-reassign.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
--TEST--
Re-assign an inferred local object to a different class across if/else
--FILE--
<?php
function main(): void {
// A local variable has no type declaration: its class is inferred from the
// first assignment. Assigning a different concrete class in a mutually
// exclusive branch is valid PHP, so the inferred local must widen to a
// dynamic object rather than being rejected.
$a = 'strlen';
if (is_string($a)) {
$ref = new ReflectionFunction($a);
} else {
$ref = new ReflectionMethod($a, 'count');
}
echo get_class($ref), "\n";

$b = [ArrayObject::class, 'count'];
if (is_string($b)) {
$ref2 = new ReflectionFunction($b);
} else {
$ref2 = new ReflectionMethod($b[0], $b[1]);
}
echo get_class($ref2), "\n";
}
?>
--EXPECT--
ReflectionFunction
ReflectionMethod