Skip to content

Commit

Permalink
Fix false positive for array unpacking
Browse files Browse the repository at this point in the history
Fix #9873
  • Loading branch information
kkmuffme committed Jun 24, 2023
1 parent f2a9e09 commit fad1768
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
Expand Up @@ -62,6 +62,18 @@ public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $ev
}

$node_type_provider = $statements_source->getNodeTypeProvider();
foreach ($call_args as $index => $call_arg) {
$type = $node_type_provider->getType($call_arg->value);
if ($type === null) {
continue;
}

// if it's an array, used with splat operator, we cannot validate it reliably below and report false positive errors
if ($type->isArray()) {
return null;
}
}

foreach ($call_args as $index => $call_arg) {
$type = $node_type_provider->getType($call_arg->value);
if ($type === null && $index === 0 && $event->getFunctionId() === 'printf') {
Expand Down
10 changes: 10 additions & 0 deletions tests/ReturnTypeProvider/SprintfTest.php
Expand Up @@ -182,6 +182,16 @@ public function providerValidCodeParse(): iterable
'$val===' => 'string',
],
];

yield 'sprintfSplatUnpackingArray' => [
'code' => '<?php
$a = ["a", "b", "c"];
$val = sprintf("%s %s %s", ...$a);
',
'assertions' => [
'$val===' => 'string',
],
];
}

public function providerInvalidCodeParse(): iterable
Expand Down

0 comments on commit fad1768

Please sign in to comment.