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
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

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

class SkipWithDefaultParams extends \DateTime
{
public function __construct(string $datetime = "tomorrow", ?\DateTimeZone $timezone = null)
{
parent::__construct($datetime, $timezone);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

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

class WithEqualDefaultParamValue extends \DateTime
{
public function __construct(string $datetime = "now", ?\DateTimeZone $timezone = null)
{
parent::__construct($datetime, $timezone);
}
}

?>
-----
<?php

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

class WithEqualDefaultParamValue extends \DateTime
{
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use PhpParser\NodeVisitor;
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionParameter;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ExtendedMethodReflection;
use Rector\Enum\ObjectReference;
use Rector\PhpParser\Node\Value\ValueResolver;
use Rector\PHPStan\ScopeFetcher;
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
use Rector\Rector\AbstractRector;
Expand All @@ -30,6 +33,7 @@ final class RemoveParentDelegatingConstructorRector extends AbstractRector
{
public function __construct(
private readonly StaticTypeMapper $staticTypeMapper,
private readonly ValueResolver $valueResolver
) {
}

Expand Down Expand Up @@ -230,9 +234,43 @@ private function areConstructorAndParentParameterTypesMatching(
if (! $this->nodeComparator->areNodesEqual($parameterType, $parentParameterType)) {
return false;
}

if (! $param->default instanceof Expr) {
continue;
}

if ($this->isDifferentDefaultValue($param->default, $extendedMethodReflection, $index)) {
return false;
}
}
}

return true;
}

private function isDifferentDefaultValue(
Expr $defaultExpr,
ExtendedMethodReflection $extendedMethodReflection,
int $index
): bool {
$methodName = $extendedMethodReflection->getName();
// native reflection is needed to get exact default value
if ($extendedMethodReflection->getDeclaringClass()->getNativeReflection()->hasMethod($methodName)) {
$parentMethod = $extendedMethodReflection->getDeclaringClass()
->getNativeReflection()
->getMethod($methodName);
$nativeParentParameterReflection = $parentMethod->getParameters()[$index] ?? null;

if (! $nativeParentParameterReflection instanceof ReflectionParameter) {
return false;
}

$parentDefault = $nativeParentParameterReflection->getDefaultValue();
if (! $this->valueResolver->isValue($defaultExpr, $parentDefault)) {
return true;
}
}

return false;
}
}