Skip to content

Commit 955a05d

Browse files
committed
Revert "Narrow variable type in switch cases"
This reverts commit f2cf5ca.
1 parent 8984ef9 commit 955a05d

File tree

6 files changed

+10
-152
lines changed

6 files changed

+10
-152
lines changed

src/Analyser/NodeScopeResolver.php

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,6 @@
206206
use function array_merge;
207207
use function array_pop;
208208
use function array_reverse;
209-
use function array_shift;
210209
use function array_slice;
211210
use function array_values;
212211
use function base64_decode;
@@ -1567,12 +1566,10 @@ private function processStmtNode(
15671566
$throwPoints = $condResult->getThrowPoints();
15681567
$impurePoints = $condResult->getImpurePoints();
15691568
$fullCondExpr = null;
1570-
$defaultCondExprs = [];
15711569
foreach ($stmt->cases as $caseNode) {
15721570
if ($caseNode->cond !== null) {
15731571
$condExpr = new BinaryOp\Equal($stmt->cond, $caseNode->cond);
15741572
$fullCondExpr = $fullCondExpr === null ? $condExpr : new BooleanOr($fullCondExpr, $condExpr);
1575-
$defaultCondExprs[] = new BinaryOp\NotEqual($stmt->cond, $caseNode->cond);
15761573
$caseResult = $this->processExprNode($stmt, $caseNode->cond, $scopeForBranches, $nodeCallback, ExpressionContext::createDeep());
15771574
$scopeForBranches = $caseResult->getScope();
15781575
$hasYield = $hasYield || $caseResult->hasYield();
@@ -1583,11 +1580,6 @@ private function processStmtNode(
15831580
$hasDefaultCase = true;
15841581
$fullCondExpr = null;
15851582
$branchScope = $scopeForBranches;
1586-
$defaultConditions = $this->createBooleanAndFromExpressions($defaultCondExprs);
1587-
if ($defaultConditions !== null) {
1588-
$branchScope = $this->processExprNode($stmt, $defaultConditions, $scope, static function (): void {
1589-
}, ExpressionContext::createDeep())->getTruthyScope()->filterByTruthyValue($defaultConditions);
1590-
}
15911583
}
15921584

15931585
$branchScope = $branchScope->mergeWith($prevScope);
@@ -6709,29 +6701,6 @@ private function getPhpDocReturnType(ResolvedPhpDocBlock $resolvedPhpDoc, Type $
67096701
return null;
67106702
}
67116703

6712-
/**
6713-
* @param list<Expr> $expressions
6714-
*/
6715-
private function createBooleanAndFromExpressions(array $expressions): ?Expr
6716-
{
6717-
if (count($expressions) === 0) {
6718-
return null;
6719-
}
6720-
6721-
if (count($expressions) === 1) {
6722-
return $expressions[0];
6723-
}
6724-
6725-
$left = array_shift($expressions);
6726-
$right = $this->createBooleanAndFromExpressions($expressions);
6727-
6728-
if ($right === null) {
6729-
throw new ShouldNotHappenException();
6730-
}
6731-
6732-
return new BooleanAnd($left, $right);
6733-
}
6734-
67356704
/**
67366705
* @param array<Node> $nodes
67376706
* @return list<Node\Stmt>

src/Analyser/TypeSpecifier.php

Lines changed: 9 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1643,45 +1643,24 @@ private function findTypeExpressionsFromBinaryOperation(Scope $scope, Node\Expr\
16431643
$leftType = $scope->getType($binaryOperation->left);
16441644
$rightType = $scope->getType($binaryOperation->right);
16451645

1646-
$rightExpr = $this->extractExpression($binaryOperation->right);
1647-
$leftExpr = $this->extractExpression($binaryOperation->left);
1648-
1649-
if (
1650-
$leftType instanceof ConstantScalarType
1651-
&& !$rightExpr instanceof ConstFetch
1652-
&& !$rightExpr instanceof ClassConstFetch
1653-
) {
1654-
return [$binaryOperation->right, $leftType, $rightType];
1655-
} elseif (
1656-
$rightType instanceof ConstantScalarType
1657-
&& !$leftExpr instanceof ConstFetch
1658-
&& !$leftExpr instanceof ClassConstFetch
1659-
) {
1660-
return [$binaryOperation->left, $rightType, $leftType];
1646+
$rightExpr = $binaryOperation->right;
1647+
if ($rightExpr instanceof AlwaysRememberedExpr) {
1648+
$rightExpr = $rightExpr->getExpr();
16611649
}
16621650

1663-
return null;
1664-
}
1665-
1666-
/**
1667-
* @return array{Expr, Type, Type}|null
1668-
*/
1669-
private function findEnumTypeExpressionsFromBinaryOperation(Scope $scope, Node\Expr\BinaryOp $binaryOperation): ?array
1670-
{
1671-
$leftType = $scope->getType($binaryOperation->left);
1672-
$rightType = $scope->getType($binaryOperation->right);
1673-
1674-
$rightExpr = $this->extractExpression($binaryOperation->right);
1675-
$leftExpr = $this->extractExpression($binaryOperation->left);
1651+
$leftExpr = $binaryOperation->left;
1652+
if ($leftExpr instanceof AlwaysRememberedExpr) {
1653+
$leftExpr = $leftExpr->getExpr();
1654+
}
16761655

16771656
if (
1678-
$leftType->getEnumCases() === [$leftType]
1657+
$leftType instanceof ConstantScalarType
16791658
&& !$rightExpr instanceof ConstFetch
16801659
&& !$rightExpr instanceof ClassConstFetch
16811660
) {
16821661
return [$binaryOperation->right, $leftType, $rightType];
16831662
} elseif (
1684-
$rightType->getEnumCases() === [$rightType]
1663+
$rightType instanceof ConstantScalarType
16851664
&& !$leftExpr instanceof ConstFetch
16861665
&& !$leftExpr instanceof ClassConstFetch
16871666
) {
@@ -1691,11 +1670,6 @@ private function findEnumTypeExpressionsFromBinaryOperation(Scope $scope, Node\E
16911670
return null;
16921671
}
16931672

1694-
private function extractExpression(Expr $expr): Expr
1695-
{
1696-
return $expr instanceof AlwaysRememberedExpr ? $expr->getExpr() : $expr;
1697-
}
1698-
16991673
/** @api */
17001674
public function create(
17011675
Expr $expr,
@@ -2087,27 +2061,6 @@ public function resolveEqual(Expr\BinaryOp\Equal $expr, Scope $scope, TypeSpecif
20872061
) {
20882062
return $this->specifyTypesInCondition($scope, new Expr\BinaryOp\Identical($expr->left, $expr->right), $context)->setRootExpr($expr);
20892063
}
2090-
2091-
if (!$context->null() && TypeCombinator::containsNull($otherType)) {
2092-
if ($constantType->toBoolean()->isTrue()->yes()) {
2093-
$otherType = TypeCombinator::remove($otherType, new NullType());
2094-
}
2095-
2096-
if (!$otherType->isSuperTypeOf($constantType)->no()) {
2097-
return $this->create($exprNode, TypeCombinator::intersect($constantType, $otherType), $context, $scope)->setRootExpr($expr);
2098-
}
2099-
}
2100-
}
2101-
2102-
$expressions = $this->findEnumTypeExpressionsFromBinaryOperation($scope, $expr);
2103-
if ($expressions !== null) {
2104-
$exprNode = $expressions[0];
2105-
$enumCaseObjectType = $expressions[1];
2106-
$otherType = $expressions[2];
2107-
2108-
if (!$context->null()) {
2109-
return $this->create($exprNode, TypeCombinator::intersect($enumCaseObjectType, $otherType), $context, $scope)->setRootExpr($expr);
2110-
}
21112064
}
21122065

21132066
$leftType = $scope->getType($expr->left);

tests/PHPStan/Analyser/NodeScopeResolverTest.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,10 @@ private static function findTestFiles(): iterable
101101
define('TEST_FALSE_CONSTANT', false);
102102
define('TEST_ARRAY_CONSTANT', [true, false, null]);
103103
define('TEST_ENUM_CONSTANT', Foo::ONE);
104-
yield __DIR__ . '/data/bug-12432-nullable-enum.php';
105104
yield __DIR__ . '/data/new-in-initializers-runtime.php';
106105
yield __DIR__ . '/data/scope-in-enum-match-arm-body.php';
107106
}
108107

109-
yield __DIR__ . '/data/bug-12432-nullable-int.php';
110-
111108
yield __DIR__ . '/../Rules/Comparison/data/bug-6473.php';
112109

113110
yield __DIR__ . '/../Rules/Methods/data/filter-iterator-child-class.php';

tests/PHPStan/Analyser/data/bug-12432-nullable-enum.php

Lines changed: 0 additions & 35 deletions
This file was deleted.

tests/PHPStan/Analyser/data/bug-12432-nullable-int.php

Lines changed: 0 additions & 26 deletions
This file was deleted.

tests/PHPStan/Analyser/nsrt/in_array_loose.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function looseComparison(
4242
assertType('int|string', $stringOrInt); // could be '1'|'2'|1|2
4343
}
4444
if (in_array($stringOrNull, ['1', 'a'])) {
45-
assertType("'1'|'a'", $stringOrNull);
45+
assertType('string|null', $stringOrNull); // could be '1'|'a'
4646
}
4747
}
4848
}

0 commit comments

Comments
 (0)