2017-08-18 22:57:27 +02:00
|
|
|
<?php declare(strict_types=1);
|
2014-02-06 14:44:16 +01:00
|
|
|
|
|
|
|
namespace PhpParser\PrettyPrinter;
|
|
|
|
|
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr;
|
|
|
|
use PhpParser\Node\Expr\AssignOp;
|
|
|
|
use PhpParser\Node\Expr\BinaryOp;
|
|
|
|
use PhpParser\Node\Expr\Cast;
|
|
|
|
use PhpParser\Node\Name;
|
2016-11-23 22:58:18 +01:00
|
|
|
use PhpParser\Node\Scalar;
|
|
|
|
use PhpParser\Node\Scalar\MagicConst;
|
|
|
|
use PhpParser\Node\Stmt;
|
2023-03-04 21:54:56 +01:00
|
|
|
use PhpParser\PrettyPrinterAbstract;
|
2014-02-06 14:44:16 +01:00
|
|
|
|
2023-03-04 21:54:56 +01:00
|
|
|
class Standard extends PrettyPrinterAbstract {
|
2014-02-06 14:44:16 +01:00
|
|
|
// Special nodes
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pParam(Node\Param $node): string {
|
2020-09-13 21:01:17 +02:00
|
|
|
return $this->pAttrGroups($node->attrGroups, true)
|
|
|
|
. $this->pModifiers($node->flags)
|
2020-06-07 18:52:12 +02:00
|
|
|
. ($node->type ? $this->p($node->type) . ' ' : '')
|
2014-02-06 14:44:16 +01:00
|
|
|
. ($node->byRef ? '&' : '')
|
2015-03-12 22:18:51 +01:00
|
|
|
. ($node->variadic ? '...' : '')
|
2017-01-19 23:38:40 +01:00
|
|
|
. $this->p($node->var)
|
2024-07-14 21:29:58 +02:00
|
|
|
. ($node->default ? ' = ' . $this->p($node->default) : '')
|
|
|
|
. ($node->hooks ? ' {' . $this->pStmts($node->hooks) . $this->nl . '}' : '');
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pArg(Node\Arg $node): string {
|
2020-08-09 17:37:31 +02:00
|
|
|
return ($node->name ? $node->name->toString() . ': ' : '')
|
|
|
|
. ($node->byRef ? '&' : '') . ($node->unpack ? '...' : '')
|
|
|
|
. $this->p($node->value);
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pVariadicPlaceholder(Node\VariadicPlaceholder $node): string {
|
2021-09-12 21:59:26 +02:00
|
|
|
return '...';
|
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pConst(Node\Const_ $node): string {
|
2014-02-06 14:44:16 +01:00
|
|
|
return $node->name . ' = ' . $this->p($node->value);
|
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pNullableType(Node\NullableType $node): string {
|
2017-04-28 19:09:39 +02:00
|
|
|
return '?' . $this->p($node->type);
|
2016-07-06 18:34:09 +02:00
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pUnionType(Node\UnionType $node): string {
|
2022-08-07 16:10:11 +01:00
|
|
|
$types = [];
|
|
|
|
foreach ($node->types as $typeNode) {
|
|
|
|
if ($typeNode instanceof Node\IntersectionType) {
|
|
|
|
$types[] = '('. $this->p($typeNode) . ')';
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$types[] = $this->p($typeNode);
|
|
|
|
}
|
|
|
|
return implode('|', $types);
|
2019-10-25 21:35:43 +02:00
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pIntersectionType(Node\IntersectionType $node): string {
|
2021-09-02 18:25:16 +02:00
|
|
|
return $this->pImplode($node->types, '&');
|
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pIdentifier(Node\Identifier $node): string {
|
2016-12-23 14:11:31 +01:00
|
|
|
return $node->name;
|
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pVarLikeIdentifier(Node\VarLikeIdentifier $node): string {
|
2016-12-23 14:11:31 +01:00
|
|
|
return '$' . $node->name;
|
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pAttribute(Node\Attribute $node): string {
|
2020-09-13 21:01:17 +02:00
|
|
|
return $this->p($node->name)
|
|
|
|
. ($node->args ? '(' . $this->pCommaSeparated($node->args) . ')' : '');
|
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pAttributeGroup(Node\AttributeGroup $node): string {
|
2020-09-13 21:01:17 +02:00
|
|
|
return '#[' . $this->pCommaSeparated($node->attrs) . ']';
|
|
|
|
}
|
|
|
|
|
2014-02-06 14:44:16 +01:00
|
|
|
// Names
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pName(Name $node): string {
|
2023-05-21 21:14:50 +02:00
|
|
|
return $node->name;
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pName_FullyQualified(Name\FullyQualified $node): string {
|
2023-05-21 21:14:50 +02:00
|
|
|
return '\\' . $node->name;
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pName_Relative(Name\Relative $node): string {
|
2023-05-21 21:14:50 +02:00
|
|
|
return 'namespace\\' . $node->name;
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Magic Constants
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pScalar_MagicConst_Class(MagicConst\Class_ $node): string {
|
2014-02-06 14:44:16 +01:00
|
|
|
return '__CLASS__';
|
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pScalar_MagicConst_Dir(MagicConst\Dir $node): string {
|
2014-02-06 14:44:16 +01:00
|
|
|
return '__DIR__';
|
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pScalar_MagicConst_File(MagicConst\File $node): string {
|
2014-02-06 14:44:16 +01:00
|
|
|
return '__FILE__';
|
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pScalar_MagicConst_Function(MagicConst\Function_ $node): string {
|
2014-02-06 14:44:16 +01:00
|
|
|
return '__FUNCTION__';
|
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pScalar_MagicConst_Line(MagicConst\Line $node): string {
|
2014-02-06 14:44:16 +01:00
|
|
|
return '__LINE__';
|
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pScalar_MagicConst_Method(MagicConst\Method $node): string {
|
2014-02-06 14:44:16 +01:00
|
|
|
return '__METHOD__';
|
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pScalar_MagicConst_Namespace(MagicConst\Namespace_ $node): string {
|
2014-02-06 14:44:16 +01:00
|
|
|
return '__NAMESPACE__';
|
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pScalar_MagicConst_Trait(MagicConst\Trait_ $node): string {
|
2014-02-06 14:44:16 +01:00
|
|
|
return '__TRAIT__';
|
|
|
|
}
|
|
|
|
|
2024-07-14 18:49:54 +02:00
|
|
|
protected function pScalar_MagicConst_Property(MagicConst\Property $node): string {
|
|
|
|
return '__PROPERTY__';
|
|
|
|
}
|
|
|
|
|
2014-02-06 14:44:16 +01:00
|
|
|
// Scalars
|
|
|
|
|
2023-08-27 22:02:31 +02:00
|
|
|
private function indentString(string $str): string {
|
|
|
|
return str_replace("\n", $this->nl, $str);
|
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pScalar_String(Scalar\String_ $node): string {
|
2016-04-02 22:22:24 +09:00
|
|
|
$kind = $node->getAttribute('kind', Scalar\String_::KIND_SINGLE_QUOTED);
|
|
|
|
switch ($kind) {
|
|
|
|
case Scalar\String_::KIND_NOWDOC:
|
|
|
|
$label = $node->getAttribute('docLabel');
|
|
|
|
if ($label && !$this->containsEndLabel($node->value, $label)) {
|
2023-08-27 22:02:31 +02:00
|
|
|
$shouldIdent = $this->phpVersion->supportsFlexibleHeredoc();
|
|
|
|
$nl = $shouldIdent ? $this->nl : $this->newline;
|
2016-04-02 22:22:24 +09:00
|
|
|
if ($node->value === '') {
|
2023-08-27 22:02:31 +02:00
|
|
|
return "<<<'$label'$nl$label{$this->docStringEndToken}";
|
2016-04-02 22:22:24 +09:00
|
|
|
}
|
|
|
|
|
2023-02-27 22:00:49 +01:00
|
|
|
// Make sure trailing \r is not combined with following \n into CRLF.
|
|
|
|
if ($node->value[strlen($node->value) - 1] !== "\r") {
|
2023-08-27 22:02:31 +02:00
|
|
|
$value = $shouldIdent ? $this->indentString($node->value) : $node->value;
|
|
|
|
return "<<<'$label'$nl$value$nl$label{$this->docStringEndToken}";
|
2023-02-27 22:00:49 +01:00
|
|
|
}
|
2016-04-02 22:22:24 +09:00
|
|
|
}
|
|
|
|
/* break missing intentionally */
|
2022-08-28 22:57:06 +02:00
|
|
|
// no break
|
2016-04-02 22:22:24 +09:00
|
|
|
case Scalar\String_::KIND_SINGLE_QUOTED:
|
2017-12-01 18:31:21 +01:00
|
|
|
return $this->pSingleQuotedString($node->value);
|
2016-04-02 22:22:24 +09:00
|
|
|
case Scalar\String_::KIND_HEREDOC:
|
|
|
|
$label = $node->getAttribute('docLabel');
|
2023-02-26 12:26:27 +01:00
|
|
|
$escaped = $this->escapeString($node->value, null);
|
|
|
|
if ($label && !$this->containsEndLabel($escaped, $label)) {
|
2023-08-27 22:02:31 +02:00
|
|
|
$nl = $this->phpVersion->supportsFlexibleHeredoc() ? $this->nl : $this->newline;
|
2023-02-26 12:26:27 +01:00
|
|
|
if ($escaped === '') {
|
2023-08-27 22:02:31 +02:00
|
|
|
return "<<<$label$nl$label{$this->docStringEndToken}";
|
2016-04-02 22:22:24 +09:00
|
|
|
}
|
|
|
|
|
2023-08-27 22:02:31 +02:00
|
|
|
return "<<<$label$nl$escaped$nl$label{$this->docStringEndToken}";
|
2016-04-02 22:22:24 +09:00
|
|
|
}
|
2022-08-28 22:57:06 +02:00
|
|
|
/* break missing intentionally */
|
|
|
|
// no break
|
2016-04-02 22:22:24 +09:00
|
|
|
case Scalar\String_::KIND_DOUBLE_QUOTED:
|
|
|
|
return '"' . $this->escapeString($node->value, '"') . '"';
|
|
|
|
}
|
|
|
|
throw new \Exception('Invalid string kind');
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pScalar_InterpolatedString(Scalar\InterpolatedString $node): string {
|
2016-04-02 22:22:24 +09:00
|
|
|
if ($node->getAttribute('kind') === Scalar\String_::KIND_HEREDOC) {
|
|
|
|
$label = $node->getAttribute('docLabel');
|
|
|
|
if ($label && !$this->encapsedContainsEndLabel($node->parts, $label)) {
|
2023-08-27 22:02:31 +02:00
|
|
|
$nl = $this->phpVersion->supportsFlexibleHeredoc() ? $this->nl : $this->newline;
|
2016-04-02 22:22:24 +09:00
|
|
|
if (count($node->parts) === 1
|
2022-09-03 13:25:23 +02:00
|
|
|
&& $node->parts[0] instanceof Node\InterpolatedStringPart
|
2016-04-02 22:22:24 +09:00
|
|
|
&& $node->parts[0]->value === ''
|
|
|
|
) {
|
2023-08-27 22:02:31 +02:00
|
|
|
return "<<<$label$nl$label{$this->docStringEndToken}";
|
2016-04-02 22:22:24 +09:00
|
|
|
}
|
|
|
|
|
2023-08-27 22:02:31 +02:00
|
|
|
return "<<<$label$nl" . $this->pEncapsList($node->parts, null)
|
|
|
|
. "$nl$label{$this->docStringEndToken}";
|
2016-04-02 22:22:24 +09:00
|
|
|
}
|
|
|
|
}
|
2014-02-06 14:44:16 +01:00
|
|
|
return '"' . $this->pEncapsList($node->parts, '"') . '"';
|
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pScalar_Int(Scalar\Int_ $node): string {
|
2023-09-17 11:08:23 +02:00
|
|
|
if ($node->value === -\PHP_INT_MAX - 1) {
|
2016-12-11 16:31:59 +01:00
|
|
|
// PHP_INT_MIN cannot be represented as a literal,
|
|
|
|
// because the sign is not part of the literal
|
|
|
|
return '(-' . \PHP_INT_MAX . '-1)';
|
|
|
|
}
|
|
|
|
|
2022-09-03 11:58:59 +02:00
|
|
|
$kind = $node->getAttribute('kind', Scalar\Int_::KIND_DEC);
|
|
|
|
if (Scalar\Int_::KIND_DEC === $kind) {
|
2016-12-11 16:31:59 +01:00
|
|
|
return (string) $node->value;
|
|
|
|
}
|
|
|
|
|
2019-07-14 04:56:13 -04:00
|
|
|
if ($node->value < 0) {
|
|
|
|
$sign = '-';
|
|
|
|
$str = (string) -$node->value;
|
|
|
|
} else {
|
|
|
|
$sign = '';
|
|
|
|
$str = (string) $node->value;
|
|
|
|
}
|
2016-12-11 16:31:59 +01:00
|
|
|
switch ($kind) {
|
2022-09-03 11:58:59 +02:00
|
|
|
case Scalar\Int_::KIND_BIN:
|
2016-12-11 16:31:59 +01:00
|
|
|
return $sign . '0b' . base_convert($str, 10, 2);
|
2022-09-03 11:58:59 +02:00
|
|
|
case Scalar\Int_::KIND_OCT:
|
2016-12-11 16:31:59 +01:00
|
|
|
return $sign . '0' . base_convert($str, 10, 8);
|
2022-09-03 11:58:59 +02:00
|
|
|
case Scalar\Int_::KIND_HEX:
|
2016-12-11 16:31:59 +01:00
|
|
|
return $sign . '0x' . base_convert($str, 10, 16);
|
2016-03-09 21:10:55 +01:00
|
|
|
}
|
2016-04-02 22:22:24 +09:00
|
|
|
throw new \Exception('Invalid number kind');
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pScalar_Float(Scalar\Float_ $node): string {
|
2016-12-11 16:31:59 +01:00
|
|
|
if (!is_finite($node->value)) {
|
|
|
|
if ($node->value === \INF) {
|
2023-02-26 09:46:15 +01:00
|
|
|
return '1.0E+1000';
|
2022-07-04 17:22:32 +02:00
|
|
|
}
|
|
|
|
if ($node->value === -\INF) {
|
2023-02-26 09:46:15 +01:00
|
|
|
return '-1.0E+1000';
|
2016-12-11 16:31:59 +01:00
|
|
|
} else {
|
|
|
|
return '\NAN';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to find a short full-precision representation
|
2015-05-01 19:15:33 +02:00
|
|
|
$stringValue = sprintf('%.16G', $node->value);
|
2022-07-04 17:10:29 +02:00
|
|
|
if ($node->value !== (float) $stringValue) {
|
2015-05-02 11:48:55 +02:00
|
|
|
$stringValue = sprintf('%.17G', $node->value);
|
|
|
|
}
|
2014-02-06 14:44:16 +01:00
|
|
|
|
2017-07-19 17:10:55 +02:00
|
|
|
// %G is locale dependent and there exists no locale-independent alternative. We don't want
|
|
|
|
// mess with switching locales here, so let's assume that a comma is the only non-standard
|
|
|
|
// decimal separator we may encounter...
|
|
|
|
$stringValue = str_replace(',', '.', $stringValue);
|
|
|
|
|
2014-02-06 14:44:16 +01:00
|
|
|
// ensure that number is really printed as float
|
2014-03-16 21:50:06 +01:00
|
|
|
return preg_match('/^-?[0-9]+$/', $stringValue) ? $stringValue . '.0' : $stringValue;
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Assignments
|
|
|
|
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
protected function pExpr_Assign(Expr\Assign $node, int $precedence, int $lhsPrecedence): string {
|
|
|
|
return $this->pPrefixOp(Expr\Assign::class, $this->p($node->var) . ' = ', $node->expr, $precedence, $lhsPrecedence);
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
protected function pExpr_AssignRef(Expr\AssignRef $node, int $precedence, int $lhsPrecedence): string {
|
|
|
|
return $this->pPrefixOp(Expr\AssignRef::class, $this->p($node->var) . ' =& ', $node->expr, $precedence, $lhsPrecedence);
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
protected function pExpr_AssignOp_Plus(AssignOp\Plus $node, int $precedence, int $lhsPrecedence): string {
|
|
|
|
return $this->pPrefixOp(AssignOp\Plus::class, $this->p($node->var) . ' += ', $node->expr, $precedence, $lhsPrecedence);
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
protected function pExpr_AssignOp_Minus(AssignOp\Minus $node, int $precedence, int $lhsPrecedence): string {
|
|
|
|
return $this->pPrefixOp(AssignOp\Minus::class, $this->p($node->var) . ' -= ', $node->expr, $precedence, $lhsPrecedence);
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
protected function pExpr_AssignOp_Mul(AssignOp\Mul $node, int $precedence, int $lhsPrecedence): string {
|
|
|
|
return $this->pPrefixOp(AssignOp\Mul::class, $this->p($node->var) . ' *= ', $node->expr, $precedence, $lhsPrecedence);
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
protected function pExpr_AssignOp_Div(AssignOp\Div $node, int $precedence, int $lhsPrecedence): string {
|
|
|
|
return $this->pPrefixOp(AssignOp\Div::class, $this->p($node->var) . ' /= ', $node->expr, $precedence, $lhsPrecedence);
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
protected function pExpr_AssignOp_Concat(AssignOp\Concat $node, int $precedence, int $lhsPrecedence): string {
|
|
|
|
return $this->pPrefixOp(AssignOp\Concat::class, $this->p($node->var) . ' .= ', $node->expr, $precedence, $lhsPrecedence);
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
protected function pExpr_AssignOp_Mod(AssignOp\Mod $node, int $precedence, int $lhsPrecedence): string {
|
|
|
|
return $this->pPrefixOp(AssignOp\Mod::class, $this->p($node->var) . ' %= ', $node->expr, $precedence, $lhsPrecedence);
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
protected function pExpr_AssignOp_BitwiseAnd(AssignOp\BitwiseAnd $node, int $precedence, int $lhsPrecedence): string {
|
|
|
|
return $this->pPrefixOp(AssignOp\BitwiseAnd::class, $this->p($node->var) . ' &= ', $node->expr, $precedence, $lhsPrecedence);
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
protected function pExpr_AssignOp_BitwiseOr(AssignOp\BitwiseOr $node, int $precedence, int $lhsPrecedence): string {
|
|
|
|
return $this->pPrefixOp(AssignOp\BitwiseOr::class, $this->p($node->var) . ' |= ', $node->expr, $precedence, $lhsPrecedence);
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
protected function pExpr_AssignOp_BitwiseXor(AssignOp\BitwiseXor $node, int $precedence, int $lhsPrecedence): string {
|
|
|
|
return $this->pPrefixOp(AssignOp\BitwiseXor::class, $this->p($node->var) . ' ^= ', $node->expr, $precedence, $lhsPrecedence);
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
protected function pExpr_AssignOp_ShiftLeft(AssignOp\ShiftLeft $node, int $precedence, int $lhsPrecedence): string {
|
|
|
|
return $this->pPrefixOp(AssignOp\ShiftLeft::class, $this->p($node->var) . ' <<= ', $node->expr, $precedence, $lhsPrecedence);
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
protected function pExpr_AssignOp_ShiftRight(AssignOp\ShiftRight $node, int $precedence, int $lhsPrecedence): string {
|
|
|
|
return $this->pPrefixOp(AssignOp\ShiftRight::class, $this->p($node->var) . ' >>= ', $node->expr, $precedence, $lhsPrecedence);
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
protected function pExpr_AssignOp_Pow(AssignOp\Pow $node, int $precedence, int $lhsPrecedence): string {
|
|
|
|
return $this->pPrefixOp(AssignOp\Pow::class, $this->p($node->var) . ' **= ', $node->expr, $precedence, $lhsPrecedence);
|
2014-03-26 19:18:16 +01:00
|
|
|
}
|
|
|
|
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
protected function pExpr_AssignOp_Coalesce(AssignOp\Coalesce $node, int $precedence, int $lhsPrecedence): string {
|
|
|
|
return $this->pPrefixOp(AssignOp\Coalesce::class, $this->p($node->var) . ' ??= ', $node->expr, $precedence, $lhsPrecedence);
|
2019-01-22 22:05:17 +01:00
|
|
|
}
|
|
|
|
|
2014-02-06 14:44:16 +01:00
|
|
|
// Binary expressions
|
|
|
|
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
protected function pExpr_BinaryOp_Plus(BinaryOp\Plus $node, int $precedence, int $lhsPrecedence): string {
|
|
|
|
return $this->pInfixOp(BinaryOp\Plus::class, $node->left, ' + ', $node->right, $precedence, $lhsPrecedence);
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
protected function pExpr_BinaryOp_Minus(BinaryOp\Minus $node, int $precedence, int $lhsPrecedence): string {
|
|
|
|
return $this->pInfixOp(BinaryOp\Minus::class, $node->left, ' - ', $node->right, $precedence, $lhsPrecedence);
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
protected function pExpr_BinaryOp_Mul(BinaryOp\Mul $node, int $precedence, int $lhsPrecedence): string {
|
|
|
|
return $this->pInfixOp(BinaryOp\Mul::class, $node->left, ' * ', $node->right, $precedence, $lhsPrecedence);
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
protected function pExpr_BinaryOp_Div(BinaryOp\Div $node, int $precedence, int $lhsPrecedence): string {
|
|
|
|
return $this->pInfixOp(BinaryOp\Div::class, $node->left, ' / ', $node->right, $precedence, $lhsPrecedence);
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
protected function pExpr_BinaryOp_Concat(BinaryOp\Concat $node, int $precedence, int $lhsPrecedence): string {
|
|
|
|
return $this->pInfixOp(BinaryOp\Concat::class, $node->left, ' . ', $node->right, $precedence, $lhsPrecedence);
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
protected function pExpr_BinaryOp_Mod(BinaryOp\Mod $node, int $precedence, int $lhsPrecedence): string {
|
|
|
|
return $this->pInfixOp(BinaryOp\Mod::class, $node->left, ' % ', $node->right, $precedence, $lhsPrecedence);
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
protected function pExpr_BinaryOp_BooleanAnd(BinaryOp\BooleanAnd $node, int $precedence, int $lhsPrecedence): string {
|
|
|
|
return $this->pInfixOp(BinaryOp\BooleanAnd::class, $node->left, ' && ', $node->right, $precedence, $lhsPrecedence);
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
protected function pExpr_BinaryOp_BooleanOr(BinaryOp\BooleanOr $node, int $precedence, int $lhsPrecedence): string {
|
|
|
|
return $this->pInfixOp(BinaryOp\BooleanOr::class, $node->left, ' || ', $node->right, $precedence, $lhsPrecedence);
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
protected function pExpr_BinaryOp_BitwiseAnd(BinaryOp\BitwiseAnd $node, int $precedence, int $lhsPrecedence): string {
|
|
|
|
return $this->pInfixOp(BinaryOp\BitwiseAnd::class, $node->left, ' & ', $node->right, $precedence, $lhsPrecedence);
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
protected function pExpr_BinaryOp_BitwiseOr(BinaryOp\BitwiseOr $node, int $precedence, int $lhsPrecedence): string {
|
|
|
|
return $this->pInfixOp(BinaryOp\BitwiseOr::class, $node->left, ' | ', $node->right, $precedence, $lhsPrecedence);
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
protected function pExpr_BinaryOp_BitwiseXor(BinaryOp\BitwiseXor $node, int $precedence, int $lhsPrecedence): string {
|
|
|
|
return $this->pInfixOp(BinaryOp\BitwiseXor::class, $node->left, ' ^ ', $node->right, $precedence, $lhsPrecedence);
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
protected function pExpr_BinaryOp_ShiftLeft(BinaryOp\ShiftLeft $node, int $precedence, int $lhsPrecedence): string {
|
|
|
|
return $this->pInfixOp(BinaryOp\ShiftLeft::class, $node->left, ' << ', $node->right, $precedence, $lhsPrecedence);
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
protected function pExpr_BinaryOp_ShiftRight(BinaryOp\ShiftRight $node, int $precedence, int $lhsPrecedence): string {
|
|
|
|
return $this->pInfixOp(BinaryOp\ShiftRight::class, $node->left, ' >> ', $node->right, $precedence, $lhsPrecedence);
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
protected function pExpr_BinaryOp_Pow(BinaryOp\Pow $node, int $precedence, int $lhsPrecedence): string {
|
|
|
|
return $this->pInfixOp(BinaryOp\Pow::class, $node->left, ' ** ', $node->right, $precedence, $lhsPrecedence);
|
2014-03-26 19:18:16 +01:00
|
|
|
}
|
|
|
|
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
protected function pExpr_BinaryOp_LogicalAnd(BinaryOp\LogicalAnd $node, int $precedence, int $lhsPrecedence): string {
|
|
|
|
return $this->pInfixOp(BinaryOp\LogicalAnd::class, $node->left, ' and ', $node->right, $precedence, $lhsPrecedence);
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
protected function pExpr_BinaryOp_LogicalOr(BinaryOp\LogicalOr $node, int $precedence, int $lhsPrecedence): string {
|
|
|
|
return $this->pInfixOp(BinaryOp\LogicalOr::class, $node->left, ' or ', $node->right, $precedence, $lhsPrecedence);
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
protected function pExpr_BinaryOp_LogicalXor(BinaryOp\LogicalXor $node, int $precedence, int $lhsPrecedence): string {
|
|
|
|
return $this->pInfixOp(BinaryOp\LogicalXor::class, $node->left, ' xor ', $node->right, $precedence, $lhsPrecedence);
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
protected function pExpr_BinaryOp_Equal(BinaryOp\Equal $node, int $precedence, int $lhsPrecedence): string {
|
|
|
|
return $this->pInfixOp(BinaryOp\Equal::class, $node->left, ' == ', $node->right, $precedence, $lhsPrecedence);
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
protected function pExpr_BinaryOp_NotEqual(BinaryOp\NotEqual $node, int $precedence, int $lhsPrecedence): string {
|
|
|
|
return $this->pInfixOp(BinaryOp\NotEqual::class, $node->left, ' != ', $node->right, $precedence, $lhsPrecedence);
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
protected function pExpr_BinaryOp_Identical(BinaryOp\Identical $node, int $precedence, int $lhsPrecedence): string {
|
|
|
|
return $this->pInfixOp(BinaryOp\Identical::class, $node->left, ' === ', $node->right, $precedence, $lhsPrecedence);
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
protected function pExpr_BinaryOp_NotIdentical(BinaryOp\NotIdentical $node, int $precedence, int $lhsPrecedence): string {
|
|
|
|
return $this->pInfixOp(BinaryOp\NotIdentical::class, $node->left, ' !== ', $node->right, $precedence, $lhsPrecedence);
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
protected function pExpr_BinaryOp_Spaceship(BinaryOp\Spaceship $node, int $precedence, int $lhsPrecedence): string {
|
|
|
|
return $this->pInfixOp(BinaryOp\Spaceship::class, $node->left, ' <=> ', $node->right, $precedence, $lhsPrecedence);
|
2015-03-12 23:13:31 +01:00
|
|
|
}
|
|
|
|
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
protected function pExpr_BinaryOp_Greater(BinaryOp\Greater $node, int $precedence, int $lhsPrecedence): string {
|
|
|
|
return $this->pInfixOp(BinaryOp\Greater::class, $node->left, ' > ', $node->right, $precedence, $lhsPrecedence);
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
protected function pExpr_BinaryOp_GreaterOrEqual(BinaryOp\GreaterOrEqual $node, int $precedence, int $lhsPrecedence): string {
|
|
|
|
return $this->pInfixOp(BinaryOp\GreaterOrEqual::class, $node->left, ' >= ', $node->right, $precedence, $lhsPrecedence);
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
protected function pExpr_BinaryOp_Smaller(BinaryOp\Smaller $node, int $precedence, int $lhsPrecedence): string {
|
|
|
|
return $this->pInfixOp(BinaryOp\Smaller::class, $node->left, ' < ', $node->right, $precedence, $lhsPrecedence);
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
protected function pExpr_BinaryOp_SmallerOrEqual(BinaryOp\SmallerOrEqual $node, int $precedence, int $lhsPrecedence): string {
|
|
|
|
return $this->pInfixOp(BinaryOp\SmallerOrEqual::class, $node->left, ' <= ', $node->right, $precedence, $lhsPrecedence);
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
protected function pExpr_BinaryOp_Coalesce(BinaryOp\Coalesce $node, int $precedence, int $lhsPrecedence): string {
|
|
|
|
return $this->pInfixOp(BinaryOp\Coalesce::class, $node->left, ' ?? ', $node->right, $precedence, $lhsPrecedence);
|
2015-03-12 22:45:38 +01:00
|
|
|
}
|
|
|
|
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
protected function pExpr_Instanceof(Expr\Instanceof_ $node, int $precedence, int $lhsPrecedence): string {
|
|
|
|
return $this->pPostfixOp(
|
|
|
|
Expr\Instanceof_::class, $node->expr,
|
|
|
|
' instanceof ' . $this->pNewOperand($node->class),
|
|
|
|
$precedence, $lhsPrecedence);
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Unary expressions
|
|
|
|
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
protected function pExpr_BooleanNot(Expr\BooleanNot $node, int $precedence, int $lhsPrecedence): string {
|
|
|
|
return $this->pPrefixOp(Expr\BooleanNot::class, '!', $node->expr, $precedence, $lhsPrecedence);
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
protected function pExpr_BitwiseNot(Expr\BitwiseNot $node, int $precedence, int $lhsPrecedence): string {
|
|
|
|
return $this->pPrefixOp(Expr\BitwiseNot::class, '~', $node->expr, $precedence, $lhsPrecedence);
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
protected function pExpr_UnaryMinus(Expr\UnaryMinus $node, int $precedence, int $lhsPrecedence): string {
|
|
|
|
return $this->pPrefixOp(Expr\UnaryMinus::class, '-', $node->expr, $precedence, $lhsPrecedence);
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
protected function pExpr_UnaryPlus(Expr\UnaryPlus $node, int $precedence, int $lhsPrecedence): string {
|
|
|
|
return $this->pPrefixOp(Expr\UnaryPlus::class, '+', $node->expr, $precedence, $lhsPrecedence);
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pExpr_PreInc(Expr\PreInc $node): string {
|
2022-07-23 21:48:10 +02:00
|
|
|
return '++' . $this->p($node->var);
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pExpr_PreDec(Expr\PreDec $node): string {
|
2022-07-23 21:48:10 +02:00
|
|
|
return '--' . $this->p($node->var);
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pExpr_PostInc(Expr\PostInc $node): string {
|
2022-07-23 21:48:10 +02:00
|
|
|
return $this->p($node->var) . '++';
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pExpr_PostDec(Expr\PostDec $node): string {
|
2022-07-23 21:48:10 +02:00
|
|
|
return $this->p($node->var) . '--';
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
protected function pExpr_ErrorSuppress(Expr\ErrorSuppress $node, int $precedence, int $lhsPrecedence): string {
|
|
|
|
return $this->pPrefixOp(Expr\ErrorSuppress::class, '@', $node->expr, $precedence, $lhsPrecedence);
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
protected function pExpr_YieldFrom(Expr\YieldFrom $node, int $precedence, int $lhsPrecedence): string {
|
|
|
|
return $this->pPrefixOp(Expr\YieldFrom::class, 'yield from ', $node->expr, $precedence, $lhsPrecedence);
|
2015-04-26 11:46:23 +02:00
|
|
|
}
|
|
|
|
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
protected function pExpr_Print(Expr\Print_ $node, int $precedence, int $lhsPrecedence): string {
|
|
|
|
return $this->pPrefixOp(Expr\Print_::class, 'print ', $node->expr, $precedence, $lhsPrecedence);
|
2015-04-26 11:52:40 +02:00
|
|
|
}
|
|
|
|
|
2014-02-06 14:44:16 +01:00
|
|
|
// Casts
|
|
|
|
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
protected function pExpr_Cast_Int(Cast\Int_ $node, int $precedence, int $lhsPrecedence): string {
|
|
|
|
return $this->pPrefixOp(Cast\Int_::class, '(int) ', $node->expr, $precedence, $lhsPrecedence);
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
protected function pExpr_Cast_Double(Cast\Double $node, int $precedence, int $lhsPrecedence): string {
|
2019-01-03 08:59:45 +01:00
|
|
|
$kind = $node->getAttribute('kind', Cast\Double::KIND_DOUBLE);
|
|
|
|
if ($kind === Cast\Double::KIND_DOUBLE) {
|
|
|
|
$cast = '(double)';
|
|
|
|
} elseif ($kind === Cast\Double::KIND_FLOAT) {
|
|
|
|
$cast = '(float)';
|
2022-09-11 12:10:41 +02:00
|
|
|
} else {
|
|
|
|
assert($kind === Cast\Double::KIND_REAL);
|
2019-01-03 08:59:45 +01:00
|
|
|
$cast = '(real)';
|
|
|
|
}
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
return $this->pPrefixOp(Cast\Double::class, $cast . ' ', $node->expr, $precedence, $lhsPrecedence);
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
protected function pExpr_Cast_String(Cast\String_ $node, int $precedence, int $lhsPrecedence): string {
|
|
|
|
return $this->pPrefixOp(Cast\String_::class, '(string) ', $node->expr, $precedence, $lhsPrecedence);
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
protected function pExpr_Cast_Array(Cast\Array_ $node, int $precedence, int $lhsPrecedence): string {
|
|
|
|
return $this->pPrefixOp(Cast\Array_::class, '(array) ', $node->expr, $precedence, $lhsPrecedence);
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
protected function pExpr_Cast_Object(Cast\Object_ $node, int $precedence, int $lhsPrecedence): string {
|
|
|
|
return $this->pPrefixOp(Cast\Object_::class, '(object) ', $node->expr, $precedence, $lhsPrecedence);
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
protected function pExpr_Cast_Bool(Cast\Bool_ $node, int $precedence, int $lhsPrecedence): string {
|
|
|
|
return $this->pPrefixOp(Cast\Bool_::class, '(bool) ', $node->expr, $precedence, $lhsPrecedence);
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
protected function pExpr_Cast_Unset(Cast\Unset_ $node, int $precedence, int $lhsPrecedence): string {
|
|
|
|
return $this->pPrefixOp(Cast\Unset_::class, '(unset) ', $node->expr, $precedence, $lhsPrecedence);
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Function calls and similar constructs
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pExpr_FuncCall(Expr\FuncCall $node): string {
|
2015-06-13 19:46:01 +02:00
|
|
|
return $this->pCallLhs($node->name)
|
2017-04-09 19:49:47 +02:00
|
|
|
. '(' . $this->pMaybeMultiline($node->args) . ')';
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pExpr_MethodCall(Expr\MethodCall $node): string {
|
2015-06-13 19:46:01 +02:00
|
|
|
return $this->pDereferenceLhs($node->var) . '->' . $this->pObjectProperty($node->name)
|
2017-04-09 19:49:47 +02:00
|
|
|
. '(' . $this->pMaybeMultiline($node->args) . ')';
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pExpr_NullsafeMethodCall(Expr\NullsafeMethodCall $node): string {
|
2020-08-02 10:30:44 +02:00
|
|
|
return $this->pDereferenceLhs($node->var) . '?->' . $this->pObjectProperty($node->name)
|
|
|
|
. '(' . $this->pMaybeMultiline($node->args) . ')';
|
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pExpr_StaticCall(Expr\StaticCall $node): string {
|
2023-02-26 15:57:37 +01:00
|
|
|
return $this->pStaticDereferenceLhs($node->class) . '::'
|
2014-02-06 14:44:16 +01:00
|
|
|
. ($node->name instanceof Expr
|
|
|
|
? ($node->name instanceof Expr\Variable
|
|
|
|
? $this->p($node->name)
|
|
|
|
: '{' . $this->p($node->name) . '}')
|
|
|
|
: $node->name)
|
2017-04-09 19:49:47 +02:00
|
|
|
. '(' . $this->pMaybeMultiline($node->args) . ')';
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pExpr_Empty(Expr\Empty_ $node): string {
|
2014-02-06 14:44:16 +01:00
|
|
|
return 'empty(' . $this->p($node->expr) . ')';
|
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pExpr_Isset(Expr\Isset_ $node): string {
|
2014-02-06 14:44:16 +01:00
|
|
|
return 'isset(' . $this->pCommaSeparated($node->vars) . ')';
|
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pExpr_Eval(Expr\Eval_ $node): string {
|
2014-02-06 14:44:16 +01:00
|
|
|
return 'eval(' . $this->p($node->expr) . ')';
|
|
|
|
}
|
|
|
|
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
protected function pExpr_Include(Expr\Include_ $node, int $precedence, int $lhsPrecedence): string {
|
2017-08-13 14:06:08 +02:00
|
|
|
static $map = [
|
2014-02-06 14:44:16 +01:00
|
|
|
Expr\Include_::TYPE_INCLUDE => 'include',
|
|
|
|
Expr\Include_::TYPE_INCLUDE_ONCE => 'include_once',
|
|
|
|
Expr\Include_::TYPE_REQUIRE => 'require',
|
|
|
|
Expr\Include_::TYPE_REQUIRE_ONCE => 'require_once',
|
2017-08-13 14:06:08 +02:00
|
|
|
];
|
2014-02-06 14:44:16 +01:00
|
|
|
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
return $this->pPrefixOp(Expr\Include_::class, $map[$node->type] . ' ', $node->expr, $precedence, $lhsPrecedence);
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pExpr_List(Expr\List_ $node): string {
|
2022-08-28 18:48:26 +02:00
|
|
|
$syntax = $node->getAttribute('kind',
|
|
|
|
$this->phpVersion->supportsShortArrayDestructuring() ? Expr\List_::KIND_ARRAY : Expr\List_::KIND_LIST);
|
|
|
|
if ($syntax === Expr\List_::KIND_ARRAY) {
|
|
|
|
return '[' . $this->pMaybeMultiline($node->items, true) . ']';
|
|
|
|
} else {
|
|
|
|
return 'list(' . $this->pMaybeMultiline($node->items, true) . ')';
|
|
|
|
}
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Other
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pExpr_Error(Expr\Error $node): string {
|
2017-01-29 21:56:21 +01:00
|
|
|
throw new \LogicException('Cannot pretty-print AST with Error nodes');
|
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pExpr_Variable(Expr\Variable $node): string {
|
2014-02-06 14:44:16 +01:00
|
|
|
if ($node->name instanceof Expr) {
|
|
|
|
return '${' . $this->p($node->name) . '}';
|
|
|
|
} else {
|
|
|
|
return '$' . $node->name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pExpr_Array(Expr\Array_ $node): string {
|
2016-03-09 21:30:39 +01:00
|
|
|
$syntax = $node->getAttribute('kind',
|
2022-07-23 18:23:39 +02:00
|
|
|
$this->shortArraySyntax ? Expr\Array_::KIND_SHORT : Expr\Array_::KIND_LONG);
|
2016-03-09 21:30:39 +01:00
|
|
|
if ($syntax === Expr\Array_::KIND_SHORT) {
|
2017-04-09 19:49:47 +02:00
|
|
|
return '[' . $this->pMaybeMultiline($node->items, true) . ']';
|
2015-09-21 15:12:59 +08:00
|
|
|
} else {
|
2017-04-09 19:49:47 +02:00
|
|
|
return 'array(' . $this->pMaybeMultiline($node->items, true) . ')';
|
2015-09-21 15:12:59 +08:00
|
|
|
}
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
2023-02-26 18:44:40 +01:00
|
|
|
protected function pKey(?Node $node): string {
|
|
|
|
if ($node === null) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
// => is not really an operator and does not typically participate in precedence resolution.
|
|
|
|
// However, there is an exception if yield expressions with keys are involved:
|
|
|
|
// [yield $a => $b] is interpreted as [(yield $a => $b)], so we need to ensure that
|
|
|
|
// [(yield $a) => $b] is printed with parentheses. We approximate this by lowering the LHS
|
|
|
|
// precedence to that of yield (which will also print unnecessary parentheses for rare low
|
|
|
|
// precedence unary operators like include).
|
|
|
|
$yieldPrecedence = $this->precedenceMap[Expr\Yield_::class][0];
|
|
|
|
return $this->p($node, self::MAX_PRECEDENCE, $yieldPrecedence) . ' => ';
|
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pArrayItem(Node\ArrayItem $node): string {
|
2023-02-26 18:44:40 +01:00
|
|
|
return $this->pKey($node->key)
|
2019-05-09 15:11:00 +02:00
|
|
|
. ($node->byRef ? '&' : '')
|
|
|
|
. ($node->unpack ? '...' : '')
|
|
|
|
. $this->p($node->value);
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pExpr_ArrayDimFetch(Expr\ArrayDimFetch $node): string {
|
2015-06-13 19:46:01 +02:00
|
|
|
return $this->pDereferenceLhs($node->var)
|
2014-02-06 14:44:16 +01:00
|
|
|
. '[' . (null !== $node->dim ? $this->p($node->dim) : '') . ']';
|
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pExpr_ConstFetch(Expr\ConstFetch $node): string {
|
2014-02-06 14:44:16 +01:00
|
|
|
return $this->p($node->name);
|
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pExpr_ClassConstFetch(Expr\ClassConstFetch $node): string {
|
2023-02-26 15:57:37 +01:00
|
|
|
return $this->pStaticDereferenceLhs($node->class) . '::' . $this->pObjectProperty($node->name);
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pExpr_PropertyFetch(Expr\PropertyFetch $node): string {
|
2015-06-13 19:46:01 +02:00
|
|
|
return $this->pDereferenceLhs($node->var) . '->' . $this->pObjectProperty($node->name);
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pExpr_NullsafePropertyFetch(Expr\NullsafePropertyFetch $node): string {
|
2020-08-02 10:30:44 +02:00
|
|
|
return $this->pDereferenceLhs($node->var) . '?->' . $this->pObjectProperty($node->name);
|
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pExpr_StaticPropertyFetch(Expr\StaticPropertyFetch $node): string {
|
2023-02-26 15:57:37 +01:00
|
|
|
return $this->pStaticDereferenceLhs($node->class) . '::$' . $this->pObjectProperty($node->name);
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pExpr_ShellExec(Expr\ShellExec $node): string {
|
2014-02-06 14:44:16 +01:00
|
|
|
return '`' . $this->pEncapsList($node->parts, '`') . '`';
|
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pExpr_Closure(Expr\Closure $node): string {
|
2020-09-13 21:01:17 +02:00
|
|
|
return $this->pAttrGroups($node->attrGroups, true)
|
2022-09-21 18:56:09 +02:00
|
|
|
. $this->pStatic($node->static)
|
2014-02-06 14:44:16 +01:00
|
|
|
. 'function ' . ($node->byRef ? '&' : '')
|
2022-08-07 16:43:53 +02:00
|
|
|
. '(' . $this->pMaybeMultiline($node->params, $this->phpVersion->supportsTrailingCommaInParamList()) . ')'
|
2022-06-04 13:22:58 +02:00
|
|
|
. (!empty($node->uses) ? ' use (' . $this->pCommaSeparated($node->uses) . ')' : '')
|
2022-06-04 13:01:02 +02:00
|
|
|
. (null !== $node->returnType ? ': ' . $this->p($node->returnType) : '')
|
2017-09-03 17:05:35 +02:00
|
|
|
. ' {' . $this->pStmts($node->stmts) . $this->nl . '}';
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pExpr_Match(Expr\Match_ $node): string {
|
2020-07-15 21:40:05 +02:00
|
|
|
return 'match (' . $this->p($node->cond) . ') {'
|
|
|
|
. $this->pCommaSeparatedMultiline($node->arms, true)
|
|
|
|
. $this->nl
|
|
|
|
. '}';
|
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pMatchArm(Node\MatchArm $node): string {
|
2023-02-27 21:36:36 +01:00
|
|
|
$result = '';
|
|
|
|
if ($node->conds) {
|
|
|
|
for ($i = 0, $c = \count($node->conds); $i + 1 < $c; $i++) {
|
|
|
|
$result .= $this->p($node->conds[$i]) . ', ';
|
|
|
|
}
|
|
|
|
$result .= $this->pKey($node->conds[$i]);
|
|
|
|
} else {
|
|
|
|
$result = 'default => ';
|
|
|
|
}
|
|
|
|
return $result . $this->p($node->body);
|
2020-07-15 21:40:05 +02:00
|
|
|
}
|
|
|
|
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
protected function pExpr_ArrowFunction(Expr\ArrowFunction $node, int $precedence, int $lhsPrecedence): string {
|
|
|
|
return $this->pPrefixOp(
|
|
|
|
Expr\ArrowFunction::class,
|
|
|
|
$this->pAttrGroups($node->attrGroups, true)
|
2022-09-21 18:56:09 +02:00
|
|
|
. $this->pStatic($node->static)
|
2019-05-09 14:17:28 +02:00
|
|
|
. 'fn' . ($node->byRef ? '&' : '')
|
2022-08-07 16:43:53 +02:00
|
|
|
. '(' . $this->pMaybeMultiline($node->params, $this->phpVersion->supportsTrailingCommaInParamList()) . ')'
|
2019-05-09 14:17:28 +02:00
|
|
|
. (null !== $node->returnType ? ': ' . $this->p($node->returnType) : '')
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
. ' => ',
|
|
|
|
$node->expr, $precedence, $lhsPrecedence);
|
2019-05-09 14:17:28 +02:00
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pClosureUse(Node\ClosureUse $node): string {
|
2017-01-19 23:32:49 +01:00
|
|
|
return ($node->byRef ? '&' : '') . $this->p($node->var);
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pExpr_New(Expr\New_ $node): string {
|
2015-04-26 23:04:31 +02:00
|
|
|
if ($node->class instanceof Stmt\Class_) {
|
2017-04-09 19:49:47 +02:00
|
|
|
$args = $node->args ? '(' . $this->pMaybeMultiline($node->args) . ')' : '';
|
2015-04-26 23:04:31 +02:00
|
|
|
return 'new ' . $this->pClassCommon($node->class, $args);
|
|
|
|
}
|
2023-02-26 15:11:36 +01:00
|
|
|
return 'new ' . $this->pNewOperand($node->class)
|
2020-08-09 22:06:34 +02:00
|
|
|
. '(' . $this->pMaybeMultiline($node->args) . ')';
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
protected function pExpr_Clone(Expr\Clone_ $node, int $precedence, int $lhsPrecedence): string {
|
|
|
|
return $this->pPrefixOp(Expr\Clone_::class, 'clone ', $node->expr, $precedence, $lhsPrecedence);
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
protected function pExpr_Ternary(Expr\Ternary $node, int $precedence, int $lhsPrecedence): string {
|
2014-02-06 14:44:16 +01:00
|
|
|
// a bit of cheating: we treat the ternary as a binary op where the ?...: part is the operator.
|
|
|
|
// this is okay because the part between ? and : never needs parentheses.
|
2017-11-13 00:11:19 +01:00
|
|
|
return $this->pInfixOp(Expr\Ternary::class,
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
$node->cond, ' ?' . (null !== $node->if ? ' ' . $this->p($node->if) . ' ' : '') . ': ', $node->else,
|
|
|
|
$precedence, $lhsPrecedence
|
2014-02-06 14:44:16 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pExpr_Exit(Expr\Exit_ $node): string {
|
2016-04-19 14:49:18 +02:00
|
|
|
$kind = $node->getAttribute('kind', Expr\Exit_::KIND_DIE);
|
|
|
|
return ($kind === Expr\Exit_::KIND_EXIT ? 'exit' : 'die')
|
2016-03-09 20:20:36 +01:00
|
|
|
. (null !== $node->expr ? '(' . $this->p($node->expr) . ')' : '');
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
2023-02-26 18:33:24 +01:00
|
|
|
protected function pExpr_Throw(Expr\Throw_ $node, int $precedence, int $lhsPrecedence): string {
|
|
|
|
return $this->pPrefixOp(Expr\Throw_::class, 'throw ', $node->expr, $precedence, $lhsPrecedence);
|
2020-08-09 20:52:55 +02:00
|
|
|
}
|
|
|
|
|
2023-02-26 18:44:40 +01:00
|
|
|
protected function pExpr_Yield(Expr\Yield_ $node, int $precedence, int $lhsPrecedence): string {
|
2014-02-06 14:44:16 +01:00
|
|
|
if ($node->value === null) {
|
2023-03-04 12:04:47 +01:00
|
|
|
$opPrecedence = $this->precedenceMap[Expr\Yield_::class][0];
|
|
|
|
return $opPrecedence >= $lhsPrecedence ? '(yield)' : 'yield';
|
2014-02-06 14:44:16 +01:00
|
|
|
} else {
|
2023-02-26 18:44:40 +01:00
|
|
|
if (!$this->phpVersion->supportsYieldWithoutParentheses()) {
|
|
|
|
return '(yield ' . $this->pKey($node->key) . $this->p($node->value) . ')';
|
|
|
|
}
|
|
|
|
return $this->pPrefixOp(
|
|
|
|
Expr\Yield_::class, 'yield ' . $this->pKey($node->key),
|
2023-05-20 18:55:12 +02:00
|
|
|
$node->value, $precedence, $lhsPrecedence);
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Declarations
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pStmt_Namespace(Stmt\Namespace_ $node): string {
|
2014-02-06 14:44:16 +01:00
|
|
|
if ($this->canUseSemicolonNamespaces) {
|
2017-09-03 17:05:35 +02:00
|
|
|
return 'namespace ' . $this->p($node->name) . ';'
|
|
|
|
. $this->nl . $this->pStmts($node->stmts, false);
|
2014-02-06 14:44:16 +01:00
|
|
|
} else {
|
|
|
|
return 'namespace' . (null !== $node->name ? ' ' . $this->p($node->name) : '')
|
2017-09-03 17:05:35 +02:00
|
|
|
. ' {' . $this->pStmts($node->stmts) . $this->nl . '}';
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pStmt_Use(Stmt\Use_ $node): string {
|
2015-06-12 23:05:28 +02:00
|
|
|
return 'use ' . $this->pUseType($node->type)
|
2014-03-26 22:33:45 +01:00
|
|
|
. $this->pCommaSeparated($node->uses) . ';';
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pStmt_GroupUse(Stmt\GroupUse $node): string {
|
2015-06-12 23:05:28 +02:00
|
|
|
return 'use ' . $this->pUseType($node->type) . $this->pName($node->prefix)
|
|
|
|
. '\{' . $this->pCommaSeparated($node->uses) . '};';
|
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pUseItem(Node\UseItem $node): string {
|
2015-06-13 11:27:38 +02:00
|
|
|
return $this->pUseType($node->type) . $this->p($node->name)
|
2017-04-28 21:05:01 +02:00
|
|
|
. (null !== $node->alias ? ' as ' . $node->alias : '');
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pUseType(int $type): string {
|
2015-06-13 11:27:38 +02:00
|
|
|
return $type === Stmt\Use_::TYPE_FUNCTION ? 'function '
|
|
|
|
: ($type === Stmt\Use_::TYPE_CONSTANT ? 'const ' : '');
|
2015-06-12 23:05:28 +02:00
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pStmt_Interface(Stmt\Interface_ $node): string {
|
2020-09-13 21:01:17 +02:00
|
|
|
return $this->pAttrGroups($node->attrGroups)
|
|
|
|
. 'interface ' . $node->name
|
2014-02-06 14:44:16 +01:00
|
|
|
. (!empty($node->extends) ? ' extends ' . $this->pCommaSeparated($node->extends) : '')
|
2017-09-03 17:05:35 +02:00
|
|
|
. $this->nl . '{' . $this->pStmts($node->stmts) . $this->nl . '}';
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pStmt_Enum(Stmt\Enum_ $node): string {
|
2021-04-25 21:11:36 +02:00
|
|
|
return $this->pAttrGroups($node->attrGroups)
|
|
|
|
. 'enum ' . $node->name
|
2023-03-04 11:52:44 +01:00
|
|
|
. ($node->scalarType ? ' : ' . $this->p($node->scalarType) : '')
|
2021-04-25 21:11:36 +02:00
|
|
|
. (!empty($node->implements) ? ' implements ' . $this->pCommaSeparated($node->implements) : '')
|
|
|
|
. $this->nl . '{' . $this->pStmts($node->stmts) . $this->nl . '}';
|
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pStmt_Class(Stmt\Class_ $node): string {
|
2015-04-26 23:04:31 +02:00
|
|
|
return $this->pClassCommon($node, ' ' . $node->name);
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pStmt_Trait(Stmt\Trait_ $node): string {
|
2020-09-13 21:01:17 +02:00
|
|
|
return $this->pAttrGroups($node->attrGroups)
|
|
|
|
. 'trait ' . $node->name
|
2017-09-03 17:05:35 +02:00
|
|
|
. $this->nl . '{' . $this->pStmts($node->stmts) . $this->nl . '}';
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pStmt_EnumCase(Stmt\EnumCase $node): string {
|
2021-04-25 21:11:36 +02:00
|
|
|
return $this->pAttrGroups($node->attrGroups)
|
|
|
|
. 'case ' . $node->name
|
|
|
|
. ($node->expr ? ' = ' . $this->p($node->expr) : '')
|
|
|
|
. ';';
|
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pStmt_TraitUse(Stmt\TraitUse $node): string {
|
2014-02-06 14:44:16 +01:00
|
|
|
return 'use ' . $this->pCommaSeparated($node->traits)
|
|
|
|
. (empty($node->adaptations)
|
|
|
|
? ';'
|
2017-09-03 17:05:35 +02:00
|
|
|
: ' {' . $this->pStmts($node->adaptations) . $this->nl . '}');
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pStmt_TraitUseAdaptation_Precedence(Stmt\TraitUseAdaptation\Precedence $node): string {
|
2014-02-06 14:44:16 +01:00
|
|
|
return $this->p($node->trait) . '::' . $node->method
|
|
|
|
. ' insteadof ' . $this->pCommaSeparated($node->insteadof) . ';';
|
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pStmt_TraitUseAdaptation_Alias(Stmt\TraitUseAdaptation\Alias $node): string {
|
2014-02-06 14:44:16 +01:00
|
|
|
return (null !== $node->trait ? $this->p($node->trait) . '::' : '')
|
|
|
|
. $node->method . ' as'
|
2014-11-03 16:16:15 +01:00
|
|
|
. (null !== $node->newModifier ? ' ' . rtrim($this->pModifiers($node->newModifier), ' ') : '')
|
2022-08-28 22:57:06 +02:00
|
|
|
. (null !== $node->newName ? ' ' . $node->newName : '')
|
2014-02-06 14:44:16 +01:00
|
|
|
. ';';
|
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pStmt_Property(Stmt\Property $node): string {
|
2020-09-13 21:01:17 +02:00
|
|
|
return $this->pAttrGroups($node->attrGroups)
|
|
|
|
. (0 === $node->flags ? 'var ' : $this->pModifiers($node->flags))
|
2019-01-05 12:06:18 +01:00
|
|
|
. ($node->type ? $this->p($node->type) . ' ' : '')
|
2024-07-14 21:29:58 +02:00
|
|
|
. $this->pCommaSeparated($node->props)
|
|
|
|
. ($node->hooks ? ' {' . $this->pStmts($node->hooks) . $this->nl . '}' : ';');
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pPropertyItem(Node\PropertyItem $node): string {
|
2014-02-06 14:44:16 +01:00
|
|
|
return '$' . $node->name
|
|
|
|
. (null !== $node->default ? ' = ' . $this->p($node->default) : '');
|
|
|
|
}
|
|
|
|
|
2024-07-14 21:29:58 +02:00
|
|
|
protected function pPropertyHook(Node\PropertyHook $node): string {
|
|
|
|
return $this->pAttrGroups($node->attrGroups)
|
|
|
|
. $this->pModifiers($node->flags)
|
|
|
|
. ($node->byRef ? '&' : '') . $node->name
|
|
|
|
. ($node->params ? '(' . $this->pMaybeMultiline($node->params, $this->phpVersion->supportsTrailingCommaInParamList()) . ')' : '')
|
|
|
|
. (\is_array($node->body) ? ' {' . $this->pStmts($node->body) . $this->nl . '}'
|
|
|
|
: ($node->body !== null ? ' => ' . $this->p($node->body) : '') . ';');
|
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pStmt_ClassMethod(Stmt\ClassMethod $node): string {
|
2020-09-13 21:01:17 +02:00
|
|
|
return $this->pAttrGroups($node->attrGroups)
|
|
|
|
. $this->pModifiers($node->flags)
|
2014-02-06 14:44:16 +01:00
|
|
|
. 'function ' . ($node->byRef ? '&' : '') . $node->name
|
2022-08-07 16:43:53 +02:00
|
|
|
. '(' . $this->pMaybeMultiline($node->params, $this->phpVersion->supportsTrailingCommaInParamList()) . ')'
|
2022-06-04 13:01:02 +02:00
|
|
|
. (null !== $node->returnType ? ': ' . $this->p($node->returnType) : '')
|
2014-02-06 14:44:16 +01:00
|
|
|
. (null !== $node->stmts
|
2017-09-03 17:05:35 +02:00
|
|
|
? $this->nl . '{' . $this->pStmts($node->stmts) . $this->nl . '}'
|
2014-02-06 14:44:16 +01:00
|
|
|
: ';');
|
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pStmt_ClassConst(Stmt\ClassConst $node): string {
|
2020-09-13 21:01:17 +02:00
|
|
|
return $this->pAttrGroups($node->attrGroups)
|
|
|
|
. $this->pModifiers($node->flags)
|
2023-05-20 19:14:49 +02:00
|
|
|
. 'const '
|
|
|
|
. (null !== $node->type ? $this->p($node->type) . ' ' : '')
|
|
|
|
. $this->pCommaSeparated($node->consts) . ';';
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pStmt_Function(Stmt\Function_ $node): string {
|
2020-09-13 21:01:17 +02:00
|
|
|
return $this->pAttrGroups($node->attrGroups)
|
|
|
|
. 'function ' . ($node->byRef ? '&' : '') . $node->name
|
2022-08-07 16:43:53 +02:00
|
|
|
. '(' . $this->pMaybeMultiline($node->params, $this->phpVersion->supportsTrailingCommaInParamList()) . ')'
|
2022-06-04 13:01:02 +02:00
|
|
|
. (null !== $node->returnType ? ': ' . $this->p($node->returnType) : '')
|
2017-09-03 17:05:35 +02:00
|
|
|
. $this->nl . '{' . $this->pStmts($node->stmts) . $this->nl . '}';
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pStmt_Const(Stmt\Const_ $node): string {
|
2014-02-06 14:44:16 +01:00
|
|
|
return 'const ' . $this->pCommaSeparated($node->consts) . ';';
|
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pStmt_Declare(Stmt\Declare_ $node): string {
|
2015-12-07 12:12:00 +01:00
|
|
|
return 'declare (' . $this->pCommaSeparated($node->declares) . ')'
|
2017-09-03 17:05:35 +02:00
|
|
|
. (null !== $node->stmts ? ' {' . $this->pStmts($node->stmts) . $this->nl . '}' : ';');
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pDeclareItem(Node\DeclareItem $node): string {
|
2015-12-07 12:12:00 +01:00
|
|
|
return $node->key . '=' . $this->p($node->value);
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Control flow
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pStmt_If(Stmt\If_ $node): string {
|
2014-02-06 14:44:16 +01:00
|
|
|
return 'if (' . $this->p($node->cond) . ') {'
|
2017-09-03 17:05:35 +02:00
|
|
|
. $this->pStmts($node->stmts) . $this->nl . '}'
|
2017-10-06 14:52:52 +02:00
|
|
|
. ($node->elseifs ? ' ' . $this->pImplode($node->elseifs, ' ') : '')
|
2017-10-06 14:56:59 +02:00
|
|
|
. (null !== $node->else ? ' ' . $this->p($node->else) : '');
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pStmt_ElseIf(Stmt\ElseIf_ $node): string {
|
2017-10-06 14:52:52 +02:00
|
|
|
return 'elseif (' . $this->p($node->cond) . ') {'
|
2017-09-03 17:05:35 +02:00
|
|
|
. $this->pStmts($node->stmts) . $this->nl . '}';
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pStmt_Else(Stmt\Else_ $node): string {
|
2023-03-05 11:30:39 +01:00
|
|
|
if (\count($node->stmts) === 1 && $node->stmts[0] instanceof Stmt\If_) {
|
|
|
|
// Print as "else if" rather than "else { if }"
|
|
|
|
return 'else ' . $this->p($node->stmts[0]);
|
|
|
|
}
|
2017-10-06 14:56:59 +02:00
|
|
|
return 'else {' . $this->pStmts($node->stmts) . $this->nl . '}';
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pStmt_For(Stmt\For_ $node): string {
|
2014-02-06 14:44:16 +01:00
|
|
|
return 'for ('
|
|
|
|
. $this->pCommaSeparated($node->init) . ';' . (!empty($node->cond) ? ' ' : '')
|
|
|
|
. $this->pCommaSeparated($node->cond) . ';' . (!empty($node->loop) ? ' ' : '')
|
|
|
|
. $this->pCommaSeparated($node->loop)
|
2017-09-03 17:05:35 +02:00
|
|
|
. ') {' . $this->pStmts($node->stmts) . $this->nl . '}';
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pStmt_Foreach(Stmt\Foreach_ $node): string {
|
2014-02-06 14:44:16 +01:00
|
|
|
return 'foreach (' . $this->p($node->expr) . ' as '
|
|
|
|
. (null !== $node->keyVar ? $this->p($node->keyVar) . ' => ' : '')
|
|
|
|
. ($node->byRef ? '&' : '') . $this->p($node->valueVar) . ') {'
|
2017-09-03 17:05:35 +02:00
|
|
|
. $this->pStmts($node->stmts) . $this->nl . '}';
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pStmt_While(Stmt\While_ $node): string {
|
2014-02-06 14:44:16 +01:00
|
|
|
return 'while (' . $this->p($node->cond) . ') {'
|
2017-09-03 17:05:35 +02:00
|
|
|
. $this->pStmts($node->stmts) . $this->nl . '}';
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pStmt_Do(Stmt\Do_ $node): string {
|
2017-09-03 17:05:35 +02:00
|
|
|
return 'do {' . $this->pStmts($node->stmts) . $this->nl
|
2014-02-06 14:44:16 +01:00
|
|
|
. '} while (' . $this->p($node->cond) . ');';
|
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pStmt_Switch(Stmt\Switch_ $node): string {
|
2014-02-06 14:44:16 +01:00
|
|
|
return 'switch (' . $this->p($node->cond) . ') {'
|
2017-09-03 17:05:35 +02:00
|
|
|
. $this->pStmts($node->cases) . $this->nl . '}';
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pStmt_Case(Stmt\Case_ $node): string {
|
2014-02-06 14:44:16 +01:00
|
|
|
return (null !== $node->cond ? 'case ' . $this->p($node->cond) : 'default') . ':'
|
2014-03-27 12:30:03 +01:00
|
|
|
. $this->pStmts($node->stmts);
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pStmt_TryCatch(Stmt\TryCatch $node): string {
|
2017-09-03 17:05:35 +02:00
|
|
|
return 'try {' . $this->pStmts($node->stmts) . $this->nl . '}'
|
2017-01-21 21:25:48 +01:00
|
|
|
. ($node->catches ? ' ' . $this->pImplode($node->catches, ' ') : '')
|
|
|
|
. ($node->finally !== null ? ' ' . $this->p($node->finally) : '');
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pStmt_Catch(Stmt\Catch_ $node): string {
|
2020-06-03 08:49:38 +02:00
|
|
|
return 'catch (' . $this->pImplode($node->types, '|')
|
|
|
|
. ($node->var !== null ? ' ' . $this->p($node->var) : '')
|
2017-09-03 17:05:35 +02:00
|
|
|
. ') {' . $this->pStmts($node->stmts) . $this->nl . '}';
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pStmt_Finally(Stmt\Finally_ $node): string {
|
2017-09-03 17:05:35 +02:00
|
|
|
return 'finally {' . $this->pStmts($node->stmts) . $this->nl . '}';
|
2016-07-25 14:25:04 +02:00
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pStmt_Break(Stmt\Break_ $node): string {
|
2014-02-06 14:44:16 +01:00
|
|
|
return 'break' . ($node->num !== null ? ' ' . $this->p($node->num) : '') . ';';
|
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pStmt_Continue(Stmt\Continue_ $node): string {
|
2014-02-06 14:44:16 +01:00
|
|
|
return 'continue' . ($node->num !== null ? ' ' . $this->p($node->num) : '') . ';';
|
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pStmt_Return(Stmt\Return_ $node): string {
|
2014-02-06 14:44:16 +01:00
|
|
|
return 'return' . (null !== $node->expr ? ' ' . $this->p($node->expr) : '') . ';';
|
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pStmt_Label(Stmt\Label $node): string {
|
2014-02-06 14:44:16 +01:00
|
|
|
return $node->name . ':';
|
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pStmt_Goto(Stmt\Goto_ $node): string {
|
2014-02-06 14:44:16 +01:00
|
|
|
return 'goto ' . $node->name . ';';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Other
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pStmt_Expression(Stmt\Expression $node): string {
|
2016-12-23 14:11:31 +01:00
|
|
|
return $this->p($node->expr) . ';';
|
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pStmt_Echo(Stmt\Echo_ $node): string {
|
2014-02-06 14:44:16 +01:00
|
|
|
return 'echo ' . $this->pCommaSeparated($node->exprs) . ';';
|
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pStmt_Static(Stmt\Static_ $node): string {
|
2014-02-06 14:44:16 +01:00
|
|
|
return 'static ' . $this->pCommaSeparated($node->vars) . ';';
|
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pStmt_Global(Stmt\Global_ $node): string {
|
2014-02-06 14:44:16 +01:00
|
|
|
return 'global ' . $this->pCommaSeparated($node->vars) . ';';
|
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pStaticVar(Node\StaticVar $node): string {
|
2017-01-19 23:35:31 +01:00
|
|
|
return $this->p($node->var)
|
2014-02-06 14:44:16 +01:00
|
|
|
. (null !== $node->default ? ' = ' . $this->p($node->default) : '');
|
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pStmt_Unset(Stmt\Unset_ $node): string {
|
2014-02-06 14:44:16 +01:00
|
|
|
return 'unset(' . $this->pCommaSeparated($node->vars) . ');';
|
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pStmt_InlineHTML(Stmt\InlineHTML $node): string {
|
2023-05-21 20:56:56 +02:00
|
|
|
$newline = $node->getAttribute('hasLeadingNewline', true) ? $this->newline : '';
|
2017-09-03 17:05:35 +02:00
|
|
|
return '?>' . $newline . $node->value . '<?php ';
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pStmt_HaltCompiler(Stmt\HaltCompiler $node): string {
|
2014-02-06 14:44:16 +01:00
|
|
|
return '__halt_compiler();' . $node->remaining;
|
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pStmt_Nop(Stmt\Nop $node): string {
|
2015-10-02 11:03:12 +02:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2023-09-24 20:27:30 +02:00
|
|
|
protected function pStmt_Block(Stmt\Block $node): string {
|
|
|
|
return '{' . $this->pStmts($node->stmts) . $this->nl . '}';
|
|
|
|
}
|
|
|
|
|
2014-02-06 14:44:16 +01:00
|
|
|
// Helpers
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pClassCommon(Stmt\Class_ $node, string $afterClassToken): string {
|
2020-09-13 21:01:17 +02:00
|
|
|
return $this->pAttrGroups($node->attrGroups, $node->name === null)
|
|
|
|
. $this->pModifiers($node->flags)
|
|
|
|
. 'class' . $afterClassToken
|
|
|
|
. (null !== $node->extends ? ' extends ' . $this->p($node->extends) : '')
|
|
|
|
. (!empty($node->implements) ? ' implements ' . $this->pCommaSeparated($node->implements) : '')
|
|
|
|
. $this->nl . '{' . $this->pStmts($node->stmts) . $this->nl . '}';
|
2015-04-26 23:04:31 +02:00
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pObjectProperty(Node $node): string {
|
2014-02-06 14:44:16 +01:00
|
|
|
if ($node instanceof Expr) {
|
|
|
|
return '{' . $this->p($node) . '}';
|
|
|
|
} else {
|
2022-09-11 17:49:41 +02:00
|
|
|
assert($node instanceof Node\Identifier);
|
|
|
|
return $node->name;
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-17 17:12:55 +02:00
|
|
|
/** @param (Expr|Node\InterpolatedStringPart)[] $encapsList */
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pEncapsList(array $encapsList, ?string $quote): string {
|
2014-02-06 14:44:16 +01:00
|
|
|
$return = '';
|
|
|
|
foreach ($encapsList as $element) {
|
2022-09-03 13:25:23 +02:00
|
|
|
if ($element instanceof Node\InterpolatedStringPart) {
|
2016-04-02 22:22:24 +09:00
|
|
|
$return .= $this->escapeString($element->value, $quote);
|
2014-02-06 14:44:16 +01:00
|
|
|
} else {
|
|
|
|
$return .= '{' . $this->p($element) . '}';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pSingleQuotedString(string $string): string {
|
2022-07-24 22:56:44 +02:00
|
|
|
// It is idiomatic to only escape backslashes when necessary, i.e. when followed by ', \ or
|
|
|
|
// the end of the string ('Foo\Bar' instead of 'Foo\\Bar'). However, we also don't want to
|
|
|
|
// produce an odd number of backslashes, so '\\\\a' should not get rendered as '\\\a', even
|
|
|
|
// though that would be legal.
|
|
|
|
$regex = '/\'|\\\\(?=[\'\\\\]|$)|(?<=\\\\)\\\\/';
|
|
|
|
return '\'' . preg_replace($regex, '\\\\$0', $string) . '\'';
|
2017-12-01 18:31:21 +01:00
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function escapeString(string $string, ?string $quote): string {
|
2016-04-02 22:22:24 +09:00
|
|
|
if (null === $quote) {
|
|
|
|
// For doc strings, don't escape newlines
|
2016-04-13 21:48:57 +09:00
|
|
|
$escaped = addcslashes($string, "\t\f\v$\\");
|
2023-02-27 22:00:49 +01:00
|
|
|
// But do escape isolated \r. Combined with the terminating newline, it might get
|
|
|
|
// interpreted as \r\n and dropped from the string contents.
|
|
|
|
$escaped = preg_replace('/\r(?!\n)/', '\\r', $escaped);
|
2023-08-27 22:02:31 +02:00
|
|
|
if ($this->phpVersion->supportsFlexibleHeredoc()) {
|
|
|
|
$escaped = $this->indentString($escaped);
|
|
|
|
}
|
2016-04-13 21:48:57 +09:00
|
|
|
} else {
|
|
|
|
$escaped = addcslashes($string, "\n\r\t\f\v$" . $quote . "\\");
|
2016-04-02 22:22:24 +09:00
|
|
|
}
|
2016-04-13 21:48:57 +09:00
|
|
|
|
2021-04-25 21:36:22 +02:00
|
|
|
// Escape control characters and non-UTF-8 characters.
|
2021-04-25 21:47:07 +02:00
|
|
|
// Regex based on https://stackoverflow.com/a/11709412/385378.
|
2021-04-25 21:36:22 +02:00
|
|
|
$regex = '/(
|
|
|
|
[\x00-\x08\x0E-\x1F] # Control characters
|
|
|
|
| [\xC0-\xC1] # Invalid UTF-8 Bytes
|
|
|
|
| [\xF5-\xFF] # Invalid UTF-8 Bytes
|
2021-04-25 21:47:07 +02:00
|
|
|
| \xE0(?=[\x80-\x9F]) # Overlong encoding of prior code point
|
|
|
|
| \xF0(?=[\x80-\x8F]) # Overlong encoding of prior code point
|
2021-04-25 21:36:22 +02:00
|
|
|
| [\xC2-\xDF](?![\x80-\xBF]) # Invalid UTF-8 Sequence Start
|
|
|
|
| [\xE0-\xEF](?![\x80-\xBF]{2}) # Invalid UTF-8 Sequence Start
|
|
|
|
| [\xF0-\xF4](?![\x80-\xBF]{3}) # Invalid UTF-8 Sequence Start
|
|
|
|
| (?<=[\x00-\x7F\xF5-\xFF])[\x80-\xBF] # Invalid UTF-8 Sequence Middle
|
|
|
|
| (?<![\xC2-\xDF]|[\xE0-\xEF]|[\xE0-\xEF][\x80-\xBF]|[\xF0-\xF4]|[\xF0-\xF4][\x80-\xBF]|[\xF0-\xF4][\x80-\xBF]{2})[\x80-\xBF] # Overlong Sequence
|
|
|
|
| (?<=[\xE0-\xEF])[\x80-\xBF](?![\x80-\xBF]) # Short 3 byte sequence
|
|
|
|
| (?<=[\xF0-\xF4])[\x80-\xBF](?![\x80-\xBF]{2}) # Short 4 byte sequence
|
|
|
|
| (?<=[\xF0-\xF4][\x80-\xBF])[\x80-\xBF](?![\x80-\xBF]) # Short 4 byte sequence (2)
|
|
|
|
)/x';
|
2022-09-11 17:49:41 +02:00
|
|
|
return preg_replace_callback($regex, function ($matches): string {
|
2021-04-25 21:47:07 +02:00
|
|
|
assert(strlen($matches[0]) === 1);
|
2022-08-28 22:57:06 +02:00
|
|
|
$hex = dechex(ord($matches[0]));
|
2021-04-25 21:22:15 +02:00
|
|
|
return '\\x' . str_pad($hex, 2, '0', \STR_PAD_LEFT);
|
2016-04-13 21:48:57 +09:00
|
|
|
}, $escaped);
|
2016-04-02 22:22:24 +09:00
|
|
|
}
|
|
|
|
|
2023-02-26 12:20:32 +01:00
|
|
|
protected function containsEndLabel(string $string, string $label, bool $atStart = true): bool {
|
2023-02-26 15:21:33 +01:00
|
|
|
$start = $atStart ? '(?:^|[\r\n])[ \t]*' : '[\r\n][ \t]*';
|
2016-04-02 22:22:24 +09:00
|
|
|
return false !== strpos($string, $label)
|
2023-02-26 12:20:32 +01:00
|
|
|
&& preg_match('/' . $start . $label . '(?:$|[^_A-Za-z0-9\x80-\xff])/', $string);
|
2016-04-02 22:22:24 +09:00
|
|
|
}
|
|
|
|
|
2022-09-17 17:12:55 +02:00
|
|
|
/** @param (Expr|Node\InterpolatedStringPart)[] $parts */
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function encapsedContainsEndLabel(array $parts, string $label): bool {
|
2016-04-02 22:22:24 +09:00
|
|
|
foreach ($parts as $i => $part) {
|
2022-09-03 13:25:23 +02:00
|
|
|
if ($part instanceof Node\InterpolatedStringPart
|
2023-02-26 12:26:27 +01:00
|
|
|
&& $this->containsEndLabel($this->escapeString($part->value, null), $label, $i === 0)
|
2016-04-02 22:22:24 +09:00
|
|
|
) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pDereferenceLhs(Node $node): string {
|
2016-12-23 14:11:31 +01:00
|
|
|
if (!$this->dereferenceLhsRequiresParens($node)) {
|
2015-06-13 19:46:01 +02:00
|
|
|
return $this->p($node);
|
2022-08-28 22:57:06 +02:00
|
|
|
} else {
|
2014-02-06 14:44:16 +01:00
|
|
|
return '(' . $this->p($node) . ')';
|
2015-06-13 19:46:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-26 15:57:37 +01:00
|
|
|
protected function pStaticDereferenceLhs(Node $node): string {
|
|
|
|
if (!$this->staticDereferenceLhsRequiresParens($node)) {
|
|
|
|
return $this->p($node);
|
|
|
|
} else {
|
|
|
|
return '(' . $this->p($node) . ')';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pCallLhs(Node $node): string {
|
2016-12-23 14:11:31 +01:00
|
|
|
if (!$this->callLhsRequiresParens($node)) {
|
2014-02-06 14:44:16 +01:00
|
|
|
return $this->p($node);
|
2022-08-28 22:57:06 +02:00
|
|
|
} else {
|
2015-06-13 19:46:01 +02:00
|
|
|
return '(' . $this->p($node) . ')';
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
|
|
|
}
|
2017-04-09 19:49:47 +02:00
|
|
|
|
2023-02-26 15:11:36 +01:00
|
|
|
protected function pNewOperand(Node $node): string {
|
|
|
|
if (!$this->newOperandRequiresParens($node)) {
|
|
|
|
return $this->p($node);
|
|
|
|
} else {
|
|
|
|
return '(' . $this->p($node) . ')';
|
|
|
|
}
|
2020-08-09 22:06:34 +02:00
|
|
|
}
|
|
|
|
|
2017-09-29 17:09:16 +02:00
|
|
|
/**
|
|
|
|
* @param Node[] $nodes
|
|
|
|
*/
|
2022-06-04 12:48:12 +02:00
|
|
|
protected function hasNodeWithComments(array $nodes): bool {
|
2017-04-09 19:49:47 +02:00
|
|
|
foreach ($nodes as $node) {
|
2017-09-29 17:09:16 +02:00
|
|
|
if ($node && $node->getComments()) {
|
2017-04-09 19:49:47 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-09-17 17:12:55 +02:00
|
|
|
/** @param Node[] $nodes */
|
2022-09-11 17:49:41 +02:00
|
|
|
protected function pMaybeMultiline(array $nodes, bool $trailingComma = false): string {
|
2017-04-09 19:49:47 +02:00
|
|
|
if (!$this->hasNodeWithComments($nodes)) {
|
|
|
|
return $this->pCommaSeparated($nodes);
|
|
|
|
} else {
|
2017-09-03 17:05:35 +02:00
|
|
|
return $this->pCommaSeparatedMultiline($nodes, $trailingComma) . $this->nl;
|
2017-04-09 19:49:47 +02:00
|
|
|
}
|
|
|
|
}
|
2020-09-13 21:01:17 +02:00
|
|
|
|
2022-09-17 17:12:55 +02:00
|
|
|
/** @param Node\AttributeGroup[] $nodes */
|
2020-10-10 17:41:52 +02:00
|
|
|
protected function pAttrGroups(array $nodes, bool $inline = false): string {
|
2020-09-13 21:01:17 +02:00
|
|
|
$result = '';
|
|
|
|
$sep = $inline ? ' ' : $this->nl;
|
|
|
|
foreach ($nodes as $node) {
|
|
|
|
$result .= $this->p($node) . $sep;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
2014-03-16 21:50:06 +01:00
|
|
|
}
|