All code should be reachable, dead code should be avoided

このページは日本語には対応しておりません。随時翻訳に取り組んでいます。
翻訳に関してご質問やご意見ございましたら、お気軽にご連絡ください

Metadata

ID: php-code-style/no-unreachable

Language: PHP

Severity: Warning

Category: Error Prone

Description

This rule requires that all code in a function or method must be reachable, and dead code (code that can never be executed) should be avoided. This rule is important because dead code can make the program more difficult to understand and maintain. It can also lead to confusion and bugs if other developers mistakenly believe the dead code is doing something.

Dead code often occurs when developers leave behind commented-out code or functions that are no longer called. It can also happen when a return statement or an exception is thrown before the code.

To ensure compliance with this rule, always remove code that is no longer needed. Use version control systems to keep track of changes instead of leaving old code in comments. Be mindful of the flow of your functions and methods to ensure all code is reachable. For example, if you have a return statement in your function, make sure it’s the last operation in your function.

Non-Compliant Code Examples

<?php
function test() {
    echo "test";
    return;
    echo "testing";
}

Compliant Code Examples

<?php
function test() {
    echo "test";
    echo "testing";
    return;
}
https://static.datadoghq.com/static/images/logos/github_avatar.svg https://static.datadoghq.com/static/images/logos/vscode_avatar.svg jetbrains

Seamless integrations. Try Datadog Code Analysis