Total Complexity | 41 |
Total Lines | 200 |
Duplicated Lines | 0 % |
Changes | 5 | ||
Bugs | 2 | Features | 0 |
Complex classes like MetadataGubbinsHolder often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use MetadataGubbinsHolder, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class MetadataGubbinsHolder |
||
15 | { |
||
16 | protected $relations = []; |
||
17 | protected $knownSides = []; |
||
18 | |||
19 | /** |
||
20 | * Add entity to holder |
||
21 | * |
||
22 | * @param EntityGubbins $entity |
||
23 | * @throws InvalidOperationException |
||
24 | */ |
||
25 | public function addEntity(EntityGubbins $entity) |
||
26 | { |
||
27 | $className = $entity->getClassName(); |
||
28 | if (array_key_exists($className, $this->relations)) { |
||
29 | $msg = $className . ' already added'; |
||
30 | throw new \InvalidArgumentException($msg); |
||
31 | } |
||
32 | $this->relations[$className] = $entity; |
||
33 | $this->knownSides[$className] = []; |
||
34 | foreach ($entity->getStubs() as $relName => $stub) { |
||
35 | if ($stub instanceof AssociationStubPolymorphic && $stub->isKnownSide()) { |
||
36 | $this->knownSides[$className][$relName] = $stub; |
||
37 | } |
||
38 | } |
||
39 | } |
||
40 | |||
41 | public function getRelationsByRelationName($className, $relName) |
||
42 | { |
||
43 | $this->checkClassExists($className); |
||
44 | |||
45 | $rels = $this->relations[$className]; |
||
46 | |||
47 | if (!array_key_exists($relName, $rels->getStubs())) { |
||
48 | $msg = 'Relation ' . $relName . ' not registered on ' . $className; |
||
49 | throw new \InvalidArgumentException($msg); |
||
50 | } |
||
51 | /** @var AssociationStubBase $stub */ |
||
52 | $stub = $rels->getStubs()[$relName]; |
||
53 | $targType = $stub->getTargType(); |
||
54 | if (!array_key_exists($targType, $this->relations)) { |
||
55 | return []; |
||
56 | } |
||
57 | $targRel = $this->relations[$targType]; |
||
58 | // now dig out compatible stubs on target type |
||
59 | $targStubs = $targRel->getStubs(); |
||
60 | $relStubs = []; |
||
61 | foreach ($targStubs as $targStub) { |
||
62 | if ($stub->isCompatible($targStub)) { |
||
63 | $relStubs[] = $targStub; |
||
64 | } |
||
65 | } |
||
66 | return $relStubs; |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * @param string $className |
||
71 | * @return array |
||
72 | * @throws InvalidOperationException |
||
73 | */ |
||
74 | public function getRelationsByClass($className) |
||
75 | { |
||
76 | $this->checkClassExists($className); |
||
77 | |||
78 | $rels = $this->relations[$className]; |
||
79 | $stubs = $rels->getStubs(); |
||
80 | |||
81 | $associations = []; |
||
82 | foreach ($stubs as $relName => $stub) { |
||
83 | $others = $this->getRelationsByRelationName($className, $relName); |
||
84 | if ($stub instanceof AssociationStubMonomorphic) { |
||
85 | $msg = 'Monomorphic relation stub on ' . $className . ' ' . $relName |
||
86 | . ' should point to at most 1 other stub'; |
||
87 | if (!(1 >= count($others))) { |
||
88 | throw new InvalidOperationException($msg); |
||
89 | } |
||
90 | } |
||
91 | if (1 === count($others)) { |
||
92 | $others = $others[0]; |
||
93 | $assoc = new AssociationMonomorphic(); |
||
94 | $first = -1 === $stub->compare($others); |
||
95 | $assoc->setFirst($first ? $stub : $others); |
||
96 | $assoc->setLast($first ? $others : $stub); |
||
97 | if (!$assoc->isOk()) { |
||
98 | throw new InvalidOperationException(''); |
||
99 | } |
||
100 | $associations[] = $assoc; |
||
101 | } |
||
102 | } |
||
103 | return $associations; |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * @return array |
||
108 | * @throws InvalidOperationException |
||
109 | */ |
||
110 | public function getRelations() |
||
111 | { |
||
112 | $classNames = array_keys($this->relations); |
||
113 | |||
114 | $associations = $this->buildRelationAssociations($classNames); |
||
115 | |||
116 | $unknowns = $this->buildUnknownSides(); |
||
117 | |||
118 | $monoAssoc = []; |
||
119 | $polyAssoc = []; |
||
120 | foreach ($associations as $assoc) { |
||
121 | if ($assoc->getFirst() instanceof AssociationStubMonomorphic) { |
||
122 | $monoAssoc[] = $assoc; |
||
123 | continue; |
||
124 | } |
||
125 | // monomorphic associations are dealt with, now for the polymorphic associations - they're a mite trickier |
||
126 | $firstKnown = $assoc->getFirst()->isKnownSide(); |
||
127 | /** @var AssociationStubPolymorphic $known */ |
||
128 | $known = $firstKnown ? $assoc->getFirst() : $assoc->getLast(); |
||
129 | /** @var AssociationStubPolymorphic $unknown */ |
||
130 | $unknown = $firstKnown ? $assoc->getLast() : $assoc->getFirst(); |
||
131 | $className = $known->getBaseType(); |
||
132 | $relName = $known->getRelationName(); |
||
133 | $unknowns[$className][$relName][] = $unknown; |
||
134 | } |
||
135 | |||
136 | foreach ($this->knownSides as $knownType => $knownDeets) { |
||
137 | foreach (array_keys($knownDeets) as $key) { |
||
138 | /** @var AssociationStubPolymorphic[] $lastCandidates */ |
||
139 | $lastCandidates = $unknowns[$knownType][$key]; |
||
140 | if (0 == count($lastCandidates)) { |
||
141 | continue; |
||
142 | } |
||
143 | foreach ($lastCandidates as $lc) { |
||
144 | /** @var AssociationStubPolymorphic $stub */ |
||
145 | $stub = clone $this->knownSides[$knownType][$key]; |
||
146 | $isMulti = ($stub->getMultiplicity() == AssociationStubRelationType::MANY()); |
||
147 | $relPolyTypeName = substr($lc->getBaseType(), strrpos($lc->getBaseType(), '\\')+1); |
||
148 | $relPolyTypeName = Str::plural($relPolyTypeName, $isMulti ? 2 : 1); |
||
149 | $stub->setRelationName($stub->getRelationName() . '_' . $relPolyTypeName); |
||
150 | $assoc = new AssociationMonomorphic(); |
||
151 | $first = -1 === $stub->compare($lc); |
||
152 | $assoc->setFirst($first ? $stub : $lc); |
||
153 | $assoc->setLast($first ? $lc : $stub); |
||
154 | if (!$assoc->isOk()) { |
||
155 | throw new InvalidOperationException(''); |
||
156 | } |
||
157 | $polyAssoc[] = $assoc; |
||
158 | } |
||
159 | } |
||
160 | } |
||
161 | $result = array_merge($monoAssoc, $polyAssoc); |
||
162 | return $result; |
||
163 | } |
||
164 | |||
165 | public function hasClass($className) |
||
166 | { |
||
167 | return array_key_exists($className, $this->relations); |
||
168 | } |
||
169 | |||
170 | /** |
||
171 | * @param $className |
||
172 | */ |
||
173 | protected function checkClassExists($className) |
||
178 | } |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * @param array $classNames |
||
183 | * @return array |
||
184 | * @throws InvalidOperationException |
||
185 | */ |
||
186 | protected function buildRelationAssociations(array $classNames) |
||
199 | } |
||
200 | |||
201 | /** |
||
202 | * @return array |
||
203 | */ |
||
204 | protected function buildUnknownSides() |
||
216 |