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
14 changes: 13 additions & 1 deletion src/Analyser/ExprHandler/MethodCallHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,19 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
$throwPoints[] = $methodThrowPoint;
}

if ($methodReflection->getName() === '__construct' || $methodReflection->hasSideEffects()->yes()) {
$hasSideEffects = $methodReflection->hasSideEffects();
if (
$methodReflection->getName() === '__construct'
|| $hasSideEffects->yes()
// When possibly impure values are not remembered, a possibly impure method
// may also have changed the object's properties, so forget them too.
// Built-in methods are exempt, as they are from invalidating volatile expressions below.
|| (
!$this->rememberPossiblyImpureFunctionValues
&& !$hasSideEffects->no()
&& !$methodReflection->getDeclaringClass()->isBuiltin()
)
) {
$nodeScopeResolver->callNodeCallback($nodeCallback, new InvalidateExprNode($normalizedExpr->var), $scope, $storage);
$scope = $scope->invalidateExpression($normalizedExpr->var, true, $methodReflection->getDeclaringClass(), $methodReflection->isStatic());
}
Expand Down
35 changes: 22 additions & 13 deletions src/Analyser/ExprHandler/StaticCallHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -373,21 +373,30 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
}
}

if (
$expr->class instanceof Name
&& $methodReflection !== null
&& (
if ($expr->class instanceof Name && $methodReflection !== null) {
$hasSideEffects = $methodReflection->hasSideEffects();
if (
(
!$methodReflection->isStatic()
&& $methodReflection->getName() === '__construct'
(
!$methodReflection->isStatic()
&& $methodReflection->getName() === '__construct'
)
|| $hasSideEffects->yes()
// When possibly impure values are not remembered, a possibly impure method
// may also have changed the object's properties, so forget them too.
// Built-in methods are exempt, as they are from invalidating volatile expressions below.
|| (
!$this->rememberPossiblyImpureFunctionValues
&& !$hasSideEffects->no()
&& !$methodReflection->getDeclaringClass()->isBuiltin()
)
)
|| $methodReflection->hasSideEffects()->yes()
)
&& $scope->isInClass()
&& $scope->getClassReflection()->is($methodReflection->getDeclaringClass()->getName())
) {
// a static method never receives $this, so property fetches on it survive
$scope = $scope->invalidateExpression(new Variable('this'), true, $methodReflection->getDeclaringClass(), $methodReflection->isStatic());
&& $scope->isInClass()
&& $scope->getClassReflection()->is($methodReflection->getDeclaringClass()->getName())
) {
// a static method never receives $this, so property fetches on it survive
$scope = $scope->invalidateExpression(new Variable('this'), true, $methodReflection->getDeclaringClass(), $methodReflection->isStatic());
}
}

if (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class DoNotRememberPossiblyImpureFunctionValuesTest extends TypeInferenceTestCas
public static function dataAsserts(): iterable
{
yield from self::gatherAssertTypes(__DIR__ . '/data/do-not-remember-possibly-impure-function-values.php');
yield from self::gatherAssertTypes(__DIR__ . '/data/do-not-remember-possibly-impure-function-values-properties.php');
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
<?php

namespace DoNotRememberPossiblyImpureFunctionValuesProperties;

use function PHPStan\Testing\assertType;

class StateMachine
{

/** @var 'ready'|'done' */
public $state = 'ready';

/** @var 'ready'|'done' */
public static $staticState = 'ready';

public function advance(): bool
{
$this->state = 'done';
return true;
}

/** @phpstan-pure */
public function pure(): bool
{
return true;
}

/** @phpstan-impure */
public function impure(): bool
{
$this->state = 'done';
return true;
}

public static function advanceStatic(): bool
{
self::$staticState = 'done';
return true;
}

/** @phpstan-pure */
public static function pureStatic(): bool
{
return true;
}

public function doFoo(): void
{
$this->state = 'ready';
$this->advance();
assertType("'done'|'ready'", $this->state);

$this->state = 'ready';
if ($this->advance()) {
assertType("'done'|'ready'", $this->state);
}

$this->state = 'ready';
$this->pure();
assertType("'ready'", $this->state);

$this->state = 'ready';
$this->impure();
assertType("'done'|'ready'", $this->state);
}

public function doLocalVariable(): void
{
$state = 'ready';
$this->advance();
assertType("'ready'", $state);
}

public function doOtherObject(self $other): void
{
$this->state = 'ready';
$other->state = 'ready';
$other->advance();
assertType("'done'|'ready'", $other->state);
assertType("'ready'", $this->state);
}

public function doNullsafe(?self $other): void
{
if ($other === null) {
return;
}

$other->state = 'ready';
$other?->advance();
assertType("'done'|'ready'", $other->state);
}

public function doStatic(): void
{
self::$staticState = 'ready';
self::advanceStatic();
assertType("'done'|'ready'", self::$staticState);

self::$staticState = 'ready';
static::advanceStatic();
assertType("'done'|'ready'", self::$staticState);

self::$staticState = 'ready';
self::pureStatic();
assertType("'ready'", self::$staticState);
}

public function doStaticKeepsInstanceProperties(): void
{
$this->state = 'ready';
self::advanceStatic();
assertType("'ready'", $this->state);
}

public static function doStaticFromStaticMethod(): void
{
self::$staticState = 'ready';
self::advanceStatic();
assertType("'done'|'ready'", self::$staticState);

self::$staticState = 'ready';
self::pureStatic();
assertType("'ready'", self::$staticState);
}

}

class Holder
{

public ?\stdClass $foo = null;

/** @phpstan-assert-if-true !null $this->foo */
public function hasFoo(): bool
{
return $this->foo !== null;
}

public function doAssertIfTrue(): void
{
if ($this->hasFoo()) {
assertType('stdClass', $this->foo);
}
}

}

/** @extends \ArrayObject<int, int> */
class Bag extends \ArrayObject
{

public ?int $n = null;

public function doBuiltinMethod(): void
{
$this->n = 1;
$this->count();
$this->getIterator();
assertType('1', $this->n);
}

}

class ChildStateMachine extends StateMachine
{

public function advance(): bool
{
$this->state = 'ready';
parent::advance();
assertType("'done'|'ready'", $this->state);

return true;
}

}
Loading
Loading