Further modules included.
[yaffs-website] / web / modules / contrib / drupalmoduleupgrader / src / Plugin / DMU / Fixer / PSR4.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\drupalmoduleupgrader\Plugin\DMU\Fixer\PSR4.
6  */
7
8 namespace Drupal\drupalmoduleupgrader\Plugin\DMU\Fixer;
9
10 use Drupal\drupalmoduleupgrader\FixerBase;
11 use Pharborist\Namespaces\NameNode;
12 use Pharborist\Parser;
13 use Pharborist\RootNode;
14 use Pharborist\WhitespaceNode;
15
16 /**
17  * @Fixer(
18  *  id = "psr4ify"
19  * )
20  */
21 class PSR4 extends FixerBase {
22
23   /**
24    * {@inheritdoc}
25    */
26   public function execute() {
27     /** @var \Pharborist\Objects\ClassNode $class */
28     $class = $this
29       ->target
30       ->getIndexer('class')
31       ->get($this->configuration['source']);
32
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);
37
38     $import = [];
39
40     if ($parent = $class->getExtends()) {
41       $import[] = $parent->getAbsolutePath();
42     }
43
44     $interfaces = $class->getImplementList();
45     if ($interfaces) {
46       foreach ($interfaces->getItems() as $interface) {
47         $import[] = $interface->getAbsolutePath();
48       }
49     }
50
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();
56         }
57       }
58     }
59
60     foreach (array_unique($import) as $i) {
61       Parser::parseSnippet('use ' . ltrim($i, '\\') . ';')->appendTo($ns);
62       WhitespaceNode::create("\n")->appendTo($ns);
63     }
64
65     WhitespaceNode::create("\n")->appendTo($ns);
66     $class->remove()->appendTo($ns);
67
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());
72   }
73
74 }