Skip to content
Open
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
17 changes: 17 additions & 0 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -5940,6 +5940,7 @@ private function processArgs(
) {
$this->callNodeCallback($nodeCallback, new InvalidateExprNode($arg->value), $scope, $storage);
$scope = $scope->invalidateExpression($arg->value, true);
$scope = $this->widenUniversalObjectCrateProperties($scope, $arg->value);
}
} elseif (!(new ResourceType())->isSuperTypeOf($argType)->no()) {
$this->callNodeCallback($nodeCallback, new InvalidateExprNode($arg->value), $scope, $storage);
Expand All @@ -5953,6 +5954,22 @@ private function processArgs(
return new ExpressionResult($scope, $hasYield, $isAlwaysTerminating, $throwPoints, $impurePoints);
}

private function widenUniversalObjectCrateProperties(MutatingScope $scope, Expr $expr): MutatingScope
{
$argType = $scope->getType($expr);
$stdClassType = new ObjectType('stdClass');

if (!in_array('stdClass', $argType->getObjectClassNames(), true)) {
return $scope;
}
Comment on lines +5960 to +5964
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this should work for any ObjectShapeType and not only stdClass.

Copy link
Contributor

@staabm staabm Mar 1, 2026

Choose a reason for hiding this comment

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

should the logic be part of MutatingScope->invalidateExpression() or MutatingScope->shouldInvalidateExpression()?

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe, but I think that's already not the right logic.


if ($stdClassType->isSuperTypeOf($argType)->yes() && !$argType->equals($stdClassType)) {
return $scope->assignExpression($expr, $stdClassType, $stdClassType);
}

return $scope;
}

/**
* @param MethodReflection|FunctionReflection|null $calleeReflection
*/
Expand Down
27 changes: 27 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-9181.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php declare(strict_types = 1);

namespace Bug9181;

use function PHPStan\Testing\assertType;

class HelloWorld
{
private function possiblyModifyObject(object $data): void {}

public function sayHello(): void
{
$data = (object)[
'search' => null,
];

assertType('null', $data->search);

$this->possiblyModifyObject($data);

assertType('mixed', $data->search);

if (($search = $data->search) !== null) {
assertType('mixed~null', $search);
}
}
}
Loading