php

package
v1.1.8 Latest Latest
Warning

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

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

README

PHP Analyzer

PHP code analyzer for extracting symbols and structure from PHP files, with Laravel framework support.

Status

PAS 8-10 COMPLETEPRODUCTION READY

Laravel Analyzer - FULLY IMPLEMENTED

Test Results:

  • 21/21 Laravel tests PASS
  • Real project tested: Barou Laravel app (38 models, 116 relations, 813 chunks)
  • E2E integration: Full indexing pipeline working
  • Language manager: Auto-detection and routing complete
Implemented Features
PAS 8: 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']
PAS 9: End-to-End Testing ✅
  • ✅ Complete Laravel project E2E tests
  • ✅ Relationship resolution tests
  • ✅ Scopes and accessors tests
  • Real Barou project: 38 models, 116 relations analyzed successfully
PAS 10: Integration ✅
  • ✅ Laravel Adapter implementing PathAnalyzer interface
  • ✅ Language manager integration
  • ✅ Auto-detection for php, laravel, php-laravel project types
  • ✅ Chunk enrichment with Laravel metadata
  • ✅ Full indexing pipeline tested

Next Steps

READY FOR PRODUCTION - Laravel analyzer is complete and tested with real projects.

Optional enhancements for future:

  1. Route groups with middleware
  2. Migration analysis
  3. Symfony framework support
Implemented Files (8 files)
  • types.go (6.1KB) - PHP-specific internal types with API support
  • analyzer.go (21KB) - PathAnalyzer implementation with PHPDoc extraction
  • api_analyzer.go (7.4KB) - APIAnalyzer implementation
  • phpdoc.go (5.3KB) - PHPDoc parser helper
  • analyzer_test.go (9.9KB, 10 tests) - CodeAnalyzer tests
  • api_analyzer_test.go (7.4KB, 4 tests) - APIAnalyzer tests
  • parser_test.go (4.4KB, 5 tests) - Parser API validation tests
  • Integration: language_manager.go, workspace detector
Test Results
19/19 TESTS PASS ✅
Coverage: 83.6% of statements
Integration: COMPLETE ✅

Test Categories:

  • CodeAnalyzer (10 tests):
    • Basic class extraction
    • Global functions (2 tests)
    • Multiple classes (2 tests)
    • Properties (1 test)
    • Constants (1 test)
    • Interfaces (1 test)
    • Traits (1 test)
    • Complete class (1 test)
  • APIAnalyzer (4 tests):
    • Full API path analysis with PHPDoc
    • Global functions with documentation
    • Interface documentation
    • Trait documentation
  • Parser API (5 tests):
    • Basic parsing, class/function/method/namespace extraction
  • Integration (6 tests):
    • Language manager recognizes PHP/Laravel
    • Workspace detector identifies composer.json/artisan
    • Collection naming: ragcode-{workspaceID}-php

Dependencies

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

Features

✅ Implemented (PAS 1-7)
  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 (PAS 6)

    • Description extraction
    • @param tags (type, name, description)
    • @return tags (type, description)
    • @throws, @var, @deprecated, @see, @example tags
    • Type hint merging with PHPDoc
  7. API Documentation (PAS 6)

    • APIAnalyzer implementation
    • Conversion to APIChunk format
    • Full documentation export
    • Multi-file API analysis
  8. Multi-file Support

    • AnalyzePaths() for batch processing
    • AnalyzeAPIPaths() for API documentation
  9. Error Handling

    • Parser error recovery
    • Graceful degradation
  10. Integration (PAS 7)

    • language_manager.go integration ✅
    • Multi-language collection support ✅
    • Workspace detection (composer.json, artisan) ✅
    • Project type normalization (php, laravel) ✅
✅ Completed (PAS 8: Laravel-Specific Features)
  1. Laravel Framework Support
    • Eloquent Models Detection:
      • Extends Illuminate\Database\Eloquent\Model
      • $fillable array extraction
      • $guarded array extraction
      • $casts array extraction
      • $table property
      • Relations: hasOne(), hasMany(), belongsTo(), belongsToMany()
      • Scopes: scopeMethodName() detection
    • Controller Detection:
      • Extends Illuminate\Routing\Controller
      • Resource controllers
      • API controllers
      • Request validation
    • Directory Structure Awareness:
      • app/Models/ - Eloquent models
      • app/Http/Controllers/ - Controllers
      • routes/ - Route definitions
      • database/migrations/ - Migrations
❌ TODO (PAS 8-9)

All PAS 8–9 milestones for the Laravel analyzer are now complete (see the Status section above).

Remaining future enhancements (post-PAS 10):

  • Route groups with middleware
  • Migration analyzer
  • Symfony framework support

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)

Laravel Support

The analyzer automatically detects Laravel projects through:

  • artisan file (Laravel CLI tool)
  • composer.json (PHP dependency manager)
  • app/Models/ directory structure

Project types recognized:

  • laravel - Laravel framework project
  • php-laravel - Alternative Laravel naming
  • php - Generic PHP project

All Laravel projects use the PHP analyzer with future Laravel-specific enhancements.

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

Next Steps

  1. PAS 8: Laravel-specific features (Eloquent models, Controllers)
  2. PAS 9: End-to-end testing with Laravel projects
  3. PAS 10: Production testing with Symfony projects

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