-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClassMerger.php
130 lines (110 loc) · 3.34 KB
/
ClassMerger.php
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
namespace Kiboko\Component\AkeneoProductValues\Builder;
use PhpParser\Builder\Class_;
use PhpParser\BuilderFactory;
use PhpParser\Node;
class ClassMerger
{
const PREFER_NONE = 0;
const PREFER_LEFT = 1;
const PREFER_RIGHT = 2;
/**
* @var int
*/
private $preference;
/**
* ClassMerger constructor.
* @param int $preference
*/
public function __construct($preference = self::PREFER_NONE)
{
if (!in_array($preference, [self::PREFER_NONE, self::PREFER_LEFT, self::PREFER_RIGHT])) {
$preference = self::PREFER_NONE;
}
$this->preference = $preference;
}
/**
* @param Node\Stmt\Class_ $left
* @param Node\Stmt\Class_ $right
* @return Node\Stmt\Class_
*/
public function merge(Node\Stmt\Class_ $left, Node\Stmt\Class_ $right)
{
$factory = new BuilderFactory();
$class = $factory->class(
$this->chooseName($left->name, $right->name)
);
$this->chooseExtend($class, $left, $right);
return $class->getNode();
}
private function normalizeName($name)
{
if (is_string($name)) {
return $name;
}
if ($name instanceof Node\Name) {
return $name->toString();
}
throw new \RuntimeException(sprintf(
'Unhandled name attribute. Expected %s, got %s',
Node\Name::class,
is_object($name) ? get_class($name) : gettype($name)
));
}
private function prefer($left, $right)
{
if ($this->preference === self::PREFER_NONE) {
throw new \RuntimeException('Could not choose between both versions, as PREFER_NONE mode is active.');
}
if ($this->preference === self::PREFER_LEFT) {
return $left;
}
if ($this->preference === self::PREFER_RIGHT) {
return $right;
}
}
/**
* @param string|Node\Name $left
* @param string|Node\Name $right
* @return Node\Name
*/
private function chooseName($left, $right)
{
if ($this->normalizeName($left) === $this->normalizeName($right)) {
if ($left === null) {
return null;
}
return new Node\Name(
$this->normalizeName($left)
);
}
return new Node\Name(
$this->normalizeName($this->prefer($left, $right))
);
}
/**
* @param Class_ $merged
* @param Node\Stmt\Class_ $left
* @param Node\Stmt\Class_ $right
*/
private function chooseExtend(Class_ $merged, Node\Stmt\Class_ $left, Node\Stmt\Class_ $right)
{
if ($left->extends !== null && $right->extends !== null) {
$merged->extend($this->chooseName($left, $right));
return;
}
if ($this->preference === self::PREFER_LEFT) {
if ($left->extends !== null) {
$merged->extend($this->normalizeName($left->extends));
}
return;
}
if ($this->preference === self::PREFER_RIGHT) {
if ($right->extends !== null) {
$merged->extend($this->normalizeName($right->extends));
}
return;
}
throw new \RuntimeException('Could not choose between both versions, as PREFER_NONE mode is active.');
}
}