class PhpAnalyzer (View source)

This utility class can be used to extract information about PHP files without having to instantiate/reflect classes.

Usage:

// extract the FQN e.g. "Some\Namespace\SomeClassName" $fullyQualifiedClassName = (new PhpAnalyzer($fileContents))->extractFullyQualifiedClassName();

// extract the namespace "Some\Namespace" $namespace = (new PhpAnalyzer($fileContents))->extractNamespace();

// extract just the class name "SomeClassName" $className = (new PhpAnalyzer($fileContents))->extractClassName();

Properties

protected string $phpCode

Methods

__construct(string $phpCode)

No description

string|null
extractFullyQualifiedClassName()

Extracts the Fully Qualified Class name from the given PHP code

string|null
extractNamespace()

Extracts the PHP namespace from the given PHP code

string|null
extractClassName()

Extracts the className of the given PHP code Note: This only returns the class name without namespace, extractFullyQualifiedClassName()

Details

__construct(string $phpCode)

No description

Parameters

string $phpCode

string|null extractFullyQualifiedClassName()

Extracts the Fully Qualified Class name from the given PHP code

Return Value

string|null

FQN in the format "Some\Fully\Qualified\ClassName" or NULL if no class was detected

string|null extractNamespace()

Extracts the PHP namespace from the given PHP code

Return Value

string|null

the PHP namespace in the form "Some\Namespace" (w/o leading backslash) - or NULL if no namespace modifier was found

string|null extractClassName()

Extracts the className of the given PHP code Note: This only returns the class name without namespace, extractFullyQualifiedClassName()

Return Value

string|null