Skip to content

ImmutableDateResultIgnored ​

warning native on by default quick-fix PHP ≥ 5.5

Group: Probable bugs

Detect discarded return from modify, add, sub, setDate, setTime, setTimestamp or setTimezone on a receiver proven DateTimeImmutable.

Example ​

php
<?php
$day=new DateTimeImmutable('2025-03-10'); $day->modify('+1 day'); echo $day->format('c');
php
<?php
$day=new DateTimeImmutable('2025-03-10'); $day = $day->modify('+1 day'); echo $day->format('c');

Reported:

  • line 2: Assign the returned immutable date.

Detection ​

  • Report discarded return from modify, add, sub, setDate, setTime, setTimestamp or setTimezone on a receiver proven DateTimeImmutable.
  • Resolve builtin calls and named arguments before checking the contract. Project-local callees are considered only when their bodies or immutable summaries establish the required facts. The default is enabled, with positive proof required.
  • For flow-dependent conditions, require a reachable source-to-use path with no intervening invalidation. Unknown calls, ambiguous aliases, recursion or analysis-budget exhaustion cannot establish definite facts.

Exceptions (no report) ​

  • Mutable DateTime, unknown receivers, subclasses overriding the method contract and deliberately returned/chained results are excluded.
  • Unresolved targets, incomplete required facts, suppressed findings and PHP versions lacking the referenced API or language feature produce no report. User declarations shadowing builtin names are not builtin contracts.

Fix ​

Assign the result back to a fresh simple receiver whose immediately following statement echoes its literal format call; preserve the original modification call and its comments.

The supported proof is deliberately narrow. The receiver must be initialized by the first statement of a file, namespace, function or method body with a builtin new DateTimeImmutable and no constructor arguments or one literal string argument. The discarded modification must be the second statement. It must call modify with one positional literal containing a decimal integer between -9999 and 9999, one space, and one of day, hour, minute, second, month or year (singular or plural). The following echo must have exactly one expression: the same receiver's format call with one positional literal string argument.

Withhold the fix for parameter receivers, closure captures, conditional or loop bodies, aliases or extra intervening statements, unknown argument evaluation, other modification methods, invalid or unsupported modifier strings, and global/superglobal receiver names. These shapes cannot establish the local assignment contract.

A fix is offered only when the concrete prerequisites above are proven. Edits use exact source byte ranges, preserve comments and argument evaluation order, and introduce syntax supported by the configured PHP version. Never invent error-handling policy.

PHP versions ​

Available from PHP 5.5. Builtin behavior and argument contracts follow the configured PHP version. Extension-specific calls require resolved extension API symbols; do not treat same-named application functions as extension calls.

Options ​

None. Enable or disable this rule through normal rule configuration.

Configure ​

In custos.json:

json
{
  "rules": {
    "ImmutableDateResultIgnored": {
      "enabled": false
    }
  }
}

Suppress ​

Before the statement or declaration (or the first statement of the file):

php
// @custos-ignore ImmutableDateResultIgnored

Released under the MIT License. Native rules and rules modeled on Php Inspections (EA Extended); independent clean-room implementation.