1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-26 20:34:47 +01:00

Compare enum cases thoroughly

Fixes vimeo/psalm#7814
This commit is contained in:
Bruce Weirdan 2023-02-13 22:39:33 -04:00
parent b0adac8fbb
commit cee90fc071
No known key found for this signature in database
GPG Key ID: CFC3AAB181751B0D
2 changed files with 78 additions and 0 deletions

View File

@ -301,6 +301,13 @@ class AtomicTypeComparator
return false;
}
if ($container_type_part instanceof TEnumCase
&& $input_type_part instanceof TEnumCase
) {
return $container_type_part->value === $input_type_part->value
&& $container_type_part->case_name === $input_type_part->case_name;
}
if (($input_type_part instanceof TNamedObject
|| ($input_type_part instanceof TTemplateParam
&& $input_type_part->as->hasObjectType())

View File

@ -861,6 +861,77 @@ class EnumTest extends TestCase
'ignored_issues' => [],
'php_version' => '8.1',
],
'functionCallWithInvalidCase' => [
'code' => '<?php
enum Status {
case Open;
case Closed;
}
/** @param Status::Open $status */
function foo(Status $status): void {}
foo(Status::Closed);
',
'error_message' => 'InvalidArgument',
'ignored_issues' => [],
'php_version' => '8.1',
],
'issue-7814-1' => [
'code' => '<?php
enum State
{
case A;
case B;
case C;
}
/**
* @param State::A|State::B $_
*/
function test(State $_): void {}
test(State::C);
',
'error_message' => 'InvalidArgument',
'ignored_issues' => [],
'php_version' => '8.1',
],
'issue-7814-2' => [
'code' => '<?php
enum State
{
case A;
case B;
case C;
}
/**
* @template T of State
*/
final class WithState
{
/**
* @param T $s
*/
public function __construct(
public readonly State $s,
) {}
}
/**
* @param WithState<State::A> $_
*/
function withA(WithState $_): void {}
// Should be issue here. But nothing
// Argument 1 of withA expects WithState<enum(State::A)>, WithState<enum(State::C)> provided
withA(new WithState(State::C));
',
'error_message' => 'InvalidArgument',
'ignored_issues' => [],
'php_version' => '8.1',
],
];
}
}