Skip to content

Commit

Permalink
Fix #5279 - don’t convert get_class($templated) into dependent type
Browse files Browse the repository at this point in the history
  • Loading branch information
muglug committed Feb 25, 2021
1 parent b2c3583 commit d484199
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -422,22 +422,28 @@ private static function handleDependentTypeFunction(
$var_id = '$' . $var->name;

if (isset($context->vars_in_scope[$var_id])) {
if ($function_id === 'get_class') {
$atomic_type = new Type\Atomic\TDependentGetClass(
$var_id,
$context->vars_in_scope[$var_id]->hasMixed()
? Type::getObject()
: $context->vars_in_scope[$var_id]
);
} elseif ($function_id === 'gettype') {
$atomic_type = new Type\Atomic\TDependentGetType($var_id);
} else {
$atomic_type = new Type\Atomic\TDependentGetDebugType($var_id);
}
if (!$context->vars_in_scope[$var_id]->hasTemplate()) {
if ($function_id === 'get_class') {
$atomic_type = new Type\Atomic\TDependentGetClass(
$var_id,
$context->vars_in_scope[$var_id]->hasMixed()
? Type::getObject()
: $context->vars_in_scope[$var_id]
);
} elseif ($function_id === 'gettype') {
$atomic_type = new Type\Atomic\TDependentGetType($var_id);
} else {
$atomic_type = new Type\Atomic\TDependentGetDebugType($var_id);
}

$statements_analyzer->node_data->setType($real_stmt, new Type\Union([$atomic_type]));

$statements_analyzer->node_data->setType($real_stmt, new Type\Union([$atomic_type]));
return;
}
}
} elseif (($var_type = $statements_analyzer->node_data->getType($var))
}

if (($var_type = $statements_analyzer->node_data->getType($var))
&& ($function_id === 'get_class'
|| $function_id === 'get_debug_type'
)
Expand Down
20 changes: 20 additions & 0 deletions tests/Template/FunctionTemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1529,6 +1529,26 @@ function toList(array $arr): array {
return reset($arr);
}'
],
'callTemplatedFunctionWithTemplatedClassString' => [
'<?php
/**
* @template Ta of object
* @psalm-param Ta $obj
* @return Ta
*/
function a(string $str, object $obj) {
$class = get_class($obj);
return deserialize_object($str, $class);
}
/**
* @psalm-template Tb
* @psalm-param class-string<Tb> $type
* @psalm-return Tb
* @psalm-suppress InvalidReturnType
*/
function deserialize_object(string $data, string $type) {}'
],
];
}

Expand Down

0 comments on commit d484199

Please sign in to comment.