Return Value Ignored

ID

php.return_value_ignored

Severity

low

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Reliability

Language

Php

Tags

reliability

Description

Reports calls to well-known pure built-in functions whose return value is discarded because the call appears on its own as an expression statement, for example trim($s);. Pure functions do not modify their arguments — they return a transformed value — so dropping that value means the call has no effect at all.

Rationale

A line like str_replace('a', 'b', $name); reads as if it mutates $name, but PHP strings are values: the function returns a new string and the original is left unchanged. The writer almost certainly meant $name = str_replace('a', 'b', $name);. Because these functions have no side effects, ignoring the result is always a bug, and surfacing it costs nothing in false positives.

<?php
function clean(string $s): string
{
    trim($s);            // FLAW — result discarded; $s is unchanged
    $s = trim($s);       // OK — result captured
    echo strtoupper($s); // OK — result consumed by echo
    return strtolower($s);
}

Remediation

Assign the result to a variable (or return / pass it on): $s = trim($s);. If the call really is unnecessary, delete the statement.