This commit is contained in:
1 parent
f7c532a648
commit
640bd67556
5801 files changed
+484532
-55857
No files matched your search
+64
-24
@@ -59,9 +59,11 @@ abstract class PrettyPrinterAbstract implements PrettyPrinter {
|
||||
BinaryOp\Mod::class => [ 40, 41, 40],
|
||||
BinaryOp\Plus::class => [ 50, 51, 50],
|
||||
BinaryOp\Minus::class => [ 50, 51, 50],
|
||||
// FIXME: This precedence is incorrect for PHP 8.
|
||||
BinaryOp\Concat::class => [ 50, 51, 50],
|
||||
BinaryOp\ShiftLeft::class => [ 60, 61, 60],
|
||||
BinaryOp\ShiftRight::class => [ 60, 61, 60],
|
||||
BinaryOp\Pipe::class => [ 65, 66, 65],
|
||||
BinaryOp\Smaller::class => [ 70, 70, 70],
|
||||
BinaryOp\SmallerOrEqual::class => [ 70, 70, 70],
|
||||
BinaryOp\Greater::class => [ 70, 70, 70],
|
||||
@@ -102,10 +104,20 @@ abstract class PrettyPrinterAbstract implements PrettyPrinter {
|
||||
Expr\Include_::class => [220, -1, -1],
|
||||
Expr\ArrowFunction::class => [230, -1, -1],
|
||||
Expr\Throw_::class => [240, -1, -1],
|
||||
Expr\Cast\Void_::class => [250, -1, -1],
|
||||
];
|
||||
|
||||
/** @var int Current indentation level. */
|
||||
protected int $indentLevel;
|
||||
/** @var string String for single level of indentation */
|
||||
private string $indent;
|
||||
/** @var int Width in spaces to indent by. */
|
||||
private int $indentWidth;
|
||||
/** @var bool Whether to use tab indentation. */
|
||||
private bool $useTabs;
|
||||
/** @var int Width in spaces of one tab. */
|
||||
private int $tabWidth = 4;
|
||||
|
||||
/** @var string Newline style. Does not include current indentation. */
|
||||
protected string $newline;
|
||||
/** @var string Newline including current indentation. */
|
||||
@@ -154,8 +166,9 @@ abstract class PrettyPrinterAbstract implements PrettyPrinter {
|
||||
* @var array<string, array{int|string|null, string, string}>
|
||||
*/
|
||||
protected array $emptyListInsertionMap;
|
||||
/** @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
|
||||
/** @var array<string, array{string, int, int}>
|
||||
* Map from "{$class}->{$subNode}" to [$printFn, $skipToken, $findToken] where $printFn is the function to
|
||||
* print the modifiers, $skipToken is the token to skip at the start and $findToken is the token before which
|
||||
* the modifiers should be reprinted. */
|
||||
protected array $modifierChangeMap;
|
||||
|
||||
@@ -170,12 +183,14 @@ abstract class PrettyPrinterAbstract implements PrettyPrinter {
|
||||
* PHP version while specifying an older target (but the result will
|
||||
* of course not be compatible with the older version in that case).
|
||||
* * string $newline: The newline style to use. Should be "\n" (default) or "\r\n".
|
||||
* * string $indent: The indentation to use. Should either be all spaces or a single
|
||||
* tab. Defaults to four spaces (" ").
|
||||
* * bool $shortArraySyntax: Whether to use [] instead of array() as the default array
|
||||
* syntax, if the node does not specify a format. Defaults to whether
|
||||
* the phpVersion support short array syntax.
|
||||
*
|
||||
* @param array{
|
||||
* phpVersion?: PhpVersion, newline?: string, shortArraySyntax?: bool
|
||||
* phpVersion?: PhpVersion, newline?: string, indent?: string, shortArraySyntax?: bool
|
||||
* } $options Dictionary of formatting options
|
||||
*/
|
||||
public function __construct(array $options = []) {
|
||||
@@ -190,6 +205,17 @@ abstract class PrettyPrinterAbstract implements PrettyPrinter {
|
||||
$options['shortArraySyntax'] ?? $this->phpVersion->supportsShortArraySyntax();
|
||||
$this->docStringEndToken =
|
||||
$this->phpVersion->supportsFlexibleHeredoc() ? null : '_DOC_STRING_END_' . mt_rand();
|
||||
|
||||
$this->indent = $indent = $options['indent'] ?? ' ';
|
||||
if ($indent === "\t") {
|
||||
$this->useTabs = true;
|
||||
$this->indentWidth = $this->tabWidth;
|
||||
} elseif ($indent === \str_repeat(' ', \strlen($indent))) {
|
||||
$this->useTabs = false;
|
||||
$this->indentWidth = \strlen($indent);
|
||||
} else {
|
||||
throw new \LogicException('Option "indent" must either be all spaces or a single tab');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -208,24 +234,29 @@ abstract class PrettyPrinterAbstract implements PrettyPrinter {
|
||||
*/
|
||||
protected function setIndentLevel(int $level): void {
|
||||
$this->indentLevel = $level;
|
||||
$this->nl = $this->newline . \str_repeat(' ', $level);
|
||||
if ($this->useTabs) {
|
||||
$tabs = \intdiv($level, $this->tabWidth);
|
||||
$spaces = $level % $this->tabWidth;
|
||||
$this->nl = $this->newline . \str_repeat("\t", $tabs) . \str_repeat(' ', $spaces);
|
||||
} else {
|
||||
$this->nl = $this->newline . \str_repeat(' ', $level);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Increase indentation level.
|
||||
*/
|
||||
protected function indent(): void {
|
||||
$this->indentLevel += 4;
|
||||
$this->nl .= ' ';
|
||||
$this->indentLevel += $this->indentWidth;
|
||||
$this->nl .= $this->indent;
|
||||
}
|
||||
|
||||
/**
|
||||
* Decrease indentation level.
|
||||
*/
|
||||
protected function outdent(): void {
|
||||
assert($this->indentLevel >= 4);
|
||||
$this->indentLevel -= 4;
|
||||
$this->nl = $this->newline . str_repeat(' ', $this->indentLevel);
|
||||
assert($this->indentLevel >= $this->indentWidth);
|
||||
$this->setIndentLevel($this->indentLevel - $this->indentWidth);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -537,7 +568,7 @@ abstract class PrettyPrinterAbstract implements PrettyPrinter {
|
||||
$this->initializeModifierChangeMap();
|
||||
|
||||
$this->resetState();
|
||||
$this->origTokens = new TokenStream($origTokens);
|
||||
$this->origTokens = new TokenStream($origTokens, $this->tabWidth);
|
||||
|
||||
$this->preprocessNodes($stmts);
|
||||
|
||||
@@ -647,9 +678,11 @@ abstract class PrettyPrinterAbstract implements PrettyPrinter {
|
||||
return $this->pFallback($fallbackNode, $precedence, $lhsPrecedence);
|
||||
}
|
||||
|
||||
[$printFn, $findToken] = $this->modifierChangeMap[$key];
|
||||
[$printFn, $skipToken, $findToken] = $this->modifierChangeMap[$key];
|
||||
$skipWSPos = $this->origTokens->skipRight($pos, $skipToken);
|
||||
$result .= $this->origTokens->getTokenCode($pos, $skipWSPos, $indentAdjustment);
|
||||
$result .= $this->$printFn($subNode);
|
||||
$pos = $this->origTokens->findRight($pos, $findToken);
|
||||
$pos = $this->origTokens->findRight($skipWSPos, $findToken);
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -709,7 +742,7 @@ abstract class PrettyPrinterAbstract implements PrettyPrinter {
|
||||
$result .= $extraLeft;
|
||||
|
||||
$origIndentLevel = $this->indentLevel;
|
||||
$this->setIndentLevel($this->origTokens->getIndentationBefore($subStartPos) + $indentAdjustment);
|
||||
$this->setIndentLevel(max($this->origTokens->getIndentationBefore($subStartPos) + $indentAdjustment, 0));
|
||||
|
||||
// If it's the same node that was previously in this position, it certainly doesn't
|
||||
// need fixup. It's important to check this here, because our fixup checks are more
|
||||
@@ -812,7 +845,7 @@ abstract class PrettyPrinterAbstract implements PrettyPrinter {
|
||||
\assert($itemStartPos >= 0 && $itemEndPos >= 0 && $itemStartPos >= $pos);
|
||||
|
||||
$origIndentLevel = $this->indentLevel;
|
||||
$lastElemIndentLevel = $this->origTokens->getIndentationBefore($itemStartPos) + $indentAdjustment;
|
||||
$lastElemIndentLevel = max($this->origTokens->getIndentationBefore($itemStartPos) + $indentAdjustment, 0);
|
||||
$this->setIndentLevel($lastElemIndentLevel);
|
||||
|
||||
$comments = $arrItem->getComments();
|
||||
@@ -1115,6 +1148,9 @@ abstract class PrettyPrinterAbstract implements PrettyPrinter {
|
||||
* @return bool Whether parentheses are required
|
||||
*/
|
||||
protected function callLhsRequiresParens(Node $node): bool {
|
||||
if ($node instanceof Expr\New_) {
|
||||
return !$this->phpVersion->supportsNewDereferenceWithoutParentheses();
|
||||
}
|
||||
return !($node instanceof Node\Name
|
||||
|| $node instanceof Expr\Variable
|
||||
|| $node instanceof Expr\ArrayDimFetch
|
||||
@@ -1146,6 +1182,9 @@ abstract class PrettyPrinterAbstract implements PrettyPrinter {
|
||||
* @return bool Whether parentheses are required
|
||||
*/
|
||||
protected function staticDereferenceLhsRequiresParens(Node $node): bool {
|
||||
if ($node instanceof Expr\New_) {
|
||||
return !$this->phpVersion->supportsNewDereferenceWithoutParentheses();
|
||||
}
|
||||
return !($node instanceof Expr\Variable
|
||||
|| $node instanceof Node\Name
|
||||
|| $node instanceof Expr\ArrayDimFetch
|
||||
@@ -1345,7 +1384,7 @@ abstract class PrettyPrinterAbstract implements PrettyPrinter {
|
||||
BinaryOp\NotIdentical::class, BinaryOp\Spaceship::class, BinaryOp\BitwiseAnd::class,
|
||||
BinaryOp\BitwiseXor::class, BinaryOp\BitwiseOr::class, BinaryOp\BooleanAnd::class,
|
||||
BinaryOp\BooleanOr::class, BinaryOp\Coalesce::class, BinaryOp\LogicalAnd::class,
|
||||
BinaryOp\LogicalXor::class, BinaryOp\LogicalOr::class,
|
||||
BinaryOp\LogicalXor::class, BinaryOp\LogicalOr::class, BinaryOp\Pipe::class,
|
||||
];
|
||||
foreach ($binaryOps as $binaryOp) {
|
||||
$this->fixupMap[$binaryOp] = [
|
||||
@@ -1607,6 +1646,7 @@ abstract class PrettyPrinterAbstract implements PrettyPrinter {
|
||||
Stmt\Trait_::class . '->attrGroups' => [null, '', "\n"],
|
||||
Expr\ArrowFunction::class . '->attrGroups' => [null, '', ' '],
|
||||
Expr\Closure::class . '->attrGroups' => [null, '', ' '],
|
||||
Stmt\Const_::class . '->attrGroups' => [null, '', "\n"],
|
||||
PrintableNewAnonClassNode::class . '->attrGroups' => [\T_NEW, ' ', ''],
|
||||
|
||||
/* These cannot be empty to start with:
|
||||
@@ -1645,15 +1685,15 @@ abstract class PrettyPrinterAbstract implements PrettyPrinter {
|
||||
}
|
||||
|
||||
$this->modifierChangeMap = [
|
||||
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],
|
||||
PrintableNewAnonClassNode::class . '->flags' => ['pModifiers', \T_CLASS],
|
||||
Param::class . '->flags' => ['pModifiers', \T_VARIABLE],
|
||||
PropertyHook::class . '->flags' => ['pModifiers', \T_STRING],
|
||||
Expr\Closure::class . '->static' => ['pStatic', \T_FUNCTION],
|
||||
Expr\ArrowFunction::class . '->static' => ['pStatic', \T_FN],
|
||||
Stmt\ClassConst::class . '->flags' => ['pModifiers', \T_WHITESPACE, \T_CONST],
|
||||
Stmt\ClassMethod::class . '->flags' => ['pModifiers', \T_WHITESPACE, \T_FUNCTION],
|
||||
Stmt\Class_::class . '->flags' => ['pModifiers', \T_WHITESPACE, \T_CLASS],
|
||||
Stmt\Property::class . '->flags' => ['pModifiers', \T_WHITESPACE, \T_VARIABLE],
|
||||
PrintableNewAnonClassNode::class . '->flags' => ['pModifiers', \T_NEW, \T_CLASS],
|
||||
Param::class . '->flags' => ['pModifiers', \T_WHITESPACE, \T_VARIABLE],
|
||||
PropertyHook::class . '->flags' => ['pModifiers', \T_WHITESPACE, \T_STRING],
|
||||
Expr\Closure::class . '->static' => ['pStatic', \T_WHITESPACE, \T_FUNCTION],
|
||||
Expr\ArrowFunction::class . '->static' => ['pStatic', \T_WHITESPACE, \T_FN],
|
||||
//Stmt\TraitUseAdaptation\Alias::class . '->newModifier' => 0, // TODO
|
||||
];
|
||||
|
||||
|
||||
Reference in new issue
Block a user