mirror of
https://github.com/danog/psalm.git
synced 2024-11-26 20:34:47 +01:00
Allow PHP major version to determine substr return type
This commit is contained in:
parent
a1fd92d9fd
commit
e371685c3b
@ -951,6 +951,10 @@ class FunctionCallAnalyzer extends CallAnalyzer
|
||||
$template_result->upper_bounds[$template_name] = [
|
||||
'fn-' . $function_id => [Type::getInt(false, count($stmt->args)), 0]
|
||||
];
|
||||
} elseif ($template_name === 'TPhpMajorVersion') {
|
||||
$template_result->upper_bounds[$template_name] = [
|
||||
'fn-' . $function_id => [Type::getInt(false, $codebase->php_major_version), 0]
|
||||
];
|
||||
} else {
|
||||
$template_result->upper_bounds[$template_name] = [
|
||||
'fn-' . $function_id => [Type::getEmpty(), 0]
|
||||
|
@ -324,6 +324,13 @@ class MethodCallReturnTypeFetcher
|
||||
0
|
||||
]
|
||||
];
|
||||
} elseif ($template_type->param_name === 'TPhpMajorVersion') {
|
||||
$template_result->upper_bounds[$template_type->param_name] = [
|
||||
'fn-' . strtolower((string) $method_id) => [
|
||||
Type::getInt(false, $codebase->php_major_version),
|
||||
0
|
||||
]
|
||||
];
|
||||
} else {
|
||||
$template_result->upper_bounds[$template_type->param_name] = [
|
||||
($template_type->defining_class) => [Type::getEmpty(), 0]
|
||||
|
@ -250,6 +250,13 @@ class ExistingAtomicStaticCallAnalyzer
|
||||
0
|
||||
]
|
||||
];
|
||||
} elseif ($template_type->param_name === 'TPhpMajorVersion') {
|
||||
$template_result->upper_bounds[$template_type->param_name] = [
|
||||
'fn-' . strtolower((string) $method_id) => [
|
||||
Type::getInt(false, $codebase->php_major_version),
|
||||
0
|
||||
]
|
||||
];
|
||||
} else {
|
||||
$template_result->upper_bounds[$template_type->param_name] = [
|
||||
($template_type->defining_class) => [Type::getEmpty(), 0]
|
||||
|
@ -714,6 +714,21 @@ class FunctionLikeDocblockScanner
|
||||
|
||||
$fixed_type_tokens[$i][0] = $template_name;
|
||||
}
|
||||
|
||||
if ($token_body === 'PHP_MAJOR_VERSION') {
|
||||
$template_name = 'TPhpMajorVersion';
|
||||
|
||||
$storage->template_types[$template_name] = [
|
||||
$template_function_id => [
|
||||
Type::getInt()
|
||||
],
|
||||
];
|
||||
|
||||
$function_template_types[$template_name]
|
||||
= $storage->template_types[$template_name];
|
||||
|
||||
$fixed_type_tokens[$i][0] = $template_name;
|
||||
}
|
||||
}
|
||||
|
||||
$storage->return_type = TypeParser::parseTokens(
|
||||
|
@ -447,7 +447,7 @@ class TypeTokenizer
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($string_type_token[0] === 'func_num_args()') {
|
||||
if ($string_type_token[0] === 'func_num_args()' || $string_type_token[0] === 'PHP_MAJOR_VERSION') {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
@ -725,7 +725,7 @@ function preg_match_all($pattern, $subject, &$matches = [], int $flags = 1, int
|
||||
/**
|
||||
* @psalm-pure
|
||||
*
|
||||
* @return string|false
|
||||
* @return (PHP_MAJOR_VERSION is 5|7 ? string|false : string)
|
||||
* @psalm-ignore-falsable-return
|
||||
*
|
||||
* @psalm-flow ($string) -> return
|
||||
|
Loading…
Reference in New Issue
Block a user