Skip to content

Commit 95e510b

Browse files
committed
Move things around in Identical specification like a madman
1 parent 8d86993 commit 95e510b

File tree

2 files changed

+66
-60
lines changed

2 files changed

+66
-60
lines changed

src/Analyser/TypeSpecifier.php

Lines changed: 66 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,9 @@ public function specifyTypesInCondition(
240240
}
241241
}
242242

243+
$leftType = $scope->getType($expr->left);
243244
$rightType = $scope->getType($expr->right);
245+
244246
if (
245247
$expr->left instanceof ClassConstFetch &&
246248
$expr->left->class instanceof Expr &&
@@ -259,71 +261,71 @@ public function specifyTypesInCondition(
259261
);
260262
}
261263

262-
if ($context->true()) {
263-
$type = TypeCombinator::intersect($scope->getType($expr->right), $scope->getType($expr->left));
264-
$leftTypes = $this->create($expr->left, $type, $context, false, $scope);
265-
$rightTypes = $this->create($expr->right, $type, $context, false, $scope);
266-
return $leftTypes->unionWith($rightTypes);
264+
$types = null;
267265

268-
} elseif ($context->false()) {
269-
$identicalType = $scope->getType($expr);
270-
if ($identicalType instanceof ConstantBooleanType) {
271-
$never = new NeverType();
272-
$contextForTypes = $identicalType->getValue() ? $context->negate() : $context;
273-
$leftTypes = $this->create($expr->left, $never, $contextForTypes, false, $scope);
274-
$rightTypes = $this->create($expr->right, $never, $contextForTypes, false, $scope);
275-
return $leftTypes->unionWith($rightTypes);
266+
if (
267+
(
268+
$leftType instanceof ConstantType
269+
&& !$expr->right instanceof Node\Scalar
270+
) || $leftType instanceof EnumCaseObjectType
271+
) {
272+
$types = $this->create(
273+
$expr->right,
274+
$leftType,
275+
$context,
276+
false,
277+
$scope,
278+
);
279+
}
280+
281+
if (
282+
(
283+
$rightType instanceof ConstantType
284+
&& !$expr->left instanceof Node\Scalar
285+
) || $rightType instanceof EnumCaseObjectType
286+
) {
287+
$leftTypes = $this->create(
288+
$expr->left,
289+
$rightType,
290+
$context,
291+
false,
292+
$scope,
293+
);
294+
if ($types !== null) {
295+
$types = $types->unionWith($leftTypes);
296+
} else {
297+
$types = $leftTypes;
276298
}
299+
}
277300

278-
$exprLeftType = $scope->getType($expr->left);
279-
$exprRightType = $scope->getType($expr->right);
301+
if ($types !== null) {
302+
return $types;
303+
}
280304

281-
$types = null;
305+
$identicalType = $scope->getType($expr);
306+
if ($identicalType instanceof ConstantBooleanType && $context->false()) {
307+
$never = new NeverType();
308+
$contextForTypes = $identicalType->getValue() ? $context->negate() : $context;
309+
$leftTypes = $this->create($expr->left, $never, $contextForTypes, false, $scope);
310+
$rightTypes = $this->create($expr->right, $never, $contextForTypes, false, $scope);
311+
return $leftTypes->unionWith($rightTypes);
312+
}
282313

283-
if (
284-
(
285-
$exprLeftType instanceof ConstantType
286-
&& !$expr->right instanceof Node\Scalar
287-
) || $exprLeftType instanceof EnumCaseObjectType
288-
) {
289-
$types = $this->create(
290-
$expr->right,
291-
$exprLeftType,
292-
$context,
293-
false,
294-
$scope,
295-
);
296-
}
297-
if (
298-
(
299-
$exprRightType instanceof ConstantType
300-
&& !$expr->left instanceof Node\Scalar
301-
) || $exprRightType instanceof EnumCaseObjectType
302-
) {
303-
$leftType = $this->create(
304-
$expr->left,
305-
$exprRightType,
306-
$context,
307-
false,
308-
$scope,
309-
);
310-
if ($types !== null) {
311-
$types = $types->unionWith($leftType);
312-
} else {
313-
$types = $leftType;
314-
}
315-
}
314+
if (
315+
$expr->left instanceof Expr\Variable || $expr->right instanceof Expr\Variable
316+
|| $expr->left instanceof Node\Scalar || $expr->right instanceof Node\Scalar
317+
) {
318+
if ($context->true()) {
319+
$type = TypeCombinator::intersect($scope->getType($expr->right), $scope->getType($expr->left));
320+
$leftTypes = $this->create($expr->left, $type, $context, false, $scope);
321+
$rightTypes = $this->create($expr->right, $type, $context, false, $scope);
322+
return $leftTypes->unionWith($rightTypes);
316323

317-
if ($types !== null) {
318-
return $types;
319324
}
320325

321-
if ($expr->left instanceof Expr\Variable && $expr->right instanceof Expr\Variable) {
322-
return $this->create($expr->left, $exprLeftType, $context, false, $scope)->normalize($scope)
323-
->intersectWith($this->create($expr->right, $exprRightType, $context, false, $scope)->normalize($scope));
324-
}
326+
return $this->create($expr->left, $leftType, $context, false, $scope)->normalize($scope)
327+
->intersectWith($this->create($expr->right, $rightType, $context, false, $scope)->normalize($scope));
325328
}
326-
327329
} elseif ($expr instanceof Node\Expr\BinaryOp\NotIdentical) {
328330
return $this->specifyTypesInCondition(
329331
$scope,
@@ -443,9 +445,14 @@ public function specifyTypesInCondition(
443445
$leftTypes = $this->create($expr->left, $leftType, $context, false, $scope);
444446
$rightTypes = $this->create($expr->right, $rightType, $context, false, $scope);
445447

446-
return $context->true()
447-
? $leftTypes->unionWith($rightTypes)
448-
: $leftTypes->normalize($scope)->intersectWith($rightTypes->normalize($scope));
448+
if (
449+
$expr->left instanceof Expr\Variable || $expr->right instanceof Expr\Variable
450+
|| $expr->left instanceof Node\Scalar || $expr->right instanceof Node\Scalar
451+
) {
452+
return $context->true()
453+
? $leftTypes->unionWith($rightTypes)
454+
: $leftTypes->normalize($scope)->intersectWith($rightTypes->normalize($scope));
455+
}
449456
} elseif ($expr instanceof Node\Expr\BinaryOp\NotEqual) {
450457
return $this->specifyTypesInCondition(
451458
$scope,

tests/PHPStan/Analyser/TypeSpecifierTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,6 @@ public function dataCondition(): array
554554
),
555555
[
556556
'$foo' => '123',
557-
123 => '123',
558557
],
559558
['$foo' => '~123'],
560559
],

0 commit comments

Comments
 (0)