|
| 1 | +Expressions |
| 2 | +=========== |
| 3 | + |
| 4 | +Starting with version 5.4, we now support parsing expressions and extracting types and references to elements from them. |
| 5 | + |
| 6 | +.. info:: |
| 7 | + |
| 8 | + An expression is, for example, the default value for a property or argument, the definition of an enum case or a |
| 9 | + constant value. These are called expressions and can contain more complex combinations of operators and values. |
| 10 | + |
| 11 | +As this library revolves around reflecting Static information, most parts of an expression are considered irrelevant; |
| 12 | +except for type information -such as type hints- and references to other elements, such as constants. As such, whenever |
| 13 | +an expression is interpreted, it will result in a string containing placeholders and an array containing the reflected |
| 14 | +parts -such as FQSENs-. |
| 15 | + |
| 16 | +This means that the getters like ``getDefault()`` will return a string or when you provide the optional argument |
| 17 | +$isString as being false, it will return an Expression object; which, when cast to string, will provide the same result. |
| 18 | + |
| 19 | +.. warning:: |
| 20 | + |
| 21 | + Deprecation: In version 6, we will remove the optional argument and always return an Expression object. When the |
| 22 | + result was used as a string nothing will change, but code that checks if the output is a string will no longer |
| 23 | + function starting from that version. |
| 24 | + |
| 25 | +This will allow consumers to be able to extract types and links to elements from expressions. This allows consumers to, |
| 26 | +for example, interpret the default value for a constructor promoted properties when it directly instantiates an object. |
| 27 | + |
| 28 | +Creating expressions |
| 29 | +-------------------- |
| 30 | + |
| 31 | +.. hint:: |
| 32 | + |
| 33 | + The description below is only for internal usage and to understand how expressions work, this library deals with |
| 34 | + this by default. |
| 35 | + |
| 36 | +In this library, we use the ExpressionPrinter to convert a PHP-Parser node -or expression- into an expression |
| 37 | +like this:: |
| 38 | + |
| 39 | + $printer = new ExpressionPrinter(); |
| 40 | + $expressionTemplate = $printer->prettyPrintExpr($phpParserNode); |
| 41 | + $expression = new Expression($expressionTemplate, $printer->getParts()); |
| 42 | + |
| 43 | +In the example above we assume that there is a PHP-Parser node representing an expression; this node is passed to the |
| 44 | +ExpressionPrinter -which is an adapted PrettyPrinter from PHP-Parser- which will render the expression as a readable |
| 45 | +template string containing placeholders, and a list of parts that can slot into the placeholders. |
| 46 | + |
| 47 | +Consuming expressions |
| 48 | +--------------------- |
| 49 | + |
| 50 | +When using this library, you can consume these expression objects either by |
| 51 | + |
| 52 | +1. Directly casting them to a string - this will replace all placeholders with the stringified version of the parts |
| 53 | +2. Use the render function - this will do the same as the previous methods but you can specify one or more overrides |
| 54 | + for the placeholders in the expression |
| 55 | + |
| 56 | +The second method can be used to create your own string values from the given parts and render, for example, links in |
| 57 | +these locations. |
| 58 | + |
| 59 | +Another way to use these expressions is to interpret the parts array, and through that way know which elements and |
| 60 | +types are referred to in that expression. |
0 commit comments