1.2.1225. Type Must Be Returned

When using a type for a method, it is compulsory to use a at least one return in the method’s body. This is true for nullable type too : return alone won’t be sufficient.

When the method contains a return expression, PHP doesn’t lint unless the return expression has a value. Any value will do, and it will actually checked at execution time.

When the method contains no return expression, PHP only checks it at execution time.

There is no need for a return expression when the method throws an expression, yield values, triggers an error or triggers an assertion. Even in case of inheritance or implementation, the return type may be replaced by never.

<?php

// The function returns a value (here, correct object)
function foo() : Bar { return new Bar(); }

// The function should at least, return a value
function foo() : Bar { }

// The function should at least, return a value : Null or an object. Void, here, is not acceptable.
function foo() : ?Bar { return; }

?>

See also Return Type Declaration and Type hint in PHP function parameters and return values.

1.2.1225.1. Suggestions

  • Add a return with a valid value

  • Add a throw expression

  • Add a trigger_error() call

  • Add a assert(false, …) expression

  • If the method doesn’t return, change the returntype to never

1.2.1225.2. Specs

Short name

Functions/TypehintMustBeReturned

Rulesets

All, Analyze, CE, CI-checks, Changed Behavior, LintButWontExec

Exakat since

1.6.9

PHP Version

All

Severity

Major

Time To Fix

Quick (30 mins)

Precision

Very high

Features

return-type, never-type

Note

This issue may lint but will not run

Available in

Entreprise Edition, Community Edition, Exakat Cloud