[DowngradePhp82] Add DowngradeReflectionMethodHasPrototypeRector - #398
Open
roxblnfk wants to merge 1 commit into
Open
[DowngradePhp82] Add DowngradeReflectionMethodHasPrototypeRector#398roxblnfk wants to merge 1 commit into
roxblnfk wants to merge 1 commit into
Conversation
ReflectionMethod::hasPrototype() was added in PHP 8.2 and cannot be polyfilled, since the method simply does not exist below 8.2. Emulate it by calling getPrototype() inside a try/catch: it returns the prototype on 8.2- when one exists and throws ReflectionException otherwise. Because the replacement needs statements, wrap it in an immediately invoked closure so the call can be downgraded in any expression context; the receiver is passed as an argument rather than captured, so complex receiver expressions keep working. Assisted-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This was referenced Sep 8, 2026
Closed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🔍 What was changed
New
DowngradeReflectionMethodHasPrototypeRector(PHP 8.2 → 8.1) that rewrites$reflectionMethod->hasPrototype()into an emulation based ongetPrototype().$reflectionMethod->hasPrototype()becomes:The rule bails out on first-class callables, on non-
ReflectionMethodreceivers, and on any other method name.Why?
ReflectionMethod::hasPrototype()was added in PHP 8.2 and does not exist below it, so the usualmethod_exists()ternary idiom used by other reflection downgrade rules would always evaluate tofalse— semantically wrong.getPrototype()(available long before) throwsReflectionExceptionexactly when there is no prototype, which is precisely whathasPrototype()reports, so it is a faithful substitute.The result must be usable in any expression context (
if (...), assignment, argument), buttry/catchis a statement — hence the immediately invoked closure. The receiver is passed as an argument instead of captured viause, so a complex receiver expression keeps working without a separate variable.Chained downgrades below 8.0 are already covered: the emitted non-capturing
catch (\ReflectionException)is lowered by the existingDowngradeNonCapturingCatchesRector.Checklist
Fixtures cover the plain return case, the
if ($m->hasPrototype()) { $m = $m->getPrototype(); }pattern, and two skip cases (non-ReflectionMethodreceiver, first-class callable). Locally green: PHPUnit, PHPStan, ECS,rector process --dry-run, class-leak.