Skip to content

Fix subtracting enums inside in_array when const set is used #3646

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/Type/Php/InArrayFunctionTypeSpecifyingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@ public function specifyTypes(FunctionReflection $functionReflection, FuncCall $n
$context->true()
|| (
$context->false()
&& count($arrayValueType->getFiniteTypes()) === 1
&& count($arrayValueType->getFiniteTypes()) > 0
&& count($needleType->getFiniteTypes()) > 0
&& $arrayType->isIterableAtLeastOnce()->yes()
)
) {
$specifiedTypes = $this->typeSpecifier->create(
Expand Down
187 changes: 187 additions & 0 deletions tests/PHPStan/Analyser/nsrt/enum-in-array.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
<?php // lint >= 8.1

use function PHPStan\Testing\assertType;

enum MyEnum: string
{

case A = 'a';
case B = 'b';
case C = 'c';

const SET_AB = [self::A, self::B];
const SET_C = [self::C];
const SET_ABC = [self::A, self::B, self::C];

public function test1(): void
{
foreach (self::cases() as $enum) {
if (in_array($enum, MyEnum::SET_AB, true)) {
assertType('MyEnum::A|MyEnum::B', $enum);
} elseif (in_array($enum, MyEnum::SET_C, true)) {
assertType('MyEnum::C', $enum);
} else {
assertType('*NEVER*', $enum);
}
}
}

public function test2(): void
{
foreach (self::cases() as $enum) {
if (in_array($enum, MyEnum::SET_ABC, true)) {
assertType('MyEnum::A|MyEnum::B|MyEnum::C', $enum);
} else {
assertType('*NEVER*', $enum);
}
}
}

public function test3(): void
{
foreach (self::cases() as $enum) {
if (in_array($enum, MyEnum::SET_C, true)) {
assertType('MyEnum::C', $enum);
} else {
assertType('MyEnum::A|MyEnum::B', $enum);
}
}
}

public function test4(): void
{
foreach ([MyEnum::C] as $enum) {
if (in_array($enum, MyEnum::SET_C, true)) {
assertType('MyEnum::C', $enum);
} else {
assertType('*NEVER*', $enum);
}
}
}

public function testNegative1(): void
{
foreach (self::cases() as $enum) {
if (!in_array($enum, MyEnum::SET_AB, true)) {
assertType('MyEnum::C', $enum);
} else {
assertType('MyEnum::A|MyEnum::B', $enum);
}
}
}

public function testNegative2(): void
{
foreach (self::cases() as $enum) {
if (!in_array($enum, MyEnum::SET_AB, true)) {
assertType('MyEnum::C', $enum);
} elseif (!in_array($enum, MyEnum::SET_AB, true)) {
assertType('*NEVER*', $enum);
}
}
}

public function testNegative3(): void
{
foreach ([MyEnum::C] as $enum) {
if (!in_array($enum, MyEnum::SET_C, true)) {
assertType('*NEVER*', $enum);
}
}
}

/**
* @param array<MyEnum|null> $array
*/
public function testNegative4(MyEnum $enum, array $array): void
{
if (!in_array($enum, $array, true)) {
assertType('MyEnum', $enum);
assertType('array<MyEnum|null>', $array);
} else {
assertType('MyEnum', $enum);
assertType('non-empty-array<MyEnum|null>', $array);
}
}

}

class InArrayEnum
{

/** @param list<MyEnum> $list */
public function testPositive(MyEnum $enum, array $list): void
{
if (in_array($enum, $list, true)) {
return;
}

assertType(MyEnum::class, $enum);
assertType('list<MyEnum>', $list);
}

/** @param list<MyEnum> $list */
public function testNegative(MyEnum $enum, array $list): void
{
if (!in_array($enum, $list, true)) {
return;
}

assertType(MyEnum::class, $enum);
assertType('non-empty-list<MyEnum>', $list);
}

}


class InArrayOtherFiniteType {

const SET_AB = ['a', 'b'];
const SET_C = ['c'];
const SET_ABC = ['a', 'b', 'c'];

public function test1(): void
{
foreach (['a', 'b', 'c'] as $item) {
if (in_array($item, self::SET_AB, true)) {
assertType("'a'|'b'", $item);
} elseif (in_array($item, self::SET_C, true)) {
assertType("'c'", $item);
} else {
assertType('*NEVER*', $item);
}
}
}

public function test2(): void
{
foreach (['a', 'b', 'c'] as $item) {
if (in_array($item, self::SET_ABC, true)) {
assertType("'a'|'b'|'c'", $item);
} else {
assertType('*NEVER*', $item);
}
}
}

public function test3(): void
{
foreach (['a', 'b', 'c'] as $item) {
if (in_array($item, self::SET_C, true)) {
assertType("'c'", $item);
} else {
assertType("'a'|'b'", $item);
}
}
}
public function test4(): void
{
foreach (['c'] as $item) {
if (in_array($item, self::SET_C, true)) {
assertType("'c'", $item);
} else {
assertType('*NEVER*', $item);
}
}
}
}
Loading