Assign Null In Function Call
ID |
php.assign_null_in_function_call |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Reliability |
Language |
Php |
Tags |
reliability, suspicious-call |
Description
Reports assignments that capture the result of a free function which never returns a value —
either because it declares a void return type or because its body has no value-bearing
return. Such a call evaluates to null, so the assigned variable holds nothing useful.
Rationale
A function that performs only side effects (logging, printing, mutating state) returns null
in PHP. Assigning that null to a variable reads as if the call produced a result, which
misleads the reader and hides the fact that the variable is always empty. The pattern usually
means the caller misunderstood the function’s contract or expected a different function. Calls
to functions that do return a value, or that declare a non-void return type, are left alone.
<?php
function logMessage($msg)
{
echo $msg; // no return — yields null
}
function compute($a, $b)
{
return $a + $b;
}
$a = logMessage('hello'); // FLAW — logMessage returns no value, $a is null
$b = compute(1, 2); // OK — compute returns a value
logMessage('plain'); // OK — result is not captured
Remediation
Call the function as a statement instead of assigning its result. If you genuinely need a return value, make the function return one (and declare its return type) or call a function that provides the value you expect.