Skip to content

Commit

Permalink
Merge branch '4.x' into upstream-master
Browse files Browse the repository at this point in the history
  • Loading branch information
weirdan committed Jan 4, 2022
2 parents 63f3460 + e41fc67 commit f5dc2ee
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
Expand Up @@ -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;
Expand Down Expand Up @@ -47,7 +45,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();
Expand Down Expand Up @@ -113,22 +112,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 = [
Expand Down
13 changes: 13 additions & 0 deletions tests/FunctionCallTest.php
Expand Up @@ -673,6 +673,19 @@ function bag(string $s) : 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' => [
'<?php
function mightLeave() : string {
Expand Down

0 comments on commit f5dc2ee

Please sign in to comment.