Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retain left and right types in equal expression #1046

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,9 @@ public function specifyTypesInCondition(
if ($types !== null) {
return $types;
}

return $this->create($expr->left, $exprLeftType, $context, false, $scope)->normalize($scope)
->intersectWith($this->create($expr->right, $exprRightType, $context, false, $scope)->normalize($scope));
}

} elseif ($expr instanceof Node\Expr\BinaryOp\NotIdentical) {
Expand Down Expand Up @@ -434,6 +437,13 @@ 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
1 change: 1 addition & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,7 @@ public function dataFileAsserts(): iterable

yield from $this->gatherAssertTypes(__DIR__ . '/data/weird-array_key_exists-issue.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/equal.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/identical.php');

if (PHP_VERSION_ID >= 80000) {
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-5698-php8.php');
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
34 changes: 34 additions & 0 deletions tests/PHPStan/Analyser/data/equal.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,38 @@ public function doIpsum(array $a): void
assertType('array', $a);
}

public function stdClass(\stdClass $a, \stdClass $b): void
{
if ($a == $a) {
assertType('stdClass', $a);
} else {
assertType('*NEVER*', $a);
}

if ($b != $b) {
assertType('*NEVER*', $b);
} else {
assertType('stdClass', $b);
}

if ($a == $b) {
assertType('stdClass', $a);
assertType('stdClass', $b);
} else {
assertType('stdClass', $a);
assertType('stdClass', $b);
}

if ($a != $b) {
assertType('stdClass', $a);
assertType('stdClass', $b);
} else {
assertType('stdClass', $a);
assertType('stdClass', $b);
}

assertType('stdClass', $a);
assertType('stdClass', $b);
}

}
44 changes: 44 additions & 0 deletions tests/PHPStan/Analyser/data/identical.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace TypeSpecifierIdentical;

use function PHPStan\Testing\assertType;

class Foo
{

public function foo(\stdClass $a, \stdClass $b): void
{
if ($a === $a) {
assertType('stdClass', $a);
} else {
assertType('*NEVER*', $a);
}

if ($b !== $b) {
herndlm marked this conversation as resolved.
Show resolved Hide resolved
assertType('*NEVER*', $b);
} else {
assertType('stdClass', $b);
}

if ($a === $b) {
assertType('stdClass', $a);
assertType('stdClass', $b);
} else {
assertType('stdClass', $a);
assertType('stdClass', $b);
}

if ($a !== $b) {
assertType('stdClass', $a);
assertType('stdClass', $b);
} else {
assertType('stdClass', $a);
assertType('stdClass', $b);
}

assertType('stdClass', $a);
assertType('stdClass', $b);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ public function testRule(): void
'Call to method ImpossibleMethodCall\Foo::isSame() with stdClass and stdClass will always evaluate to true.',
78,
],
[
'Call to method ImpossibleMethodCall\Foo::isNotSame() with stdClass and stdClass will always evaluate to false.',
81,
],
[
'Call to method ImpossibleMethodCall\Foo::isSame() with *NEVER* and stdClass will always evaluate to false.',
herndlm marked this conversation as resolved.
Show resolved Hide resolved
84,
],
]);
}

Expand Down