Skip to content

Commit

Permalink
[Php74] Mirror comment on Closure return to ArrowFunction expr on Clo…
Browse files Browse the repository at this point in the history
…sureToArrowFunctionRector (#1677)

Co-authored-by: Bruce Weirdan <weirdan@gmail.com>
Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
3 people committed Jan 14, 2022
1 parent 0947481 commit a17759c
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 4 deletions.
5 changes: 5 additions & 0 deletions packages/NodeTypeResolver/Node/AttributeKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,4 +225,9 @@ final class AttributeKey
* @var string
*/
final public const WRAPPED_IN_PARENTHESES = 'wrapped_in_parentheses';

/**
* @var string
*/
final public const COMMENT_CLOSURE_RETURN_MIRRORED = 'comment_closure_return_mirrored';
}
8 changes: 4 additions & 4 deletions packages/NodeTypeResolver/TypeComparator/TypeComparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,14 +263,14 @@ private function areTypesSameWithLiteralTypeInPhpDoc(
bool $areDifferentScalarTypes,
Type $phpStanDocType,
Type $phpParserNodeType
): bool
{
): bool {
return $areDifferentScalarTypes
&& $phpStanDocType instanceof ConstantScalarType
&& $phpParserNodeType->isSuperTypeOf($phpStanDocType)->yes();
&& $phpParserNodeType->isSuperTypeOf($phpStanDocType)
->yes();
}

private function isThisTypeInFinalClass(Type $phpStanDocType, Type $phpParserNodeType, Node $node) : bool
private function isThisTypeInFinalClass(Type $phpStanDocType, Type $phpParserNodeType, Node $node): bool
{
// special case for non-final $this/self compare; in case of interface/abstract class, it can be another $this
if ($phpStanDocType instanceof ThisType && $phpParserNodeType instanceof ThisType) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

function() {
/** @psalm-suppress UndefinedFunction */
return ff();
};
?>
-----
<?php

fn() => /** @psalm-suppress UndefinedFunction */
ff();
?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

function() {
// @psalm-suppress UndefinedFunction
return ff();
};
?>
-----
<?php

fn() => // @psalm-suppress UndefinedFunction
ff();
?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

function() {
/**
* comment
*/
// something
// @psalm-suppress UndefinedFunction
return ff();
};
?>
-----
<?php

fn() => /**
* comment
*/
// something
// @psalm-suppress UndefinedFunction
ff();
?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

fn() =>
/**
* comment
*/
// something
// @psalm-suppress UndefinedFunction
ff();
7 changes: 7 additions & 0 deletions rules/Php74/Rector/Closure/ClosureToArrowFunctionRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use PhpParser\Node\Expr\Closure;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Php74\NodeAnalyzer\ClosureArrowFunctionAnalyzer;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
Expand Down Expand Up @@ -87,6 +88,12 @@ public function refactor(Node $node): ?Node
$arrowFunction->static = true;
}

$comments = $node->stmts[0]->getAttribute(AttributeKey::COMMENTS) ?? [];
if ($comments !== []) {
$this->mirrorComments($arrowFunction->expr, $node->stmts[0]);
$arrowFunction->setAttribute(AttributeKey::COMMENT_CLOSURE_RETURN_MIRRORED, true);
}

return $arrowFunction;
}

Expand Down
35 changes: 35 additions & 0 deletions src/PhpParser/Printer/BetterStandardPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

use Nette\Utils\Strings;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ArrowFunction;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\Ternary;
Expand Down Expand Up @@ -148,6 +150,10 @@ protected function p(Node $node, $parentFormatPreserved = false): string
{
$content = parent::p($node, $parentFormatPreserved);

if ($node instanceof Expr) {
$content = $this->resolveContentOnExpr($node, $content);
}

return $node->getAttribute(AttributeKey::WRAPPED_IN_PARENTHESES) === true
? ('(' . $content . ')')
: $content;
Expand Down Expand Up @@ -437,6 +443,35 @@ protected function pCommaSeparated(array $nodes): string
return $result;
}

private function resolveContentOnExpr(Expr $expr, string $content): string
{
$parentNode = $expr->getAttribute(AttributeKey::PARENT_NODE);
if (! $parentNode instanceof ArrowFunction) {
return $content;
}

if ($parentNode->expr !== $expr) {
return $content;
}

if (! $parentNode->hasAttribute(AttributeKey::COMMENT_CLOSURE_RETURN_MIRRORED)) {
return $content;
}

$comments = $expr->getAttribute(AttributeKey::COMMENTS) ?? [];

if ($comments === []) {
return $content;
}

$text = '';
foreach ($comments as $comment) {
$text .= $comment->getText() . "\n";
}

return $content = $text . $content;
}

/**
* @param Node[] $stmts
* @return Node[]|mixed[]
Expand Down

0 comments on commit a17759c

Please sign in to comment.