Skip to content

Commit 3dffa19

Browse files
staabmondrejmirtes
authored andcommitted
Fixed comparison of empty array with a scalar type
1 parent da5db91 commit 3dffa19

File tree

5 files changed

+637
-0
lines changed

5 files changed

+637
-0
lines changed

Diff for: src/Reflection/InitializerExprTypeResolver.php

+9
Original file line numberDiff line numberDiff line change
@@ -1253,6 +1253,15 @@ public function resolveEqualType(Type $leftType, Type $rightType): BooleanType
12531253
return $this->resolveIdenticalType($leftType, $rightType);
12541254
}
12551255

1256+
if ($leftType instanceof ConstantArrayType && $leftType->isEmpty() && $rightType instanceof ConstantScalarType) {
1257+
// @phpstan-ignore-next-line
1258+
return new ConstantBooleanType($rightType->getValue() == []); // phpcs:ignore
1259+
}
1260+
if ($rightType instanceof ConstantArrayType && $rightType->isEmpty() && $leftType instanceof ConstantScalarType) {
1261+
// @phpstan-ignore-next-line
1262+
return new ConstantBooleanType($leftType->getValue() == []); // phpcs:ignore
1263+
}
1264+
12561265
if ($leftType instanceof ConstantScalarType && $rightType instanceof ConstantScalarType) {
12571266
// @phpstan-ignore-next-line
12581267
return new ConstantBooleanType($leftType->getValue() == $rightType->getValue()); // phpcs:ignore

Diff for: tests/PHPStan/Analyser/NodeScopeResolverTest.php

+8
Original file line numberDiff line numberDiff line change
@@ -983,6 +983,14 @@ public function dataFileAsserts(): iterable
983983
yield from $this->gatherAssertTypes(__DIR__ . '/data/composer-non-empty-array-after-unset.php');
984984
yield from $this->gatherAssertTypes(__DIR__ . '/../Rules/Arrays/data/bug-6000.php');
985985
yield from $this->gatherAssertTypes(__DIR__ . '/data/prestashop-breakdowns-empty-array.php');
986+
987+
if (PHP_VERSION_ID < 80000) {
988+
yield from $this->gatherAssertTypes(__DIR__ . '/data/loose-comparisons-php7.php');
989+
}
990+
if (PHP_VERSION_ID >= 80000) {
991+
yield from $this->gatherAssertTypes(__DIR__ . '/data/loose-comparisons-php8.php');
992+
}
993+
yield from $this->gatherAssertTypes(__DIR__ . '/data/loose-comparisons.php');
986994
}
987995

988996
/**
+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace LooseSemanticsPhp7;
6+
7+
use function PHPStan\Testing\assertType;
8+
9+
class HelloWorld
10+
{
11+
/**
12+
* @param 0 $zero
13+
* @param 'php' $phpStr
14+
* @param '' $emptyStr
15+
*/
16+
public function sayZero(
17+
$zero,
18+
$phpStr,
19+
$emptyStr
20+
): void
21+
{
22+
assertType('true', $zero == $phpStr);
23+
assertType('true', $zero == $emptyStr);
24+
}
25+
26+
/**
27+
* @param 0 $zero
28+
* @param 'php' $phpStr
29+
*/
30+
public function sayPhpStr(
31+
$zero,
32+
$phpStr,
33+
): void
34+
{
35+
assertType('true', $phpStr == $zero);
36+
}
37+
38+
/**
39+
* @param 0 $zero
40+
* @param '' $emptyStr
41+
*/
42+
public function sayEmptyStr(
43+
$zero,
44+
$emptyStr
45+
): void
46+
{
47+
assertType('true', $emptyStr == $zero);
48+
}
49+
}
+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace LooseSemanticsPhp8;
6+
7+
use function PHPStan\Testing\assertType;
8+
9+
class HelloWorld
10+
{
11+
/**
12+
* @param 0 $zero
13+
* @param 'php' $phpStr
14+
* @param '' $emptyStr
15+
*/
16+
public function sayZero(
17+
$zero,
18+
$phpStr,
19+
$emptyStr
20+
): void
21+
{
22+
assertType('false', $zero == $phpStr); // PHP8+ only
23+
assertType('false', $zero == $emptyStr); // PHP8+ only
24+
}
25+
26+
/**
27+
* @param 0 $zero
28+
* @param 'php' $phpStr
29+
*/
30+
public function sayPhpStr(
31+
$zero,
32+
$phpStr,
33+
): void
34+
{
35+
assertType('false', $phpStr == $zero); // PHP8+ only
36+
}
37+
38+
/**
39+
* @param 0 $zero
40+
* @param '' $emptyStr
41+
*/
42+
public function sayEmptyStr(
43+
$zero,
44+
$emptyStr
45+
): void
46+
{
47+
assertType('false', $emptyStr == $zero); // PHP8+ only
48+
}
49+
}

0 commit comments

Comments
 (0)