Skip to content
Merged
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
10 changes: 8 additions & 2 deletions src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,10 @@ public function specifyTypesInCondition(
$types = null;
$exprLeftType = $scope->getType($expr->left);
$exprRightType = $scope->getType($expr->right);
if ($exprLeftType instanceof ConstantType || $exprLeftType instanceof EnumCaseObjectType) {
if (
($exprLeftType instanceof ConstantType && !$exprRightType->equals($exprLeftType) && $exprRightType->isSuperTypeOf($exprLeftType)->yes())
|| $exprLeftType instanceof EnumCaseObjectType
) {
$types = $this->create(
$expr->right,
$exprLeftType,
Expand All @@ -314,7 +317,10 @@ public function specifyTypesInCondition(
$rootExpr,
);
}
if ($exprRightType instanceof ConstantType || $exprRightType instanceof EnumCaseObjectType) {
if (
($exprRightType instanceof ConstantType && !$exprLeftType->equals($exprRightType) && $exprLeftType->isSuperTypeOf($exprRightType)->yes())
|| $exprRightType instanceof EnumCaseObjectType
) {
$leftType = $this->create(
$expr->left,
$exprRightType,
Expand Down
27 changes: 27 additions & 0 deletions tests/PHPStan/Analyser/data/identical.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,31 @@ public static function createStdClass(): \stdClass

}

public function doBar(array $arr1, array $arr2): void
{
/**
* @var array{foo: bool, bar: int} $arr1
* @var array{foo: bool, bar: int} $arr2
*/
if ($arr1 === $arr2) {
assertType('array{foo: bool, bar: int}', $arr1);
assertType('array{foo: bool, bar: int}', $arr2);
} else {
assertType('array{foo: bool, bar: int}', $arr1);
assertType('array{foo: bool, bar: int}', $arr2);
}

/**
* @var array{foo: true, bar: 17} $arr1
* @var array{foo: bool, bar: int} $arr2
*/
if ($arr1 === $arr2) {
assertType('array{foo: true, bar: 17}', $arr1);
assertType('array{foo: true, bar: 17}', $arr2);
} else {
assertType('array{foo: true, bar: 17}', $arr1);
assertType('array{foo: bool, bar: int}', $arr2);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -566,4 +566,10 @@ public function testBug7555(): void
]);
}

public function testBug7257(): void
{
$this->checkAlwaysTrueStrictComparison = false;
$this->analyse([__DIR__ . '/data/bug-7257.php'], []);
}

}
31 changes: 31 additions & 0 deletions tests/PHPStan/Rules/Comparison/data/bug-7257.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php declare(strict_types = 1);

namespace Bug7257;

function rangesInConflict(): bool
{
$ranges1 = array_filter([
(int) rand(0,10),
(int) rand(0,10),
(int) rand(0,10),
(int) rand(0,10),
]);

$ranges2 = array_filter([
(int) rand(0,10),
(int) rand(0,10),
(int) rand(0,10),
(int) rand(0,10),
]);

$sorted = array_filter([
(int) rand(0,10),
(int) rand(0,10),
(int) rand(0,10),
(int) rand(0,10),
]);

sort($sorted);

return !($sorted === $ranges1 || $sorted === $ranges2);
}