Skip to content

Commit

Permalink
Fix edge-case with in_array and enums
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Feb 21, 2023
1 parent 7331bc5 commit 7d6f0f6
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
8 changes: 8 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -1307,6 +1307,14 @@ parameters:
count: 2
path: src/Type/Php/InArrayFunctionTypeSpecifyingExtension.php

-
message: """
#^Call to deprecated method getEnumCaseObjects\\(\\) of class PHPStan\\\\Type\\\\TypeUtils\\:
Use Type\\:\\:getEnumCases\\(\\)$#
"""
count: 2
path: src/Type/Php/InArrayFunctionTypeSpecifyingExtension.php

-
message: "#^Doing instanceof PHPStan\\\\Type\\\\Constant\\\\ConstantStringType is error\\-prone and deprecated\\. Use Type\\:\\:getConstantStrings\\(\\) instead\\.$#"
count: 1
Expand Down
4 changes: 2 additions & 2 deletions src/Type/Php/InArrayFunctionTypeSpecifyingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function specifyTypes(FunctionReflection $functionReflection, FuncCall $n
if (
$context->truthy()
|| count(TypeUtils::getConstantScalars($arrayValueType)) > 0
|| count($arrayValueType->getEnumCases()) > 0
|| count(TypeUtils::getEnumCaseObjects($arrayValueType)) > 0
) {
$specifiedTypes = $this->typeSpecifier->create(
$node->getArgs()[0]->value,
Expand All @@ -68,7 +68,7 @@ public function specifyTypes(FunctionReflection $functionReflection, FuncCall $n
if (
$context->truthy()
|| count(TypeUtils::getConstantScalars($needleType)) > 0
|| count($needleType->getEnumCases()) > 0
|| count(TypeUtils::getEnumCaseObjects($needleType)) > 0
) {
if ($context->truthy()) {
$arrayValueType = TypeCombinator::union($arrayValueType, $needleType);
Expand Down
18 changes: 18 additions & 0 deletions tests/PHPStan/Analyser/data/enums.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace EnumTypeAssertions;

use function in_array;
use function PHPStan\Testing\assertType;

enum Foo
Expand Down Expand Up @@ -266,3 +267,20 @@ public function doBar()
}

}

class InArrayEnum
{

/** @var list<Foo> */
private $list;

public function doFoo(Foo $foo): void
{
if (in_array($foo, $this->list, true)) {
return;
}

assertType(Foo::class, $foo);
}

}

0 comments on commit 7d6f0f6

Please sign in to comment.