public function visitReturn(Node $node) : Context
{
// Don't check return types in traits
if ($this->context->isInClassScope()) {
$clazz = $this->context->getClassInScope($this->code_base);
if ($clazz->isTrait()) {
return $this->context;
}
}
// Make sure we're actually returning from a method.
if (!$this->context->isInFunctionLikeScope()) {
return $this->context;
}
// Get the method/function/closure we're in
$method = $this->context->getFunctionLikeInScope($this->code_base);
assert(!empty($method), "We're supposed to be in either method or closure scope.");
// Figure out what we intend to return
$method_return_type = $method->getUnionType();
// Figure out what is actually being returned
$expression_type = UnionType::fromNode($this->context, $this->code_base, $node->children['expr']);
if ($expression_type->hasStaticType()) {
$expression_type = $expression_type->withStaticResolvedInContext($this->context);
}
if ($method->getHasYield()) {
// Function that is syntactically a Generator.
return $this->context;
// Analysis was completed in PreOrderAnalysisVisitor
}
// This leaves functions which aren't syntactically generators.
// If there is no declared type, see if we can deduce
// what it should be based on the return type
if ($method_return_type->isEmpty() || $method->isReturnTypeUndefined()) {
$method->setIsReturnTypeUndefined(true);
// Set the inferred type of the method based
// on what we're returning
$method->getUnionType()->addUnionType($expression_type);
// No point in comparing this type to the
// type we just set
return $this->context;
}
// C
if (!$method->isReturnTypeUndefined() && !$expression_type->canCastToExpandedUnionType($method_return_type, $this->code_base)) {
$this->emitIssue(Issue::TypeMismatchReturn, $node->lineno ?? 0, (string) $expression_type, $method->getName(), (string) $method_return_type);
}
// For functions that aren't syntactically Generators,
// update the set/existence of return values.
if ($method->isReturnTypeUndefined()) {
// Add the new type to the set of values returned by the
// method
$method->getUnionType()->addUnionType($expression_type);
}
// Mark the method as returning something
$method->setHasReturn(isset($node->children['expr']));
return $this->context;
}