Search PHP source code for function & method calls, variable assignments, classes and more directly from PHP.
composer require permafrost-dev/php-code-search
To search a file, use the search
method. Its only parameter may be either a string containing a valid filename or an instance of \Permafrost\PhpCodeSearch\Support\File
.
To search a string instead, use the searchCode
method.
The search methods return an instance of Permafrost\PhpCodeSearch\Results\FileSearchResults
, which has a results
property.
Each result
is an instance of Permafrost\PhpCodeSearch\Results\SearchResult
with the following properties:
node
- the specific item that was foundnode->name(): string
location
- the location in the file that the item was foundlocation->startLine(): int
location->endLine(): int
snippet
- a snippet of code lines from the file with the result line in the middlesnippet->toString(): string
file()
(method) - provides access to the file that was searched
To search through the code in a string or file, use the Searcher
class:
use Permafrost\PhpCodeSearch\Searcher;
$searcher = new Searcher();
To search a file, use the search
method, and the searchCode
method to search a string of code.
$results = $searcher
->functions(['strtolower', 'strtoupper'])
->search('./file1.php');
$results = $searcher
->variables(['twoA', '/^one[AB]$/'])
->searchCode('<?php $oneA = "1a"; $oneB = "1b"; $oneC = "1c"; $twoA = "2a"; $twoB = "2b";');
To search for variables by name, use the variables
method before calling search
. To use regular expressions, surround the values with /
.
$results = $searcher
->variables(['twoA', '/^one[AB]$/'])
->searchCode('<?php $oneA = "1a"; $oneB = "1b"; $oneC = "1c"; $twoA = "2a"; $twoB = "2b";');
foreach($results->results as $result) {
echo "Found '{$result->node->name()}' on line {$result->location->startLine}" . PHP_EOL;
}
To search for function calls or definitions, use the functions
method. Regular expressions can be used by surrounding the name with slashes /
, i.e. /test\d+/
.
// this will search for references AND definitions for 'strtolower' and 'myfunc'
$results = $searcher
->functions(['strtolower', 'myfunc'])
->search('./file1.php');
foreach($results->results as $result) {
echo "Found '{$result->node->name()}' on line {$result->location->startLine}" . PHP_EOL;
}
To search for a method call by name, use the methods
method before calling search
. Surround the search terms with slashes to use regular expressions.
Method call nodes have an args
property that can be looped through to retrieve the arguments for the method call.
$results = $searcher
->methods(['/test(One|Two)/'])
->searchCode('<?php '.
' $obj->testOne("hello world 1"); '.
' $obj->testTwo("hello world", 2); '.
'');
foreach($results->results as $result) {
echo "Found '{$result->node->name()}' on line {$result->location->startLine}" . PHP_EOL;
foreach($result->node->args as $arg) {
echo " argument: '{$arg->value}'" . PHP_EOL;
}
}
To search for static method or property calls, use the static
method before calling search
.
Valid search terms are either a class name like Cache
, or a class name and a method name like Cache::remember
.
$results = $searcher
->static(['Ray', 'Cache::has', 'Request::$myProperty'])
->search('./app/Http/Controllers/MyController.php');
foreach($results->results as $result) {
echo "Found '{$result->node->name()}' on line {$result->location->startLine}" . PHP_EOL;
}
To search for a class created by the new
keyword, use the classes
method before calling search
.
$results = $searcher
->classes(['MyClass'])
->search('./app/Http/Controllers/MyController.php');
foreach($results->results as $result) {
echo "Found '{$result->node->name()}' on line {$result->location->startLine}" . PHP_EOL;
}
To search for a variable assignment by variable name, use the assignments
method before calling search
. Note: The $
should be omitted.
$results = $searcher
->assignments(['myVar'])
->search('./app/Http/Controllers/MyController.php');
foreach($results->results as $result) {
echo "Found '{$result->node->name()}' on line {$result->location->startLine}" . PHP_EOL;
}
To return search results without associated code snippets, use the withoutSnippets
method:
$results = $searcher
->functions(['strtolower'])
->withoutSnippets()
->searchCode('<?php $str = strtolower("TEST");');
./vendor/bin/phpunit
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.