Skip to content

Commit 607e6cd

Browse files
committed
enable rule
1 parent ce32d57 commit 607e6cd

File tree

4 files changed

+89
-29
lines changed

4 files changed

+89
-29
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveParentDelegatingConstructorRector\Fixture;
4+
5+
use PhpParser\Node\Scalar\String_;
6+
7+
final class SomeDifferentArgs extends String_
8+
{
9+
public function __construct($value, $attributes)
10+
{
11+
parent::__construct($value, []);
12+
}
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveParentDelegatingConstructorRector\Fixture;
4+
5+
use PhpParser\Node\Scalar\String_;
6+
7+
final class SkipDifferntCount extends String_
8+
{
9+
public function __construct($value)
10+
{
11+
parent::__construct($value, []);
12+
}
13+
}

rules/DeadCode/Rector/ClassMethod/RemoveParentDelegatingConstructorRector.php

Lines changed: 59 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@
55
namespace Rector\DeadCode\Rector\ClassMethod;
66

77
use PhpParser\Node;
8+
use PhpParser\Node\Arg;
9+
use PhpParser\Node\Expr\StaticCall;
10+
use PhpParser\Node\Stmt;
811
use PhpParser\Node\Stmt\ClassMethod;
12+
use PhpParser\Node\Stmt\Expression;
913
use PhpParser\NodeVisitor;
1014
use PHPStan\Reflection\ClassReflection;
1115
use Rector\Enum\ObjectReference;
12-
use Rector\NodeAnalyzer\ArgsAnalyzer;
13-
use Rector\NodeAnalyzer\ExprAnalyzer;
14-
use Rector\PhpParser\Node\Value\ValueResolver;
1516
use Rector\PHPStan\ScopeFetcher;
1617
use Rector\Rector\AbstractRector;
17-
use Rector\Reflection\ReflectionResolver;
1818
use Rector\ValueObject\MethodName;
1919
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
2020
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@@ -24,14 +24,6 @@
2424
*/
2525
final class RemoveParentDelegatingConstructorRector extends AbstractRector
2626
{
27-
public function __construct(
28-
private readonly ReflectionResolver $reflectionResolver,
29-
private readonly ArgsAnalyzer $argsAnalyzer,
30-
private readonly ValueResolver $valueResolver,
31-
private readonly ExprAnalyzer $exprAnalyzer
32-
) {
33-
}
34-
3527
public function getRuleDefinition(): RuleDefinition
3628
{
3729
return new RuleDefinition(
@@ -93,44 +85,82 @@ public function refactor(Node $node): ?int
9385
return null;
9486
}
9587

96-
$scope = ScopeFetcher::fetch($node);
97-
$classReflection = $scope->getClassReflection();
98-
if (! $classReflection->getParentClass() instanceof ClassReflection) {
88+
if (! $this->hasParentClassWithConstructor($node)) {
9989
return null;
10090
}
10191

102-
$parentClassReflection = $classReflection->getParentClass();
103-
if (! $parentClassReflection->hasConstructor()) {
92+
$soleStmt = $node->stmts[0];
93+
$parentCallArgs = $this->matchParentConstructorCallArgs($soleStmt);
94+
if ($parentCallArgs === null) {
10495
return null;
10596
}
10697

107-
// $parentClassReflectionConstructor = $parentClassReflection->getConstructor();
108-
109-
$soleStmt = $node->stmts[0];
110-
if (! $soleStmt instanceof Node\Stmt\Expression) {
98+
$constructorParams = $node->getParams();
99+
if (count($constructorParams) !== count($parentCallArgs)) {
111100
return null;
112101
}
113102

114-
if (! $soleStmt->expr instanceof Node\Expr\StaticCall) {
103+
// match passed names in the same order
104+
$paramNames = [];
105+
foreach ($constructorParams as $param) {
106+
$paramNames[] = $this->getName($param->var);
107+
}
108+
109+
$argNames = [];
110+
foreach ($parentCallArgs as $parentCallArg) {
111+
$argValue = $parentCallArg->value;
112+
if (! $argValue instanceof Node\Expr\Variable) {
113+
return null;
114+
}
115+
116+
$argNames[] = $this->getName($argValue);
117+
}
118+
119+
if ($paramNames !== $argNames) {
115120
return null;
116121
}
117122

118-
$staticCall = $soleStmt->expr;
119-
if (! $this->isName($staticCall->class, ObjectReference::PARENT)) {
123+
return NodeVisitor::REMOVE_NODE;
124+
}
125+
126+
private function hasParentClassWithConstructor(ClassMethod $classMethod): bool
127+
{
128+
$scope = ScopeFetcher::fetch($classMethod);
129+
130+
$classReflection = $scope->getClassReflection();
131+
132+
if (! $classReflection->getParentClass() instanceof ClassReflection) {
133+
return false;
134+
}
135+
136+
$parentClassReflection = $classReflection->getParentClass();
137+
return $parentClassReflection->hasConstructor();
138+
}
139+
140+
/**
141+
* Looking for parent::__construct()
142+
*
143+
* @return Arg[]|null
144+
*/
145+
private function matchParentConstructorCallArgs(Stmt $stmt): ?array
146+
{
147+
if (! $stmt instanceof Expression) {
120148
return null;
121149
}
122150

123-
if (! $this->isName($staticCall->name, MethodName::CONSTRUCT)) {
151+
if (! $stmt->expr instanceof StaticCall) {
124152
return null;
125153
}
126154

127-
$constructorParams = $node->getParams();
128-
$parentCallArgs = $staticCall->getArgs();
155+
$staticCall = $stmt->expr;
156+
if (! $this->isName($staticCall->class, ObjectReference::PARENT)) {
157+
return null;
158+
}
129159

130-
if (count($constructorParams) !== count($parentCallArgs)) {
160+
if (! $this->isName($staticCall->name, MethodName::CONSTRUCT)) {
131161
return null;
132162
}
133163

134-
return NodeVisitor::REMOVE_NODE;
164+
return $staticCall->getArgs();
135165
}
136166
}

src/Config/Level/DeadCodeLevel.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Rector\DeadCode\Rector\ClassMethod\RemoveArgumentFromDefaultParentCallRector;
1818
use Rector\DeadCode\Rector\ClassMethod\RemoveEmptyClassMethodRector;
1919
use Rector\DeadCode\Rector\ClassMethod\RemoveNullTagValueNodeRector;
20+
use Rector\DeadCode\Rector\ClassMethod\RemoveParentDelegatingConstructorRector;
2021
use Rector\DeadCode\Rector\ClassMethod\RemoveUnusedConstructorParamRector;
2122
use Rector\DeadCode\Rector\ClassMethod\RemoveUnusedPrivateMethodParameterRector;
2223
use Rector\DeadCode\Rector\ClassMethod\RemoveUnusedPrivateMethodRector;
@@ -132,7 +133,10 @@ final class DeadCodeLevel
132133
RemoveConditionExactReturnRector::class,
133134
RemoveDeadStmtRector::class,
134135
UnwrapFutureCompatibleIfPhpVersionRector::class,
136+
135137
RemoveParentCallWithoutParentRector::class,
138+
RemoveParentDelegatingConstructorRector::class,
139+
136140
RemoveDeadConditionAboveReturnRector::class,
137141
RemoveDeadLoopRector::class,
138142

0 commit comments

Comments
 (0)