Skip to content

Commit

Permalink
Merge pull request #10666 from weirdan/10662-strip-callmap-prefixes-f…
Browse files Browse the repository at this point in the history
…rom-parameter-names
  • Loading branch information
weirdan committed Feb 6, 2024
2 parents 2fc2a37 + 1c36da6 commit b54e0b2
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/Psalm/Internal/Codebase/InternalCallMapHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,11 +285,20 @@ public static function getCallablesFromCallMap(string $function_id): ?array

$out_type = null;

if (strlen($arg_name) > 2 && $arg_name[0] === 'w' && $arg_name[1] === '_') {
if ($by_reference && strlen($arg_name) > 2 && $arg_name[0] === 'w' && $arg_name[1] === '_') {
// strip prefix that is not actually a part of the parameter name
$arg_name = substr($arg_name, 2);
$out_type = $param_type;
$param_type = Type::getMixed();
}

// removes `rw_` leftover from `&rw_haystack` or `&rw_needle` or `&rw_actual_name`
// it doesn't have any specific meaning apart from `&` signifying that
// the parameter is passed by reference (handled above)
if ($by_reference && strlen($arg_name) > 3 && strpos($arg_name, 'rw_') === 0) {
$arg_name = substr($arg_name, 3);
}

$function_param = new FunctionLikeParameter(
$arg_name,
$by_reference,
Expand Down
20 changes: 20 additions & 0 deletions tests/Internal/Codebase/InternalCallMapHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,26 @@ public function testGetcallmapReturnsAValidCallmap(): void
}
}

public function testGetCallablesFromCallmapRemovesRwPrefixFromParameterNames(): void
{
$entries = InternalCallMapHandler::getCallablesFromCallMap('collator_sort'); // has &rw_array parameter as second parameter
$this->assertNotNull($entries);
$collator_sort_entry = $entries[0];
$this->assertIsArray($collator_sort_entry->params);
$this->assertArrayHasKey(1, $collator_sort_entry->params);
$this->assertEquals('array', $collator_sort_entry->params[1]->name);
}

public function testGetCallablesFromCallmapRemovesWPrefixFromParameterNames(): void
{
$entries = InternalCallMapHandler::getCallablesFromCallMap('curl_multi_exec'); // has &w_still_running parameter as second parameter
$this->assertNotNull($entries);
$curl_multi_exec_entry = $entries[0];
$this->assertIsArray($curl_multi_exec_entry->params);
$this->assertArrayHasKey(1, $curl_multi_exec_entry->params);
$this->assertEquals('still_running', $curl_multi_exec_entry->params[1]->name);
}

/**
* @return iterable<string, array{string, array<int|string, string>}>
*/
Expand Down
18 changes: 18 additions & 0 deletions tests/MethodSignatureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -957,6 +957,24 @@ public function &foo(): int {
}
',
],
'callmapInheritedMethodParamsDoNotHavePrefixes' => [
'code' => <<<'PHP'
<?php
class NoopFilter extends \php_user_filter
{
/**
* @param resource $in
* @param resource $out
* @param int $consumed -- this is called &rw_consumed in the callmap
*/
public function filter($in, $out, &$consumed, bool $closing): int
{
return PSFS_PASS_ON;
}
}
PHP,
],
];
}

Expand Down

0 comments on commit b54e0b2

Please sign in to comment.