rkr / php-structure-locator
A PHP library to locate classes, functions, and other stuff in PHP files using xml and xpath.
dev-main
2025-06-24 21:35 UTC
Requires
- php: >= 8.1
- ext-dom: *
- nikic/php-parser: >= 5.5
- psr/log: >= 1.0
- symfony/finder: >= 6.0
Requires (Dev)
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^12.2
This package is auto-updated.
Last update: 2025-06-24 21:35:27 UTC
README
A tool for indexing PHP code elements including functions, classes, methods, properties, parameters, PHPDoc annotations,
attributes and their relative file paths to enable efficient searching using xpath
.
Overview
The indexer performs the following steps:
- Uses symfony/finder to locate PHP files
- Detects which files have changed, been added or removed
- Indexes the code elements in an XML data structure
- Stores the index in an easily searchable XML file
- Enables searching via XPath queries
Usage Example
Find all attributes of class-methods with a specific name:
//class/method/attribute[@name='NS\\MyAttribute']
First, index your PHP files:
use PhpLocate\UpdateIndexService; use Psr\Log\NullLogger; use Symfony\Component\Finder\Finder; $files = (new Finder()) ->in(__DIR__ . '/src') ->name('*.php'); $service = new UpdateIndexService(new NullLogger()); $service->updateIndex(indexPath: __DIR__ . '/index.xml', files: $files);
Then, search the index using XPath:
use PhpLocate\Index; $index = Index::fromFile(__DIR__ . '/index.xml'); $path = $index->getFirstString("/files/file[class/method/attribute[@name='NS\\MyAttribute']]/@path"); echo $path;
Progress
- Functions
- Parameters
- Attributes
- Type hint
- Return type
- PHPDoc annotations
- Parameters
- Class definitions
- Attributes
- PHPDoc annotations
- Final mark
- Abstract mark
- Implementing Interfaces
- Extending class
- Traits
- Methods
- Attributes
- Arguments
- Visibility
- Static mark
- Final mark
- Abstract mark
- Constructor methods
- Parameters
- Attributes
- Type hint
- Return type
- Attributes