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:
commit
e41fc678d3
@ -15,8 +15,6 @@ use Psalm\Type\Atomic\TNull;
|
||||
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;
|
||||
@ -44,7 +42,8 @@ class ParseUrlReturnTypeProvider implements FunctionReturnTypeProviderInterface
|
||||
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,9 +109,15 @@ class ParseUrlReturnTypeProvider implements FunctionReturnTypeProviderInterface
|
||||
|
||||
return $nullable_falsable_int;
|
||||
}
|
||||
|
||||
if ($component_type->isSingleIntLiteral()) {
|
||||
$component_type_type = $component_type->getSingleIntLiteral();
|
||||
$is_default_component = $component_type_type->value <= -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!$is_default_component) {
|
||||
$nullable_string_or_int = new Union([
|
||||
new TString,
|
||||
new TInt,
|
||||
@ -127,6 +132,7 @@ class ParseUrlReturnTypeProvider implements FunctionReturnTypeProviderInterface
|
||||
|
||||
return $nullable_string_or_int;
|
||||
}
|
||||
}
|
||||
|
||||
$component_types = [
|
||||
'scheme' => Type::getString(),
|
||||
|
@ -673,6 +673,19 @@ class FunctionCallTest extends TestCase
|
||||
'$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' => [
|
||||
'<?php
|
||||
function mightLeave() : string {
|
||||
|
Loading…
x
Reference in New Issue
Block a user