Skip to content

Declare that __call returns static - #19

Merged
jarstelfox merged 1 commit into
masterfrom
return-static-from-call
Nov 23, 2026
Merged

jarstelfox merged 1 commit into
masterfrom
return-static-from-call

Conversation

@jarstelfox

Copy link
Copy Markdown
Member

🤖
Every clause method on QueryGenerator (select, from, where and the rest) runs through __call, which always returns $this but declared no return type. Psalm therefore read each chained call as returning mixed, so in $q->select(...)->from(...) everything after the first call went unchecked. Tracked in iFixit/ifixit#66899.

__call now declares : static. The native type beats a @return $this docblock here because PHP enforces it at runtime, and composer.json already requires PHP 8.2 (static return types exist since 8.0). Psalm resolves static to the calling class, so subclasses keep their own methods through a chain.

The Psalm baseline loses its PossiblyUnusedMethod entry for useOr: the test's ->where(...)->useOr() chain is now typed, so Psalm finds that call.

Left out: @method lines per clause. They would add argument checking at every call site in the iFixit repo, which is a separate change.

Checks

Run locally on PHP 8.3, the version CI uses:

  • vendor/bin/phpunit QueryGeneratorTest.php: 17 tests, 111 assertions, OK
  • vendor/bin/psalm --no-cache: no errors (before the baseline edit, it reported the one unused useOr entry)

🤖 Generated with Claude Code

Every clause method (select, from, where, ...) goes through __call,
which always ends with `return $this`, but it had no return type. Static
analysis therefore treated each chained call as returning mixed, so a
chain like `$q->select(...)->from(...)` lost its type after the first
call and every method called on the result was unchecked.

A native `: static` return type fixes that and is enforced at runtime.
The package already requires PHP 8.2, and `static` has existed since
8.0. At a call site Psalm resolves `static` to the caller's class, so a
subclass keeps its own methods through the chain.

The baseline drops its PossiblyUnusedMethod entry for useOr: the test
calls `->where(...)->useOr()`, which Psalm can now see.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jarstelfox
jarstelfox marked this pull request as ready for review November 23, 2026 22:51
@jarstelfox
jarstelfox merged commit a2de374 into master Nov 23, 2026
4 checks passed
@jarstelfox jarstelfox mentioned this pull request Nov 24, 2026
2 tasks done
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.

1 participant