1.2.434. Exit() Usage

Using exit or die() in the code makes the code untestable (it will break unit tests). Moreover, if there is no reason or string to display, it may take a long time to spot where the application is stuck.

Try exiting the function/class with return, or throw exception that may be caught later in the code.

<?php

// Throw an exception, that may be caught somewhere
throw new Exception('error');

// Dying with error message.
die('error');

function foo() {
    //exiting the function but not dying
    if (somethingWrong()) {
        return true;
    }
}
?>

1.2.434.1. Suggestions

  • Avoid exit and die. Let the script finish.

  • Throw an exception and let it be handled before finishing

1.2.434.2. Specs

Short name

Structures/ExitUsage

Rulesets

All, Analyze, Appinfo, CE, CI-checks

Exakat since

0.8.4

PHP Version

All

Severity

Major

Time To Fix

Quick (30 mins)

Precision

Very high

Features

exit

ClearPHP

no-exit

Examples

Traq, ThinkPHP

Available in

Entreprise Edition, Community Edition, Exakat Cloud