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

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:

  1. Uses symfony/finder to locate PHP files
  2. Detects which files have changed, been added or removed
  3. Indexes the code elements in an XML data structure
  4. Stores the index in an easily searchable XML file
  5. 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
  • 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