Skip to content

RedundantTypeCheck ​

warning native off by default

Group: Probable bugs

Report a type test whose answer the code already decides: instanceof, is_int() and the other type predicates, or a strict comparison with null, on a value whose native type makes it always true or always false. An always-true check is dead defensive code; an always-false one guards a branch that never runs, often a sign that the value is not what the author thinks. Types that come only from PHPDoc never count, so a guard that enforces a documented type at run time stays silent.

Example ​

php
<?php
interface Priced {}
final class Order implements Priced {}
function make(): Order { return new Order(); }
function checks(int $qty, Order $order, ?Order $maybe): void {
    $flags = [
        is_int($qty),
        $order instanceof Priced,
        make() === null,
    ];
    if ($maybe === null) {
        return;
    }
    if ($maybe !== null) {
        echo 'set';
    }
}

Reported:

  • line 7: This check is always true: the value is int.
  • line 8: This check is always true: the value is Order.
  • line 9: This check is always false: the value is Order.
  • line 14: This check is always true: the value is Order.

Detection ​

  • One of these tests on a value:
    • x instanceof C, with C a written name (not self, static or parent) of a class or interface whose whole hierarchy is known;
    • a call to the global function is_null, is_int, is_integer, is_long, is_float, is_double, is_string, is_bool, is_array, is_object, is_scalar or is_iterable, with the tested value as its only argument (by position, or named value);
    • x === null or x !== null, in either operand order.
  • The value's type comes from native declarations only, narrowed by the conditions guarding the test, as for PossiblyNullAccess. The test must also be decided the same way with PHPDoc taken into account, and with the newest supported PHP version as target.
  • Every value of that type passes the test (always true), or none does (always false). A class passes instanceof C when it is C or a subtype of it, and never does when it cannot be: an unrelated class, or a final class (or enum) outside C's subtypes. callable, iterable and object may pass the tests their values may pass.

Exceptions (no report) ​

  • A value whose type is unknown, includes mixed, or is an intersection; an unreachable test.
  • A constant (global or class constant, or an element of one), whose value may depend on configuration; $this inside a closure or an arrow function, which may run bound to another object.
  • A value written again later in a loop around the test (assigned, incremented, iterated into, destructured into, unset), itself or a value it reads through: its type may change from one iteration to the next.
  • A value that is, or a local assigned, a call to a PHP function or method whose declared return type changes after the target PHP version (sort() returns bool before PHP 8.2 and true after).
  • Classes whose hierarchy is not wholly known, traits ($this in a trait is the class using it), and classes that are or extend a class declared in the project's vendor directory (Composer's config.vendor-dir, vendor by default): another release of that package, which the project may also support, may implement more interfaces.
  • A test that is always true and is, through parentheses and !, the whole condition of the last elseif (or else if) of a chain without else: it names the case left rather than testing anything.
  • Code in a test context (a file ending with Test.php, Spec.php or .phpt, a Fixtures directory, or a test class), and tests inside an assert() call.
  • Tests other rules report, when they run and check that code: an always-false is_int(), is_float(), is_string(), is_bool(), is_array() or is_object() on a parameter of the enclosing function or method (CallableParameterUseCaseInTypeContext); in the condition of an if or elseif split on one symbolic && or || operator, an instanceof another instanceof of the same value makes redundant (under || the more specific one, under && the broader one) or, in an && chain, a null comparison of the value the first instanceof tests (NotOptimalIfConditions, when it reports instanceof flaws); a null comparison of getAttribute() on a DOMElement or a subclass, directly or through a local (DomMissingAttributeCheckedAsNull). An instanceof next to an unrelated one, or outside an if condition, stays this rule's. An instanceof of a trait is left to SuspiciousBinaryOperation.

Fix ​

None: whether to drop the test, or to fix what the value holds, is the author's choice.

PHP versions ​

No gating.

Configure ​

In custos.json:

json
{
  "rules": {
    "RedundantTypeCheck": {
      "enabled": true
    }
  }
}

Suppress ​

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

php
// @custos-ignore RedundantTypeCheck

Released under the MIT License. Native rules and rules modeled on Php Inspections (EA Extended); independent clean-room implementation.