Skip to content

ArrayReduceMissingInitialValue ​

warning native off by default quick-fix

Group: Probable bugs

Detect array_reduce without an initial argument when a local callback passes its accumulator to a builtin requiring an array on the first iteration.

Example ​

php
<?php
array_reduce($parts, fn($a,$v)=>array_merge($a,[$v]));
php
<?php
array_reduce($parts, fn($a,$v)=>array_merge($a,[$v]), []);

Reported:

  • line 2: Supply an array as the initial accumulator.

Detection ​

  • Report array_reduce without an initial argument when a local callback passes its accumulator to a builtin requiring an array on the first iteration.
  • 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 disabled; enable only when the stated intent matches the application.
  • For flow-dependent conditions, require a reachable source-to-use path with no intervening invalidation. Unknown calls, ambiguous aliases or recursion cannot establish definite facts. When the proof is inconclusive, nothing is reported.

Exceptions (no report) ​

  • Unknown callbacks, explicit initial values, and callbacks guarding or replacing null are excluded.
  • A callback whose first parameter is variadic is excluded: that parameter gathers the carry and the item into a list, so it is an array on every iteration and is not the accumulator itself.
  • 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 ​

Append an empty-array initial argument only when the first required accumulator type is unambiguously array and no conflicting accumulator use exists.

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 ​

Apply only on PHP versions supporting the referenced APIs and syntax (within PHP 5.3–8.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": {
    "ArrayReduceMissingInitialValue": {
      "enabled": true
    }
  }
}

Suppress ​

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

php
// @custos-ignore ArrayReduceMissingInitialValue

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