Skip to content

Commit

Permalink
Fixed regression from commit 9665e16
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jan 17, 2020
1 parent 7e57ca0 commit c981d89
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 4 deletions.
5 changes: 2 additions & 3 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -2574,15 +2574,14 @@ public function assignExpression(Expr $expr, Type $type): self
return $scope->specifyExpressionType($expr, $type);
}

public function invalidateExpression(Expr $expressionToInvalidate): self
public function invalidateExpression(Expr $expressionToInvalidate, bool $requireMoreCharacters = false): self
{
$exprStringToInvalidate = $this->printer->prettyPrintExpr($expressionToInvalidate);
$moreSpecificTypeHolders = $this->moreSpecificTypes;
foreach (array_keys($moreSpecificTypeHolders) as $exprString) {
$exprString = (string) $exprString;
if (Strings::startsWith($exprString, $exprStringToInvalidate)) {
if ($exprString === $exprStringToInvalidate) {
unset($moreSpecificTypeHolders[$exprString]);
if ($exprString === $exprStringToInvalidate && $requireMoreCharacters) {
continue;
}
$nextLetter = substr($exprString, strlen($exprStringToInvalidate), 1);
Expand Down
2 changes: 1 addition & 1 deletion src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -1492,7 +1492,7 @@ function (MutatingScope $scope) use ($expr, $nodeCallback, $context): Expression
$result = $this->processArgs($methodReflection, $parametersAcceptor, $expr->args, $scope, $nodeCallback, $context);
$scope = $result->getScope();
if ($methodReflection !== null && $methodReflection->hasSideEffects()->yes()) {
$scope = $scope->invalidateExpression($expr->var);
$scope = $scope->invalidateExpression($expr->var, true);
}
$hasYield = $hasYield || $result->hasYield();
} elseif ($expr instanceof StaticCall) {
Expand Down
6 changes: 6 additions & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9772,6 +9772,11 @@ public function dataBug2750(): array
return $this->gatherAssertTypes(__DIR__ . '/data/bug-2750.php');
}

public function dataBug2850(): array
{
return $this->gatherAssertTypes(__DIR__ . '/data/bug-2850.php');
}

/**
* @dataProvider dataBug2574
* @dataProvider dataBug2577
Expand Down Expand Up @@ -9799,6 +9804,7 @@ public function dataBug2750(): array
* @dataProvider dataBug2835
* @dataProvider dataBug2443
* @dataProvider dataBug2750
* @dataProvider dataBug2850
* @param ConstantStringType $expectedType
* @param Type $actualType
*/
Expand Down
34 changes: 34 additions & 0 deletions tests/PHPStan/Analyser/data/bug-2850.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Bug2850;

use function PHPStan\Analyser\assertType;

class Foo
{
public function y(): void {}
}

class Bar
{
/** @var Foo|null */
private $x;

public function getFoo(): Foo
{
if ($this->x === null) {
$this->x = new Foo();
assertType(Foo::class, $this->x);
$this->x->y();
assertType(Foo::class, $this->x);
$this->y();
assertType(Foo::class . '|null', $this->x);
}
return $this->x;
}

public function y(): void
{
$this->x = null;
}
}

0 comments on commit c981d89

Please sign in to comment.