RedundantTypeCheck
warning native off by defaultGroup: 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, withCa written name (notself,staticorparent) 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_scalaroris_iterable, with the tested value as its only argument (by position, or namedvalue); x === nullorx !== 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 Cwhen it isCor a subtype of it, and never does when it cannot be: an unrelated class, or a final class (or enum) outsideC's subtypes.callable,iterableandobjectmay 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;
$thisinside 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()returnsboolbefore PHP 8.2 andtrueafter). - Classes whose hierarchy is not wholly known, traits (
$thisin a trait is the class using it), and classes that are or extend a class declared in the project's vendor directory (Composer'sconfig.vendor-dir,vendorby 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 lastelseif(orelse if) of a chain withoutelse: it names the case left rather than testing anything. - Code in a test context (a file ending with
Test.php,Spec.phpor.phpt, aFixturesdirectory, or a test class), and tests inside anassert()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()oris_object()on a parameter of the enclosing function or method (CallableParameterUseCaseInTypeContext); in the condition of aniforelseifsplit on one symbolic&&or||operator, aninstanceofanotherinstanceofof the same value makes redundant (under||the more specific one, under&&the broader one) or, in an&&chain, anullcomparison of the value the firstinstanceoftests (NotOptimalIfConditions, when it reportsinstanceofflaws); anullcomparison ofgetAttribute()on aDOMElementor a subclass, directly or through a local (DomMissingAttributeCheckedAsNull). Aninstanceofnext to an unrelated one, or outside anifcondition, stays this rule's. Aninstanceofof 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