Skip to content

fix(hook): count the parameters of invokable objects and Class::method strings - #53

Merged
ogorzalka merged 1 commit into
mainfrom
fix/argument-detection-invokable-and-static-string
Nov 25, 2026
Merged

ogorzalka merged 1 commit into
mainfrom
fix/argument-detection-invokable-and-static-string

Conversation

@ogorzalka

Copy link
Copy Markdown
Member

Branch: fix/argument-detection-invokable-and-static-string (base main at 22bc9be, after the 2.0.2 release). Found while typing countParameters() for PHPStan level 7.

Bug

When no argument count is given, AbstractHook::countParameters() used ReflectionFunction for every callable that is not an array. It reads only closures and function names:

  • Action::add('init', new Handler) (invokable object) threw a TypeError (ReflectionFunction::__construct(): Argument #1 must be of type Closure|string, Handler given), not a ReflectionException, so it escaped the CallbackAnalysisFailedException wrapper.
  • Action::add('init', 'Handler::run') (static method as a string, callable in PHP and in WordPress) threw CallbackAnalysisFailedException: Function Handler::run() does not exist.
    Both are valid WordPress callbacks. Reproduced on main.

Fix

The count is read from __invoke() for an invokable object and from the named static method for a Class::method string (ReflectionMethod; the single-argument form is deprecated in PHP 8.4, so the string is split). The reflection cache key of an object callback is Class::__invoke@id instead of serialize($object) (which could throw or be expensive).

Proof

Two new tests (AbstractHookTest, "Argument detection"): without the fix, 2 failed (TypeError, CallbackAnalysisFailedException); with it, they pass. Full suite 554 pass + 1 incomplete (on main too). CHANGELOG [Unreleased] / Fixed added (nothing else in the changelog touched; the release PR for 2.0.2 is merged).

Not verified

Semgrep not run locally. Not run in a real WordPress (the WordPress suite needs a database).

@ogorzalka
ogorzalka merged commit 8531c52 into main Nov 25, 2026
15 checks passed
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