Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make array_map argument take into account unpack #2901

Merged
merged 3 commits into from
May 30, 2024
Merged
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
15 changes: 14 additions & 1 deletion src/Reflection/ParametersAcceptorSelector.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,20 @@ public static function selectFromArgs(
$parameters = $acceptor->getParameters();
$callbackParameters = [];
foreach ($arrayMapArgs as $arg) {
$callbackParameters[] = new DummyParameter('item', $scope->getIterableValueType($scope->getType($arg->value)), false, PassedByReference::createNo(), false, null);
$argType = $scope->getType($arg->value);
if ($arg->unpack) {
$constantArrays = $argType->getConstantArrays();
if (count($constantArrays) > 0) {
foreach ($constantArrays as $constantArray) {
$valueTypes = $constantArray->getValueTypes();
foreach ($valueTypes as $valueType) {
$callbackParameters[] = new DummyParameter('item', $scope->getIterableValueType($valueType), false, PassedByReference::createNo(), false, null);
}
}
}
} else {
$callbackParameters[] = new DummyParameter('item', $scope->getIterableValueType($argType), false, PassedByReference::createNo(), false, null);
}
}
$parameters[0] = new NativeParameterReflection(
$parameters[0]->getName(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1627,6 +1627,15 @@ public function testDiscussion10454(): void
]);
}

public function testBug10527(): void
{
if (PHP_VERSION_ID < 70400) {
$this->markTestSkipped('Test requires PHP 7.4');
}

$this->analyse([__DIR__ . '/data/bug-10527.php'], []);
}

public function testBug10626(): void
{
$this->analyse([__DIR__ . '/data/bug-10626.php'], [
Expand Down
14 changes: 14 additions & 0 deletions tests/PHPStan/Rules/Functions/data/bug-10527.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php declare(strict_types=1);

namespace Bug10527;

class HelloWorld
{
/**
* @param array{0: list<string>, 1: list<string>} $tuple
*/
public function sayHello(array $tuple): void
{
array_map(fn (string $first, string $second) => $first . $second, ...$tuple);
}
}
Loading