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
18 changes: 17 additions & 1 deletion src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -590,11 +590,18 @@ public function specifyTypesInCondition(
));
$specifiedTypes = $this->specifyTypesFromAsserts($context, $expr, $asserts, $parametersAcceptor, $scope);
if ($specifiedTypes !== null) {
if ($context->null() && $expr->var instanceof MethodCall) {
return $specifiedTypes->unionWith($this->specifyTypesInCondition($scope, $expr->var, $context));
}
return $specifiedTypes;
}
}
}

if ($context->null() && $expr->var instanceof MethodCall) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think we need something which does not only work on $context->null()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The best solution would be if ExpressionResult (returned by processExprNode) would also contain SpecifiedTypes, effectively merging NodeScopeResolver and TypeSpecifier.

I think that half-measures like this are not worth it.

return $this->specifyTypesInCondition($scope, $expr->var, $context);
}

return $this->handleDefaultTruthyOrFalseyContext($context, $expr, $scope);
} elseif ($expr instanceof StaticCall && $expr->name instanceof Node\Identifier) {
if ($expr->class instanceof Name) {
Expand Down Expand Up @@ -1665,7 +1672,16 @@ private function specifyTypesFromAsserts(TypeSpecifierContext $context, Expr\Cal
}

if ($call instanceof MethodCall) {
$argsMap['this'] = [$call->var];
$callVar = $call->var;
while ($callVar instanceof MethodCall) {
$callerType = $scope->getType($callVar->var);
$returnType = $scope->getType($callVar);
if (!$callerType->equals($returnType)) {
break;
}
$callVar = $callVar->var;
}
$argsMap['this'] = [$callVar];
}

/** @var SpecifiedTypes|null $types */
Expand Down
46 changes: 46 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-13902.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php // lint >= 8.0

declare(strict_types = 1);

namespace Bug13902;

use function PHPStan\Testing\assertType;

class HelloWorld
{
public mixed $a = null;
public mixed $b = null;

/**
* @phpstan-assert int $this->a
* @return $this
*/
public function setA()
{
$this->a = 1;
return $this;
}
/**
* @phpstan-assert int $this->b
* @return $this
*/
public function setB()
{
$this->b = 1;
return $this;
}
}

function test(): void
{
$o = new HelloWorld;
$o->setA()->setB();
assertType('int', $o->a);
assertType('int', $o->b);

$o2 = new HelloWorld;
$o2->setA();
$o2->setB();
assertType('int', $o2->a);
assertType('int', $o2->b);
}
Loading