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

Fix analysis on array_map with named arguments #3763

Open
wants to merge 4 commits into
base: 2.1.x
Choose a base branch
from
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
48 changes: 36 additions & 12 deletions src/Parser/ArrayMapArgVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use PhpParser\Node;
use PhpParser\NodeVisitorAbstract;
use function array_slice;
use function array_splice;
use function count;

final class ArrayMapArgVisitor extends NodeVisitorAbstract
Expand All @@ -14,19 +14,43 @@ final class ArrayMapArgVisitor extends NodeVisitorAbstract

public function enterNode(Node $node): ?Node
{
if ($node instanceof Node\Expr\FuncCall && $node->name instanceof Node\Name && !$node->isFirstClassCallable()) {
$functionName = $node->name->toLowerString();
if ($functionName === 'array_map') {
$args = $node->getArgs();
if (isset($args[0])) {
$slicedArgs = array_slice($args, 1);
if (count($slicedArgs) > 0) {
$args[0]->value->setAttribute(self::ATTRIBUTE_NAME, $slicedArgs);
}
}
}
if (!$this->isArrayMapCall($node)) {
return null;
}

$args = $node->getArgs();
if (count($args) < 2) {
return null;
}

$callbackPos = 0;
if ($args[1]->name !== null && $args[1]->name->name === 'callback') {
$callbackPos = 1;
}
$callbackArg = $args[$callbackPos];
$arrayArgs = $args;
array_splice($arrayArgs, $callbackPos, 1);
$callbackArg->value->setAttribute(self::ATTRIBUTE_NAME, $arrayArgs);

return null;
}

/**
* @phpstan-assert-if-true Node\Expr\FuncCall $node
*/
private function isArrayMapCall(Node $node): bool
{
if (!$node instanceof Node\Expr\FuncCall) {
return false;
}
if (!$node->name instanceof Node\Name) {
return false;
}
if ($node->isFirstClassCallable()) {
return false;
}

return $node->name->toLowerString() === 'array_map';
}

}
4 changes: 4 additions & 0 deletions src/Reflection/ParametersAcceptorSelector.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ public static function selectFromArgs(
&& count($parametersAcceptors) > 0
) {
$arrayMapArgs = $args[0]->value->getAttribute(ArrayMapArgVisitor::ATTRIBUTE_NAME);
if ($arrayMapArgs === null && isset($args[1])) {
// callback argument of array_map() may be the second one (named argument)
$arrayMapArgs = $args[1]->value->getAttribute(ArrayMapArgVisitor::ATTRIBUTE_NAME);
}
if ($arrayMapArgs !== null) {
$acceptor = $parametersAcceptors[0];
$parameters = $acceptor->getParameters();
Expand Down
20 changes: 20 additions & 0 deletions tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1936,4 +1936,24 @@ public function testBug12051(): void
$this->analyse([__DIR__ . '/data/bug-12051.php'], []);
}

public function testBug12317(): void
{
if (PHP_VERSION_ID < 80000) {
$this->markTestSkipped('Test requires PHP 8.0.');
}

$this->checkExplicitMixed = true;
$this->checkImplicitMixed = true;
$this->analyse([__DIR__ . '/data/bug-12317.php'], [
[
'Parameter #1 $callback of function array_map expects (callable(Bug12317\Uuid): mixed)|null, Closure(string): string given.',
28,
],
[
'Parameter $callback of function array_map expects (callable(Bug12317\Uuid): mixed)|null, Closure(string): string given.',
29,
],
]);
}

}
31 changes: 31 additions & 0 deletions tests/PHPStan/Rules/Functions/data/bug-12317.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php declare(strict_types = 1);

namespace Bug12317;

class Uuid {
public function __construct(public string $uuid) {}
public function __toString() { return $this->uuid; }
}

class HelloWorld
{
/**
* @param list<Uuid> $arr
*/
public function sayHello(array $arr): void
{
$callback = static fn(Uuid $uuid): string => (string) $uuid;

// ok
array_map(array: $arr, callback: $callback);
array_map(callback: $callback, array: $arr);
array_map($callback, $arr);
array_map($callback, array: $arr);
array_map(static fn (Uuid $u1, Uuid $u2): string => (string) $u1, $arr, $arr);

// should be reported
$invalidCallback = static fn(string $uuid): string => $uuid;
array_map($invalidCallback, $arr);
array_map(array: $arr, callback: $invalidCallback);
}
}
Loading