Further modules included.
[yaffs-website] / web / modules / contrib / drupalmoduleupgrader / src / FixerBase.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\drupalmoduleupgrader\FixerBase.
6  */
7
8 namespace Drupal\drupalmoduleupgrader;
9
10 use Drupal\Core\Plugin\PluginBase as CorePluginBase;
11 use Pharborist\Constants\ConstantNode;
12 use Pharborist\Functions\ParameterNode;
13 use Pharborist\Node;
14 use Pharborist\NodeInterface;
15
16 /**
17  * Base class for fixers, containing a lot of helpful utilities.
18  */
19 abstract class FixerBase extends CorePluginBase implements FixerInterface {
20
21   /**
22    * @var \Drupal\drupalmoduleupgrader\TargetInterface
23    */
24   protected $target;
25
26   /**
27    * {@inheritdoc}
28    */
29   public function setTarget(TargetInterface $target) {
30     $this->target = $target;
31   }
32
33   protected function getUnaliasedPath($path) {
34     return preg_replace('/^~/', $this->target->getBasePath(), $path);
35   }
36
37   /**
38    * Returns if a node uses a specific trait anywhere in its lineage.
39    *
40    * @param \Pharborist\NodeInterface $node
41    *
42    * @return boolean
43    */
44   protected function usesTrait($trait, NodeInterface $node) {
45     $hierarchy = class_parents($node);
46     array_unshift($hierarchy, get_class($node));
47
48     $traits = [];
49     foreach ($hierarchy as $parent) {
50       $this->collectTraits($parent, $traits);
51     }
52
53     return in_array($trait, $traits);
54   }
55
56   private function collectTraits($class, array &$all_traits = []) {
57     $traits = class_uses($class);
58
59     foreach ($traits as $trait) {
60       $this->collectTraits($trait, $traits);
61     }
62
63     $all_traits += $traits;
64   }
65
66 }