1
0
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:
Matt Brown 2020-11-16 16:31:25 -05:00 committed by Daniil Gentili
parent a1fd92d9fd
commit e371685c3b
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
6 changed files with 35 additions and 2 deletions

View File

@ -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]

View File

@ -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]

View File

@ -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]

View File

@ -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(

View File

@ -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;
}

View File

@ -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