5 * Contains \Drupal\drupalmoduleupgrader\Plugin\DMU\Fixer\PSR4.
8 namespace Drupal\drupalmoduleupgrader\Plugin\DMU\Fixer;
10 use Drupal\drupalmoduleupgrader\FixerBase;
11 use Pharborist\Namespaces\NameNode;
12 use Pharborist\Parser;
13 use Pharborist\RootNode;
14 use Pharborist\WhitespaceNode;
21 class PSR4 extends FixerBase {
26 public function execute() {
27 /** @var \Pharborist\Objects\ClassNode $class */
31 ->get($this->configuration['source']);
33 $ns = substr($this->configuration['destination'], 0, strrpos($this->configuration['destination'], '\\'));
34 $doc = RootNode::create($ns);
35 $ns = $doc->getNamespace($ns);
36 WhitespaceNode::create("\n")->appendTo($ns);
40 if ($parent = $class->getExtends()) {
41 $import[] = $parent->getAbsolutePath();
44 $interfaces = $class->getImplementList();
46 foreach ($interfaces->getItems() as $interface) {
47 $import[] = $interface->getAbsolutePath();
51 foreach ($class->getMethods() as $method) {
52 foreach ($method->getParameters() as $parameter) {
53 $type_hint = $parameter->getTypeHint();
54 if ($type_hint instanceof NameNode) {
55 $import[] = $type_hint->getAbsolutePath();
60 foreach (array_unique($import) as $i) {
61 Parser::parseSnippet('use ' . ltrim($i, '\\') . ';')->appendTo($ns);
62 WhitespaceNode::create("\n")->appendTo($ns);
65 WhitespaceNode::create("\n")->appendTo($ns);
66 $class->remove()->appendTo($ns);
68 $search_for = ['Drupal\\' . $this->target->id(), '\\'];
69 $replace_with = ['src', '/'];
70 $path = str_replace($search_for, $replace_with, $this->configuration['destination']) . '.php';
71 file_put_contents($this->target->getPath($path), $doc->getText());