php

package
v1.1.16 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 26, 2025 License: MIT Imports: 15 Imported by: 0

README ยถ

PHP Code Analyzer

Code analyzer for extracting symbols, structure, and relationships from PHP files. Includes full support for the Laravel framework. Indexes code for semantic search in Qdrant.

Status: โœ… PRODUCTION READY


๐ŸŽฏ What This Analyzer Does

The PHP analyzer parses .php files and extracts:

  1. Symbols - classes, methods, functions, interfaces, traits, constants
  2. Relationships - inheritance, implementations, Eloquent relations
  3. Metadata - PHPDoc, visibility, types, Laravel-specific
  4. Framework - Eloquent models, Controllers, Routes (Laravel)

๐Ÿ“Š Data Flow

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”     โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”     โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚   .php Files    โ”‚โ”€โ”€โ”€โ”€โ–ถโ”‚   PHP Analyzer   โ”‚โ”€โ”€โ”€โ”€โ–ถโ”‚   CodeChunks    โ”‚
โ”‚  (source code)  โ”‚     โ”‚  (VKCOM parser)  โ”‚     โ”‚  (structured)   โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜     โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜     โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                                                          โ”‚
                        โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”              โ”‚
                        โ”‚ Laravel Analyzer โ”‚โ—€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
                        โ”‚ (Eloquent, etc.) โ”‚              โ”‚
                        โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜              โ–ผ
                                                 โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                                                 โ”‚     Qdrant      โ”‚
                                                 โ”‚  (vector store) โ”‚
                                                 โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐Ÿ” What We Index

1. Classes (type: "class")
<?php
namespace App\Models;

/**
 * Represents a user in the system.
 */
class User extends Model implements Authenticatable
{
    use SoftDeletes, Notifiable;
    
    protected $fillable = ['name', 'email'];
    protected $casts = ['email_verified_at' => 'datetime'];
}

Extracted information:

Field Value Description
name "User" Class name
namespace "App\\Models" Namespace
full_name "App\\Models\\User" Fully qualified name
extends "Model" Parent class
implements ["Authenticatable"] Implemented interfaces
traits ["SoftDeletes", "Notifiable"] Used traits
is_abstract false If abstract
is_final false If final
docstring "Represents a user..." PHPDoc
2. Methods (type: "method")
/**
 * Returns the user's orders.
 * 
 * @param int $limit Maximum number of orders
 * @return Collection<Order>
 */
public function getOrders(int $limit = 10): Collection
{
    return $this->orders()->limit($limit)->get();
}

Extracted information:

Field Value Description
name "getOrders" Method name
visibility "public" Visibility
is_static false If static
is_abstract false If abstract
parameters [{name: "limit", type: "int", default: "10"}] Parameters
return_type "Collection" Return type
phpdoc.params [{name: "limit", type: "int", desc: "..."}] PHPDoc params
phpdoc.return {type: "Collection<Order>", desc: ""} PHPDoc return
3. Interfaces (type: "interface")
interface PaymentGateway extends Gateway
{
    public function charge(float $amount): bool;
    public function refund(string $transactionId): bool;
}
4. Traits (type: "trait")
trait Auditable
{
    public function getCreatedBy(): ?User { ... }
    public function logActivity(string $action): void { ... }
}
5. Global Functions (type: "function")
/**
 * Helper for price formatting.
 */
function format_price(float $amount, string $currency = 'USD'): string
{
    return number_format($amount, 2) . ' ' . $currency;
}

๐Ÿ”— Laravel Framework Support

Eloquent Models
class Order extends Model
{
    protected $fillable = ['user_id', 'total', 'status'];
    protected $casts = ['total' => 'decimal:2'];
    
    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class);
    }
    
    public function items(): HasMany
    {
        return $this->hasMany(OrderItem::class);
    }
    
    public function scopeCompleted($query)
    {
        return $query->where('status', 'completed');
    }
    
    public function getTotalFormattedAttribute(): string
    {
        return number_format($this->total, 2) . ' USD';
    }
}

Extracted Laravel metadata:

{
  "is_eloquent_model": true,
  "fillable": ["user_id", "total", "status"],
  "casts": {"total": "decimal:2"},
  "relations": [
    {"name": "user", "type": "belongsTo", "related": "User"},
    {"name": "items", "type": "hasMany", "related": "OrderItem"}
  ],
  "scopes": ["completed"],
  "accessors": ["total_formatted"]
}
Controllers
class OrderController extends Controller
{
    public function index(): View { ... }
    public function store(OrderRequest $request): RedirectResponse { ... }
    public function show(Order $order): View { ... }
}

Controller metadata:

{
  "is_controller": true,
  "is_resource_controller": true,
  "actions": ["index", "store", "show"],
  "http_methods": {
    "index": "GET",
    "store": "POST",
    "show": "GET"
  }
}
Routes
Route::get('/orders', [OrderController::class, 'index'])->name('orders.index');
Route::resource('users', UserController::class);

๐Ÿ—๏ธ File Structure

php/
โ”œโ”€โ”€ types.go              # PHP types: ClassInfo, MethodInfo, etc.
โ”œโ”€โ”€ analyzer.go           # PathAnalyzer implementation (21KB)
โ”œโ”€โ”€ api_analyzer.go       # APIAnalyzer for documentation
โ”œโ”€โ”€ phpdoc.go             # PHPDoc parser
โ”œโ”€โ”€ analyzer_test.go      # 10 CodeAnalyzer tests
โ”œโ”€โ”€ api_analyzer_test.go  # 4 APIAnalyzer tests
โ”œโ”€โ”€ parser_test.go        # 5 parser tests
โ”œโ”€โ”€ README.md             # This documentation
โ””โ”€โ”€ laravel/              # Laravel module
    โ”œโ”€โ”€ types.go          # Laravel-specific types
    โ”œโ”€โ”€ analyzer.go       # Laravel coordinator
    โ”œโ”€โ”€ adapter.go        # PathAnalyzer adapter
    โ”œโ”€โ”€ eloquent.go       # Eloquent Models analyzer
    โ”œโ”€โ”€ controller.go     # Controllers analyzer
    โ”œโ”€โ”€ routes.go         # Routes analyzer
    โ””โ”€โ”€ README.md         # Laravel documentation

๐Ÿ’ป Usage

import "github.com/doITmagic/rag-code-mcp/internal/ragcode/analyzers/php/laravel"

// For Laravel projects (recommended)
analyzer := laravel.NewAdapter()

// Analyze directories/files
chunks, err := analyzer.AnalyzePaths([]string{"./app"})

for _, chunk := range chunks {
    fmt.Printf("[%s] %s\n", chunk.Type, chunk.Name)
    if relations, ok := chunk.Metadata["relations"]; ok {
        fmt.Printf("  Relations: %v\n", relations)
    }
}

๐Ÿ”Œ Integration

Language Manager

The PHP/Laravel analyzer is automatically selected for:

  • php - generic PHP projects
  • laravel - Laravel projects
  • php-laravel - Laravel alternative
Workspace Detection
File/Directory Project Type
artisan Laravel
composer.json PHP
app/Models/ Laravel
routes/web.php Laravel

๐Ÿ“‹ CodeChunk Types

Type Description Example
class PHP class class User extends Model
method Class method public function save()
function Global function function helper()
interface Interface interface Payable
trait Trait trait Auditable
const Class constant const STATUS_ACTIVE = 1
property Property protected $fillable

๐Ÿท๏ธ Complete Metadata

Class Metadata
{
  "namespace": "App\\Models",
  "extends": "Model",
  "implements": ["Authenticatable"],
  "traits": ["SoftDeletes"],
  "is_abstract": false,
  "is_final": false,
  "is_eloquent_model": true,
  "fillable": ["name", "email"],
  "relations": [...]
}
Method Metadata
{
  "class_name": "UserController",
  "visibility": "public",
  "is_static": false,
  "is_abstract": false,
  "is_final": false,
  "parameters": [...],
  "return_type": "View",
  "phpdoc": {
    "description": "...",
    "params": [...],
    "return": {...}
  }
}

๐Ÿงช Testing

# All PHP tests (19 tests)
go test ./internal/ragcode/analyzers/php/...

# Laravel only (21 tests)
go test ./internal/ragcode/analyzers/php/laravel/...

# With coverage
go test -cover ./internal/ragcode/analyzers/php/...

Results:

  • โœ… 19/19 PHP tests PASS
  • โœ… 21/21 Laravel tests PASS
  • โœ… Coverage: 83.6%

๐Ÿ“ฆ Dependencies

  • VKCOM/php-parser v0.8.2 - PHP parser with PHP 8.0-8.2 support

โš ๏ธ Limitations

Limitation Description
No Runtime Static analysis, doesn't execute code
Single-file Each file is analyzed independently
No Autoload Doesn't resolve Composer autoload

๐Ÿ”ฎ Future Improvements

  • Route groups with middleware
  • Migration analyzer
  • Symfony framework support
  • WordPress support
  • Cross-file type resolution

Implemented Features

Laravel-Specific Features โœ…
  1. Eloquent Models (COMPLETE):

    • โœ… Model detection (extends Model)
    • โœ… Property extraction: $fillable, $guarded, $casts, $table, $primaryKey
    • โœ… All Relations: hasOne, hasMany, belongsTo, belongsToMany, hasManyThrough, morphTo, morphMany, morphToMany, morphedByMany
    • โœ… Foreign key & local key extraction
    • โœ… Fully-qualified name resolution with imports
    • โœ… Scopes: scopeMethodName() detection
    • โœ… Accessors/Mutators: getXxxAttribute(), setXxxAttribute()
    • โœ… SoftDeletes trait detection
  2. Controllers (COMPLETE):

    • โœ… Controller detection (extends Controller)
    • โœ… Resource controller identification (7 RESTful actions)
    • โœ… API controller detection
    • โœ… HTTP method inference from action names
    • โœ… Parameter extraction
  3. Routes (COMPLETE):

    • โœ… Route file parsing (routes/web.php, routes/api.php)
    • โœ… Route::get(), Route::post(), etc.
    • โœ… Route::match() support
    • โœ… Route::resource() expansion
    • โœ… Controller@action binding
    • โœ… Array syntax [Controller::class, 'action']
Core PHP Features โœ…
  1. Namespace Support

    • Multi-namespace per-file
    • Fully qualified names
  2. Class Analysis

    • Class declarations with extends/implements
    • Method extraction (visibility, static, abstract, final)
    • Property extraction (visibility, static, readonly, typed)
    • Class constants (visibility, value extraction)
    • Parameter and return type support
    • PHPDoc extraction (description, @param, @return)
  3. Interface Support

    • Interface declarations
    • Method signatures
    • Multiple interface extends
    • PHPDoc documentation
  4. Trait Support

    • Trait declarations
    • Methods and properties
    • PHPDoc documentation
  5. Function Analysis

    • Global functions
    • Namespaced functions
    • Parameters and return types
    • PHPDoc documentation
  6. PHPDoc Parsing

    • Description extraction
    • @param tags (type, name, description)
    • @return tags (type, description)
    • @throws, @var, @deprecated, @see, @example tags
    • Type hint merging with PHPDoc

Code Metrics

  • Total Lines: ~1,800
  • Core Implementation:
    • analyzer.go (21KB, 814 lines)
    • api_analyzer.go (7.4KB, 293 lines)
    • phpdoc.go (5.3KB, 217 lines)
  • Helper Functions: 25+
  • Test Coverage: 83.6%
  • Tests: 19 (5 parser + 10 analyzer + 4 API)
  • Integration Tests: 6 (language manager + workspace detector)

Architecture

Follows the same pattern as golang analyzer with modular framework support:

php/
โ”œโ”€โ”€ types.go              - Internal type definitions
โ”œโ”€โ”€ analyzer.go           - PathAnalyzer implementation
โ”œโ”€โ”€ api_analyzer.go       - APIAnalyzer implementation
โ”œโ”€โ”€ phpdoc.go             - PHPDoc parser (PHP-specific helper)
โ”œโ”€โ”€ analyzer_test.go      - CodeAnalyzer tests
โ”œโ”€โ”€ api_analyzer_test.go  - APIAnalyzer tests
โ”œโ”€โ”€ parser_test.go        - Parser validation tests
โ””โ”€โ”€ laravel/              - Laravel framework module (separate package)
    โ”œโ”€โ”€ types.go          - Laravel-specific types (Eloquent, Controllers, Routes)
    โ”œโ”€โ”€ analyzer.go       - Laravel framework analyzer coordinator
    โ”œโ”€โ”€ eloquent.go       - Eloquent Model analyzer
    โ”œโ”€โ”€ controller.go     - Controller analyzer
    โ””โ”€โ”€ README.md         - Laravel module documentation
Framework Modules

Framework-specific analyzers are separated into their own packages:

  • laravel/ - Laravel framework support (Eloquent, Controllers, Routes)
  • symfony/ - Symfony framework support (planned)
  • wordpress/ - WordPress support (planned)

This modular design allows:

  • Clean separation of concerns
  • Independent testing of framework features
  • Easy addition of new frameworks
  • Reusable base PHP analyzer

Documentation ยถ

Index ยถ

Constants ยถ

This section is empty.

Variables ยถ

This section is empty.

Functions ยถ

This section is empty.

Types ยถ

type ClassInfo ยถ

type ClassInfo struct {
	Name        string            `json:"name"`
	Namespace   string            `json:"namespace"`
	FullName    string            `json:"full_name"`   // Fully qualified name (FQN)
	Description string            `json:"description"` // PHPDoc description
	Extends     string            `json:"extends,omitempty"`
	Implements  []string          `json:"implements,omitempty"`
	Uses        []string          `json:"uses,omitempty"` // Trait usage
	Methods     []MethodInfo      `json:"methods"`
	Properties  []PropertyInfo    `json:"properties"`
	Constants   []ConstantInfo    `json:"constants"`
	IsAbstract  bool              `json:"is_abstract"`
	IsFinal     bool              `json:"is_final"`
	FilePath    string            `json:"file_path,omitempty"`
	StartLine   int               `json:"start_line,omitempty"`
	EndLine     int               `json:"end_line,omitempty"`
	Code        string            `json:"code,omitempty"`
	Imports     map[string]string `json:"imports,omitempty"` // Map of alias -> full name
}

ClassInfo describes a PHP class

type CodeAnalyzer ยถ

type CodeAnalyzer struct {
	// contains filtered or unexported fields
}

CodeAnalyzer implements PathAnalyzer for PHP

func NewCodeAnalyzer ยถ

func NewCodeAnalyzer() *CodeAnalyzer

NewCodeAnalyzer creates a new PHP code analyzer

func (*CodeAnalyzer) AnalyzeFile ยถ

func (ca *CodeAnalyzer) AnalyzeFile(filePath string) ([]codetypes.CodeChunk, error)

AnalyzeFile analyzes a single PHP file

func (*CodeAnalyzer) AnalyzePaths ยถ

func (ca *CodeAnalyzer) AnalyzePaths(paths []string) ([]codetypes.CodeChunk, error)

AnalyzePaths implements the PathAnalyzer interface

func (*CodeAnalyzer) GetPackages ยถ

func (ca *CodeAnalyzer) GetPackages() []*PackageInfo

GetPackages returns the internal package information Useful for framework-specific analyzers (e.g., Laravel)

func (*CodeAnalyzer) IsLaravelProject ยถ

func (ca *CodeAnalyzer) IsLaravelProject() bool

IsLaravelProject detects if the analyzed code is from a Laravel project

type ConstantInfo ยถ

type ConstantInfo struct {
	Name        string `json:"name"`
	Value       string `json:"value"`
	Type        string `json:"type,omitempty"`
	Description string `json:"description"`
	Visibility  string `json:"visibility,omitempty"` // For class constants (PHP 7.1+)
	ClassName   string `json:"class_name,omitempty"` // If class constant
	FilePath    string `json:"file_path,omitempty"`
	StartLine   int    `json:"start_line,omitempty"`
	EndLine     int    `json:"end_line,omitempty"`
}

ConstantInfo describes a class/interface constant or global constant

type FunctionInfo ยถ

type FunctionInfo struct {
	Name        string                 `json:"name"`
	Signature   string                 `json:"signature"`
	Description string                 `json:"description"`
	Parameters  []codetypes.ParamInfo  `json:"parameters"`
	ReturnType  string                 `json:"return_type,omitempty"`
	Returns     []codetypes.ReturnInfo `json:"returns,omitempty"`
	Namespace   string                 `json:"namespace,omitempty"`
	IsMethod    bool                   `json:"is_method"`
	ClassName   string                 `json:"class_name,omitempty"` // If method
	Visibility  string                 `json:"visibility,omitempty"` // If method
	IsStatic    bool                   `json:"is_static,omitempty"`  // If method
	FilePath    string                 `json:"file_path,omitempty"`
	StartLine   int                    `json:"start_line,omitempty"`
	EndLine     int                    `json:"end_line,omitempty"`
	Code        string                 `json:"code,omitempty"`
}

FunctionInfo describes a global function or method

type InterfaceInfo ยถ

type InterfaceInfo struct {
	Name        string         `json:"name"`
	Namespace   string         `json:"namespace"`
	FullName    string         `json:"full_name"`
	Description string         `json:"description"`
	Extends     []string       `json:"extends,omitempty"` // Interfaces can extend multiple
	Methods     []MethodInfo   `json:"methods"`
	Constants   []ConstantInfo `json:"constants"`
	FilePath    string         `json:"file_path,omitempty"`
	StartLine   int            `json:"start_line,omitempty"`
	EndLine     int            `json:"end_line,omitempty"`
	Code        string         `json:"code,omitempty"`
}

InterfaceInfo describes a PHP interface

type MethodInfo ยถ

type MethodInfo struct {
	Name        string                 `json:"name"`
	Signature   string                 `json:"signature"`
	Description string                 `json:"description"`
	Parameters  []codetypes.ParamInfo  `json:"parameters"`
	ReturnType  string                 `json:"return_type,omitempty"`
	Returns     []codetypes.ReturnInfo `json:"returns,omitempty"`
	Visibility  string                 `json:"visibility"` // public, protected, private
	IsStatic    bool                   `json:"is_static"`
	IsAbstract  bool                   `json:"is_abstract"`
	IsFinal     bool                   `json:"is_final"`
	ClassName   string                 `json:"class_name,omitempty"` // Parent class/interface/trait
	FilePath    string                 `json:"file_path,omitempty"`
	StartLine   int                    `json:"start_line,omitempty"`
	EndLine     int                    `json:"end_line,omitempty"`
	Code        string                 `json:"code,omitempty"`
}

MethodInfo describes a class/interface/trait method

type PHPDocInfo ยถ

type PHPDocInfo struct {
	Description string
	Params      []ParamDoc
	Returns     []ReturnDoc
	Throws      []string
	Var         string
	VarType     string
	Deprecated  string
	See         []string
	Examples    []string
}

PHPDocInfo contains parsed PHPDoc information

type PackageInfo ยถ

type PackageInfo struct {
	Namespace   string          `json:"namespace"`   // Namespace name (e.g., "App\\Http\\Controllers")
	Path        string          `json:"path"`        // Directory path
	Description string          `json:"description"` // From file-level docblock
	Classes     []ClassInfo     `json:"classes"`
	Interfaces  []InterfaceInfo `json:"interfaces"`
	Traits      []TraitInfo     `json:"traits"`
	Functions   []FunctionInfo  `json:"functions"` // Global functions
	Constants   []ConstantInfo  `json:"constants"` // Global constants
	Uses        []string        `json:"uses"`      // Use imports

	// AST nodes for advanced analysis (not serialized to JSON)
	ClassNodes map[string]*ast.StmtClass `json:"-"` // Map: full class name -> AST node
}

PackageInfo contains comprehensive information about a PHP namespace/package In PHP context, this represents a namespace or the global scope

type ParamDoc ยถ

type ParamDoc struct {
	Name        string
	Type        string
	Description string
}

ParamDoc represents a @param tag

type PropertyInfo ยถ

type PropertyInfo struct {
	Name         string `json:"name"`
	Type         string `json:"type,omitempty"` // Type hint if available
	DefaultValue string `json:"default_value,omitempty"`
	Description  string `json:"description"`
	Visibility   string `json:"visibility"` // public, protected, private
	IsStatic     bool   `json:"is_static"`
	IsReadonly   bool   `json:"is_readonly"` // PHP 8.1+
	FilePath     string `json:"file_path,omitempty"`
	StartLine    int    `json:"start_line,omitempty"`
	EndLine      int    `json:"end_line,omitempty"`
}

PropertyInfo describes a class/trait property

type ReturnDoc ยถ

type ReturnDoc struct {
	Type        string
	Description string
}

ReturnDoc represents a @return tag

type TraitInfo ยถ

type TraitInfo struct {
	Name        string         `json:"name"`
	Namespace   string         `json:"namespace"`
	FullName    string         `json:"full_name"`
	Description string         `json:"description"`
	Methods     []MethodInfo   `json:"methods"`
	Properties  []PropertyInfo `json:"properties"`
	FilePath    string         `json:"file_path,omitempty"`
	StartLine   int            `json:"start_line,omitempty"`
	EndLine     int            `json:"end_line,omitempty"`
	Code        string         `json:"code,omitempty"`
}

TraitInfo describes a PHP trait

Directories ยถ

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL