raphhh/trex-parser

A PHP tool to parse code and extract statements

1.1.0 2015-12-28 13:15 UTC

This package is auto-updated.

Last update: 2024-10-25 22:40:55 UTC


README

Latest Stable Version Build Status Scrutinizer Quality Score Code Coverage SensioLabsInsight Dependency Status Total Downloads Reference Status License

PHP tool to parse code and extract statements.

Install

$ composer require raphhh/trex-parser

Usage

ClassAnalyzer

First, give the PHP code to analyze:

$code = 'class Foo{}';

$analyzer = new ClassAnalyzer();
$classReflections = $analyzer->getClassReflections($code);

You can also give a file path to the analyzer:

$filePath = 'MyClass.php';

$analyzer = new ClassAnalyzer();
$classReflections = $analyzer->getClassReflectionsFromFile($filePath);

$classReflections is an array of ReflectionClass.

$objects = [];
foreach($classReflections as $className => $classReflection){
    $objects[$className] = $classReflection->newInstance();
}        

ClassName

Retrieve base name

$className = new ClassName('\Foo\Qux\Bar');
$className->getBaseName(); //"Bar"

Retrieve namespace

$className = new ClassName('\Foo\Qux\Bar');
$className->getNamespace(); //"Foo\Qux"

Split

$className = new ClassName('\Foo\Qux\Bar');
$className->split(); //["Foo", "Qux", "Bar"]