Linter to check if the result of the new operator is silently discarded
lexidor opened this issue · comments
Lexidor Digital commented
If the return value of a constructor is not used, the constructor is only called for its side effect or the intent was to do something with the value, but the author forgot.
Constructors with side effects that the caller cares about (without caring about the newly constructed object) are very rare and possibly bad form. Let's require that the new object is used in some manner. This makes statements that only contain a NewExpression a lint error.
Example of error that this linter would prevent.
if ($permission->hasNotBeenGrantedToUser($user)){
new PermissionDeniedException(...);
}
The intent was probably to throw the exception, but throw
was left out.