1.2.1188. Throw Functioncall

The throw keyword expects to use an exception. Calling a function to prepare that exception before throwing it is possible, but forgetting the new keyword is also possible.

When the new keyword is forgotten, then the class constructor is used as a function name, and now exception is emitted, but an Undefined function fatal error is emitted.

<?php

// Forgotten new
throw \RuntimeException('error!');

// Code is OK, function returns an exception
throw getException(ERROR_TYPE, 'error!');

function getException(ERROR_TYPE, $message) {
    return new \RuntimeException($messsage);
}

?>

1.2.1188.1. Suggestions

  • Add the new operator to the call

  • Make sure the function is really a functioncall, not a class name

  • Use return type for functions, so that Exception may be detected

1.2.1188.2. Specs

Short name

Exceptions/ThrowFunctioncall

Rulesets

All, Analyze, CE, CI-checks, Changed Behavior

Exakat since

0.8.4

PHP Version

All

Severity

Major

Time To Fix

Instant (5 mins)

Precision

Medium

Features

exception

Examples

SugarCrm, Zurmo

Available in

Entreprise Edition, Community Edition, Exakat Cloud