permafrost-dev/php-code-search
Search PHP projects by symbol or text to quickly find classes, methods, functions, and constants across your codebase. Fast, lightweight code search for local repositories, useful for audits, refactors, and navigating large Laravel or PHP applications.
Installation
composer require permafrost-dev/php-code-search
Add to composer.json under require-dev if only for local development.
First Use Case
Search for all method calls to Auth::login() in your Laravel app:
use Permafrost\CodeSearch\Searcher;
$searcher = new Searcher();
$results = $searcher->search(
base_path('app'),
'Auth::login('
);
foreach ($results as $result) {
echo "Found in: {$result['file']} at line {$result['line']}\n";
}
Where to Look First
Searcher class methods: search(), searchInFiles(), searchForFunctions()src/Permafrost/CodeSearch/Project-Wide Searches
$searcher = new Searcher();
$results = $searcher->search(
base_path(), // Root directory
'->where(' // Look for Eloquent where clauses
);
File-Specific Searches
$results = $searcher->searchInFiles(
[app_path('Http/Controllers/UserController.php')],
'dd('
);
Function/Method Call Tracking
// Find all calls to a custom helper
$results = $searcher->searchForFunctions(
'my_custom_helper',
base_path('app')
);
Integration with Laravel Artisan Create a custom command:
use Permafrost\CodeSearch\Searcher;
class SearchCommand extends Command {
protected $signature = 'search:code {query} {--directory=}';
public function handle(Searcher $searcher) {
$results = $searcher->search(
$this->option('directory') ?? base_path(),
$this->argument('query')
);
$this->line(print_r($results, true));
}
}
Exclude Directories
$searcher->search(base_path(), '->', [
'exclude' => ['vendor', 'storage']
]);
Case-Insensitive Search
$searcher->search(base_path(), 'auth::', [
'flags' => Searcher::FLAG_CASE_INSENSITIVE
]);
Callback Filtering
$results = $searcher->search(base_path(), '->')
->filter(fn($result) => str_contains($result['file'], 'Controller'));
Performance with Large Codebases
vendor/ or node_modules/ directories.--directory to limit scope in CLI commands.False Positives in Regex
Auth::login( may match Auth::loginUser().Auth::login\( (with escaped parentheses).Line Number Accuracy
\n vs \r\n).$searcher->normalizeFiles([$filePath]);
Multibyte Characters
/).$searcher->setVerbose(true);
Custom Search Patterns
Extend Searcher to add domain-specific patterns:
class LaravelSearcher extends Searcher {
public function searchForEloquentQueries($query) {
return $this->search(base_path(), $query, [
'flags' => self::FLAG_CASE_INSENSITIVE
]);
}
}
Post-Processing Results
Use ->map() or ->filter() to transform results:
$results->map(fn($r) => [
'file' => str_replace(base_path(), '', $r['file']),
'line' => $r['line'],
'context' => $this->getContext($r['file'], $r['line'])
]);
Integration with IDEs Export results to JSON for IDE plugins:
file_put_contents(
storage_path('code-search.json'),
json_encode($results)
);
.env). Override with:
$searcher->setExcludes([]);
memory_limit. Increase if needed:
memory_limit = 512M
How can I help you explore Laravel packages today?