Guides

Expression
in package

FinalYes

Represents an expression with a define statement, constant, property, enum case and any other location.

Certain expressions contain useful references to other elements or types. Examples of these are:

  • Define statements that use an expression to refer to a class or function
  • Properties whose default value refers to a constant
  • Arguments whose default value initialize an object
  • Enum Cases that refer to a function or constant

This class represents every location where an expression is used and contains 2 pieces of information:

  • The expression string containing placeholders linking to useful information
  • An array of 'parts' whose keys equal the placeholders in the expression string and whose values is the extracted information, such as an FQSEN or Type.

In a way, the expression string is similar in nature to a URI Template (see links) where you have a string containing variables that can be replaced. These variables are delimited by {{ and }}, and are build up of the prefix PHPDOC and then an MD5 hash of the name of the extracted information.

It is not necessary for a consumer to interpret the information when they do not need it, a self::__toString() magic method is provided that will replace the placeholders with the toString() output of each part.

Tags
link

for the definition of expressions in PHP.

link

for more information on URI Templates.

see
ExpressionPrinter

how an expression coming from PHP-Parser is transformed into an expression.

Table of Contents

Properties

$expression  : string
$parts  : array<string, Fqsen|Type>
The collection of placeholders with the value that their holding.

Methods

__construct()  : mixed
__toString()  : string
Returns a rendered version of the expression string where all placeholders are replaced by the stringified versions of the included parts.
generatePlaceholder()  : string
Returns the recommended placeholder string format given a name.
getExpression()  : string
The raw expression string containing placeholders for any extracted Types or FQSENs.
getParts()  : array<string, Fqsen|Type>
A list of extracted parts for which placeholders exist in the expression string.
render()  : string
Renders the expression as a string and replaces all placeholders with either a provided value, or the stringified value from the parts in this expression.

Properties

$expression

private string $expression

The expression string containing placeholders for any extracted Types or FQSENs.

$parts

The collection of placeholders with the value that their holding.

private array<string, Fqsen|Type> $parts

In the expression string there can be several placeholders, this array contains a placeholder => value pair that can be used by consumers to map the data to another formatting, adding links for example, and then render the expression.

Methods

__construct()

public __construct(string $expression[, array<string, Fqsen|Type$parts = [] ]) : mixed
Parameters
$expression : string
$parts : array<string, Fqsen|Type> = []

__toString()

Returns a rendered version of the expression string where all placeholders are replaced by the stringified versions of the included parts.

public __toString() : string
Tags
see
self::$parts

for the list of parts used in rendering

see
self::render()

to influence rendering of the expression.

Return values
string

generatePlaceholder()

Returns the recommended placeholder string format given a name.

public static generatePlaceholder(string $name) : string

Consumers can use their own formats when needed, the placeholders are all keys in the self::$parts array and not interpreted by this class. However, to prevent collisions it is recommended to use this method to generate a placeholder.

Parameters
$name : string

a string identifying the element for which the placeholder is generated.

Return values
string

getExpression()

The raw expression string containing placeholders for any extracted Types or FQSENs.

public getExpression() : string
Tags
see
self::render()

to render a human-readable expression and to replace some parts with custom values.

see
self::__toString()

to render a human-readable expression with the previously extracted parts.

Return values
string

getParts()

A list of extracted parts for which placeholders exist in the expression string.

public getParts() : array<string, Fqsen|Type>

The returned array will have the placeholders of the expression string as keys, and the related FQSEN or Type as value. This can be used as a basis for doing your own transformations to self::render() the expression in a custom way; or to extract type information from an expression and use that elsewhere in your application.

Tags
see
ExpressionPrinter

to transform a PHP-Parser expression into an expression string and list of parts.

Return values
array<string, Fqsen|Type>

render()

Renders the expression as a string and replaces all placeholders with either a provided value, or the stringified value from the parts in this expression.

public render([array<string, string> $replacementParts = [] ]) : string

The keys of the replacement parts should match those of self::getParts(), any unrecognized key is not handled.

Parameters
$replacementParts : array<string, string> = []
Return values
string

        
On this page

Search results