*/ private $oldToNewInterfaces = []; public function getRuleDefinition() : RuleDefinition { return new RuleDefinition('Merges old interface to a new one, that already has its methods', [new ConfiguredCodeSample(<<<'CODE_SAMPLE' class SomeClass implements SomeInterface, SomeOldInterface { } CODE_SAMPLE , <<<'CODE_SAMPLE' class SomeClass implements SomeInterface { } CODE_SAMPLE , ['SomeOldInterface' => 'SomeInterface'])]); } /** * @return array> */ public function getNodeTypes() : array { return [Class_::class]; } /** * @param Class_ $node */ public function refactor(Node $node) : ?Node { if ($node->implements === []) { return null; } $hasChanged = \false; foreach ($node->implements as $key => $implement) { $oldInterfaces = \array_keys($this->oldToNewInterfaces); if (!$this->isNames($implement, $oldInterfaces)) { continue; } $interface = $this->getName($implement); $node->implements[$key] = new Name($this->oldToNewInterfaces[$interface]); $hasChanged = \true; } if (!$hasChanged) { return null; } $this->makeImplementsUnique($node); return $node; } /** * @param mixed[] $configuration */ public function configure(array $configuration) : void { Assert::allString(\array_keys($configuration)); Assert::allString($configuration); $this->oldToNewInterfaces = $configuration; } private function makeImplementsUnique(Class_ $class) : void { $alreadyAddedNames = []; /** @var array $implements */ $implements = $class->implements; foreach ($implements as $key => $name) { $fqnName = $this->getName($name); if (\in_array($fqnName, $alreadyAddedNames, \true)) { unset($class->implements[$key]); continue; } $alreadyAddedNames[] = $fqnName; } } }