1.2.703. Missing Some Returntype

The specified typehints are not compatible with the returned values.

The code of the method may return other types, which are not specified and will lead to a PHP fatal error. It is the case for insufficient typehints, when a typehint is missing, or inconsistent typehints, when the method returns varied types. The analysis reports a method when it finds other return types than the one expected. In the case of multiple typehints, as for the last example, the PHP code may require an upgrade to PHP 8.0.

<?php

// correct return typehint
function fooSN() : ?string  {
    return shell_exec('ls -hla');
}

// insufficient return typehint
// shell_exec() may return null or string. Here, only string in specified for fooS, and that may lead to a Fatal error
function fooS() : string  {
    return shell_exec('ls -hla');
}

// inconsistent return typehint
function bar() : int {
    return rand(0, 10) ? 1 : "b";
}

?>

1.2.703.1. Suggestions

  • Update the typehint to accept more types

  • Update the code of the method to fit the expected returntype

1.2.703.2. Specs

Short name

Typehints/MissingReturntype

Rulesets

All, Analyze, CE, CI-checks

Exakat since

2.1.7

PHP Version

All

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

Very high

Features

return-typehint

Available in

Entreprise Edition, Community Edition, Exakat Cloud