final class PhpFile

Represents a complete PHP file with namespaces, classes, and functions.

Methods

static PhpFile
fromCode(string $code)

Parses PHP source code and returns the resulting PhpFile representation.

add(PhpNamespace $item)

Adds a namespace, class-like type, or function to the file.

addClass(string $name)

Adds a class to the file. Accepts the fully qualified name including namespace.

addInterface(string $name)

Adds an interface to the file. Accepts the fully qualified name including namespace.

addTrait(string $name)

Adds a trait to the file. Accepts the fully qualified name including namespace.

addEnum(string $name)

Adds an enum to the file. Accepts the fully qualified name including namespace.

addFunction(string $name)

Adds a function to the file. Accepts the fully qualified name including namespace.

addNamespace(PhpNamespace $namespace)

Adds a namespace to the file, or returns the existing one if it already exists.

removeNamespace(PhpNamespace $namespace)

Removes a namespace from the file.

array
getNamespaces()

No description

array
getClasses()

No description

array
getFunctions()

No description

addUse(string $name, string|null $alias = null, string $of = PhpNamespace::NameNormal)

Adds a use statement to the root (unnamed) namespace.

setStrictTypes(bool $state = true)

Enables or disables the declare(strict_types=1) statement.

bool
hasStrictTypes()

No description

string
__toString()

No description

Details

at line 29
static PhpFile fromCode(string $code)

Parses PHP source code and returns the resulting PhpFile representation.

Parameters

string $code

Return Value

PhpFile

at line 39
PhpFile add(PhpNamespace $item)

Adds a namespace, class-like type, or function to the file.

Class-like types and functions are placed into their respective namespace (created if needed).

Parameters

PhpNamespace $item

Return Value

PhpFile

at line 63
ClassType addClass(string $name)

Adds a class to the file. Accepts the fully qualified name including namespace.

Parameters

string $name

Return Value

ClassType

Exceptions

InvalidStateException

at line 75
InterfaceType addInterface(string $name)

Adds an interface to the file. Accepts the fully qualified name including namespace.

Parameters

string $name

Return Value

InterfaceType

Exceptions

InvalidStateException

at line 87
TraitType addTrait(string $name)

Adds a trait to the file. Accepts the fully qualified name including namespace.

Parameters

string $name

Return Value

TraitType

Exceptions

InvalidStateException

at line 99
EnumType addEnum(string $name)

Adds an enum to the file. Accepts the fully qualified name including namespace.

Parameters

string $name

Return Value

EnumType

Exceptions

InvalidStateException

at line 111
GlobalFunction addFunction(string $name)

Adds a function to the file. Accepts the fully qualified name including namespace.

Parameters

string $name

Return Value

GlobalFunction

Exceptions

InvalidStateException

at line 122
PhpNamespace addNamespace(PhpNamespace $namespace)

Adds a namespace to the file, or returns the existing one if it already exists.

Parameters

PhpNamespace $namespace

Return Value

PhpNamespace

at line 136
PhpFile removeNamespace(PhpNamespace $namespace)

Removes a namespace from the file.

Parameters

PhpNamespace $namespace

Return Value

PhpFile

at line 145
array getNamespaces()

No description

Return Value

array

at line 152
array getClasses()

No description

Return Value

array

fully qualified name => class

at line 167
array getFunctions()

No description

Return Value

array

fully qualified name => function

at line 185
PhpFile addUse(string $name, string|null $alias = null, string $of = PhpNamespace::NameNormal)

Adds a use statement to the root (unnamed) namespace.

Parameters

string $name
string|null $alias
string $of

Return Value

PhpFile

at line 195
PhpFile setStrictTypes(bool $state = true)

Enables or disables the declare(strict_types=1) statement.

Parameters

bool $state

Return Value

PhpFile

at line 202
bool hasStrictTypes()

No description

Return Value

bool

at line 208
string __toString()

No description

Return Value

string

Traits