Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/permafrost-dev/php-code-search
Search PHP source code for function & method calls, variables, and more from PHP.
https://github.com/permafrost-dev/php-code-search
code-search package php php-library search
Last synced: 5 days ago
JSON representation
Search PHP source code for function & method calls, variables, and more from PHP.
- Host: GitHub
- URL: https://github.com/permafrost-dev/php-code-search
- Owner: permafrost-dev
- License: mit
- Created: 2021-07-04T12:34:10.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2024-09-03T04:34:58.000Z (4 months ago)
- Last Synced: 2024-12-24T11:32:48.891Z (12 days ago)
- Topics: code-search, package, php, php-library, search
- Language: PHP
- Homepage:
- Size: 362 KB
- Stars: 27
- Watchers: 3
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: .github/CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE.md
- Security: .github/SECURITY.md
Awesome Lists containing this project
README
# Searching PHP source code made easy
---
Search PHP source code for function & method calls, variable assignments, classes and more directly from PHP.
---
## Installation
```bash
composer require permafrost-dev/php-code-search
```## Searching
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 found
- `node->name(): string`
- `location` - the location in the file that the item was found
- `location->startLine(): int`
- `location->endLine(): int`
- `snippet` - a snippet of code lines from the file with the result line in the middle
- `snippet->toString(): string`
- `file()` _(method)_ - provides access to the file that was searched### Searching
To search through the code in a string or file, use the `Searcher` class:
```php
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.
```php
$searcher
->functions(['strtolower', 'strtoupper'])
->search('./file1.php');$searcher
->variables(['/^one[A-Z]$/'])
->searchCode('variables(['twoA', '/^one.$/'])
->searchCode('results as $result) {
echo "Found '{$result->node->name()}' on line {$result->location->startLine}" . PHP_EOL;
}
```### Functions
To search for function calls or definitions, use the `functions` method.
```php
// search for references AND definitions for 'strtolower' and/or 'myfunc'
$searcher
->functions(['strtolower', 'myfunc'])
->search('file1.php');
```### Method calls
To search for a method call by name, use the `methods` method.
Method call nodes have an `args` property that can be looped through to retrieve the arguments for the method call.
```php
$results = $searcher
->methods(['/test(One|Two)/'])
->searchCode('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;
}
}
```### Static calls
To search for static method or property calls, use the `static` method.
Valid search terms are either a class name like `Cache`, or a class name and a method name like `Cache::remember`.
```php
$searcher
->static(['Ray', 'Cache::has', 'Request::$myProperty'])
->search('./app/Http/Controllers/MyController.php');
```### Classes
To search for either a class definition or a class created by the `new` keyword, use the `classes` method.
```php
$searcher
->classes(['MyController'])
->search('./app/Http/Controllers/MyController.php');
```### Variable assignments
To search for a variable assignment by variable name, use the `assignments` method. _Note: The `$` should be omitted._
```php
$searcher
->assignments(['myVar'])
->search('./app/Http/Controllers/MyController.php');
```### Results without code snippets
To return search results without associated code snippets, use the `withoutSnippets` method:
```php
$searcher
->withoutSnippets()
->functions(['strtolower'])
->search('file1.php');
```## Testing
```bash
./vendor/bin/phpunit
```## Changelog
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
## Contributing
Please see [CONTRIBUTING](.github/CONTRIBUTING.md) for details.
## Security Vulnerabilities
Please review [our security policy](../../security/policy) on how to report security vulnerabilities.
## Credits
- [Patrick Organ](https://github.com/patinthehat)
- [All Contributors](../../contributors)## License
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.