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
1 change: 1 addition & 0 deletions config/set/php/php80.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ services:
Rector\Php80\Rector\Class_\AnnotationToAttributeRector: null
Rector\Php80\Rector\FuncCall\ClassOnObjectRector: null
Rector\Php80\Rector\Ternary\GetDebugTypeRector: null
Rector\Php80\Rector\FuncCall\TokenGetAllToObjectRector: null
3 changes: 3 additions & 0 deletions ecs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,7 @@ parameters:
# part of the comparison logic
- 'packages/polyfill/src/ConditionEvaluator.php'

# often soo many cases that need manual attention → skip and resole in automated way
SlevomatCodingStandard\Sniffs\Namespaces\ReferenceUsedNamesOnlySniff.PartialUse: null

line_ending: "\n"
37 changes: 36 additions & 1 deletion packages/post-rector/src/Rector/NodeRemovingRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
namespace Rector\PostRector\Rector;

use PhpParser\Node;
use PhpParser\Node\Expr\BinaryOp;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\NodeTraverser;
use Rector\Core\PhpParser\Node\NodeFactory;
use Rector\Core\RectorDefinition\RectorDefinition;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PostRector\Collector\NodesToRemoveCollector;

final class NodeRemovingRector extends AbstractPostRector
Expand Down Expand Up @@ -64,7 +66,11 @@ public function enterNode(Node $node): ?Node
return $this->nodeFactory->createMethodCall($nestedMethodCall->var, $methodName, $node->args);
}

return null;
if (! $node instanceof BinaryOp) {
return null;
}

return $this->removePartOfBinaryOp($node);
}

/**
Expand All @@ -73,6 +79,11 @@ public function enterNode(Node $node): ?Node
public function leaveNode(Node $node)
{
foreach ($this->nodesToRemoveCollector->getNodesToRemove() as $key => $nodeToRemove) {
$nodeToRemoveParent = $nodeToRemove->getAttribute(AttributeKey::PARENT_NODE);
if ($nodeToRemoveParent instanceof BinaryOp) {
continue;
}

if ($node === $nodeToRemove) {
$this->nodesToRemoveCollector->unset($key);

Expand Down Expand Up @@ -101,4 +112,28 @@ private function isChainMethodCallNodeToBeRemoved(Node $node, Node $nodeToRemove

return $methodName !== null;
}

private function removePartOfBinaryOp(BinaryOp $binaryOp): ?Node
{
// handle left/right binary remove, e.g. "true && false" → remove false → "true"
foreach ($this->nodesToRemoveCollector->getNodesToRemove() as $key => $nodeToRemove) {
// remove node
$nodeToRemoveParentNode = $nodeToRemove->getAttribute(AttributeKey::PARENT_NODE);
if (! $nodeToRemoveParentNode instanceof BinaryOp) {
continue;
}

if ($binaryOp->left === $nodeToRemove) {
$this->nodesToRemoveCollector->unset($key);
return $binaryOp->right;
}

if ($binaryOp->right === $nodeToRemove) {
$this->nodesToRemoveCollector->unset($key);
return $binaryOp->left;
}
}

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function test(string $file): void
*/
public function provideDataForTest(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixtures');
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

protected function getRectorClass(): string
Expand Down
Loading