From 1fa6b612b100baf7bccf5c6266c792502eb1c77b Mon Sep 17 00:00:00 2001 From: Fabien Villepinte Date: Tue, 4 Jan 2022 16:09:18 +0100 Subject: [PATCH 1/3] Fix parse_url() return type --- .../ParseUrlReturnTypeProvider.php | 30 ++++++++++++------- tests/FunctionCallTest.php | 13 ++++++++ 2 files changed, 32 insertions(+), 11 deletions(-) diff --git a/src/Psalm/Internal/Provider/ReturnTypeProvider/ParseUrlReturnTypeProvider.php b/src/Psalm/Internal/Provider/ReturnTypeProvider/ParseUrlReturnTypeProvider.php index 8beea77c32d..be87e28051c 100644 --- a/src/Psalm/Internal/Provider/ReturnTypeProvider/ParseUrlReturnTypeProvider.php +++ b/src/Psalm/Internal/Provider/ReturnTypeProvider/ParseUrlReturnTypeProvider.php @@ -44,7 +44,8 @@ public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $ev 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->hasMixed()) { $codebase = $statements_source->getCodebase(); @@ -110,22 +111,29 @@ public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $ev 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([ - new TString, - new TInt, - new TNull, - ]); + if (!$is_default_component) { + $nullable_string_or_int = new Union([ + new TString, + new TInt, + new TNull, + ]); - $codebase = $statements_source->getCodebase(); + $codebase = $statements_source->getCodebase(); - if ($codebase->config->ignore_internal_nullable_issues) { - $nullable_string_or_int->ignore_nullable_issues = true; - } + if ($codebase->config->ignore_internal_nullable_issues) { + $nullable_string_or_int->ignore_nullable_issues = true; + } - return $nullable_string_or_int; + return $nullable_string_or_int; + } } $component_types = [ diff --git a/tests/FunctionCallTest.php b/tests/FunctionCallTest.php index 4835ce61e91..dc0185bc60c 100644 --- a/tests/FunctionCallTest.php +++ b/tests/FunctionCallTest.php @@ -673,6 +673,19 @@ function bag(string $s) : string { '$fragment' => 'false|null|string', ], ], + 'parseUrlDefaultComponent' => [ + ' [ + '$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' => [ ' Date: Tue, 4 Jan 2022 16:13:48 +0100 Subject: [PATCH 2/3] Remove unused import --- .../Provider/ReturnTypeProvider/ParseUrlReturnTypeProvider.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Psalm/Internal/Provider/ReturnTypeProvider/ParseUrlReturnTypeProvider.php b/src/Psalm/Internal/Provider/ReturnTypeProvider/ParseUrlReturnTypeProvider.php index be87e28051c..c53224b650f 100644 --- a/src/Psalm/Internal/Provider/ReturnTypeProvider/ParseUrlReturnTypeProvider.php +++ b/src/Psalm/Internal/Provider/ReturnTypeProvider/ParseUrlReturnTypeProvider.php @@ -15,8 +15,6 @@ use Psalm\Type\Atomic\TString; use Psalm\Type\Union; -use function count; - use const PHP_URL_FRAGMENT; use const PHP_URL_HOST; use const PHP_URL_PASS; From c42f3d51bd92a948afee18d11b2602cd25241a52 Mon Sep 17 00:00:00 2001 From: Fabien Villepinte Date: Tue, 4 Jan 2022 16:27:06 +0100 Subject: [PATCH 3/3] Fix typo --- .../Provider/ReturnTypeProvider/ParseUrlReturnTypeProvider.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Psalm/Internal/Provider/ReturnTypeProvider/ParseUrlReturnTypeProvider.php b/src/Psalm/Internal/Provider/ReturnTypeProvider/ParseUrlReturnTypeProvider.php index c53224b650f..6d51c7d2d9d 100644 --- a/src/Psalm/Internal/Provider/ReturnTypeProvider/ParseUrlReturnTypeProvider.php +++ b/src/Psalm/Internal/Provider/ReturnTypeProvider/ParseUrlReturnTypeProvider.php @@ -112,7 +112,7 @@ public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $ev if ($component_type->isSingleIntLiteral()) { $component_type_type = $component_type->getSingleIntLiteral(); - $is_default_component = $component_type_type->value <= 1; + $is_default_component = $component_type_type->value <= -1; } } }