How to locate all usages of a method in PHP code with Bash and Git

bdelespierre profile image Benjamin Delespierre ・1 min read

Using Git grep you can locate code fragment very, very fast. Much faster than any IDE. So I made a very simple script to help me refactor my code.


Create a file find-usage.sh with the following content

git grep -C2 -p -E "(-[>]|::)$1\("

make it executable with

chmod +x find-usage.sh

It will locate usage like $object->method() as well as Object::staticMethod().


find-usage.sh methodName


>_bin/find-usage.sh getName
app/Models/Addworking/User/Concerns/User/HasLogs.php=trait HasLogs
app/Models/Addworking/User/Concerns/User/HasLogs.php-    {
app/Models/Addworking/User/Concerns/User/HasLogs.php-        $this->log()->create([
app/Models/Addworking/User/Concerns/User/HasLogs.php:            'route'         => optional($request->route())->getName() ?? 'n/a',
app/Models/Addworking/User/Concerns/User/HasLogs.php-            'url'           => substr($request->fullUrl(), 0, 254),
app/Models/Addworking/User/Concerns/User/HasLogs.php-            'http_method'   => $request->getMethod(),

Leave a like and a comment if you need help using it.

Posted on by:

bdelespierre profile

Benjamin Delespierre


CTO at AddWorking, I write Laravel snippets all the time


markdown guide