mirror of
https://github.com/danog/psalm.git
synced 2024-11-27 04:45:20 +01:00
Merge pull request #10109 from weirdan/fix-10090
This commit is contained in:
commit
a645331026
@ -228,7 +228,7 @@ class AtomicPropertyFetchAnalyzer
|
|||||||
self::handleEnumValue($statements_analyzer, $stmt, $stmt_var_type, $class_storage);
|
self::handleEnumValue($statements_analyzer, $stmt, $stmt_var_type, $class_storage);
|
||||||
} elseif ($prop_name === 'name') {
|
} elseif ($prop_name === 'name') {
|
||||||
$has_valid_fetch_type = true;
|
$has_valid_fetch_type = true;
|
||||||
self::handleEnumName($statements_analyzer, $stmt, $lhs_type_part);
|
self::handleEnumName($statements_analyzer, $stmt, $stmt_var_type, $class_storage);
|
||||||
} else {
|
} else {
|
||||||
self::handleNonExistentProperty(
|
self::handleNonExistentProperty(
|
||||||
$statements_analyzer,
|
$statements_analyzer,
|
||||||
@ -979,16 +979,31 @@ class AtomicPropertyFetchAnalyzer
|
|||||||
private static function handleEnumName(
|
private static function handleEnumName(
|
||||||
StatementsAnalyzer $statements_analyzer,
|
StatementsAnalyzer $statements_analyzer,
|
||||||
PropertyFetch $stmt,
|
PropertyFetch $stmt,
|
||||||
Atomic $lhs_type_part
|
Union $stmt_var_type,
|
||||||
|
ClassLikeStorage $class_storage
|
||||||
): void {
|
): void {
|
||||||
if ($lhs_type_part instanceof TEnumCase) {
|
$relevant_enum_cases = array_filter(
|
||||||
$statements_analyzer->node_data->setType(
|
$stmt_var_type->getAtomicTypes(),
|
||||||
$stmt,
|
static fn(Atomic $type): bool => $type instanceof TEnumCase,
|
||||||
new Union([Type::getAtomicStringFromLiteral($lhs_type_part->case_name)]),
|
);
|
||||||
);
|
$relevant_enum_case_names = array_map(
|
||||||
} else {
|
static fn(TEnumCase $enumCase): string => $enumCase->case_name,
|
||||||
$statements_analyzer->node_data->setType($stmt, Type::getNonEmptyString());
|
$relevant_enum_cases,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (empty($relevant_enum_case_names)) {
|
||||||
|
$relevant_enum_case_names = array_keys($class_storage->enum_cases);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$statements_analyzer->node_data->setType(
|
||||||
|
$stmt,
|
||||||
|
empty($relevant_enum_case_names)
|
||||||
|
? Type::getNonEmptyString()
|
||||||
|
: new Union(array_map(
|
||||||
|
fn(string $name): TString => Type::getAtomicStringFromLiteral($name),
|
||||||
|
$relevant_enum_case_names,
|
||||||
|
)),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static function handleEnumValue(
|
private static function handleEnumValue(
|
||||||
|
@ -574,6 +574,42 @@ class EnumTest extends TestCase
|
|||||||
'ignored_issues' => [],
|
'ignored_issues' => [],
|
||||||
'php_version' => '8.1',
|
'php_version' => '8.1',
|
||||||
],
|
],
|
||||||
|
'nameTypeOnKnownCases' => [
|
||||||
|
'code' => <<<'PHP'
|
||||||
|
<?php
|
||||||
|
enum Transport: string {
|
||||||
|
case CAR = 'car';
|
||||||
|
case BIKE = 'bike';
|
||||||
|
case BOAT = 'boat';
|
||||||
|
}
|
||||||
|
|
||||||
|
$val = Transport::from(uniqid());
|
||||||
|
$_name = $val->name;
|
||||||
|
PHP,
|
||||||
|
'assertions' => [
|
||||||
|
'$_name===' => "'BIKE'|'BOAT'|'CAR'",
|
||||||
|
],
|
||||||
|
'ignored_issues' => [],
|
||||||
|
'php_version' => '8.1',
|
||||||
|
],
|
||||||
|
'nameTypeOnUnknownCases' => [
|
||||||
|
'code' => <<<'PHP'
|
||||||
|
<?php
|
||||||
|
enum Transport: string {
|
||||||
|
case CAR = 'car';
|
||||||
|
case BIKE = 'bike';
|
||||||
|
case BOAT = 'boat';
|
||||||
|
}
|
||||||
|
|
||||||
|
function f(Transport $e): void {
|
||||||
|
$_name = $e->name;
|
||||||
|
/** @psalm-check-type-exact $_name='BIKE'|'BOAT'|'CAR' */;
|
||||||
|
}
|
||||||
|
PHP,
|
||||||
|
'assertions' => [],
|
||||||
|
'ignored_issues' => [],
|
||||||
|
'php_version' => '8.1',
|
||||||
|
],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user