Skip to content

Commit c340d13

Browse files
committed
Fix subtracting enums inside in_array when const set is used
1 parent a34c2f4 commit c340d13

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

src/Type/Php/InArrayFunctionTypeSpecifyingExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public function specifyTypes(FunctionReflection $functionReflection, FuncCall $n
110110
$context->true()
111111
|| (
112112
$context->false()
113-
&& count($arrayValueType->getFiniteTypes()) === 1
113+
&& count($arrayValueType->getFiniteTypes()) > 0
114114
)
115115
) {
116116
$specifiedTypes = $this->typeSpecifier->create(

tests/PHPStan/Analyser/AnalyserIntegrationTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1480,6 +1480,12 @@ public function testBug11913(): void
14801480
$this->assertNoErrors($errors);
14811481
}
14821482

1483+
public function testBug12083InArrayEnum(): void
1484+
{
1485+
$errors = $this->runAnalyse(__DIR__ . '/data/enum-in-array.php');
1486+
$this->assertNoErrors($errors);
1487+
}
1488+
14831489
/**
14841490
* @param string[]|null $allAnalysedFiles
14851491
* @return Error[]
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
enum MyEnum: string
4+
{
5+
6+
case A = 'a';
7+
case B = 'b';
8+
case C = 'c';
9+
10+
const SET1 = [self::A, self::B, self::C];
11+
12+
}
13+
14+
15+
foreach ([MyEnum::A, MyEnum::B, MyEnum::C] as $enum) {
16+
17+
if (in_array($enum, MyEnum::SET1, true)) {
18+
19+
} else {
20+
\PHPStan\Testing\assertType('*NEVER*', $enum);
21+
}
22+
}

0 commit comments

Comments
 (0)