Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use PhpParser\Node\Stmt\Function_;
use PhpParser\NodeFinder;
use PHPStan\Node\ExecutionEndNode;
use PHPStan\Node\Expr\AfterStaticMethodCall;
use PHPStan\Node\Expr\AlwaysRememberedExpr;
use PHPStan\Node\Expr\ExistingArrayDimFetch;
use PHPStan\Node\Expr\GetIterableKeyTypeExpr;
Expand Down Expand Up @@ -4561,9 +4562,9 @@ private function shouldInvalidateExpression(string $exprStringToInvalidate, Expr

$nodeFinder = new NodeFinder();
$expressionToInvalidateClass = get_class($exprToInvalidate);
$found = $nodeFinder->findFirst([$expr], function (Node $node) use ($expressionToInvalidateClass, $exprStringToInvalidate): bool {
$found = $nodeFinder->findFirst([$expr], function (Node $node) use ($expressionToInvalidateClass, $exprToInvalidate, $exprStringToInvalidate): bool {
if (
$exprStringToInvalidate === '$this'
($exprStringToInvalidate === '$this' || $exprToInvalidate instanceof AfterStaticMethodCall)
&& $node instanceof Name
&& (
in_array($node->toLowerString(), ['self', 'static', 'parent'], true)
Expand All @@ -4573,6 +4574,15 @@ private function shouldInvalidateExpression(string $exprStringToInvalidate, Expr
return true;
}

if (
$exprToInvalidate instanceof AfterStaticMethodCall
&& $node instanceof MethodCall
&& $node->var instanceof Variable
&& $node->var->name === 'this'
) {
return true;
}

if (!$node instanceof $expressionToInvalidateClass) {
return false;
}
Expand Down
7 changes: 6 additions & 1 deletion src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
use PHPStan\Node\ClosureReturnStatementsNode;
use PHPStan\Node\DoWhileLoopConditionNode;
use PHPStan\Node\ExecutionEndNode;
use PHPStan\Node\Expr\AfterStaticMethodCall;
use PHPStan\Node\Expr\AlwaysRememberedExpr;
use PHPStan\Node\Expr\ExistingArrayDimFetch;
use PHPStan\Node\Expr\ForeachValueByRefExpr;
Expand Down Expand Up @@ -3117,7 +3118,11 @@ static function (): void {
&& $scope->isInClass()
&& $scope->getClassReflection()->is($methodReflection->getDeclaringClass()->getName())
) {
$scope = $scope->invalidateExpression(new Variable('this'), true);
if ($methodReflection->isStatic()) {
$scope = $scope->invalidateExpression(new AfterStaticMethodCall(), true);
} else {
$scope = $scope->invalidateExpression(new Variable('this'), true);
}
}

if (
Expand Down
27 changes: 27 additions & 0 deletions src/Node/Expr/AfterStaticMethodCall.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php declare(strict_types = 1);

namespace PHPStan\Node\Expr;

use Override;
use PhpParser\Node\Expr;
use PHPStan\Node\VirtualNode;

final class AfterStaticMethodCall extends Expr implements VirtualNode
{

#[Override]
public function getType(): string
{
return 'PHPStan_Node_AfterStaticMethodCall';
}

/**
* @return string[]
*/
#[Override]
public function getSubNodeNames(): array
{
return [];
}

}
6 changes: 6 additions & 0 deletions src/Node/Printer/Printer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PhpParser\PrettyPrinter\Standard;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Node\Expr\AfterStaticMethodCall;
use PHPStan\Node\Expr\AlwaysRememberedExpr;
use PHPStan\Node\Expr\ExistingArrayDimFetch;
use PHPStan\Node\Expr\ForeachValueByRefExpr;
Expand All @@ -29,6 +30,11 @@
final class Printer extends Standard
{

protected function pPHPStan_Node_AfterStaticMethodCall(AfterStaticMethodCall $expr): string // phpcs:ignore
{
return sprintf('__phpstanAfterStaticMethodCall');
}

protected function pPHPStan_Node_TypeExpr(TypeExpr $expr): string // phpcs:ignore
{
return sprintf('__phpstanType(%s)', $expr->getExprType()->describe(VerbosityLevel::precise()));
Expand Down
59 changes: 59 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-13735.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Bug13735;

use function PHPStan\Testing\assertType;
use function rand;

class Bug13735Test
{
private ?Foo $foo = null;
public static ?Foo $staticFoo = null;

public function testFoo(): void
{
$this->foo = new Foo();
assertType('Bug13735\Foo', $this->foo);
self::assertTrue(true);
assertType('Bug13735\Foo', $this->foo);

assertType('bool', $this->foo->aBool);
$this->foo->aBool = true;
assertType('true', $this->foo->aBool);
self::assertTrue(true);
assertType('true', $this->foo->aBool);
}

public function testCallFoo(): void
{
if ($this->getFoo() === null) {
return;
}

// the getFoo() method could reference a static property in its body,
// so self::assertTrue() still needs to invalidate $this->getFoo().
assertType('Bug13735\Foo', $this->getFoo());
self::assertTrue(true);
assertType('Bug13735\Foo|null', $this->getFoo());
}

public function testStaticFoo(): void
{
self::$staticFoo = new Foo();
assertType('Bug13735\Foo', self::$staticFoo);
self::assertTrue(true);
assertType('Bug13735\Foo|null', self::$staticFoo);
}

public static function assertTrue(mixed $condition, string $message = ''): void
{
}

public function getFoo(): ?Foo {
return rand(0 ,1) ? null : new Foo();
}
}

class Foo {
public bool $aBool = false;
}
Loading