paramAnalyzer = $paramAnalyzer;
$this->reflectionResolver = $reflectionResolver;
$this->classMethodParamRemover = $classMethodParamRemover;
}
public function getRuleDefinition() : RuleDefinition
{
return new RuleDefinition('Remove unused parameter in constructor', [new CodeSample(<<<'CODE_SAMPLE'
final class SomeClass
{
private $hey;
public function __construct($hey, $man)
{
$this->hey = $hey;
}
}
CODE_SAMPLE
, <<<'CODE_SAMPLE'
final class SomeClass
{
private $hey;
public function __construct($hey)
{
$this->hey = $hey;
}
}
CODE_SAMPLE
)]);
}
/**
* @return array>
*/
public function getNodeTypes() : array
{
return [Class_::class];
}
/**
* @param Class_ $node
*/
public function refactor(Node $node) : ?Node
{
$constructorClassMethod = $node->getMethod(MethodName::CONSTRUCT);
if (!$constructorClassMethod instanceof ClassMethod) {
return null;
}
if ($constructorClassMethod->params === []) {
return null;
}
if ($this->paramAnalyzer->hasPropertyPromotion($constructorClassMethod->params)) {
return null;
}
if ($constructorClassMethod->isAbstract()) {
return null;
}
$classReflection = $this->reflectionResolver->resolveClassReflection($node);
if (!$classReflection instanceof ClassReflection) {
return null;
}
$interfaces = $classReflection->getInterfaces();
foreach ($interfaces as $interface) {
if ($interface->hasNativeMethod(MethodName::CONSTRUCT)) {
return null;
}
}
$changedConstructorClassMethod = $this->classMethodParamRemover->processRemoveParams($constructorClassMethod);
if (!$changedConstructorClassMethod instanceof ClassMethod) {
return null;
}
return $node;
}
}