Skip to content

SplFixedArrayShrinkDiscardsValues ​

warning native off by default PHP ≥ 5.3

Group: Probable bugs

Preserve populated entries before shrinking. This opt-in inspection applies the policy stated in Detection.

Example ​

Finding with the rule explicitly enabled:

php
<?php
$a = SplFixedArray::fromArray(["east", "west"]); $a->setSize(1);

Reported:

  • line 2: Preserve populated entries before shrinking.

Detection ​

  • Enabled policy forbids discarding populated fixed-array slots. Track known size and assigned slots of builtin SplFixedArray; report setSize with smaller nonnegative literal size when at least one populated slot is outside the new bounds. A null value still counts as populated when assignment is proven.
  • Emit one finding per violating operation. Flow facts require a reachable path with no intervening mutation, escaped alias, unknown call involving the tracked value or analysis-budget exhaustion. Uncertain facts cannot prove a violation.
  • offsetSet with a literal index populates that slot and offsetUnset with a literal index releases it, exactly like the bracket forms; writes and releases are applied in source order, so a slot refilled after its release is populated again. An earlier setSize on the same array already dropped the slots past its bound.

Exceptions (no report) ​

  • Fresh unpopulated arrays, explicit unset of all discarded slots, enlargement, unchanged size and unknown occupancy are excluded.
  • Occupancy is unknown, and nothing is reported, when before the shrink the array variable is captured by a closure or read inside an arrow function, copied or bound by reference to another variable, stored in a container, passed to a call other than the inspecting builtins count, sizeof, iterator_count, iterator_to_array, var_dump, print_r and var_export, used with a method other than setSize, offsetSet, offsetUnset and the read-only accessors (getSize, offsetGet, offsetExists, toArray, count, jsonSerialize, getIterator and the iterator methods), or released with a non-literal index or on only some paths (unset or offsetUnset inside a branch, a conditional earlier setSize). Reading a slot, cloning the array and iterating with foreach keep the occupancy known.
  • Exclude shadowed builtin symbols, unresolved required types, incomplete syntax and unsupported PHP versions.
  • When fromArray is given a preserveKeys value that is not a known boolean, or one through an unpacked argument, the populated entries are unknown: no report.

Fix ​

No automatic fix. Choosing a repair requires runtime information, error-handling policy or a semantic decision.

All edits use exact byte ranges, preserve comments and evaluation order, and require syntax supported by the target PHP version. A fix must never add an evaluation or silently change unrelated arguments.

PHP versions ​

Minimum PHP 5.3.

Options ​

None. Enabling the rule declares the policy stated in Detection; the rule is disabled by default.

Configure ​

In custos.json:

json
{
  "rules": {
    "SplFixedArrayShrinkDiscardsValues": {
      "enabled": true
    }
  }
}

Suppress ​

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

php
// @custos-ignore SplFixedArrayShrinkDiscardsValues

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