Skip to content

Fix InvalidComparisonOperationRule for UnionType #4168

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

Open
wants to merge 1 commit into
base: 2.1.x
Choose a base branch
from
Open
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
52 changes: 14 additions & 38 deletions src/Rules/Operators/InvalidComparisonOperationRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Rules\RuleLevelHelper;
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\BenevolentUnionType;
use PHPStan\Type\ArrayType;
use PHPStan\Type\ErrorType;
use PHPStan\Type\FloatType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\MixedType;
use PHPStan\Type\NullType;
use PHPStan\Type\ObjectWithoutClassType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\UnionType;
use PHPStan\Type\VerbosityLevel;
use function get_class;
Expand Down Expand Up @@ -51,15 +52,17 @@ public function processNode(Node $node, Scope $scope): array
return [];
}

if ($this->isNumberType($scope, $node->left) && $this->isNumberType($scope, $node->right)) {
$isLeftNumberType = $this->isNumberType($scope, $node->left);
$isRightNumberType = $this->isNumberType($scope, $node->right);
if (($isLeftNumberType && $isRightNumberType) || (!$isLeftNumberType && !$isRightNumberType)) {
return [];
}

if (
($this->isNumberType($scope, $node->left) && (
($isLeftNumberType && (
$this->isPossiblyNullableObjectType($scope, $node->right) || $this->isPossiblyNullableArrayType($scope, $node->right)
))
|| ($this->isNumberType($scope, $node->right) && (
|| ($isRightNumberType && (
$this->isPossiblyNullableObjectType($scope, $node->left) || $this->isPossiblyNullableArrayType($scope, $node->left)
))
) {
Expand Down Expand Up @@ -125,45 +128,18 @@ private function isNumberType(Scope $scope, Node\Expr $expr): bool

private function isPossiblyNullableObjectType(Scope $scope, Node\Expr $expr): bool
{
$acceptedType = new ObjectWithoutClassType();
$type = $scope->getType($expr);
$acceptedType = new UnionType([new ObjectWithoutClassType(), new NullType()]);

$type = $this->ruleLevelHelper->findTypeToCheck(
$scope,
$expr,
'',
static fn (Type $type): bool => $acceptedType->isSuperTypeOf($type)->yes(),
)->getType();

if ($type instanceof ErrorType) {
return false;
}

if (TypeCombinator::containsNull($type) && !$type->isNull()->yes()) {
$type = TypeCombinator::removeNull($type);
}

$isSuperType = $acceptedType->isSuperTypeOf($type);
if ($type instanceof BenevolentUnionType) {
return !$isSuperType->no();
}

return $isSuperType->yes();
return !$type->isNull()->yes() && $acceptedType->isSuperTypeOf($type)->yes();
}

private function isPossiblyNullableArrayType(Scope $scope, Node\Expr $expr): bool
{
$type = $this->ruleLevelHelper->findTypeToCheck(
$scope,
$expr,
'',
static fn (Type $type): bool => $type->isArray()->yes(),
)->getType();

if (TypeCombinator::containsNull($type) && !$type->isNull()->yes()) {
$type = TypeCombinator::removeNull($type);
}
$type = $scope->getType($expr);
$acceptedType = new UnionType([new ArrayType(new MixedType(), new MixedType()), new NullType()]);

return !($type instanceof ErrorType) && $type->isArray()->yes();
return !$type->isNull()->yes() && $acceptedType->isSuperTypeOf($type)->yes();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
class InvalidComparisonOperationRuleTest extends RuleTestCase
{

private bool $checkUnion = true;

protected function getRule(): Rule
{
return new InvalidComparisonOperationRule(
new RuleLevelHelper(self::createReflectionProvider(), true, false, true, false, false, false, true),
new RuleLevelHelper(self::createReflectionProvider(), true, false, $this->checkUnion, false, false, false, true),
);
}

Expand Down Expand Up @@ -165,6 +167,21 @@ public function testRuleWithNullsafeVariant(): void
]);
}

public function testBug3364(): void
{
$this->checkUnion = false;
$this->analyse([__DIR__ . '/data/bug-3364.php'], [
[
'Comparison operation "!=" between array<int|string>|null and 1 results in an error.',
18,
],
[
'Comparison operation "!=" between object|null and 1 results in an error.',
26,
],
]);
}

public function testBug11119(): void
{
$this->analyse([__DIR__ . '/data/bug-11119.php'], []);
Expand Down
42 changes: 42 additions & 0 deletions tests/PHPStan/Rules/Operators/data/bug-3364.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php declare(strict_types = 1);

namespace Bug3364;

class HelloWorld
{
/**
* @param string|array<int|string>|null $value
*/
public function transform($value) {
$value != 1;
}

/**
* @param array<int|string>|null $value
*/
public function transform2($value) {
$value != 1;
}


/**
* @param object|null $value
*/
public function transform3($value) {
$value != 1;
}

/**
* @param array<int|string>|object $value
*/
public function transform4($value) {
$value != 1;
}

/**
* @param null $value
*/
public function transform5($value) {
$value != 1;
}
}
Loading