1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-27 12:55:26 +01:00

Extract FunctionLikeStorage::setParams()/addParam()

This commit is contained in:
Claas Augner 2021-07-06 11:46:51 +02:00
parent 9acbb6061e
commit 601a981898
4 changed files with 23 additions and 9 deletions

View File

@ -280,7 +280,7 @@ class Reflection
$callables = InternalCallMapHandler::getCallablesFromCallMap($method_id); $callables = InternalCallMapHandler::getCallablesFromCallMap($method_id);
if ($callables && $callables[0]->params !== null && $callables[0]->return_type !== null) { if ($callables && $callables[0]->params !== null && $callables[0]->return_type !== null) {
$storage->params = []; $storage->setParams([]);
foreach ($callables[0]->params as $param) { foreach ($callables[0]->params as $param) {
if ($param->type) { if ($param->type) {
@ -288,18 +288,18 @@ class Reflection
} }
} }
$storage->params = $callables[0]->params; $storage->setParams($callables[0]->params);
$storage->return_type = $callables[0]->return_type; $storage->return_type = $callables[0]->return_type;
$storage->return_type->queueClassLikesForScanning($this->codebase); $storage->return_type->queueClassLikesForScanning($this->codebase);
} else { } else {
$params = $method->getParameters(); $params = $method->getParameters();
$storage->params = []; $storage->setParams([]);
foreach ($params as $param) { foreach ($params as $param) {
$param_array = $this->getReflectionParamData($param); $param_array = $this->getReflectionParamData($param);
$storage->params[] = $param_array; $storage->addParam($param_array);
$storage->param_lookup[$param->name] = true; $storage->param_lookup[$param->name] = true;
} }
} }
@ -367,14 +367,14 @@ class Reflection
&& $callmap_callable->params !== null && $callmap_callable->params !== null
&& $callmap_callable->return_type !== null && $callmap_callable->return_type !== null
) { ) {
$storage->params = $callmap_callable->params; $storage->setParams($callmap_callable->params);
$storage->return_type = $callmap_callable->return_type; $storage->return_type = $callmap_callable->return_type;
} else { } else {
$reflection_params = $reflection_function->getParameters(); $reflection_params = $reflection_function->getParameters();
foreach ($reflection_params as $param) { foreach ($reflection_params as $param) {
$param_obj = $this->getReflectionParamData($param); $param_obj = $this->getReflectionParamData($param);
$storage->params[] = $param_obj; $storage->addParam($param_obj);
} }
if ($reflection_return_type = $reflection_function->getReturnType()) { if ($reflection_return_type = $reflection_function->getReturnType()) {

View File

@ -678,7 +678,7 @@ class FunctionLikeDocblockScanner
null null
); );
$storage->params[] = $storage_param; $storage->addParam($storage_param);
} }
try { try {

View File

@ -169,7 +169,7 @@ class FunctionLikeNodeScanner
$has_optional_param = false; $has_optional_param = false;
$existing_params = []; $existing_params = [];
$storage->params = []; $storage->setParams([]);
foreach ($stmt->getParams() as $param) { foreach ($stmt->getParams() as $param) {
if ($param->var instanceof PhpParser\Node\Expr\Error) { if ($param->var instanceof PhpParser\Node\Expr\Error) {
@ -219,7 +219,7 @@ class FunctionLikeNodeScanner
$existing_params['$' . $param_storage->name] = $i; $existing_params['$' . $param_storage->name] = $i;
$storage->param_lookup[$param_storage->name] = !!$param->type; $storage->param_lookup[$param_storage->name] = !!$param->type;
$storage->params[] = $param_storage; $storage->addParam($param_storage);
if (!$param_storage->is_optional && !$param_storage->is_variadic) { if (!$param_storage->is_optional && !$param_storage->is_variadic) {
$required_param_count = $i + 1; $required_param_count = $i + 1;

View File

@ -23,6 +23,7 @@ abstract class FunctionLikeStorage
public $stmt_location; public $stmt_location;
/** /**
* @psalm-readonly-allow-private-mutation
* @var list<FunctionLikeParameter> * @var list<FunctionLikeParameter>
*/ */
public $params = []; public $params = [];
@ -271,4 +272,17 @@ abstract class FunctionLikeStorage
return $visibility_text . ' ' . $symbol_text; return $visibility_text . ' ' . $symbol_text;
} }
/**
* @param list<FunctionLikeParameter> $params
*/
public function setParams(array $params): void
{
$this->params = $params;
}
public function addParam(FunctionLikeParameter $param): void
{
$this->params[] = $param;
}
} }