Skip to content

Commit 7997a54

Browse files
committed
Better tests
1 parent 302feda commit 7997a54

File tree

3 files changed

+46
-7
lines changed

3 files changed

+46
-7
lines changed

src/Type/Php/InArrayFunctionTypeSpecifyingExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public function specifyTypes(FunctionReflection $functionReflection, FuncCall $n
111111
|| (
112112
$context->false()
113113
&& count($arrayValueType->getFiniteTypes()) > 0
114-
&& TypeCombinator::union(...$arrayValueType->getFiniteTypes())->equals($arrayValueType)
114+
&& TypeCombinator::union(...$arrayValueType->getFiniteTypes())->equals($arrayValueType) // avoid edge-case of in_array(Enum, list<Enum>, true)
115115
)
116116
) {
117117
$specifiedTypes = $this->typeSpecifier->create(

tests/PHPStan/Analyser/AnalyserIntegrationTest.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use PHPStan\Testing\PHPStanTestCase;
1212
use PHPStan\Type\Constant\ConstantIntegerType;
1313
use PHPStan\Type\Constant\ConstantStringType;
14+
use function array_filter;
1415
use function extension_loaded;
1516
use function restore_error_handler;
1617
use function sprintf;
@@ -1484,12 +1485,10 @@ public function testBug12083InArrayEnum(): void
14841485
{
14851486
$errors = $this->runAnalyse(__DIR__ . '/data/enum-in-array.php');
14861487

1487-
$this->assertCount(2, $errors);
1488+
$filteredErrors = array_filter($errors, static fn (Error $error): bool => $error->getIdentifier() !== 'function.alreadyNarrowedType'
1489+
&& $error->getIdentifier() !== 'function.impossibleType');
14881490

1489-
$this->assertSame('Call to function in_array() with arguments \'c\', array{\'c\'} and true will always evaluate to true.', $errors[0]->getMessage());
1490-
$this->assertSame(92, $errors[0]->getLine());
1491-
$this->assertSame('Call to function in_array() with arguments \'c\', array{\'c\'} and true will always evaluate to true.', $errors[1]->getMessage());
1492-
$this->assertSame(124, $errors[1]->getLine());
1491+
$this->assertNoErrors($filteredErrors);
14931492
}
14941493

14951494
/**

tests/PHPStan/Analyser/data/enum-in-array.php

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,37 @@ public function test4(): void
5858
}
5959
}
6060

61+
public function testNegative1(): void
62+
{
63+
foreach (self::cases() as $enum) {
64+
if (!in_array($enum, MyEnum::SET_AB, true)) {
65+
assertType('MyEnum::C', $enum);
66+
} else {
67+
assertType('MyEnum::A|MyEnum::B', $enum);
68+
}
69+
}
70+
}
71+
72+
public function testNegative2(): void
73+
{
74+
foreach (self::cases() as $enum) {
75+
if (!in_array($enum, MyEnum::SET_AB, true)) {
76+
assertType('MyEnum::C', $enum);
77+
} elseif (!in_array($enum, MyEnum::SET_AB, true)) {
78+
assertType('*NEVER*', $enum);
79+
}
80+
}
81+
}
82+
83+
public function testNegative3(): void
84+
{
85+
foreach ([MyEnum::C] as $enum) {
86+
if (!in_array($enum, MyEnum::SET_C, true)) {
87+
assertType('*NEVER*', $enum);
88+
}
89+
}
90+
}
91+
6192
}
6293

6394
class InArrayEnum
@@ -66,7 +97,7 @@ class InArrayEnum
6697
/** @var list<MyEnum> */
6798
private array $list = [];
6899

69-
public function doFoo(MyEnum $enum): void
100+
public function testPositive(MyEnum $enum): void
70101
{
71102
if (in_array($enum, $this->list, true)) {
72103
return;
@@ -75,6 +106,15 @@ public function doFoo(MyEnum $enum): void
75106
assertType(MyEnum::class, $enum);
76107
}
77108

109+
public function testNegative(MyEnum $enum): void
110+
{
111+
if (!in_array($enum, $this->list, true)) {
112+
return;
113+
}
114+
115+
assertType(MyEnum::class, $enum);
116+
}
117+
78118
}
79119

80120

0 commit comments

Comments
 (0)