Skip to content

Commit

Permalink
Retain left and right types in equal expression
Browse files Browse the repository at this point in the history
  • Loading branch information
herndlm committed Mar 3, 2022
1 parent 2d52a5b commit 3dc9167
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,10 @@ public function specifyTypesInCondition(
) {
return $this->specifyTypesInCondition($scope, new Expr\BinaryOp\Identical($expr->left, $expr->right), $context);
}

$leftTypes = $this->create($expr->left, $leftType, $context, false, $scope);
$rightTypes = $this->create($expr->right, $rightType, $context, false, $scope);
return $context->true() ? $leftTypes->unionWith($rightTypes) : $leftTypes->normalize($scope)->intersectWith($rightTypes->normalize($scope));
} elseif ($expr instanceof Node\Expr\BinaryOp\NotEqual) {
return $this->specifyTypesInCondition(
$scope,
Expand Down
32 changes: 32 additions & 0 deletions tests/PHPStan/Analyser/TypeSpecifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
use PHPStan\Type\ArrayType;
use PHPStan\Type\ClassStringType;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\FloatType;
use PHPStan\Type\Generic\GenericClassStringType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\MixedType;
use PHPStan\Type\NullType;
use PHPStan\Type\ObjectType;
Expand Down Expand Up @@ -68,6 +70,8 @@ protected function setUp(): void
$this->scope = $this->scope->assignVariable('classString', new ClassStringType());
$this->scope = $this->scope->assignVariable('genericClassString', new GenericClassStringType(new ObjectType('Bar')));
$this->scope = $this->scope->assignVariable('object', new ObjectWithoutClassType());
$this->scope = $this->scope->assignVariable('int', new IntegerType());
$this->scope = $this->scope->assignVariable('float', new FloatType());
}

/**
Expand Down Expand Up @@ -1120,6 +1124,34 @@ public function dataCondition(): array
'$foo' => 'mixed~non-empty-string',
],
],
[
new Expr\BinaryOp\BooleanAnd(
$this->createFunctionCall('is_numeric', 'int'),
new Expr\BinaryOp\Equal(
new Variable('int'),
new Expr\Cast\Int_(new Variable('int')),
),
),
[
'$int' => 'int',
'(int) $int' => 'int',
],
[],
],
[
new Expr\BinaryOp\BooleanAnd(
$this->createFunctionCall('is_numeric', 'float'),
new Expr\BinaryOp\Equal(
new Variable('float'),
new Expr\Cast\Int_(new Variable('float')),
),
),
[
'$float' => 'float',
'(int) $float' => 'int',
],
[],
],
];
}

Expand Down

0 comments on commit 3dc9167

Please sign in to comment.