mirror of
https://github.com/danog/psalm.git
synced 2025-01-21 21:31:13 +01:00
Handle the case where a switch return on each case (#5819)
* Handle the case where a switch return on each case * introduce a "hybrid" case exit type for cases where there's both "none" and something else
This commit is contained in:
parent
7354ec9903
commit
de1bb954cf
@ -87,6 +87,8 @@ class SwitchAnalyzer
|
||||
} elseif (in_array(ScopeAnalyzer::ACTION_LEAVE_SWITCH, $case_actions, true)) {
|
||||
$last_case_exit_type = 'break';
|
||||
}
|
||||
} elseif (count($case_actions) !== 1) {
|
||||
$last_case_exit_type = 'hybrid';
|
||||
}
|
||||
|
||||
$case_exit_types[$i] = $last_case_exit_type;
|
||||
@ -98,11 +100,16 @@ class SwitchAnalyzer
|
||||
|
||||
$statements_analyzer->node_data->cache_assertions = false;
|
||||
|
||||
$all_options_returned = true;
|
||||
|
||||
for ($i = 0, $l = count($stmt->cases); $i < $l; $i++) {
|
||||
$case = $stmt->cases[$i];
|
||||
|
||||
/** @var string */
|
||||
$case_exit_type = $case_exit_types[$i];
|
||||
if ($case_exit_type !== 'return_throw') {
|
||||
$all_options_returned = false;
|
||||
}
|
||||
|
||||
$case_actions = $case_action_map[$i];
|
||||
|
||||
@ -215,6 +222,9 @@ class SwitchAnalyzer
|
||||
$switch_scope->new_vars_possibly_in_scope
|
||||
);
|
||||
|
||||
//a switch can't return in all options without a default
|
||||
$context->has_returned = $all_options_returned && $has_default;
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
@ -639,6 +639,36 @@ class ReturnTypeManipulationTest extends FileManipulationTestCase
|
||||
false,
|
||||
true,
|
||||
],
|
||||
'switchReturn' => [
|
||||
'<?php
|
||||
/**
|
||||
* @param string $a
|
||||
*/
|
||||
function get_form_fields(string $a) {
|
||||
switch($a){
|
||||
default:
|
||||
return [];
|
||||
}
|
||||
}',
|
||||
'<?php
|
||||
/**
|
||||
* @param string $a
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @psalm-return array<empty, empty>
|
||||
*/
|
||||
function get_form_fields(string $a): array {
|
||||
switch($a){
|
||||
default:
|
||||
return [];
|
||||
}
|
||||
}',
|
||||
'7.3',
|
||||
['MissingReturnType'],
|
||||
false,
|
||||
true,
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user