1.2.678. Method Is Not An If

When a method consists only in one if statement, it might be worth refactoring.

Each of the blocks of the if/then structure may be turned into their own method, so has to keep operations distinct.

Then, the condition can be used as part of a larger method.

<?php

function foo($a) {
     if ($a === 1) {
             return 1;
     } else {
             return 2;
     }
}

?>

1.2.678.1. Suggestions

  • Export the blocks to distinct functions

  • Bail out early

1.2.678.2. Specs

Short name

Functions/MethodIsNotAnIf

Rulesets

All, Analyze

Exakat since

2.5.3

PHP Version

All

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

Medium

Available in

Entreprise Edition, Exakat Cloud