Skip to content
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

Compare enum cases thoroughly #9286

Merged
merged 1 commit into from
Feb 14, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/Psalm/Internal/Type/Comparator/AtomicTypeComparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,13 @@ public static function isContainedBy(
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())
Expand Down
71 changes: 71 additions & 0 deletions tests/EnumTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -861,6 +861,77 @@ public static function tryFrom(string $value): ?self
'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',
],
];
}
}