ScandirFailureIterated
warning native on by defaultGroup: Probable bugs
Identify foreach iterable directly produced by scandir or its unchanged local result without a dominating false-rejecting guard.
Detection
Follow each producer reaching a local read, including results written on an earlier loop turn or before a backward jump. A check of an earlier result does not check a later replacement; a check repeated before every consumption does. Ignore producers belonging to other result contracts when selecting an unchecked result.
- Report foreach iterable directly produced by scandir or its unchanged local result without a dominating false-rejecting guard.
- A guard rejects false when it tests the same unchanged local with: strict or loose comparison against
false(=== false,!== false,== false,!= false, either operand order), plain truthiness ($entries,!$entries),empty($entries), or the builtinis_array($entries), each optionally negated or parenthesized. Truthiness tests count because an empty listing gives the loop nothing to do. - A guard dominates the loop in two positions. Inside a branch: the loop sits in an
if/elseifbody whose condition proves non-false when true, or in a laterelseif/elsebranch after a condition that proves non-false when false. After an early exit: an earlier statement of the same or an enclosing statement list is anifwhose body always leaves the path (return, throw, exit, break, continue) and whose condition proves non-false when false, or anif/elsewithoutelseifwhoseelsebody always leaves and whose condition proves non-false when true. - Composition is directional. A condition known to hold proves non-false through any
&&/andoperand but only through every||/oroperand; a condition known to fail proves it through any||/oroperand but only through every&&/andoperand. Soif (!$entries || $skip) { return; }guards the loop whileif (!$entries && $skip) { return; }does not. - Resolve builtin functions, classes, constants and methods, including imports and supported named arguments. Follow only bounded local facts within one lexical scope. Receiver and argument provenance must establish the specific API contract; variable names and comments never prove intent.
- Report one finding per violating operation. A reachable path, unchanged value identity and applicable API behavior are required. Intervening mutation, unknown calls involving tracked values, escaped aliases or exhausted analysis budgets invalidate proof.
A resolved same-file reference parameter, reference capture or global writer can replace a checked directory result. Retain the replacement's producer; require another check before iterating it. An untracked reference write remains unknown and does not establish a directory-enumeration producer.
Only completely followed same-file bodies export replacement producers. An unstarted generator closure does not establish that its body wrote a new result; advancing generators is outside this local producer contract.
Exceptions (no report)
- Exclude unresolved required symbols, user-defined lookalikes, incomplete syntax, unknown required values and unsupported versions.
- A guard located outside a loop that assigns the iterated local is ignored: later iterations may see a different value.
assert()is not a guard. Assertions can be compiled out, so the loop still receives false in production and the finding stays.- A value the shared flow analysis proves is not false at the use is never reported, whatever form the proof takes: a truthiness or
empty()test, a loose comparison withfalseornull, a type predicate, or an early exit whoseifalso has anelsebranch that continues. For exampleif ($entries == null) { return; }leaves the path for a failed listing, sincefalse == nullholds. The same holds for a statement that leaves on the failing outcome ($v !== false or exit;,$v or throw …;,$v ?: throw …;), for a failing branch that ends in a call that never returns (a callee declarednever, or a function or a final or private method every path of which throws or exits), for a test stored in a boolean and tested later while neither the boolean nor the tested variable has been written, and, on PHP 8.0 and later outside anytrywith acatch, for a builtin call that returned after receiving the value for a parameter that only accepts arrays, objects or callables: such a call throws aTypeErrorforfalse. Aswitch (true)case is read like the condition of anif/elseifchain; in aswitchon the variable itself with constant labels, the cases after afalseornulllabel and thedefaulthold a value that is neither, as does atruecase. Positions PHP may skip prove nothing: the right side of??=, the arguments of a null-safe call and anything chained after?->. A null-safe call to a never-returning method does not end the path, andf(...)creates a closure without callingf. An arm ofmatch (true)is read like aswitch (true)case; an arm that throws, exits or calls a function that never returns takes no part in what holds after thematch.|and&between two tests are read like||and&&for the outcome that fixes both operands (a false|, a true&), and a strictin_array($v, [...], true)over literals that include neitherfalsenornullexcludes both where it holds.gotojumps are followed, including a jump out of atryorcatchblock, which runs thefinallyblocks it leaves on its way. Where agotolabel is written before the use, and after the place where its scope first names the checked variable, a variable that can hold the same value, or code that may write any variable (extract,compact,get_defined_vars,parse_strgiven one argument, a variable variable,$GLOBALS,eval, an included file), a jump can land between a guard and the use: only what the flow analysis establishes counts there, and a guard recognized by where it is written proves nothing. Two variables can hold the same value when one is written from the other by a plain assignment, an assignment by reference or??=, when aforeachtakes one out of the other, or through a chain of those; in the value assigned, the receiver and the arguments of a call, the operands of an operator other than??, the variables a string embeds and an element key do not count, and a statement that only reads (echo $prefix . $parts['host'];) ties nothing. A parameter is named from the start of the function, and a checked expression that reads$thisor no variable counts every label before it. In a scope the flow analysis does not follow (one that evaluates or includes code, which may write any variable) a guarded use after such a label is therefore reported. A generator is followed: while it is suspended at ayield, the code that consumes it may write only what an unknown call may write, and, when the generator returns by reference, the variable it yields. A label written before all of that, such as aretry:label ahead of thetrythat produces and checks the value, changes nothing, whatever else the function reads beside the checked value; at file level every label written before the use counts. What was established of astaticlocal holds until the next call, or code that runs without a call being written (an object converted to text, a magic property or offset access, the destructor of an object a variable held when it is given another value), that may enter the function again and assign the variable. A call of PHP's own counts as such when it may run code of the project: it is given a callback other than the name of a PHP function that only computes on text and numbers and raises no warning whatever text it is given (a closed list:strlen,trim,intvaland the like, nothexdec,absorchr, which warn about text that is no number), an object it may convert, a class name it loads (method_exists($name, 'm'),is_callable('A::b'),defined('A::B'),class_alias($name, 'B'),new ReflectionClass($name),$arrayObject->setIteratorClass($name)), a class to construct ($statement->fetchObject('Row')) or a filter that one of PHP's ownFILTER_…constants does not name, which may be the one that calls back, or it runs what was registered earlier (an error, session or output-buffer handler, the callbacks of a cURL transfer or an XML parser, an iterator wrapped in another, a filter added to a stream). A warning that a PHP function raises by itself when called directly (hex2bingiven an odd number of digits, a file that cannot be read) is not counted, although the error handler of the project receives it. This limits every guard form of this specification, not only the ones the flow analysis reads. - Both
@custos-ignore ScandirFailureIteratedand@noinspection ScandirFailureIteratedsuppress this native rule.
Fix
None. A repair requires a semantic decision or application error-handling policy.
PHP versions
Available in PHP 5.3–8.5 where the referenced API exists. Do not apply extension contracts when the resolved API is unavailable. Gate syntax and version-specific behavior to the configured target.
Configure
In custos.json:
{
"rules": {
"ScandirFailureIterated": {
"enabled": false
}
}
}Suppress
Before the statement or declaration (or the first statement of the file):
// @custos-ignore ScandirFailureIterated