Skip to content

Commit

Permalink
Fix param-type specification interferes with root-expression types
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm committed Jun 7, 2023
1 parent ffc0495 commit 2c30a91
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,15 +180,13 @@ public function specifyTypesInCondition(
return $this->create($exprNode, new ObjectWithoutClassType(), $context, false, $scope, $rootExpr);
}
} elseif ($expr instanceof Node\Expr\BinaryOp\Identical) {
$binaryExpressionSpecifiedType = null;
$expressions = $this->findTypeExpressionsFromBinaryOperation($scope, $expr);
if ($expressions !== null) {
$exprNode = $expressions[0];
$constantType = $expressions[1];

$specifiedType = $this->specifyTypesForConstantBinaryExpression($exprNode, $constantType, $context, $scope, $rootExpr);
if ($specifiedType !== null) {
return $specifiedType;
}
$binaryExpressionSpecifiedType = $this->specifyTypesForConstantBinaryExpression($exprNode, $constantType, $context, $scope, $rootExpr);
}

$rightType = $scope->getType($expr->right);
Expand Down Expand Up @@ -259,7 +257,12 @@ public function specifyTypesInCondition(
}

if ($types !== null) {
if ($binaryExpressionSpecifiedType !== null) {
return $types->unionWith($binaryExpressionSpecifiedType);
}
return $types;
} elseif ($binaryExpressionSpecifiedType !== null) {
return $binaryExpressionSpecifiedType;
}

$leftExprString = $this->exprPrinter->printExpr($expr->left);
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 @@ -1256,6 +1256,7 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/data/extract.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/image-size.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/base64_decode.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-9404.php');
}

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

namespace Bug9404;

use function PHPStan\Testing\assertType;

function foo(int|string $x): void
{
if (gettype($x) === 'integer') {
assertType("'integer'", gettype($x));
}
}

0 comments on commit 2c30a91

Please sign in to comment.