Skip to content

Commit 7d6f0f6

Browse files
committed
Fix edge-case with in_array and enums
1 parent 7331bc5 commit 7d6f0f6

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

phpstan-baseline.neon

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1307,6 +1307,14 @@ parameters:
13071307
count: 2
13081308
path: src/Type/Php/InArrayFunctionTypeSpecifyingExtension.php
13091309

1310+
-
1311+
message: """
1312+
#^Call to deprecated method getEnumCaseObjects\\(\\) of class PHPStan\\\\Type\\\\TypeUtils\\:
1313+
Use Type\\:\\:getEnumCases\\(\\)$#
1314+
"""
1315+
count: 2
1316+
path: src/Type/Php/InArrayFunctionTypeSpecifyingExtension.php
1317+
13101318
-
13111319
message: "#^Doing instanceof PHPStan\\\\Type\\\\Constant\\\\ConstantStringType is error\\-prone and deprecated\\. Use Type\\:\\:getConstantStrings\\(\\) instead\\.$#"
13121320
count: 1

src/Type/Php/InArrayFunctionTypeSpecifyingExtension.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function specifyTypes(FunctionReflection $functionReflection, FuncCall $n
5454
if (
5555
$context->truthy()
5656
|| count(TypeUtils::getConstantScalars($arrayValueType)) > 0
57-
|| count($arrayValueType->getEnumCases()) > 0
57+
|| count(TypeUtils::getEnumCaseObjects($arrayValueType)) > 0
5858
) {
5959
$specifiedTypes = $this->typeSpecifier->create(
6060
$node->getArgs()[0]->value,
@@ -68,7 +68,7 @@ public function specifyTypes(FunctionReflection $functionReflection, FuncCall $n
6868
if (
6969
$context->truthy()
7070
|| count(TypeUtils::getConstantScalars($needleType)) > 0
71-
|| count($needleType->getEnumCases()) > 0
71+
|| count(TypeUtils::getEnumCaseObjects($needleType)) > 0
7272
) {
7373
if ($context->truthy()) {
7474
$arrayValueType = TypeCombinator::union($arrayValueType, $needleType);

tests/PHPStan/Analyser/data/enums.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace EnumTypeAssertions;
44

5+
use function in_array;
56
use function PHPStan\Testing\assertType;
67

78
enum Foo
@@ -266,3 +267,20 @@ public function doBar()
266267
}
267268

268269
}
270+
271+
class InArrayEnum
272+
{
273+
274+
/** @var list<Foo> */
275+
private $list;
276+
277+
public function doFoo(Foo $foo): void
278+
{
279+
if (in_array($foo, $this->list, true)) {
280+
return;
281+
}
282+
283+
assertType(Foo::class, $foo);
284+
}
285+
286+
}

0 commit comments

Comments
 (0)