PHP

PHP

Made by DeepSource

Missing native return type declaration for closure/anonymous function PHP-T1003

Type check
Minor

The closure/anonymous function is missing the native return type declaration. Native return type can help enforce type, which can lead to more readable, reliable code. They ensure that the value is of the specified type at call time, otherwise a TypeError is thrown.

Bad practice

function () { // missing native return type declaration
    return 'Hello there!';
};
function (bool $bool) { // missing native return type declaration
    if ($bool) {
        return;
    } else {
        return 1;
    }
};

Recommended

function (): string {
    return 'Hello there!';
};
function (bool $bool): ?int {
    if ($bool) {
        return;
    } else {
        return 1;
    }
};

References