Feat: Add Factory has()/for() to Magic Method Rector - #525
Open
MizouziE wants to merge 1 commit into
Open
Conversation
Converts Eloquent factory `has()` and `for()` calls into their magic-method equivalents, e.g. `has(Comment::factory()->count(3))` becomes `hasComments(3)` and `for(User::factory())` becomes `forUser()`. Relation names are resolved from the related model's factory and the relation methods declared on the model, so polymorphic and singular relations are skipped rather than rewritten incorrectly. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
MizouziE
force-pushed
the
factory-has-for-to-magic-method-rector
branch
from
July 30, 2026 09:03
645eb8d to
eea2fa5
Compare
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.
See (now removed, as per repo convention) description below:
Hi, I work with an ancient (18 years old) PHP codebase which has been converted to use Laravel over the last 5+ years so a lot of it has very old ways of working still, but we are trying our hardest to be progressive and make use of the multitude of Laravel 13 conveniences. We had previously decided that, going forward, we would make more use of the eloquent magic methods for factory relationship definitions as we often find complex nested factories to be quite distracting when reading the setup for tests.
However, we all attended Laravel Live London recently and it was brought to our attention that we could use Rector (better than we already had been already) to help enforce it going forward and convert existing cases in one fell swoop.
This rule in the PR comes from the custom rule I got working with our 4000+ PHPUnit tests (+ a lot of Dusk tests) and we were all quite happy with the result, so I have tried to share it here and make it fit better with Rector-Laravel conventions.
Please do let me know if I can make further changes or take different approaches anywhere.
Possible concerns
Thanks for your time and a great package. It's value is immeasurable to teams like mine!