Skip to content

Conversation

@jradtilbrook
Copy link
Contributor

@jradtilbrook jradtilbrook commented Jun 20, 2025

This adds a new rule that adds the HasFactory trait to Eloquent models if not present. Useful as part of the other factory related rules to upgrade pre-v8.0 code.

I think might be worth including this in the set and updating some docs - let me know 👍

@jradtilbrook jradtilbrook marked this pull request as ready for review June 20, 2025 08:52
The set is not really complete without it. It'll work if you use the
factory directly, but the other rectors convert to use the static method
which this rector fixes
@peterfox
Copy link
Collaborator

Sorry @jradtilbrook things have been a bit busy lately. It looks overall good, I need to consider adding to the rule group and just the structure of the tests

@peterfox
Copy link
Collaborator

@jradtilbrook would you be able to refactor your branch to structure your tests like you did in the other branch? #351

@jradtilbrook
Copy link
Contributor Author

👍 I'll try get onto that tonight

@jradtilbrook
Copy link
Contributor Author

@peterfox ready to go now

@GeniJaho GeniJaho merged commit 2d37fc6 into driftingly:main Aug 8, 2025
5 checks passed
@Tamas-hi
Copy link

Does it make sense to add HasFactory to all Models automatically / by default with the laravel-legacy-factories-to-classes set?

I still want to convert legacy factories to classes, but now all my models received a hasFactory trait, even those which do not a have factory at all.

@jradtilbrook
Copy link
Contributor Author

Hmm good question 👍 In my opinion, I think it does when used as part of the set. Since the set contains FactoryDefinitionRector to migrate the factory, it makes sense for it to also mark the model with HasFactory so its a complete migration to the newest standard for eloquent factories.

However, as it allows for configuration, if that does not work for you, you can use each part of the set separately and configure the allowlist of models on which it runs.

@GeniJaho
Copy link
Collaborator

Upon second thought, I agree that keeping this rule out of the Legacy Factories set makes more sense.

I'm not sure that it's easy to configure a rule when it's part of a set. You either have to use it without configuration, or not use the set at all, and get the rules one by one. Either way, it's not a great experience. We might have 'broken' the set with the last release 😓

Given we have other rules coming in for the Factories, we'll probably exclude this HasFactory rule from the legacy migration set, and include it in a new one, just for Factories.

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.

4 participants