Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/Analyser/ArgumentsNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -325,15 +325,25 @@ public static function reorderArgs(ParametersAcceptor $parametersAcceptor, array
}

$hasNamedArgs = false;
$hasUnpackedArgs = false;
foreach ($callArgs as $arg) {
if ($arg->name !== null) {
$hasNamedArgs = true;
break;
}
if (!$arg->unpack) {
continue;
}

$hasUnpackedArgs = true;
}
if (!$hasNamedArgs) {
return array_values($callArgs);
}
if ($hasUnpackedArgs) {
// the number of parameters an unpacked argument fills is unknown here,
// so the positions of the named arguments that follow it cannot be determined
return null;
}

$hasVariadic = false;
$argumentPositions = [];
Expand Down
58 changes: 58 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-15778.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php // lint >= 8.1

declare(strict_types=1);

namespace Bug15778;

use function PHPStan\Testing\assertType;

/** @return ($key is null ? null : string) */
function label(?string $key = null, ?string $locale = null): ?string
{
return $key;
}

/** @return ($b is null ? null : string) */
function second(?string $a = null, ?string $b = null, ?string $key = null): ?string
{
return $b;
}

class Foo
{

/** @return ($key is null ? null : string) */
public function label(?string $key = null, ?string $locale = null): ?string
{
return $key;
}

/** @return ($key is null ? null : string) */
public static function staticLabel(?string $key = null, ?string $locale = null): ?string
{
return $key;
}

/** @return ($b is null ? null : string) */
public function second(?string $a = null, ?string $b = null, ?string $key = null): ?string
{
return $b;
}

}

function doFoo(Foo $foo): void
{
assertType('string', label(locale: 'en', key: 'welcome'));
assertType('string', label(...['locale' => 'en'], key: 'welcome'));
assertType('string', label(...[], key: 'welcome'));
assertType('string', second(...['x', 'y'], key: 'welcome'));
assertType('null', second(...['x'], key: 'welcome'));
assertType('array', array_slice(...[[1, 2, 3]], offset: 1));

assertType('string', $foo->label(...['locale' => 'en'], key: 'welcome'));
assertType('string', $foo->label(...[], key: 'welcome'));
assertType('string', $foo->second(...['x', 'y'], key: 'welcome'));
assertType('string', Foo::staticLabel(...['locale' => 'en'], key: 'welcome'));
assertType('string', Foo::staticLabel(...[], key: 'welcome'));
}
14 changes: 12 additions & 2 deletions turbo-ext/src/ArgumentsNormalizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -288,16 +288,26 @@ class ArgumentsNormalizer
if (zend_hash_num_elements(args) == 0) return zv::Val(zv::Arr::empty());

bool hasNamedArgs = false;
bool hasUnpackedArgs = false;
for (zv::ArrayEntry entry : zv::TableRef(args)) {
zval *arg = entry.value().deref().raw();
zv::Val nameHold;
zval *name = readProperty(pt_an_arg_name_site, entry.value().deref().raw(), PT_LC("name"), nameHold);
zval *name = readProperty(pt_an_arg_name_site, arg, PT_LC("name"), nameHold);
if (UNEXPECTED(name == NULL)) return zv::Val();
if (Z_TYPE_P(name) != IS_NULL) {
hasNamedArgs = true;
break;
}
zv::Val unpackHold;
zval *unpack = readProperty(pt_an_arg_unpack_site, arg, PT_LC("unpack"), unpackHold);
if (UNEXPECTED(unpack == NULL)) return zv::Val();
if (zend_is_true(unpack)) {
hasUnpackedArgs = true;
}
}
if (EXPECTED(!hasNamedArgs)) return arrayValues(callArgs);
// the number of parameters an unpacked argument fills is unknown here,
// so the positions of the named arguments that follow it cannot be determined
if (hasUnpackedArgs) return zv::Val::null();

return reorderNamedArgs(parametersAcceptor, args);
}
Expand Down
Loading