Skip to content

Commit

Permalink
Merge pull request #8181 from Nicelocal/avoid_reparsing
Browse files Browse the repository at this point in the history
Avoid re-parsing template types
  • Loading branch information
orklah committed Jun 27, 2022
2 parents 8b7bc07 + 38089eb commit 47d8309
Showing 1 changed file with 27 additions and 22 deletions.
Expand Up @@ -451,39 +451,44 @@ private static function getConditionalSanitizedTypeTokens(
);

$param_type_mapping = [];
$template_function_id = 'fn-' . strtolower($cased_function_id);

// This checks for param references in the return type tokens
// If found, the param is replaced with a generated template param
foreach ($fixed_type_tokens as $i => $type_token) {
$token_body = $type_token[0];
$template_function_id = 'fn-' . strtolower($cased_function_id);

if ($token_body[0] === '$') {
foreach ($storage->params as $j => $param_storage) {
if ('$' . $param_storage->name === $token_body) {
if (!isset($param_type_mapping[$token_body])) {
$template_name = 'TGeneratedFromParam' . $j;

$template_as_type = $param_storage->type
? clone $param_storage->type
: Type::getMixed();

$storage->template_types[$template_name] = [
$template_function_id => $template_as_type,
];

$function_template_types[$template_name]
= $storage->template_types[$template_name];

$param_type_mapping[$token_body] = $template_name;

$param_storage->type = new Union([
new TTemplateParam(
$template_name,
$template_as_type,
$template_function_id
)
]);
if (isset($storage->template_types[$template_name])) {
$function_template_types[$template_name]
= $storage->template_types[$template_name];
$param_type_mapping[$token_body] = $template_name;
} else {
$template_as_type = $param_storage->type
? clone $param_storage->type
: Type::getMixed();

$storage->template_types[$template_name] = [
$template_function_id => $template_as_type,
];

$function_template_types[$template_name]
= $storage->template_types[$template_name];

$param_type_mapping[$token_body] = $template_name;

$param_storage->type = new Union([
new TTemplateParam(
$template_name,
$template_as_type,
$template_function_id
)
]);
}
}

// spaces are allowed before $foo in get(string $foo) magic method
Expand Down

0 comments on commit 47d8309

Please sign in to comment.