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,26 @@
<?php

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

class SomeScalarType
{
public function __construct(array $config)
{
}
}

/**
* @phpstan-type CustomScalarConfig array{name?: string, serialize?: callable}
*/
class SomeRefiningScalarType extends SomeScalarType
{
/**
* @param array<string, mixed> $config
*
* @phpstan-param CustomScalarConfig $config
*/
public function __construct(array $config)
{
parent::__construct($config);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionParameter;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ExtendedMethodReflection;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\Enum\ObjectReference;
use Rector\PhpParser\Node\Value\ValueResolver;
use Rector\PHPStan\ScopeFetcher;
Expand All @@ -33,7 +34,8 @@ final class RemoveParentDelegatingConstructorRector extends AbstractRector
{
public function __construct(
private readonly StaticTypeMapper $staticTypeMapper,
private readonly ValueResolver $valueResolver
private readonly ValueResolver $valueResolver,
private readonly PhpDocInfoFactory $phpDocInfoFactory
) {
}

Expand Down Expand Up @@ -131,9 +133,23 @@ public function refactor(Node $node): ?int
return null;
}

// keep when the docblock refines parameter types beyond the native signature,
// e.g. @param array<string, mixed> or @phpstan-param SomeShape $config — removing
// the constructor would drop that type information
if ($this->hasParamRefiningDocblock($node)) {
return null;
}

return NodeVisitor::REMOVE_NODE;
}

private function hasParamRefiningDocblock(ClassMethod $classMethod): bool
{
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($classMethod);

return $phpDocInfo->hasByNames(['@param', '@phpstan-param', '@psalm-param']);
}

private function matchParentConstructorReflection(ClassMethod $classMethod): ?ExtendedMethodReflection
{
$scope = ScopeFetcher::fetch($classMethod);
Expand Down
Loading