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

Draft
wants to merge 3 commits into
base: 12.x
Choose a base branch
from
Draft

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.


namespace Illuminate\Cache;

class ClassUsesRecursive
Copy link
Contributor

Choose a reason for hiding this comment

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

I might say, we could go as far as understanding the class as broader (e.g. ClassMetaData) to potentially include other methods.

Copy link
Contributor

Choose a reason for hiding this comment

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

Okay, maybe \Illuminate\Support\Reflector is that class already 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for your comments! 🙏
You're right: I think I will move my code to Reflector class.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@@ -11,6 +11,43 @@

class Reflector
{
private static array $classesUsesRecursive = [];
Copy link
Contributor

@cosmastech cosmastech Jun 21, 2025

Choose a reason for hiding this comment

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

I think of Laravel/package code uses protected to allow for extension.

Suggested change
private static array $classesUsesRecursive = [];
/**
* @var array<class-string, list<class-string>>
*/
protected static array $classesUsesRecursive = [];

@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.

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.

3 participants