ArrayChunkInvalidSize
error native on by defaultGroup: Probable bugs
Detect array_chunk with a size proven an integer less than one.
Example
php
<?php
array_chunk($items,0);Reported:
- line 2: Supply a positive chunk size.
Detection
- Report array_chunk with a size proven an integer less than one.
- 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 or recursion cannot establish definite facts. When the proof is inconclusive, nothing is reported.
- D4. A size read from a variable is proven only when every change to that variable in the enclosing function (or top-level code) is a plain assignment the flow analysis follows. The same holds for every variable whose value an assignment in that scope copies into it, directly or through further copies (
$size = $seen;).++/--on the variable, a closure capturing it by reference (use (&$size)), an array item holding a reference to it ([&$size]) and anextract()call in the same scope each make the size unknown, wherever they appear in that scope.
Exceptions (no report)
- Unknown sizes and values proven positive 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.
- A size variable that is incremented, decremented, referenced from a closure or array item, or possibly rebound by
extract()in the calling scope (D4) is unknown:$size = 0; foreach ($headers as $h) { $size++; } array_chunk($items, $size);is not reported. Bodies of nested functions and closures are separate scopes and do not count, nor does a by-value capture.
Fix
No automatic fix: a replacement size cannot be inferred.
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": {
"ArrayChunkInvalidSize": {
"enabled": false
}
}
}Suppress
Before the statement or declaration (or the first statement of the file):
php
// @custos-ignore ArrayChunkInvalidSize