1.2.1197. 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.1197.2. Connex PHP features¶
1.2.1197.2.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.1197.2.2. Specs¶
Short name |
Exceptions/ThrowFunctioncall |
Rulesets |
|
Exakat since |
0.8.4 |
PHP Version |
All |
Severity |
Major |
Time To Fix |
Instant (5 mins) |
Precision |
Medium |
Examples |
|
Available in |