Skip to content

Fix remaining falsy index guards and cache the unary lookup - #81

Merged
dereuromark merged 1 commit into
masterfrom
falsy-index-guards-and-lookup-cache
Aug 6, 2026
Merged

Fix remaining falsy index guards and cache the unary lookup#81
dereuromark merged 1 commit into
masterfrom
falsy-index-guards-and-lookup-cache

Conversation

@dereuromark

Copy link
Copy Markdown
Contributor

Both points from Copilot's review of #80.

NoIsNull skipped calls at the top of a file

<?php is_null($x);        // not reported
<?php
$y = 1;
is_null($x);              // reported

findPrevious() returns index 0 for the open tag, and if (!$possibleCastIndex) reads that as "nothing found". #80 fixed the same shape on the sniff's first guard; this one sits further down the same method, with two more like it. The two guards in that file that already compared against false are untouched.

The unary lookup is built once

isUnaryOperator() rebuilt its prefix set on every call. That was cheap while the sniff only saw ! and @, but it now registers T_MINUS, so it runs for every subtraction in a file. The set is constant, so it moves behind a static.

NoIsNull still skipped a call that opens a file. findPrevious() returns index 0
for the open tag, and `if (!$possibleCastIndex)` treats that as not found, so
`<?php is_null($x);` went unreported while the same call one line lower was
caught. Two further guards in the same file had the same shape. The two that
already compared against false are left alone.

isUnaryOperator() rebuilt its prefix lookup on every call, and since the sniff
registers T_MINUS that now happens for every subtraction in a file. The set is
constant, so it is built once.
Copilot AI lite review requested due to automatic review settings August 6, 2026 13:39
@dereuromark
dereuromark merged commit e1c734c into master Aug 6, 2026
6 checks passed
@dereuromark
dereuromark deleted the falsy-index-guards-and-lookup-cache branch August 6, 2026 13:41

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses two follow-ups from the review of #80 by fixing remaining “falsy zero” index guards in NoIsNullSniff and improving performance in ImplicitCastSpacingSniff by caching the unary-prefix lookup.

Changes:

  • Replace if (!$index)-style guards with strict === false checks in NoIsNullSniff so token index 0 (open tag) is not misinterpreted as “not found”.
  • Cache the isUnaryOperator() unary-prefix token set behind a static so it’s built once instead of on every call.
  • Keep behavior unchanged aside from the intended fixes (no functional expansion beyond the guard correctness and cached lookup).

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
PhpCollective/Sniffs/WhiteSpace/ImplicitCastSpacingSniff.php Caches unary-prefix token set in isUnaryOperator() to avoid rebuilding it for every operator occurrence.
PhpCollective/Sniffs/PHP/NoIsNullSniff.php Fixes incorrect “falsy index” guards by using === false checks for findPrevious() results.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants