Skip to content

Redundancy Rules

This document details the rules available in the Redundancy category.

Available Rules

RuleCode
Constant Conditionconstant-condition
No Closing Tagno-closing-tag
No Empty Commentno-empty-comment
No Empty Loopno-empty-loop
No Noopno-noop
No Redundant Blockno-redundant-block
No Redundant Continueno-redundant-continue
No Redundant Fileno-redundant-file
No Redundant Finalno-redundant-final
No Redundant Labelno-redundant-label
No Redundant Mathno-redundant-math
No Redundant Method Overrideno-redundant-method-override
No Redundant Nullsafeno-redundant-nullsafe
No Redundant Parenthesesno-redundant-parentheses
No Redundant String Concatno-redundant-string-concat
No Redundant Write Visibilityno-redundant-write-visibility

constant-condition

Detects if statements where the condition is a constant that always evaluates to true or false.

Such statements are redundant. If the condition is always true, the if wrapper is unnecessary. If it's always false, the enclosed code is dead and can be removed or refactored.

Configuration

OptionTypeDefault
enabledbooleantrue
levelstring"help"

Examples

Correct Code

php
<?php
if ($variable > 10) {
    echo "Greater than 10";
}

Incorrect Code

php
<?php
if (true) {
    echo "This will always run";
}

if (false) {
    echo "This is dead code";
}

no-closing-tag

Detects redundant closing tags ( ?> ) at the end of a file.

Configuration

OptionTypeDefault
enabledbooleantrue
levelstring"help"

Examples

Correct Code

php
<?php

echo "Hello, world!";

Incorrect Code

php
<?php

echo "Hello, world!";

?>

no-empty-comment

Detects empty comments in the codebase. Empty comments are not useful and should be removed to keep the codebase clean and maintainable.

Configuration

OptionTypeDefault
enabledbooleantrue
levelstring"note"
preserve-single-line-commentsbooleanfalse

Examples

Correct Code

php
<?php

// This is a useful comment.
# This is also a useful comment.
/**
 * This is a docblock.
 */

Incorrect Code

php
<?php

//
#
/**/

no-empty-loop

Detects loops (for, foreach, while, do-while) that have an empty body. An empty loop body does not perform any actions and is likely a mistake or redundant code.

Configuration

OptionTypeDefault
enabledbooleantrue
levelstring"note"

Examples

Correct Code

php
<?php

foreach ($items as $item) {
    process($item);
}

Incorrect Code

php
<?php

while (should_wait()) {
    // Empty loop body
}

no-noop

Detects redundant noop statements.

Configuration

OptionTypeDefault
enabledbooleantrue
levelstring"help"

Examples

Correct Code

php
<?php

echo "Hello, world!";

Incorrect Code

php
<?php

;

no-redundant-block

Detects redundant blocks around statements.

Configuration

OptionTypeDefault
enabledbooleantrue
levelstring"help"

Examples

Correct Code

php
<?php

echo "Hello, world!";

Incorrect Code

php
<?php

{
    echo "Hello, world!";
}

no-redundant-continue

Detects redundant continue statements in loops.

Configuration

OptionTypeDefault
enabledbooleantrue
levelstring"help"

Examples

Correct Code

php
<?php

while (true) {
    echo "Hello, world!";
}

Incorrect Code

php
<?php

while (true) {
    echo "Hello, world!";
    continue; // Redundant `continue` statement
}

no-redundant-file

Detects redundant files that contain no executable code or declarations.

Configuration

OptionTypeDefault
enabledbooleantrue
levelstring"help"

Examples

Correct Code

php
<?php

declare(strict_types=1);

function foo(): void {
    return 42;
}

Incorrect Code

php
<?php

declare(strict_types=1);
// This file is redundant.

no-redundant-final

Detects redundant final modifiers on methods in final classes or enum methods.

Configuration

OptionTypeDefault
enabledbooleantrue
levelstring"help"

Examples

Correct Code

php
<?php

final class Foo {
    public function bar(): void {
        // ...
    }
}

Incorrect Code

php
<?php

final class Foo {
    final public function bar(): void {
        // ...
    }
}

no-redundant-label

Detects redundant goto labels that are declared but not used.

Configuration

OptionTypeDefault
enabledbooleantrue
levelstring"help"

Examples

Correct Code

php
<?php

goto end;
echo "Hello, world!";
end:

Incorrect Code

php
<?php

label:
echo "Hello, world!";

no-redundant-math

Detects redundant mathematical operations that can be simplified or removed. Includes operations like multiplying by 1/-1, adding 0, modulo 1/-1, etc.

Configuration

OptionTypeDefault
enabledbooleantrue
levelstring"help"

Examples

Correct Code

php
<?php

$result = $value * 2;
$sum = 1 + $total;
$difference = $value - 1;
$remainder = $x % 2;

Incorrect Code

php
<?php

$result = $value * 1;
$sum = 0 + $total;
$difference = $value - 0;
$remainder = $x % 1;
$negative = $value * -1;

no-redundant-method-override

Detects methods that override a parent method but only call the parent method with the same arguments.

Configuration

OptionTypeDefault
enabledbooleantrue
levelstring"help"

Examples

Correct Code

php
<?php

class Parent
{
    public function foo(): void
    {
        // ...
    }
}

class Child extends Parent
{
    public function foo(): void
    {
        parent::foo();

        echo 'Additional logic here';
    }
}

Incorrect Code

php
<?php

class Parent
{
    public function foo(): void
    {
        // ...
    }
}

class Child extends Parent
{
    public function foo(): void
    {
        parent::foo();
    }
}

no-redundant-nullsafe

Flags the use of the nullsafe operator (?->) in contexts where its null-checking behavior is redundant.

This occurs in two common situations:

  1. When an expression using ?-> is immediately followed by the null coalescing operator (??).
  2. When an expression using ?-> is checked with isset().

In both scenarios, the surrounding language construct (?? or isset()) already handles null values safely, making the ?-> operator superfluous and the code unnecessarily verbose.

Configuration

OptionTypeDefault
enabledbooleantrue
levelstring"help"

Examples

Correct Code

php
<?php

$name = $user->name ?? 'Guest';

if (isset($user->profile)) {
    // Do something with $user->profile
}

Incorrect Code

php
<?php

$name = $user?->name ?? 'Guest';

if (isset($user?->profile)) {
    // Do something with $user->profile
}

no-redundant-parentheses

Detects redundant parentheses around expressions.

Configuration

OptionTypeDefault
enabledbooleantrue
levelstring"help"

Examples

Correct Code

php
<?php

$foo = 42;

Incorrect Code

php
<?php

$foo = (42);

no-redundant-string-concat

Detects redundant string concatenation expressions.

Configuration

OptionTypeDefault
enabledbooleantrue
levelstring"help"

Examples

Correct Code

php
<?php

$foo = "Hello World";

Incorrect Code

php
<?php

$foo = "Hello" . " World";

no-redundant-write-visibility

Detects redundant write visibility modifiers on properties.

Configuration

OptionTypeDefault
enabledbooleantrue
levelstring"help"

Examples

Correct Code

php
<?php

final class User
{
    public $name;
}

Incorrect Code

php
<?php

final class User
{
    public public(set) $name;
}