mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-25 04:12:36 +00:00
keep visually similar lines in SimplifyUselessVariableRector
This commit is contained in:
parent
f7bf21fec3
commit
665e58a7fa
|
@ -1,6 +1,6 @@
|
|||
services:
|
||||
Rector\CodeQuality\Rector\Assign\CombinedAssignRector: ~
|
||||
Rector\CodeQuality\Rector\BinaryOp\SimplifyDeMorganBinaryRector: ~
|
||||
# Rector\CodeQuality\Rector\BinaryOp\SimplifyDeMorganBinaryRector: ~
|
||||
Rector\CodeQuality\Rector\BooleanAnd\SimplifyEmptyArrayCheckRector: ~
|
||||
Rector\CodeQuality\Rector\Expression\SimplifyMirrorAssignRector: ~
|
||||
Rector\CodeQuality\Rector\Foreach_\ForeachToInArrayRector: ~
|
||||
|
|
|
@ -114,6 +114,29 @@ CODE_SAMPLE
|
|||
}
|
||||
|
||||
// is the same variable
|
||||
return ! $this->areNodesEqual($previousNode->var, $variableNode);
|
||||
if (! $this->areNodesEqual($previousNode->var, $variableNode)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($this->isPreviousExpressionVisuallySimilar($previousExpression, $previousNode)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param AssignOp|Assign $previousNode
|
||||
*/
|
||||
private function isPreviousExpressionVisuallySimilar(Expression $previousExpression, Node $previousNode): bool
|
||||
{
|
||||
$prePreviousExpression = $previousExpression->getAttribute(Attribute::PREVIOUS_EXPRESSION);
|
||||
if ($prePreviousExpression instanceof Expression && $prePreviousExpression->expr instanceof AssignOp) {
|
||||
if ($this->areNodesEqual($prePreviousExpression->expr->var, $previousNode->var)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\CodeQuality\Tests\Rector\Return_\SimplifyUselessVariableRector\Fixture;
|
||||
|
||||
// covers https://github.com/Sylius/Sylius/pull/9902/files#r231437165
|
||||
function simplifyUselessVariable5()
|
||||
{
|
||||
$shippingTotal = getAdjustmentsTotal(AdjustmentInterface::SHIPPING_ADJUSTMENT);
|
||||
$shippingTotal += getAdjustmentsTotal(AdjustmentInterface::ORDER_SHIPPING_PROMOTION_ADJUSTMENT);
|
||||
$shippingTotal += getAdjustmentsTotal(AdjustmentInterface::TAX_ADJUSTMENT);
|
||||
|
||||
return $shippingTotal;
|
||||
}
|
|
@ -14,14 +14,13 @@ final class SimplifyUselessVariableRectorTest extends AbstractRectorTestCase
|
|||
{
|
||||
public function test(): void
|
||||
{
|
||||
$this->doTestFiles(
|
||||
[
|
||||
__DIR__ . '/Fixture/fixture.php.inc',
|
||||
__DIR__ . '/Fixture/fixture2.php.inc',
|
||||
__DIR__ . '/Fixture/fixture3.php.inc',
|
||||
__DIR__ . '/Fixture/fixture4.php.inc',
|
||||
]
|
||||
);
|
||||
$this->doTestFiles([
|
||||
__DIR__ . '/Fixture/fixture.php.inc',
|
||||
__DIR__ . '/Fixture/fixture2.php.inc',
|
||||
__DIR__ . '/Fixture/fixture3.php.inc',
|
||||
__DIR__ . '/Fixture/fixture4.php.inc',
|
||||
__DIR__ . '/Fixture/keep_visual.php.inc',
|
||||
]);
|
||||
}
|
||||
|
||||
public function getRectorClass(): string
|
||||
|
|
Loading…
Reference in New Issue
Block a user