Skip to content

Commit

Permalink
Add support for perserving formatting for static modifier change
Browse files Browse the repository at this point in the history
Closes nikicGH-891.
  • Loading branch information
nikic committed Sep 21, 2022
1 parent 46558ed commit 4bcdf74
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 27 deletions.
4 changes: 2 additions & 2 deletions lib/PhpParser/PrettyPrinter/Standard.php
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ protected function pExpr_ShellExec(Expr\ShellExec $node): string {

protected function pExpr_Closure(Expr\Closure $node): string {
return $this->pAttrGroups($node->attrGroups, true)
. ($node->static ? 'static ' : '')
. $this->pStatic($node->static)
. 'function ' . ($node->byRef ? '&' : '')
. '(' . $this->pMaybeMultiline($node->params, $this->phpVersion->supportsTrailingCommaInParamList()) . ')'
. (!empty($node->uses) ? ' use (' . $this->pCommaSeparated($node->uses) . ')' : '')
Expand All @@ -654,7 +654,7 @@ protected function pMatchArm(Node\MatchArm $node): string {

protected function pExpr_ArrowFunction(Expr\ArrowFunction $node): string {
return $this->pAttrGroups($node->attrGroups, true)
. ($node->static ? 'static ' : '')
. $this->pStatic($node->static)
. 'fn' . ($node->byRef ? '&' : '')
. '(' . $this->pMaybeMultiline($node->params, $this->phpVersion->supportsTrailingCommaInParamList()) . ')'
. (null !== $node->returnType ? ': ' . $this->p($node->returnType) : '')
Expand Down
44 changes: 22 additions & 22 deletions lib/PhpParser/PrettyPrinterAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,9 @@ abstract class PrettyPrinterAbstract {
* @var array<string, array{int|string|null, string, string}>
*/
protected $emptyListInsertionMap;
/** @var array<string, int> Map from "{$class}->{$subNode}" to token before which the modifiers
* should be reprinted. */
/** @var array<string, array{string, int}> Map from "{$class}->{$subNode}" to [$printFn, $token]
* where $printFn is the function to print the modifiers and $token is the token before which
* the modifiers should be reprinted. */
protected $modifierChangeMap;

/**
Expand Down Expand Up @@ -587,23 +588,16 @@ protected function p(Node $node, bool $parentFormatPreserved = false): string {
continue;
}

if (is_int($subNode) && is_int($origSubNode)) {
// Check if this is a modifier change
$key = $class . '->' . $subNodeName;
if (!isset($this->modifierChangeMap[$key])) {
return $this->pFallback($fallbackNode);
}

$findToken = $this->modifierChangeMap[$key];
$result .= $this->pModifiers($subNode);
$pos = $this->origTokens->findRight($pos, $findToken);
continue;
// Check if this is a modifier change
$key = $class . '->' . $subNodeName;
if (!isset($this->modifierChangeMap[$key])) {
return $this->pFallback($fallbackNode);
}

// If a non-node, non-array subnode changed, we don't be able to do a partial
// reconstructions, as we don't have enough offset information. Pretty print the
// whole node instead.
return $this->pFallback($fallbackNode);
[$printFn, $findToken] = $this->modifierChangeMap[$key];
$result .= $this->$printFn($subNode);
$pos = $this->origTokens->findRight($pos, $findToken);
continue;
}

$extraLeft = '';
Expand Down Expand Up @@ -1098,6 +1092,10 @@ protected function pModifiers(int $modifiers): string {
. ($modifiers & Modifiers::READONLY ? 'readonly ' : '');
}

protected function pStatic(bool $static): string {
return $static ? 'static ' : '';
}

/**
* Determine whether a list of nodes uses multiline formatting.
*
Expand Down Expand Up @@ -1520,11 +1518,13 @@ protected function initializeModifierChangeMap(): void {
}

$this->modifierChangeMap = [
Stmt\ClassConst::class . '->flags' => \T_CONST,
Stmt\ClassMethod::class . '->flags' => \T_FUNCTION,
Stmt\Class_::class . '->flags' => \T_CLASS,
Stmt\Property::class . '->flags' => \T_VARIABLE,
Param::class . '->flags' => \T_VARIABLE,
Stmt\ClassConst::class . '->flags' => ['pModifiers', \T_CONST],
Stmt\ClassMethod::class . '->flags' => ['pModifiers', \T_FUNCTION],
Stmt\Class_::class . '->flags' => ['pModifiers', \T_CLASS],
Stmt\Property::class . '->flags' => ['pModifiers', \T_VARIABLE],
Param::class . '->flags' => ['pModifiers', \T_VARIABLE],
Expr\Closure::class . '->static' => ['pStatic', \T_FUNCTION],
Expr\ArrowFunction::class . '->static' => ['pStatic', \T_FN],
//Stmt\TraitUseAdaptation\Alias::class . '->newModifier' => 0, // TODO
];

Expand Down
9 changes: 6 additions & 3 deletions test/code/formatPreservation/arrow_function.test
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,17 @@ static fn($a)
: int
=> $a;
-----
// TODO: Format preserving currently not supported
$stmts[0]->expr->static = true;
$stmts[1]->expr->static = false;
-----
<?php
static fn($a): int => $a;
static fn($a)
: int
=> $a;

fn($a): int => $a;
fn($a)
: int
=> $a;
-----
<?php
fn($a)
Expand Down

0 comments on commit 4bcdf74

Please sign in to comment.