public function PSR4::execute in Drupal 7 to 8/9 Module Upgrader 8
Executes the plugin.
Overrides ExecutableInterface::execute
File
- src/
Plugin/ DMU/ Fixer/ PSR4.php, line 21
Class
- PSR4
- Plugin annotation @Fixer( id = "psr4ify" )
Namespace
Drupal\drupalmoduleupgrader\Plugin\DMU\FixerCode
public function execute() {
/** @var \Pharborist\Objects\ClassNode $class */
$class = $this->target
->getIndexer('class')
->get($this->configuration['source']);
$ns = substr($this->configuration['destination'], 0, strrpos($this->configuration['destination'], '\\'));
$doc = RootNode::create($ns);
$ns = $doc
->getNamespace($ns);
WhitespaceNode::create("\n")
->appendTo($ns);
$import = [];
if ($parent = $class
->getExtends()) {
$import[] = $parent
->getAbsolutePath();
}
$interfaces = $class
->getImplementList();
if ($interfaces) {
foreach ($interfaces
->getItems() as $interface) {
$import[] = $interface
->getAbsolutePath();
}
}
foreach ($class
->getMethods() as $method) {
foreach ($method
->getParameters() as $parameter) {
$type_hint = $parameter
->getTypeHint();
if ($type_hint instanceof NameNode) {
$import[] = $type_hint
->getAbsolutePath();
}
}
}
foreach (array_unique($import) as $i) {
Parser::parseSnippet('use ' . ltrim($i, '\\') . ';')
->appendTo($ns);
WhitespaceNode::create("\n")
->appendTo($ns);
}
WhitespaceNode::create("\n")
->appendTo($ns);
$class
->remove()
->appendTo($ns);
$search_for = [
'Drupal\\' . $this->target
->id(),
'\\',
];
$replace_with = [
'src',
'/',
];
$path = str_replace($search_for, $replace_with, $this->configuration['destination']) . '.php';
file_put_contents($this->target
->getPath($path), $doc
->getText());
}