diff --git a/rules-tests/Php74/Rector/Closure/ClosureToArrowFunctionRector/Fixture/keep_docblock_on_return.php.inc b/rules-tests/Php74/Rector/Closure/ClosureToArrowFunctionRector/Fixture/keep_docblock_on_return.php.inc index d2e332b5d02..810e66e27d1 100644 --- a/rules-tests/Php74/Rector/Closure/ClosureToArrowFunctionRector/Fixture/keep_docblock_on_return.php.inc +++ b/rules-tests/Php74/Rector/Closure/ClosureToArrowFunctionRector/Fixture/keep_docblock_on_return.php.inc @@ -28,13 +28,15 @@ class KeepDocblockOnReturn { public function run() { - fn() => + /** @psalm-suppress UndefinedFunction */ - ff(); - fn() => + fn() => ff(); + + // @psalm-suppress UndefinedFunction - ff(); + + fn() => ff(); } } diff --git a/rules-tests/Php74/Rector/Closure/ClosureToArrowFunctionRector/Fixture/with_equal_var_doc_type.php.inc b/rules-tests/Php74/Rector/Closure/ClosureToArrowFunctionRector/Fixture/with_equal_var_doc_type.php.inc index a4cce8e9692..f7b0535b810 100644 --- a/rules-tests/Php74/Rector/Closure/ClosureToArrowFunctionRector/Fixture/with_equal_var_doc_type.php.inc +++ b/rules-tests/Php74/Rector/Closure/ClosureToArrowFunctionRector/Fixture/with_equal_var_doc_type.php.inc @@ -23,10 +23,11 @@ class WithEqualVarDocType { public function run() { - fn(string $var) => + /** @var string $var */ - $var; + + fn(string $var) => $var; } } -?> \ No newline at end of file +?> diff --git a/src/PhpParser/Printer/BetterStandardPrinter.php b/src/PhpParser/Printer/BetterStandardPrinter.php index caa9c211a4f..df7a233bb5d 100644 --- a/src/PhpParser/Printer/BetterStandardPrinter.php +++ b/src/PhpParser/Printer/BetterStandardPrinter.php @@ -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_; @@ -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, @@ -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) { @@ -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 @@ -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); } @@ -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); } /** @@ -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);