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