Complex classes like MetadataManager 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 MetadataManager, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | class MetadataManager |
||
29 | { |
||
30 | private $V3Edmx = null; |
||
31 | private $lastError = null; |
||
32 | private $serializer = null; |
||
33 | |||
34 | public function __construct($namespaceName = "Data", $containerName = "DefaultContainer", Edmx $edmx = null) |
||
35 | { |
||
36 | $msg = null; |
||
37 | $this->V3Edmx = (null == $edmx) ? new Edmx($namespaceName, $containerName) : $edmx; |
||
38 | assert($this->V3Edmx->isOK($msg), $msg); |
||
39 | $this->initSerialiser(); |
||
40 | assert(null != $this->serializer, "Serializer must not be null at end of constructor"); |
||
41 | } |
||
42 | |||
43 | public function getEdmx() |
||
44 | { |
||
45 | $msg = null; |
||
46 | assert($this->V3Edmx->isOK($msg), $msg); |
||
47 | return $this->V3Edmx; |
||
48 | } |
||
49 | |||
50 | public function getEdmxXML() |
||
51 | { |
||
52 | $cereal = $this->getSerialiser(); |
||
|
|||
53 | assert(null != $cereal, "Serializer must not be null when trying to get edmx xml"); |
||
54 | return $cereal->serialize($this->getEdmx(), "xml"); |
||
55 | } |
||
56 | |||
57 | public function addEntityType($name, $accessType = "Public", $summary = null, $longDescription = null) |
||
58 | { |
||
59 | $NewEntity = new TEntityTypeType(); |
||
60 | $NewEntity->setName($name); |
||
61 | $this->addDocumentation($summary, $longDescription, $NewEntity); |
||
62 | |||
63 | $entitySet = new EntitySetAnonymousType(); |
||
64 | $entitySet->setName(Str::plural($NewEntity->getName())); |
||
65 | $namespace = $this->getNamespace(); |
||
66 | $entityTypeName = $namespace . $NewEntity->getName(); |
||
67 | $entitySet->setEntityType($entityTypeName); |
||
68 | $entitySet->setGetterAccess($accessType); |
||
69 | |||
70 | $this->V3Edmx->getDataServiceType()->getSchema()[0]->addToEntityType($NewEntity); |
||
71 | $this->V3Edmx->getDataServiceType()->getSchema()[0]->getEntityContainer()[0]->addToEntitySet($entitySet); |
||
72 | assert($this->V3Edmx->isOK($this->lastError), $this->lastError); |
||
73 | return [$NewEntity, $entitySet]; |
||
74 | } |
||
75 | |||
76 | public function addComplexType($name, $accessType = "Public", $summary = null, $longDescription = null) |
||
77 | { |
||
78 | $NewEntity = new TComplexTypeType(); |
||
79 | $NewEntity->setName($name); |
||
80 | $NewEntity->setTypeAccess($accessType); |
||
81 | $this->addDocumentation($summary, $longDescription, $NewEntity); |
||
82 | assert($NewEntity->isOK($this->lastError), $this->lastError); |
||
83 | $this->V3Edmx->getDataServiceType()->getSchema()[0]->addToComplexType($NewEntity); |
||
84 | |||
85 | return $NewEntity; |
||
86 | } |
||
87 | |||
88 | public function getSerialiser() |
||
89 | { |
||
90 | return $this->serializer; |
||
91 | } |
||
92 | |||
93 | public function addPropertyToComplexType( |
||
94 | \AlgoWeb\ODataMetadata\MetadataV3\edm\TComplexTypeType $complexType, |
||
95 | $name, |
||
96 | $type, |
||
97 | $defaultValue = null, |
||
98 | $nullable = false, |
||
99 | $summary = null, |
||
100 | $longDescription = null |
||
101 | ) { |
||
102 | if (is_array($defaultValue) || is_object($defaultValue)) { |
||
103 | throw new \InvalidArgumentException("Default value cannot be object or array"); |
||
104 | } |
||
105 | if (null != $defaultValue) { |
||
106 | $defaultValue = var_export($defaultValue, true); |
||
107 | } |
||
108 | $NewProperty = new TComplexTypePropertyType(); |
||
109 | $NewProperty->setName($name); |
||
110 | $NewProperty->setType($type); |
||
111 | $NewProperty->setNullable($nullable); |
||
112 | $this->addDocumentation($summary, $longDescription, $NewProperty); |
||
113 | if (null != $defaultValue) { |
||
114 | $NewProperty->setDefaultValue($defaultValue); |
||
115 | } |
||
116 | assert($NewProperty->isOK($this->lastError), $this->lastError); |
||
117 | $complexType->addToProperty($NewProperty); |
||
118 | return $NewProperty; |
||
119 | } |
||
120 | |||
121 | public function addPropertyToEntityType( |
||
122 | TEntityTypeType $entityType, |
||
123 | $name, |
||
124 | $type, |
||
125 | $defaultValue = null, |
||
126 | $nullable = false, |
||
127 | $isKey = false, |
||
128 | $storeGeneratedPattern = null, |
||
129 | $summary = null, |
||
130 | $longDescription = null |
||
131 | ) { |
||
132 | $NewProperty = new TEntityPropertyType(); |
||
133 | $NewProperty->setName($name); |
||
134 | $NewProperty->setType($type); |
||
135 | $NewProperty->setStoreGeneratedPattern($storeGeneratedPattern); |
||
136 | $NewProperty->setNullable($nullable); |
||
137 | $this->addDocumentation($summary, $longDescription, $NewProperty); |
||
138 | if (null != $defaultValue) { |
||
139 | $NewProperty->setDefaultValue($defaultValue); |
||
140 | } |
||
141 | $entityType->addToProperty($NewProperty); |
||
142 | if ($isKey) { |
||
143 | $Key = new TPropertyRefType(); |
||
144 | $Key->setName($name); |
||
145 | $entityType->addToKey($Key); |
||
146 | } |
||
147 | return $NewProperty; |
||
148 | } |
||
149 | |||
150 | public function addNavigationPropertyToEntityType( |
||
151 | TEntityTypeType $principalType, |
||
152 | $principalMultiplicity, |
||
153 | $principalProperty, |
||
154 | TEntityTypeType $dependentType, |
||
155 | $dependentMultiplicity, |
||
156 | $dependentProperty = "", |
||
157 | array $principalConstraintProperty = null, |
||
158 | array $dependentConstraintProperty = null, |
||
159 | $principalGetterAccess = "Public", |
||
160 | $principalSetterAccess = "Public", |
||
161 | $dependentGetterAccess = "Public", |
||
162 | $dependentSetterAccess = "Public", |
||
163 | $principalSummery = null, |
||
164 | $principalLongDescription = null, |
||
165 | $dependentSummery = null, |
||
166 | $dependentLongDescription = null |
||
167 | ) { |
||
168 | $principalEntitySetName = Str::plural($principalType->getName()); |
||
169 | $dependentEntitySetName = Str::plural($dependentType->getName()); |
||
170 | $relationName = $principalType->getName() . "_" . $principalProperty . "_" |
||
171 | . $dependentType->getName() . "_" . $dependentProperty; |
||
172 | $relationName = trim($relationName, "_"); |
||
173 | |||
174 | $namespace = $this->getNamespace(); |
||
175 | $relationFQName = $namespace . $relationName; |
||
176 | |||
177 | $principalNavigationProperty = new TNavigationPropertyType(); |
||
178 | $principalNavigationProperty->setName($principalProperty); |
||
179 | $principalNavigationProperty->setToRole(trim($dependentEntitySetName . "_" . $dependentProperty, "_")); |
||
180 | $principalNavigationProperty->setFromRole($principalEntitySetName . "_" . $principalProperty); |
||
181 | $principalNavigationProperty->setRelationship($relationFQName); |
||
182 | $principalNavigationProperty->setGetterAccess($principalGetterAccess); |
||
183 | $principalNavigationProperty->setSetterAccess($principalSetterAccess); |
||
184 | $this->addDocumentation($principalSummery, $principalLongDescription, $principalNavigationProperty); |
||
185 | $principalType->addToNavigationProperty($principalNavigationProperty); |
||
186 | $dependentNavigationProperty = null; |
||
187 | if (!empty($dependentProperty)) { |
||
188 | $dependentNavigationProperty = new TNavigationPropertyType(); |
||
189 | $dependentNavigationProperty->setName($dependentProperty); |
||
190 | $dependentNavigationProperty->setToRole($principalEntitySetName . "_" . $principalProperty); |
||
191 | $dependentNavigationProperty->setFromRole($dependentEntitySetName . "_" . $dependentProperty); |
||
192 | $dependentNavigationProperty->setRelationship($relationFQName); |
||
193 | $dependentNavigationProperty->setGetterAccess($dependentGetterAccess); |
||
194 | $dependentNavigationProperty->setSetterAccess($dependentSetterAccess); |
||
195 | $this->addDocumentation($dependentSummery, $dependentLongDescription, $dependentNavigationProperty); |
||
196 | $dependentType->addToNavigationProperty($dependentNavigationProperty); |
||
197 | } |
||
198 | |||
199 | $assocation = $this->createAssocationFromNavigationProperty( |
||
200 | $principalType, |
||
201 | $dependentType, |
||
202 | $principalNavigationProperty, |
||
203 | $dependentNavigationProperty, |
||
204 | $principalMultiplicity, |
||
205 | $dependentMultiplicity, |
||
206 | $principalConstraintProperty, |
||
207 | $dependentConstraintProperty |
||
208 | ); |
||
209 | |||
210 | $this->V3Edmx->getDataServiceType()->getSchema()[0]->addToAssociation($assocation); |
||
211 | |||
212 | $associationSet = $this->createAssocationSetForAssocation( |
||
213 | $assocation, |
||
214 | $principalEntitySetName, |
||
215 | $dependentEntitySetName |
||
216 | ); |
||
217 | |||
218 | $this->V3Edmx->getDataServiceType()->getSchema()[0] |
||
219 | ->getEntityContainer()[0]->addToAssociationSet($associationSet); |
||
220 | |||
221 | assert($this->V3Edmx->isOK($this->lastError), $this->lastError); |
||
222 | return [$principalNavigationProperty, $dependentNavigationProperty]; |
||
223 | } |
||
224 | |||
225 | protected function createAssocationFromNavigationProperty( |
||
226 | TEntityTypeType $principalType, |
||
227 | TEntityTypeType $dependentType, |
||
228 | TNavigationPropertyType $principalNavigationProperty, |
||
229 | TNavigationPropertyType $dependentNavigationProperty = null, |
||
230 | $principalMultiplicity, |
||
231 | $dependentMultiplicity, |
||
232 | array $principalConstraintProperty = null, |
||
233 | array $dependentConstraintProperty = null |
||
234 | ) { |
||
235 | $multCombo = [ '*' => ['*', '1'], '0..1' => ['1'], '1' => ['*', '0..1']]; |
||
236 | $multKeys = array_keys($multCombo); |
||
237 | if (null != $dependentNavigationProperty) { |
||
238 | if ($dependentNavigationProperty->getRelationship() != $principalNavigationProperty->getRelationship()) { |
||
239 | $msg = "If you have both a dependent property and a principal property," |
||
240 | ." relationship should match"; |
||
241 | throw new \InvalidArgumentException($msg); |
||
242 | } |
||
243 | if ($dependentNavigationProperty->getFromRole() != $principalNavigationProperty->getToRole() || |
||
244 | $dependentNavigationProperty->getToRole() != $principalNavigationProperty->getFromRole() |
||
245 | ) { |
||
246 | throw new \InvalidArgumentException( |
||
247 | "Principal to role should match dependent from role, and vice versa" |
||
248 | ); |
||
249 | } |
||
250 | } |
||
251 | if (!in_array($principalMultiplicity, $multKeys) || !in_array($dependentMultiplicity, $multKeys)) { |
||
252 | throw new \InvalidArgumentException("Malformed multiplicity - valid values are *, 0..1 and 1"); |
||
253 | } |
||
254 | if (!in_array($dependentMultiplicity, $multCombo[$principalMultiplicity])) { |
||
255 | throw new \InvalidArgumentException( |
||
256 | "Invalid multiplicity combination - ". $principalMultiplicity . ' ' . $dependentMultiplicity |
||
257 | ); |
||
258 | } |
||
259 | |||
260 | $namespace = $this->getNamespace(); |
||
261 | $principalTypeFQName = $namespace . $principalType->getName(); |
||
262 | $dependentTypeFQName = $namespace . $dependentType->getName(); |
||
263 | $association = new TAssociationType(); |
||
264 | $relationship = $principalNavigationProperty->getRelationship(); |
||
265 | if (false !== strpos($relationship, '.')) { |
||
266 | $relationship = substr($relationship, strpos($relationship, '.') + 1); |
||
267 | } |
||
268 | |||
269 | $principalTargRole = $principalNavigationProperty->getToRole(); |
||
270 | $principalSrcRole = $principalNavigationProperty->getFromRole(); |
||
271 | $dependentTargRole = null != $dependentNavigationProperty ? $dependentNavigationProperty->getToRole() : null; |
||
272 | |||
273 | $association->setName($relationship); |
||
274 | $principalEnd = new TAssociationEndType(); |
||
275 | $principalEnd->setType($principalTypeFQName); |
||
276 | $principalEnd->setRole($principalTargRole); |
||
277 | $principalEnd->setMultiplicity($principalMultiplicity); |
||
278 | $association->addToEnd($principalEnd); |
||
279 | $dependentEnd = new TAssociationEndType(); |
||
280 | $dependentEnd->setType($dependentTypeFQName); |
||
281 | $dependentEnd->setMultiplicity($dependentMultiplicity); |
||
282 | $association->addToEnd($dependentEnd); |
||
283 | |||
284 | $dependentEnd->setRole(null != $dependentNavigationProperty ? $dependentTargRole : $principalSrcRole); |
||
285 | |||
286 | $hasPrincipalReferral = null != $principalConstraintProperty && 0 < count($principalConstraintProperty); |
||
287 | $hasDependentReferral = null != $dependentConstraintProperty && 0 < count($dependentConstraintProperty); |
||
288 | |||
289 | if ($hasPrincipalReferral && $hasDependentReferral) { |
||
290 | $principalReferralConstraint = $this->makeReferentialConstraint( |
||
291 | $principalConstraintProperty, $principalTargRole |
||
292 | ); |
||
293 | $dependentReferralConstraint = $this->makeReferentialConstraint( |
||
294 | $dependentConstraintProperty, $dependentTargRole |
||
295 | ); |
||
296 | $constraint = new TConstraintType(); |
||
297 | $constraint->setPrincipal($principalReferralConstraint); |
||
298 | $constraint->setDependent($dependentReferralConstraint); |
||
299 | $association->setReferentialConstraint($constraint); |
||
300 | } |
||
301 | return $association; |
||
302 | } |
||
303 | |||
304 | protected function createAssocationSetForAssocation( |
||
305 | TAssociationType $association, |
||
306 | $principalEntitySetName, |
||
307 | $dependentEntitySetName |
||
308 | ) { |
||
309 | $as = new AssociationSetAnonymousType(); |
||
310 | $name = $association->getName(); |
||
311 | $as->setName($name); |
||
312 | $namespace = $this->getNamespace(); |
||
313 | $associationSetName = $namespace . $association->getName(); |
||
314 | $as->setAssociation($associationSetName); |
||
315 | $end1 = new EndAnonymousType(); |
||
316 | $end1->setRole($association->getEnd()[0]->getRole()); |
||
317 | $end1->setEntitySet($principalEntitySetName); |
||
318 | $end2 = new EndAnonymousType(); |
||
319 | $end2->setRole($association->getEnd()[1]->getRole()); |
||
320 | $end2->setEntitySet($dependentEntitySetName); |
||
321 | assert($end1->getRole() != $end2->getRole()); |
||
322 | $as->addToEnd($end1); |
||
323 | $as->addToEnd($end2); |
||
324 | return $as; |
||
325 | } |
||
326 | |||
327 | public function getLastError() |
||
328 | { |
||
329 | return $this->lastError; |
||
330 | } |
||
331 | |||
332 | /** |
||
333 | * @param string $name |
||
334 | * @param IsOK $expectedReturnType |
||
335 | * @param TTextType $shortDesc |
||
336 | * @param TTextType $longDesc |
||
337 | * @return FunctionImportAnonymousType |
||
338 | */ |
||
339 | public function createSingleton( |
||
340 | $name, |
||
341 | IsOK $expectedReturnType, |
||
342 | TTextType $shortDesc = null, |
||
343 | TTextType $longDesc = null |
||
344 | ) { |
||
345 | if (!($expectedReturnType instanceof TEntityTypeType) && !($expectedReturnType instanceof TComplexTypeType)) { |
||
346 | $msg = "Expected return type must be either TEntityType or TComplexType"; |
||
347 | throw new \InvalidArgumentException($msg); |
||
348 | } |
||
349 | |||
350 | if (!is_string($name) || empty($name)) { |
||
351 | $msg = "Name must be a non-empty string"; |
||
352 | throw new \InvalidArgumentException($msg); |
||
353 | } |
||
354 | |||
355 | $funcType = new FunctionImportAnonymousType(); |
||
356 | $funcType->setName($name); |
||
357 | |||
358 | $typeName = $expectedReturnType->getName(); |
||
359 | $returnType = new TFunctionImportReturnTypeType(); |
||
360 | $returnType->setType($typeName); |
||
361 | $returnType->setEntitySetAttribute($typeName); |
||
362 | assert($returnType->isOK($msg), $msg); |
||
363 | $funcType->addToReturnType($returnType); |
||
364 | $this->addDocumentation($shortDesc, $longDesc, $funcType); |
||
365 | |||
366 | $this->getEdmx()->getDataServiceType()->getSchema()[0]->getEntityContainer()[0]->addToFunctionImport($funcType); |
||
367 | |||
368 | return $funcType; |
||
369 | } |
||
370 | |||
371 | private function initSerialiser() |
||
379 | |||
380 | public function __sleep() |
||
386 | |||
387 | public function __wakeup() |
||
391 | |||
392 | /** |
||
393 | * @param $summary |
||
394 | * @param $longDescription |
||
395 | * @return TDocumentationType |
||
396 | */ |
||
397 | private function generateDocumentation(TTextType $summary, TTextType $longDescription) |
||
404 | |||
405 | /** |
||
406 | * @return string |
||
407 | */ |
||
408 | private function getNamespace() |
||
409 | { |
||
410 | $namespace = $this->V3Edmx->getDataServiceType()->getSchema()[0]->getNamespace(); |
||
411 | if (0 == strlen(trim($namespace))) { |
||
412 | $namespace = ""; |
||
413 | } else { |
||
414 | $namespace .= "."; |
||
415 | } |
||
416 | return $namespace; |
||
417 | } |
||
418 | |||
419 | /** |
||
420 | * @param array $constraintProperty |
||
421 | * @param string $targRole |
||
422 | * @return TReferentialConstraintRoleElementType |
||
423 | */ |
||
424 | protected function makeReferentialConstraint(array $constraintProperty, $targRole) |
||
437 | |||
438 | /** |
||
439 | * @param $summary |
||
440 | * @param $longDescription |
||
441 | * @param $NewEntity |
||
442 | */ |
||
443 | private function addDocumentation($summary, $longDescription, IsOK &$NewEntity) |
||
454 | } |
||
455 |
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()
can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.