Skip to content

Instantly share code, notes, and snippets.

@DominikStyp
Last active February 16, 2022 17:29
Show Gist options
  • Save DominikStyp/80cb72fc722146602c57c1d9e3ed093d to your computer and use it in GitHub Desktop.
Save DominikStyp/80cb72fc722146602c57c1d9e3ed093d to your computer and use it in GitHub Desktop.
PHPStorm: How to find usages of the magic method in the project with Laravel example

How it works?

  1. You make a new instance of the Repository class
  2. You use reflection to invoke the getUser method on the $repository object
  3. You now try to find in PHPStorm (or other IDE which supports it) the usages of the method getUser in your project (Alt + F7)

IDE can't find it until you put the @uses \App\Repository::getUser() in the comments for method which uses it.

<?php
namespace App;
class Repository
{
public function getUser(): string
{
return 'user123';
}
}
<?php
// Laravel model fetching relation example
namespace App\Models;
class Sale {
/**
* @uses \App\Models\SalesGroup::details()
*
* @return \Illuminate\Database\Eloquent\Relations\HasOne
*/
public function offer()
{
return $this->hasOne('App\Models\SalesGroup', 'id', 'sale_group_id')
->with('details');
}
}
<?php
namespace App;
class SomeReflectionService
{
private function fetch(string $method)
{
$repository = new Repository();
return $repository->{$method}();
}
/**
* @uses \App\Repository::getUser()
* @return mixed
*/
public function fetchUser()
{
return $this->fetch('getUser');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment