Skip to content

ArrowFunctionCapturedCounter ​

warning native off by default PHP ≥ 7.4

Group: Probable bugs

Detect an arrow function updating a captured scalar counter when the same callable is invoked at least twice without reassignment.

Example ​

php
<?php
$n = 0; $next = fn() => ++$n; echo $next(), $next();

Reported:

  • line 2: Use persistent shared state for this counter.

Detection ​

  • Report an arrow function updating a captured scalar counter when the same callable is invoked at least twice without reassignment.
  • 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) ​

  • Parameters, object member updates, single calls, and unknown captured types 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 ​

No automatic fix: choosing reference capture and state lifetime requires intent.

PHP versions ​

Available from PHP 7.4. 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": {
    "ArrowFunctionCapturedCounter": {
      "enabled": true
    }
  }
}

Suppress ​

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

php
// @custos-ignore ArrowFunctionCapturedCounter

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