Skip to content

IfReturnReturnSimplification ​

warning on by default quick-fix

Group: Control flow · PhpStorm name: IfReturnReturnSimplificationInspection

An if whose only job is to return true in one branch and false in the other is a long-winded way of returning the condition itself (or its negation). Return the expression directly.

Example ​

php
<?php
function isAdult(int $age): bool {
    if ($age >= 18) {
        return true;
    }
    return false;
}

function isBlank(string $s): bool {
    if (($s === '')) { return FALSE; }
    // fallthrough
    return TRUE;
}

function hasBoth($m, $n) {
    if ($m && $n) { return false; }
    else { return true; }
}

function notWidget($w) {
    if ($w instanceof Widget) {
        return false;
    } else {
        return true;
    }
}

function guarded($q) {
    if ($q === null) { log_it(); return true; }
    if ($q > 9) { return true; }
    return false;
}

function guardedElse($q) {
    if ($q === null) { return true; }
    if ($q < 3) { return true; }
    else { return false; }
}

function notBinary($q) {
    if (is_int($q)) { return true; }
    return false;
}

function unbraced($q) {
    if ($q > 1) return true;
    return false;
}

function sameValue($q) {
    if ($q > 1) { return false; }
    return false;
}

function hasElseif($q) {
    if ($q > 1) { return true; }
    elseif ($q < 0) { return true; }
    else { return false; }
}
php
<?php
function isAdult(int $age): bool {
    return $age >= 18;
}

function isBlank(string $s): bool {
    return $s !== '';
}

function hasBoth($m, $n) {
    return !($m && $n);
}

function notWidget($w) {
    return !($w instanceof Widget);
}

function guarded($q) {
    if ($q === null) { log_it(); return true; }
    if ($q > 9) { return true; }
    return false;
}

function guardedElse($q) {
    if ($q === null) { return true; }
    return $q < 3;
}

function notBinary($q) {
    if (is_int($q)) { return true; }
    return false;
}

function unbraced($q) {
    if ($q > 1) return true;
    return false;
}

function sameValue($q) {
    if ($q > 1) { return false; }
    return false;
}

function hasElseif($q) {
    if ($q > 1) { return true; }
    elseif ($q < 0) { return true; }
    else { return false; }
}

Reported:

  • line 3: Return the condition directly: 'return $age >= 18'.
  • line 10: Return the condition directly: 'return $s !== '''.
  • line 16: Return the condition directly: 'return !($m && $n)'.
  • line 21: Return the condition directly: 'return !($w instanceof Widget)'.
  • line 36: Return the condition directly: 'return $q < 3'.

Configure ​

In custos.json:

json
{
  "rules": {
    "IfReturnReturnSimplification": {
      "enabled": false
    }
  }
}

Suppress ​

Before the statement or declaration (or the first statement of the file), either of:

php
// @custos-ignore IfReturnReturnSimplification

/**
 * @noinspection IfReturnReturnSimplificationInspection
 */

Released under the MIT License. Rule catalogue modelled on Php Inspections (EA Extended); independent clean-room implementation.