1.2.377. Double Checks

Double checks happen when data is checked at one point, and then, checked again, with the same test, in a following call.

Some of the testing may be pushed to the type system, for example is_int() and int type. Others can’t, as the check is not integrated in the type system, such as is_readable() and string, for a path.

The check may be removed from the method, when the method is not called elsewhere without protection. Cleaning such structure leads to micro-optimisation.

<?php

if (is_writeable($path)) {
     foo($path);
}

function foo(string $path) {
     // This was already tested
     if (!is_writeable($path)) {
             return;
     }
}

?>

1.2.377.1. Suggestions

  • Remove the check in the method

  • Remove the check in the caller code

  • Use type system

1.2.377.2. Specs

Short name

Structures/DoubleChecks

Rulesets

All, Analyze

Exakat since

2.5.2

PHP Version

All

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

High

Available in

Entreprise Edition, Exakat Cloud