Skip to content

Commit ae7ccd4

Browse files
committed
[DeadCode] Skip with different default param value child vs parent on RemoveParentDelegatingConstructorRector
1 parent 40d87af commit ae7ccd4

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveParentDelegatingConstructorRector\Fixture;
4+
5+
class SkipWithDefaultParams extends \DateTime
6+
{
7+
public function __construct(string $datetime = "tomorrow", ?\DateTimeZone $timezone = null)
8+
{
9+
parent::__construct($datetime, $timezone);
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveParentDelegatingConstructorRector\Fixture;
4+
5+
class WithEqualDefaultParamValue extends \DateTime
6+
{
7+
public function __construct(string $datetime = "now", ?\DateTimeZone $timezone = null)
8+
{
9+
parent::__construct($datetime, $timezone);
10+
}
11+
}
12+
13+
?>
14+
-----
15+
<?php
16+
17+
namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveParentDelegatingConstructorRector\Fixture;
18+
19+
class WithEqualDefaultParamValue extends \DateTime
20+
{
21+
}
22+
23+
?>

rules/DeadCode/Rector/ClassMethod/RemoveParentDelegatingConstructorRector.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use PhpParser\Node;
88
use PhpParser\Node\Arg;
9+
use PhpParser\Node\Expr;
910
use PhpParser\Node\Expr\StaticCall;
1011
use PhpParser\Node\Expr\Variable;
1112
use PhpParser\Node\Stmt;
@@ -15,9 +16,11 @@
1516
use PHPStan\Reflection\ClassReflection;
1617
use PHPStan\Reflection\ExtendedMethodReflection;
1718
use Rector\Enum\ObjectReference;
19+
use Rector\PhpParser\Node\Value\ValueResolver;
1820
use Rector\PHPStan\ScopeFetcher;
1921
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
2022
use Rector\Rector\AbstractRector;
23+
use Rector\Reflection\ReflectionResolver;
2124
use Rector\StaticTypeMapper\StaticTypeMapper;
2225
use Rector\ValueObject\MethodName;
2326
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
@@ -30,6 +33,7 @@ final class RemoveParentDelegatingConstructorRector extends AbstractRector
3033
{
3134
public function __construct(
3235
private readonly StaticTypeMapper $staticTypeMapper,
36+
private readonly ValueResolver $valueResolver
3337
) {
3438
}
3539

@@ -207,6 +211,7 @@ private function areConstructorAndParentParameterTypesMatching(
207211
ClassMethod $classMethod,
208212
ExtendedMethodReflection $extendedMethodReflection
209213
): bool {
214+
$methodName = $this->getName($classMethod);
210215
foreach ($classMethod->getParams() as $position => $param) {
211216
$parameterType = $param->type;
212217

@@ -230,6 +235,18 @@ private function areConstructorAndParentParameterTypesMatching(
230235
if (! $this->nodeComparator->areNodesEqual($parameterType, $parentParameterType)) {
231236
return false;
232237
}
238+
239+
if ($extendedMethodReflection->getDeclaringClass()->getNativeReflection()->hasMethod($methodName)) {
240+
$parentMethod = $extendedMethodReflection->getDeclaringClass()->getNativeReflection()->getMethod($methodName);
241+
$nativeParentParameterReflection = $parentMethod->getParameters()[$index] ?? null;
242+
243+
$parentDefault = $nativeParentParameterReflection->getDefaultValue();
244+
$currentDefault = $this->valueResolver->getValue($param->default);
245+
246+
if ($parentDefault !== $currentDefault) {
247+
return false;
248+
}
249+
}
233250
}
234251
}
235252

0 commit comments

Comments
 (0)