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

Strip callmap prefixes from parameter names #10666

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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