1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-22 13:51:54 +01:00

Merge pull request #7293 from villfa/fix/7078

Fix parse_url() return type
This commit is contained in:
orklah 2022-01-04 17:56:51 +01:00 committed by GitHub
commit e41fc678d3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 13 deletions

View File

@ -15,8 +15,6 @@ use Psalm\Type\Atomic\TNull;
use Psalm\Type\Atomic\TString; use Psalm\Type\Atomic\TString;
use Psalm\Type\Union; use Psalm\Type\Union;
use function count;
use const PHP_URL_FRAGMENT; use const PHP_URL_FRAGMENT;
use const PHP_URL_HOST; use const PHP_URL_HOST;
use const PHP_URL_PASS; use const PHP_URL_PASS;
@ -44,7 +42,8 @@ class ParseUrlReturnTypeProvider implements FunctionReturnTypeProviderInterface
return Type::getMixed(); return Type::getMixed();
} }
if (count($call_args) > 1) { if (isset($call_args[1])) {
$is_default_component = false;
if ($component_type = $statements_source->node_data->getType($call_args[1]->value)) { if ($component_type = $statements_source->node_data->getType($call_args[1]->value)) {
if (!$component_type->hasMixed()) { if (!$component_type->hasMixed()) {
$codebase = $statements_source->getCodebase(); $codebase = $statements_source->getCodebase();
@ -110,22 +109,29 @@ class ParseUrlReturnTypeProvider implements FunctionReturnTypeProviderInterface
return $nullable_falsable_int; return $nullable_falsable_int;
} }
if ($component_type->isSingleIntLiteral()) {
$component_type_type = $component_type->getSingleIntLiteral();
$is_default_component = $component_type_type->value <= -1;
}
} }
} }
$nullable_string_or_int = new Union([ if (!$is_default_component) {
new TString, $nullable_string_or_int = new Union([
new TInt, new TString,
new TNull, new TInt,
]); new TNull,
]);
$codebase = $statements_source->getCodebase(); $codebase = $statements_source->getCodebase();
if ($codebase->config->ignore_internal_nullable_issues) { if ($codebase->config->ignore_internal_nullable_issues) {
$nullable_string_or_int->ignore_nullable_issues = true; $nullable_string_or_int->ignore_nullable_issues = true;
}
return $nullable_string_or_int;
} }
return $nullable_string_or_int;
} }
$component_types = [ $component_types = [

View File

@ -673,6 +673,19 @@ class FunctionCallTest extends TestCase
'$fragment' => 'false|null|string', '$fragment' => 'false|null|string',
], ],
], ],
'parseUrlDefaultComponent' => [
'<?php
$component = -1;
$url = "foo";
$a = parse_url($url, -1);
$b = parse_url($url, -42);
$c = parse_url($url, $component);',
'assertions' => [
'$a' => 'array{fragment?: string, host?: string, pass?: string, path?: string, port?: int, query?: string, scheme?: string, user?: string}|false',
'$b' => 'array{fragment?: string, host?: string, pass?: string, path?: string, port?: int, query?: string, scheme?: string, user?: string}|false',
'$c' => 'array{fragment?: string, host?: string, pass?: string, path?: string, port?: int, query?: string, scheme?: string, user?: string}|false',
],
],
'triggerUserError' => [ 'triggerUserError' => [
'<?php '<?php
function mightLeave() : string { function mightLeave() : string {