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/dead-code/dead-code.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ services:

Rector\CodeQuality\Rector\Return_\SimplifyUselessVariableRector: ~
Rector\DeadCode\Rector\Plus\RemoveZeroAndOneBinaryRector: ~
Rector\DeadCode\Rector\ClassMethod\RemoveDelegatingParentCallRector: ~
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php declare(strict_types=1);

namespace Rector\DeadCode\Rector\ClassMethod;

use PhpParser\Node;
use PhpParser\Node\Stmt\ClassMethod;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;

final class RemoveDelegatingParentCallRector extends AbstractRector
{
public function getDefinition(): RectorDefinition
{
return new RectorDefinition('', [
new CodeSample(
<<<'CODE_SAMPLE'
class SomeClass
{
public function prettyPrint(array $stmts): string
{
return parent::prettyPrint($stmts);
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class SomeClass
{
}
CODE_SAMPLE
),
]);
}

/**
* @return string[]
*/
public function getNodeTypes(): array
{
return [ClassMethod::class];
}

/**
* @param ClassMethod $node
*/
public function refactor(Node $node): ?Node
{
if (count((array) $node->stmts) !== 1) {
return null;
}

if ($node->stmts[0] instanceof Node\Stmt\Expression) {
$onlyStmt = $node->stmts[0]->expr;
} else {
$onlyStmt = $node->stmts[0];
}

// are both return?
if ($node->returnType && ! $this->isName(
$node->returnType,
'void'
) && ! $onlyStmt instanceof Node\Stmt\Return_) {
return null;
}

/** @var Node $onlyStmt */
$staticCall = $this->matchStaticCall($onlyStmt);

if (! $this->isParentCallMatching($node, $staticCall)) {
return null;
}

// the method is just delegation, nothing extra
$this->removeNode($node);

return null;
}

private function matchStaticCall(Node $node): ?Node\Expr\StaticCall
{
// must be static call
if ($node instanceof Node\Stmt\Return_) {
if ($node->expr instanceof Node\Expr\StaticCall) {
return $node->expr;
}

return null;
}

if ($node instanceof Node\Expr\StaticCall) {
return $node;
}

return null;
}

private function isParentCallMatching(ClassMethod $classMethod, ?Node\Expr\StaticCall $staticCall): bool
{
if ($staticCall === null) {
return false;
}

if (! $this->areNamesEqual($staticCall, $classMethod)) {
return false;
}

if (! $this->isName($staticCall->class, 'parent')) {
return false;
}

// are arguments the same?
if (count($staticCall->args) !== count($classMethod->params)) {
return false;
}

return true;
}
}
162 changes: 162 additions & 0 deletions packages/DeadCode/src/Rector/Plus/RemoveZeroAndOneBinaryRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
<?php declare(strict_types=1);

namespace Rector\DeadCode\Rector\Plus;

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\AssignOp;
use PhpParser\Node\Expr\BinaryOp;
use PhpParser\Node\Expr\BinaryOp\Div;
use PhpParser\Node\Expr\BinaryOp\Minus;
use PhpParser\Node\Expr\BinaryOp\Mul;
use PhpParser\Node\Expr\BinaryOp\Plus;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;

/**
* @see https://3v4l.org/I0BGs
*/
final class RemoveZeroAndOneBinaryRector extends AbstractRector
{
public function getDefinition(): RectorDefinition
{
return new RectorDefinition('', [
new CodeSample(
<<<'CODE_SAMPLE'
class SomeClass
{
public function run()
{
$value = 5 * 1;
$value = 5 + 0;
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class SomeClass
{
public function run()
{
$value = 5;
$value = 5;
}
}
CODE_SAMPLE
),
]);
}

/**
* @return string[]
*/
public function getNodeTypes(): array
{
return [
Plus::class,
Minus::class,
Mul::class,
Div::class,
Node\Expr\AssignOp\Plus::class,
Node\Expr\AssignOp\Minus::class,
Node\Expr\AssignOp\Mul::class,
Node\Expr\AssignOp\Div::class,
];
}

/**
* @param AssignOp|BinaryOp $node
*/
public function refactor(Node $node): ?Node
{
if ($node instanceof AssignOp) {
return $this->processAssignOp($node);
}

// -, +
if ($node instanceof BinaryOp) {
return $this->processBinaryOp($node);
}
}

/**
* @param Plus|Minus $binaryOp
*/
private function processBinaryPlusAndMinus(BinaryOp $binaryOp): ?Expr
{
if ($this->isValue($binaryOp->left, 0)) {
if ($this->isNumberType($binaryOp->right)) {
return $binaryOp->right;
}
}

if ($this->isValue($binaryOp->right, 0)) {
if ($this->isNumberType($binaryOp->left)) {
return $binaryOp->left;
}
}

return null;
}

/**
* @param Mul|Div $binaryOp
*/
private function processBinaryMulAndDiv(BinaryOp $binaryOp): ?Expr
{
if ($this->isValue($binaryOp->left, 1)) {
if ($this->isNumberType($binaryOp->right)) {
return $binaryOp->right;
}
}

if ($this->isValue($binaryOp->right, 1)) {
if ($this->isNumberType($binaryOp->left)) {
return $binaryOp->left;
}
}

return null;
}

private function processAssignOp(Node $node): ?Expr
{
// +=, -=
if ($node instanceof Node\Expr\AssignOp\Plus || $node instanceof Node\Expr\AssignOp\Minus) {
if (! $this->isValue($node->expr, 0)) {
return null;
}

if ($this->isNumberType($node->var)) {
return $node->var;
}
}

// *, /
if ($node instanceof Node\Expr\AssignOp\Mul || $node instanceof Node\Expr\AssignOp\Div) {
if (! $this->isValue($node->expr, 1)) {
return null;
}
if ($this->isNumberType($node->var)) {
return $node->var;
}
}

return null;
}

private function processBinaryOp(Node $node): ?Expr
{
if ($node instanceof Plus || $node instanceof Minus) {
return $this->processBinaryPlusAndMinus($node);
}

// *, /
if ($node instanceof Mul || $node instanceof Div) {
return $this->processBinaryMulAndDiv($node);
}

return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Rector\DeadCode\Tests\Rector\ClassMethod\RemoveDelegatingParentCallRector\Fixture;

class SomeClass
{
public function prettyPrint(array $stmts): string
{
return parent::prettyPrint($stmts);
}

public function process(array $stmts): void
{
parent::process($stmts);
}
}

?>
-----
<?php

namespace Rector\DeadCode\Tests\Rector\ClassMethod\RemoveDelegatingParentCallRector\Fixture;

class SomeClass
{
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Rector\DeadCode\Tests\Rector\ClassMethod\RemoveDelegatingParentCallRector\Fixture;

class SkipChangedArguments
{
public function prettyPrint(array $stmts): string
{
return parent::prettyPrint($stmts, false);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Rector\DeadCode\Tests\Rector\ClassMethod\RemoveDelegatingParentCallRector\Fixture;

class SkipDifferentMethodName
{
public function prettyPrint(array $stmts): string
{
return parent::prettyPrintThat($stmts);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Rector\DeadCode\Tests\Rector\ClassMethod\RemoveDelegatingParentCallRector\Fixture;

class SkipExtraContent
{
public function prettyPrint(array $stmts): string
{
$stmts = $stmts + [1];

return parent::prettyPrint($stmts);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php declare(strict_types=1);

namespace Rector\DeadCode\Tests\Rector\ClassMethod\RemoveDelegatingParentCallRector;

use Rector\DeadCode\Rector\ClassMethod\RemoveDelegatingParentCallRector;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class RemoveDelegatingParentCallRectorTest extends AbstractRectorTestCase
{
public function test(): void
{
$this->doTestFiles([
__DIR__ . '/Fixture/fixture.php.inc',
__DIR__ . '/Fixture/skip_extra_content.php.inc',
__DIR__ . '/Fixture/skip_different_method_name.php.inc',
__DIR__ . '/Fixture/skip_changed_arguments.php.inc',
]);
}

protected function getRectorClass(): string
{
return RemoveDelegatingParentCallRector::class;
}
}
Loading