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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"rector/type-perfect": "^2.1",
"shipmonk/composer-dependency-analyser": "^1.8",
"symplify/phpstan-extensions": "^12.0.2",
"symplify/phpstan-rules": "^14.9.3",
"symplify/phpstan-rules": "^14.9.4",
"symplify/vendor-patches": "^11.5",
"tomasvotruba/class-leak": "^2.1",
"tomasvotruba/unused-public": "^2.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@

namespace Rector\Tests\CodeQuality\Rector\Stmt\DeadCodeRemovingRector\Fixture\ArrayDimFetch;

function wrapToPreventPhpStanCallingMethods ()
function wrapToPreventPhpStanCallingMethods()
{
$var[0] = 1;

$var[0];

//comment
//comment 1
$var[methodCall()];

//comment
//comment 2
${methodCall()}[0];

//comment
//comment 3
${methodCall1()}[methodCall2()];
}
?>
Expand All @@ -23,19 +23,18 @@ function wrapToPreventPhpStanCallingMethods ()

namespace Rector\Tests\CodeQuality\Rector\Stmt\DeadCodeRemovingRector\Fixture\ArrayDimFetch;

function wrapToPreventPhpStanCallingMethods ()
function wrapToPreventPhpStanCallingMethods()
{
$var[0] = 1;

//comment
//comment 1
methodCall();

//comment
//comment 2
methodCall();
//comment
methodCall2();

//comment
//comment 3
methodCall1();
}
?>
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ class KeepDocblockOnReturn
/** @psalm-suppress UndefinedFunction */
return ff();
};

function() {
// @psalm-suppress UndefinedFunction
return ff();
};
}
}

Expand All @@ -26,6 +31,10 @@ class KeepDocblockOnReturn
fn() =>
/** @psalm-suppress UndefinedFunction */
ff();

fn() =>
// @psalm-suppress UndefinedFunction
ff();
}
}

Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

10 changes: 3 additions & 7 deletions rules/DeadCode/Rector/Expression/RemoveDeadStmtRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@

namespace Rector\DeadCode\Rector\Expression;

use PhpParser\Comment\Doc;
use PhpParser\Node;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticPropertyFetch;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Nop;
use PhpParser\NodeVisitor;
use PHPStan\Reflection\Php\PhpPropertyReflection;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\DeadCode\NodeManipulator\LivingCodeManipulator;
use Rector\NodeAnalyzer\PropertyFetchAnalyzer;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Rector\AbstractRector;
use Rector\Reflection\ReflectionResolver;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
Expand All @@ -29,7 +28,6 @@ public function __construct(
private readonly LivingCodeManipulator $livingCodeManipulator,
private readonly PropertyFetchAnalyzer $propertyFetchAnalyzer,
private readonly ReflectionResolver $reflectionResolver,
private readonly PhpDocInfoFactory $phpDocInfoFactory
) {
}

Expand Down Expand Up @@ -111,11 +109,9 @@ private function hasGetMagic(Expression $expression): bool
*/
private function removeNodeAndKeepComments(Expression $expression): int|Node
{
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($expression);

if ($expression->getComments() !== []) {
if ($expression->getDocComment() instanceof Doc) {
$nop = new Nop();
$nop->setAttribute(AttributeKey::PHP_DOC_INFO, $phpDocInfo);
$nop->setDocComment($expression->getDocComment());

return $nop;
}
Expand Down
12 changes: 7 additions & 5 deletions rules/DeadCode/Rector/If_/RemoveAlwaysTrueIfConditionRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,13 @@ public function refactor(Node $node): int|null|array|If_
return NodeVisitor::REMOVE_NODE;
}

$node->stmts[0]->setAttribute(AttributeKey::COMMENTS, array_merge(
$node->getComments(),
$node->stmts[0]->getComments(),
));
$node->stmts[0]->setAttribute(AttributeKey::HAS_MERGED_COMMENTS, true);
// keep original comments
if ($node->getComments() !== []) {
$node->stmts[0]->setAttribute(AttributeKey::COMMENTS, array_merge(
$node->getComments(),
$node->stmts[0]->getComments(),
));
}

return $node->stmts;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Rector\DeadCode\ConditionEvaluator;
use Rector\DeadCode\ConditionResolver;
use Rector\DeadCode\Contract\ConditionInterface;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand Down Expand Up @@ -100,7 +101,7 @@ private function refactorIsMatch(If_ $if): ?array
return null;
}

return $if->stmts;
return $this->keepIfLevelComments($if, $if->stmts);
}

/**
Expand All @@ -113,7 +114,22 @@ private function refactorIsNotMatch(If_ $if): array|int
return NodeVisitor::REMOVE_NODE;
}

// else is always used
return $if->else->stmts;
return $this->keepIfLevelComments($if, $if->else->stmts);
}

/**
* @param Stmt[] $stmts
* @return Stmt[]
*/
private function keepIfLevelComments(If_ $if, array $stmts): array
{
if ($stmts !== []) {
$stmts[0]->setAttribute(
AttributeKey::COMMENTS,
array_merge($if->getComments(), $stmts[0]->getComments())
);
}

return $stmts;
}
}
7 changes: 2 additions & 5 deletions rules/Php72/Rector/FuncCall/GetClassOnNullRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function refactor(Node $node): ?Node
}

// just created func call
if ($node->getAttribute(AttributeKey::DO_NOT_CHANGE) === true) {
if ($node->getAttribute(AttributeKey::ORIGINAL_NODE) === null) {
return null;
}

Expand Down Expand Up @@ -124,9 +124,6 @@ public function refactor(Node $node): ?Node

private function createGetClassFuncCall(FuncCall $oldFuncCall): FuncCall
{
$funcCall = new FuncCall($oldFuncCall->name, $oldFuncCall->args);
$funcCall->setAttribute(AttributeKey::DO_NOT_CHANGE, true);

return $funcCall;
return new FuncCall($oldFuncCall->name, $oldFuncCall->args);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function refactor(Node $node): ?Node
$comments = $node->stmts[0]->getAttribute(AttributeKey::COMMENTS) ?? [];
if ($comments !== []) {
$this->mirrorComments($arrowFunction->expr, $node->stmts[0]);
$arrowFunction->setAttribute(AttributeKey::COMMENT_CLOSURE_RETURN_MIRRORED, true);
$arrowFunction->setAttribute(AttributeKey::COMMENTS, true);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TomasVotruba this seems cause crash as will cause ->getComments() that returns comments attribute that has array native return, see issue:

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

}

return $arrowFunction;
Expand Down
16 changes: 1 addition & 15 deletions src/NodeTypeResolver/Node/AttributeKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,6 @@ final class AttributeKey
*/
public const IS_REGULAR_PATTERN = 'is_regular_pattern';

/**
* @var string
*/
public const DO_NOT_CHANGE = 'do_not_change';

/**
* Helps with infinite loop detection
* @var string
Expand All @@ -97,11 +92,6 @@ final class AttributeKey
*/
public const WRAPPED_IN_PARENTHESES = 'wrapped_in_parentheses';

/**
* @var string
*/
public const COMMENT_CLOSURE_RETURN_MIRRORED = 'comment_closure_return_mirrored';

/**
* To pass PHP 8.0 attribute FQN names
* @var string
Expand All @@ -114,6 +104,7 @@ final class AttributeKey
public const EXTRA_USE_IMPORT = 'extra_use_import';

/**
* Used internally by php-parser
* @var string
*/
public const DOC_LABEL = 'docLabel';
Expand Down Expand Up @@ -266,11 +257,6 @@ final class AttributeKey
*/
public const IS_FIRST_LEVEL_STATEMENT = 'first_level_stmt';

/**
* @var string
*/
public const HAS_MERGED_COMMENTS = 'has_merged_comments';

public const IS_DEFAULT_PROPERTY_VALUE = 'is_default_property_value';

public const IS_CLASS_CONST_VALUE = 'is_default_class_const_value';
Expand Down
2 changes: 1 addition & 1 deletion src/PhpParser/Printer/BetterStandardPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ protected function p(

protected function pExpr_ArrowFunction(ArrowFunction $arrowFunction, int $precedence, int $lhsPrecedence): string
{
if (! $arrowFunction->hasAttribute(AttributeKey::COMMENT_CLOSURE_RETURN_MIRRORED)) {
if (! $arrowFunction->hasAttribute(AttributeKey::COMMENTS)) {
return parent::pExpr_ArrowFunction($arrowFunction, $precedence, $lhsPrecedence);
}

Expand Down
6 changes: 0 additions & 6 deletions src/Rector/AbstractRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,12 +291,6 @@ private function postRefactorProcess(
$currentScope = $node->getAttribute(AttributeKey::SCOPE);

if (is_array($refactoredNode)) {
$firstNode = current($refactoredNode);

if ($firstNode->getAttribute(AttributeKey::HAS_MERGED_COMMENTS, false) === false) {
$this->mirrorComments($firstNode, $originalNode);
}

$this->refreshScopeNodes($refactoredNode, $filePath, $currentScope);

// search "infinite recursion" in https://github.com/nikic/PHP-Parser/blob/master/doc/component/Walking_the_AST.markdown
Expand Down