Skip to content

Commit

Permalink
TypeSpecifier - fix assignments
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed May 8, 2021
1 parent ea1313b commit b2bf703
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
10 changes: 4 additions & 6 deletions src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,6 @@ public function specifyTypesInCondition(
{
if ($expr instanceof Instanceof_) {
$exprNode = $expr->expr;
if ($exprNode instanceof Expr\Assign) {
$exprNode = $exprNode->var;
}
if ($expr->class instanceof Name) {
$className = (string) $expr->class;
$lowercasedClassName = strtolower($className);
Expand Down Expand Up @@ -178,9 +175,6 @@ public function specifyTypesInCondition(
if ($expressions !== null) {
/** @var Expr $exprNode */
$exprNode = $expressions[0];
if ($exprNode instanceof Expr\Assign) {
$exprNode = $exprNode->var;
}
/** @var \PHPStan\Type\ConstantScalarType $constantType */
$constantType = $expressions[1];
if ($constantType->getValue() === false) {
Expand Down Expand Up @@ -914,6 +908,10 @@ public function create(
return new SpecifiedTypes();
}

while ($expr instanceof Expr\Assign) {
$expr = $expr->var;
}

if ($scope !== null) {
if ($context->true()) {
$resultType = TypeCombinator::intersect($scope->getType($expr), $type);
Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,7 @@ public function dataFileAsserts(): iterable
}

yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-4814.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-4982.php');
}

/**
Expand Down
22 changes: 22 additions & 0 deletions tests/PHPStan/Analyser/data/bug-4982.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Bug4982;

use function PHPStan\Testing\assertType;

class Foo
{

public function doFoo(): void
{
if (!is_null($c = $this->doBar())) {
assertType('int', $c);
}
}

public function doBar(): ?int
{

}

}

0 comments on commit b2bf703

Please sign in to comment.