Skip to content

[12.x] Class uses recursive #56079

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 9 commits into from
Closed

Conversation

fgaroby
Copy link
Contributor

@fgaroby fgaroby commented Jun 18, 2025

This PR adds a new class which allow to cache all traits used by a class, its parent classes and trait of their traits.

As suggested in #56069 (comment)

Copy link

Thanks for submitting a PR!

Note that draft PR's are not reviewed. If you would like a review, please mark your pull request as ready for review in the GitHub user interface.

Pull requests that are abandoned in draft may be closed due to inactivity.

Copy link
Contributor

@shaedrich shaedrich left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome! Are you going to add trait_uses_recursive() as well?

* @param $haystackClassName
* @return bool
*/
public static function inArray($needleClassName, $haystackClassName): bool
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do you feel about naming it uses()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm totally aware about the naming.

@cosmastech
Copy link
Contributor

This is very much what I had envisioned.

I would suggest:

  1. Modify class_uses_recursive() so that it relies on this new static class. You've replaced it inside the framework code (which if you didn't do today, someone would come along in a month and do), which is great. However, userland libs will most definitely leverage the class_uses_recursive() function as well, and this will give a win across the board.
  2. I think in the base test case, it makes sense to clear the memoized values. https://github.com/laravel/framework/blob/12.x/src/Illuminate/Foundation/Testing/Concerns/InteractsWithTestCaseLifecycle.php#L169-L190
  3. Obviously function naming can be improved, as you said.
  4. I feel the namespace should be Illuminate\Support, so that it's available globally to all components. Otherwise, Illuminate sub-component have to update their dependencies in their composer.json.

@shaedrich
Copy link
Contributor

One thing, you might also cover here, is the following scenario:

We have ChildParentGrandparent

Reflector::classesUsesRecursive(Child::class); // returns [Parent::class, Grandparent::class] (uncached)
Reflector::classesUsesRecursive(Child::class); // returns [Parent::class, Grandparent::class] (now cached)
Reflector::classesUsesRecursive(Parent::class); // returns [Grandparent::class] (uncached)

The invocation with Parent could potentially also already be cached when called with Child beforehand.

@fgaroby fgaroby force-pushed the class_uses_recursive branch from 88bf9db to 8cd0f9b Compare June 27, 2025 21:18
@fgaroby
Copy link
Contributor Author

fgaroby commented Jun 27, 2025

I don't understand why all these tests fail 🤔

@fgaroby fgaroby marked this pull request as ready for review June 27, 2025 22:06
@taylorotwell
Copy link
Member

Thanks for your pull request to Laravel!

Unfortunately, I'm going to delay merging this code for now. To preserve our ability to adequately maintain the framework, we need to be very careful regarding the amount of code we include.

If applicable, please consider releasing your code as a package so that the community can still take advantage of your contributions!

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