Skip to content

ZipOpenTruthyResult ​

warning native on by default quick-fix PHP ≥ 5.3

Group: Probable bugs

ZIP opening returns true for success and integer codes for errors. Check for true explicitly instead of using truthiness.

Example ​

php
<?php
$z=new ZipArchive(); if($z->open($path)){echo "ready";}
php
<?php
$z=new ZipArchive(); if(($z->open($path)===true)){echo "ready";}

Reported:

  • line 2: Accept only true from ZipArchive::open.

Detection ​

  • Resolved ZipArchive::open call is used directly as an if/elseif/while truthiness condition or direct logical negation. An elseif clause counts exactly like if, whether written as one word or as else if. Its error integers are nonzero; success is boolean true. Require receiver proven ZipArchive. Highlight call.
  • Report a direct logical negation even outside a branch condition, but offer a fix only for direct if/elseif/while truthiness or its negation. Withhold the fix if replacing its condition span would discard comments.
  • Resolve builtin functions, classes, constants and methods; user-defined lookalikes are excluded. Follow only bounded local proof in one lexical scope.
  • Emit one finding per violating operation, not per supporting evidence statement. When the proof is inconclusive, nothing is reported.

Exceptions (no report) ​

  • Do not report unresolved or shadowed builtin names, unknown values, or incomplete receiver/type provenance. Unknown calls, aliases escaping, and intervening writes invalidate local proof. Similar code outside the stated detection contract is not a finding.
  • Both standard suppression forms suppress this native ID.

Fix ​

F1. In the proven truthiness condition replace the call with (originalCall === true); preserve all original argument bytes and call evaluation count. For negated condition use originalCall !== true. No fix in numeric comparisons or assignments whose value is used elsewhere.

Comments introduced by # are comments too: suppress a fix whenever its replacement would discard them, just as for line and block comments.

PHP versions ​

Requires PHP 5.3 or later.

Configure ​

In custos.json:

json
{
  "rules": {
    "ZipOpenTruthyResult": {
      "enabled": false
    }
  }
}

Suppress ​

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

php
// @custos-ignore ZipOpenTruthyResult

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