Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@ class KeepDocblockOnReturn
{
public function run()
{
fn() =>

/** @psalm-suppress UndefinedFunction */
ff();

fn() =>
fn() => ff();


// @psalm-suppress UndefinedFunction
ff();

fn() => ff();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ class WithEqualVarDocType
{
public function run()
{
fn(string $var) =>

/** @var string $var */
$var;

fn(string $var) => $var;
}
}

?>
?>
49 changes: 12 additions & 37 deletions src/PhpParser/Printer/BetterStandardPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\Ternary;
use PhpParser\Node\Expr\Yield_;
use PhpParser\Node\InterpolatedStringPart;
use PhpParser\Node\Scalar\InterpolatedString;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\Declare_;
Expand Down Expand Up @@ -113,14 +112,6 @@ public function pFileWithoutNamespace(FileWithoutNamespace $fileWithoutNamespace
return $this->pStmts($fileWithoutNamespace->stmts);
}

/**
* @api magic method in parent
*/
public function pInterpolatedStringPart(InterpolatedStringPart $interpolatedStringPart): string
{
return $interpolatedStringPart->value;
}

protected function p(
Node $node,
int $precedence = self::MAX_PRECEDENCE,
Expand All @@ -129,14 +120,12 @@ protected function p(
): string {
// handle already AlwaysRememberedExpr
// @see https://github.com/rectorphp/rector/issues/8815#issuecomment-2503453191
while ($node instanceof AlwaysRememberedExpr) {
$node = $node->getExpr();
if ($node instanceof AlwaysRememberedExpr) {
return $this->p($node->getExpr(), $precedence, $lhsPrecedence, $parentFormatPreserved);
}

// handle overlapped origNode is Match_
// and its subnodes still have AlwaysRememberedExpr
// handle overlapped origNode is Match_ and its subnodes still have AlwaysRememberedExpr
$originalNode = $node->getAttribute(AttributeKey::ORIGINAL_NODE);

if ($originalNode instanceof Match_) {
$subNodeNames = $node->getSubNodeNames();
foreach ($subNodeNames as $subNodeName) {
Expand All @@ -146,17 +135,20 @@ protected function p(
}
}

$this->wrapBinaryOp($node);
$this->wrapBinaryOpWithBrackets($node);

$content = parent::p($node, $precedence, $lhsPrecedence, $parentFormatPreserved);

// remove once its fixed in php-parser, https://github.com/nikic/PHP-Parser/pull/1126
if ($node instanceof CallLike) {
$this->cleanVariadicPlaceHolderTrailingComma($node);
}

return $node->getAttribute(AttributeKey::WRAPPED_IN_PARENTHESES) === true
? ('(' . $content . ')')
: $content;
if ($node->getAttribute(AttributeKey::WRAPPED_IN_PARENTHESES)) {
return '(' . $content . ')';
}

return $content;
}

protected function pExpr_ArrowFunction(ArrowFunction $arrowFunction, int $precedence, int $lhsPrecedence): string
Expand All @@ -169,7 +161,6 @@ protected function pExpr_ArrowFunction(ArrowFunction $arrowFunction, int $preced

/** @var Comment[] $comments */
$comments = $expr->getAttribute(AttributeKey::COMMENTS) ?? [];

if ($comments === []) {
return parent::pExpr_ArrowFunction($arrowFunction, $precedence, $lhsPrecedence);
}
Expand All @@ -182,23 +173,7 @@ protected function pExpr_ArrowFunction(ArrowFunction $arrowFunction, int $preced
$text .= $commentText . "\n";
}

return $this->pPrefixOp(
ArrowFunction::class,
$this->pAttrGroups($arrowFunction->attrGroups, true)
. $this->pStatic($arrowFunction->static)
. 'fn' . ($arrowFunction->byRef ? '&' : '')
. '(' . $this->pMaybeMultiline(
$arrowFunction->params,
$this->phpVersion->supportsTrailingCommaInParamList()
) . ')'
. ($arrowFunction->returnType instanceof Node ? ': ' . $this->p($arrowFunction->returnType) : '')
. ' =>'
. $text
. $indent,
$arrowFunction->expr,
$precedence,
$lhsPrecedence
);
return $text . "\n" . $indent . parent::pExpr_ArrowFunction($arrowFunction, $precedence, $lhsPrecedence);
}

/**
Expand Down Expand Up @@ -463,7 +438,7 @@ private function cleanVariadicPlaceHolderTrailingComma(CallLike $callLike): void
}
}

private function wrapBinaryOp(Node $node): void
private function wrapBinaryOpWithBrackets(Node $node): void
{
if ($this->exprAnalyzer->isExprWithExprPropertyWrappable($node)) {
$node->expr->setAttribute(AttributeKey::ORIGINAL_NODE, null);
Expand Down