Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like SimpleMetadataProvider 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 SimpleMetadataProvider, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class SimpleMetadataProvider implements IMetadataProvider |
||
19 | { |
||
20 | public $OdataEntityMap = []; |
||
21 | protected $resourceSets = []; |
||
22 | protected $resourceTypes = []; |
||
23 | protected $associationSets = []; |
||
24 | protected $containerName; |
||
25 | protected $namespaceName; |
||
26 | private $metadataManager; |
||
27 | private $typeSetMapping = []; |
||
28 | protected $singletons = []; |
||
29 | |||
30 | /** |
||
31 | * @param string $containerName container name for the datasource |
||
32 | * @param string $namespaceName namespace for the datasource |
||
33 | */ |
||
34 | public function __construct($containerName, $namespaceName) |
||
40 | |||
41 | //Begin Implementation of IMetadataProvider |
||
42 | |||
43 | public function getXML() |
||
47 | |||
48 | /** |
||
49 | * get the Container name for the data source. |
||
50 | * |
||
51 | * @return string container name |
||
52 | */ |
||
53 | public function getContainerName() |
||
57 | |||
58 | /** |
||
59 | * get Namespace name for the data source. |
||
60 | * |
||
61 | * @return string namespace |
||
62 | */ |
||
63 | public function getContainerNamespace() |
||
67 | |||
68 | /** |
||
69 | * get all entity set information. |
||
70 | * |
||
71 | * @return ResourceSet[] |
||
72 | */ |
||
73 | public function getResourceSets($params = null) |
||
100 | |||
101 | /** |
||
102 | * get all resource types in the data source. |
||
103 | * |
||
104 | * @return ResourceType[] |
||
105 | */ |
||
106 | public function getTypes() |
||
110 | |||
111 | /** |
||
112 | * get a resource set based on the specified resource set name. |
||
113 | * |
||
114 | * @param string $name Name of the resource set |
||
115 | * |
||
116 | * @return ResourceSet|null resource set with the given name if found else NULL |
||
117 | */ |
||
118 | public function resolveResourceSet($name) |
||
125 | |||
126 | /** |
||
127 | * get a resource type based on the resource type name. |
||
128 | * |
||
129 | * @param string $name Name of the resource type |
||
130 | * |
||
131 | * @return ResourceType|null resource type with the given resource type name if found else NULL |
||
132 | */ |
||
133 | public function resolveResourceType($name) |
||
140 | |||
141 | /** |
||
142 | * get a singelton based on the specified singleton name. |
||
143 | * |
||
144 | * @param string $name Name of the resource set |
||
145 | * |
||
146 | * @return ResourceFunctionType|null singleton with the given name if found else NULL |
||
147 | */ |
||
148 | public function resolveSingleton($name) |
||
155 | |||
156 | /** |
||
157 | * get a resource set based on the specified resource association set name. |
||
158 | * |
||
159 | * @param string $name Name of the resource assocation set |
||
160 | * |
||
161 | * @return ResourceAssociationSet|null resource association set with the given name if found else NULL |
||
162 | */ |
||
163 | public function resolveAssociationSet($name) |
||
170 | |||
171 | /* |
||
172 | * Get number of association sets hooked up |
||
173 | */ |
||
174 | public function getAssociationCount() |
||
178 | |||
179 | /** |
||
180 | * The method must return a collection of all the types derived from |
||
181 | * $resourceType The collection returned should NOT include the type |
||
182 | * passed in as a parameter. |
||
183 | * |
||
184 | * @param ResourceEntityType $resourceType Resource to get derived resource types from |
||
185 | * |
||
186 | * @return ResourceType[] |
||
187 | */ |
||
188 | public function getDerivedTypes(ResourceEntityType $resourceType) |
||
192 | |||
193 | /** |
||
194 | * @param ResourceType $resourceType Resource to check for derived resource types |
||
195 | * |
||
196 | * @return bool true if $resourceType represents an Entity Type which has derived Entity Types, else false |
||
197 | */ |
||
198 | public function hasDerivedTypes(ResourceEntityType $resourceType) |
||
202 | |||
203 | //End Implementation of IMetadataProvider |
||
204 | |||
205 | /** |
||
206 | * Gets the ResourceAssociationSet instance for the given source |
||
207 | * association end. |
||
208 | * |
||
209 | * @param ResourceSet $sourceResourceSet Resource set |
||
210 | * of the source |
||
211 | * association end |
||
212 | * @param ResourceType $sourceResourceType Resource type of the source |
||
213 | * association end |
||
214 | * @param ResourceProperty $targetResourceProperty Resource property of |
||
215 | * the source |
||
216 | * association end |
||
217 | * |
||
218 | * @return ResourceAssociationSet|null |
||
219 | */ |
||
220 | public function getResourceAssociationSet( |
||
259 | |||
260 | /** |
||
261 | * Add an entity type. |
||
262 | * |
||
263 | * @param \ReflectionClass $refClass reflection class of the entity |
||
264 | * @param string $name name of the entity |
||
265 | * @return ResourceType when the name is already in use |
||
266 | * |
||
267 | * @throws InvalidOperationException when the name is already in use |
||
268 | * @internal param string $namespace namespace of the data source |
||
269 | * |
||
270 | */ |
||
271 | public function addEntityType(\ReflectionClass $refClass, $name) |
||
275 | |||
276 | /** |
||
277 | * @param \ReflectionClass $refClass |
||
278 | * @param string $name |
||
279 | * @param $typeKind |
||
280 | * @return ResourceType |
||
281 | * @throws InvalidOperationException |
||
282 | * @internal param null|string $namespace |
||
283 | * @internal param null|ResourceType $baseResourceType |
||
284 | * |
||
285 | */ |
||
286 | private function createResourceType( |
||
318 | |||
319 | /** |
||
320 | * Add a complex type. |
||
321 | * |
||
322 | * @param \ReflectionClass $refClass reflection class of the complex entity type |
||
323 | * @param string $name name of the entity |
||
324 | * @return ResourceType when the name is already in use |
||
325 | * |
||
326 | * @throws InvalidOperationException when the name is already in use |
||
327 | * @internal param string $namespace namespace of the data source |
||
328 | * @internal param ResourceType $baseResourceType base resource type |
||
329 | * |
||
330 | */ |
||
331 | public function addComplexType(\ReflectionClass $refClass, $name) |
||
335 | |||
336 | /** |
||
337 | * @param string $name name of the resource set (now taken from resource type) |
||
338 | * @param ResourceEntityType $resourceType resource type |
||
339 | * |
||
340 | * @throws InvalidOperationException |
||
341 | * |
||
342 | * @return ResourceSet |
||
343 | */ |
||
344 | public function addResourceSet($name, ResourceEntityType $resourceType) |
||
360 | |||
361 | /** |
||
362 | * To add a Key-primitive property to a resource (Complex/Entity). |
||
363 | * |
||
364 | * @param ResourceType $resourceType resource type to which key property |
||
365 | * is to be added |
||
366 | * @param string $name name of the key property |
||
367 | * @param TypeCode $typeCode type of the key property |
||
368 | */ |
||
369 | public function addKeyProperty($resourceType, $name, $typeCode) |
||
373 | |||
374 | /** |
||
375 | * To add a Key/NonKey-primitive property to a resource (complex/entity). |
||
376 | * |
||
377 | * @param ResourceType $resourceType Resource type |
||
378 | * @param string $name name of the property |
||
379 | * @param TypeCode $typeCode type of property |
||
380 | * @param bool $isKey property is key or not |
||
381 | * @param bool $isBag property is bag or not |
||
382 | * @param bool $isETagProperty property is etag or not |
||
383 | */ |
||
384 | private function _addPrimitivePropertyInternal( |
||
385 | $resourceType, |
||
386 | $name, |
||
387 | $typeCode, |
||
388 | $isKey = false, |
||
389 | $isBag = false, |
||
390 | $isETagProperty = false, |
||
391 | $defaultValue = null, |
||
392 | $nullable = false |
||
393 | ) { |
||
394 | $this->checkInstanceProperty($name, $resourceType); |
||
395 | |||
396 | // check that property and resource name don't up and collide - would violate OData spec |
||
397 | if (strtolower($name) == strtolower($resourceType->getName())) { |
||
398 | throw new InvalidOperationException( |
||
399 | 'Property name must be different from resource name.' |
||
400 | ); |
||
401 | } |
||
402 | |||
403 | $primitiveResourceType = ResourceType::getPrimitiveResourceType($typeCode); |
||
|
|||
404 | |||
405 | if ($isETagProperty && $isBag) { |
||
406 | throw new InvalidOperationException( |
||
407 | 'Only primitve property can be etag property, bag property cannot be etag property.' |
||
408 | ); |
||
409 | } |
||
410 | |||
411 | $kind = $isKey ? ResourcePropertyKind::PRIMITIVE | ResourcePropertyKind::KEY : ResourcePropertyKind::PRIMITIVE; |
||
412 | if ($isBag) { |
||
413 | $kind = $kind | ResourcePropertyKind::BAG; |
||
414 | } |
||
415 | |||
416 | if ($isETagProperty) { |
||
417 | $kind = $kind | ResourcePropertyKind::ETAG; |
||
418 | } |
||
419 | |||
420 | $resourceProperty = new ResourceProperty($name, null, $kind, $primitiveResourceType); |
||
421 | $resourceType->addProperty($resourceProperty); |
||
422 | if (array_key_exists($resourceType->getFullName(), $this->OdataEntityMap)) { |
||
423 | $this->metadataManager->addPropertyToEntityType( |
||
424 | $this->OdataEntityMap[$resourceType->getFullName()], |
||
425 | $name, |
||
426 | $primitiveResourceType->getFullName(), |
||
427 | $defaultValue, |
||
428 | $nullable, |
||
429 | $isKey |
||
430 | ); |
||
431 | } |
||
432 | } |
||
433 | |||
434 | /** |
||
435 | * @param string $name |
||
436 | * @param ResourceType $resourceType |
||
437 | * |
||
438 | * @throws InvalidOperationException |
||
439 | */ |
||
440 | private function checkInstanceProperty($name, ResourceType $resourceType) |
||
441 | { |
||
442 | $instance = $resourceType->getInstanceType(); |
||
443 | $hasMagicGetter = $instance instanceof IType || $instance->hasMethod('__get'); |
||
444 | |||
445 | if (!$hasMagicGetter) { |
||
446 | try { |
||
447 | if ($instance instanceof \ReflectionClass) { |
||
448 | $instance->getProperty($name); |
||
449 | } |
||
450 | } catch (\ReflectionException $exception) { |
||
451 | throw new InvalidOperationException( |
||
452 | 'Can\'t add a property which does not exist on the instance type.' |
||
453 | ); |
||
454 | } |
||
455 | } |
||
456 | } |
||
457 | |||
458 | /** |
||
459 | * To add a NonKey-primitive property (Complex/Entity). |
||
460 | * |
||
461 | * @param ResourceType $resourceType resource type to which key property |
||
462 | * is to be added |
||
463 | * @param string $name name of the key property |
||
464 | * @param TypeCode $typeCode type of the key property |
||
465 | * @param bool $isBag property is bag or not |
||
466 | */ |
||
467 | View Code Duplication | public function addPrimitiveProperty( |
|
486 | |||
487 | /** |
||
488 | * To add a non-key etag property. |
||
489 | * |
||
490 | * @param ResourceType $resourceType resource type to which key property |
||
491 | * is to be added |
||
492 | * @param string $name name of the property |
||
493 | * @param TypeCode $typeCode type of the etag property |
||
494 | */ |
||
495 | View Code Duplication | public function addETagProperty($resourceType, $name, $typeCode, $defaultValue = null, $nullable = false) |
|
508 | |||
509 | /** |
||
510 | * To add a resource reference property. |
||
511 | * |
||
512 | * @param ResourceEntityType $resourceType The resource type to add the resource |
||
513 | * reference property to |
||
514 | * @param string $name The name of the property to add |
||
515 | * @param ResourceSet $targetResourceSet The resource set the resource reference |
||
516 | * property points to |
||
517 | */ |
||
518 | public function addResourceReferenceProperty($resourceType, $name, $targetResourceSet) |
||
527 | |||
528 | /** |
||
529 | * To add a 1:N resource reference property. |
||
530 | * |
||
531 | * @param ResourceType $sourceResourceType The resource type to add the resource |
||
532 | * reference property from |
||
533 | * @param ResourceType $targetResourceType The resource type to add the resource |
||
534 | * reference property to |
||
535 | * @param string $sourceProperty The name of the property to add, on source type |
||
536 | * @param string $targetProperty The name of the property to add, on target type |
||
537 | */ |
||
538 | View Code Duplication | public function addResourceReferencePropertyBidirectional( |
|
539 | ResourceEntityType $sourceResourceType, |
||
540 | ResourceEntityType $targetResourceType, |
||
541 | $sourceProperty, |
||
542 | $targetProperty |
||
543 | ) { |
||
544 | $this->_addReferencePropertyInternalBidirectional( |
||
545 | $sourceResourceType, |
||
546 | $targetResourceType, |
||
547 | $sourceProperty, |
||
548 | $targetProperty, |
||
549 | '1', |
||
550 | '*' |
||
551 | ); |
||
552 | // verify resource property types are what we expect them to be |
||
553 | $sourceResourceKind = $sourceResourceType->resolveProperty($sourceProperty)->getKind(); |
||
554 | assert( |
||
555 | ResourcePropertyKind::RESOURCE_REFERENCE == $sourceResourceKind, |
||
556 | "1 side of 1:N relationship not pointing to resource reference" |
||
557 | ); |
||
558 | $targetResourceKind = $targetResourceType->resolveProperty($targetProperty)->getKind(); |
||
559 | assert( |
||
560 | ResourcePropertyKind::RESOURCESET_REFERENCE == $targetResourceKind, |
||
561 | "N side of 1:N relationship not pointing to resource set reference" |
||
562 | ); |
||
563 | } |
||
564 | |||
565 | /** |
||
566 | * To add a navigation property (resource set or resource reference) |
||
567 | * to a resource type. |
||
568 | * |
||
569 | * @param ResourceEntityType $sourceResourceType The resource type to add |
||
570 | * the resource reference |
||
571 | * or resource |
||
572 | * reference set property to |
||
573 | * @param string $name The name of the |
||
574 | * property to add |
||
575 | * @param ResourceSet $targetResourceSet The resource set the |
||
576 | * resource reference |
||
577 | * or reference |
||
578 | * set property |
||
579 | * points to |
||
580 | * @param string $resourceMult The multiplicity of relation being added |
||
581 | */ |
||
582 | private function _addReferencePropertyInternal( |
||
637 | |||
638 | /** |
||
639 | * To add a navigation property (resource set or resource reference) |
||
640 | * to a resource type. |
||
641 | * |
||
642 | * @param ResourceEntityType $sourceResourceType The source resource type to add |
||
643 | * the resource reference |
||
644 | * or resource reference set property to |
||
645 | * @param ResourceEntityType $targetResourceType The target resource type to add |
||
646 | * the resource reference |
||
647 | * or resource reference set property to |
||
648 | * @param string $sourceProperty The name of the |
||
649 | * property to add to source type |
||
650 | * @param string $targetProperty The name of the |
||
651 | * property to add to target type |
||
652 | * @param string $sourceMultiplicity The multiplicity at the source end of relation |
||
653 | * @param string $targetMultiplicity The multiplicity at the target end of relation |
||
654 | */ |
||
655 | private function _addReferencePropertyInternalBidirectional( |
||
748 | |||
749 | /** |
||
750 | * To add a resource set reference property. |
||
751 | * |
||
752 | * @param ResourceEntityType $resourceType The resource type to add the |
||
753 | * resource reference set property to |
||
754 | * @param string $name The name of the property to add |
||
755 | * @param ResourceSet $targetResourceSet The resource set the resource |
||
756 | * reference set property points to |
||
757 | */ |
||
758 | public function addResourceSetReferenceProperty(ResourceEntityType $resourceType, $name, $targetResourceSet) |
||
767 | |||
768 | /** |
||
769 | * To add a M:N resource reference property. |
||
770 | * |
||
771 | * @param ResourceEntityType $sourceResourceType The resource type to add the resource |
||
772 | * reference property from |
||
773 | * @param ResourceEntityType $targetResourceType The resource type to add the resource |
||
774 | * reference property to |
||
775 | * @param string $sourceProperty The name of the property to add, on source type |
||
776 | * @param string $targetProperty The name of the property to add, on target type |
||
777 | */ |
||
778 | View Code Duplication | public function addResourceSetReferencePropertyBidirectional( |
|
804 | |||
805 | /** |
||
806 | * To add a 1-1 resource reference. |
||
807 | * |
||
808 | * @param ResourceEntityType $sourceResourceType The resource type to add the resource |
||
809 | * reference property from |
||
810 | * @param ResourceEntityType $targetResourceType The resource type to add the resource |
||
811 | * reference property to |
||
812 | * @param string $sourceProperty The name of the property to add, on source type |
||
813 | * @param string $targetProperty The name of the property to add, on target type |
||
814 | */ |
||
815 | View Code Duplication | public function addResourceReferenceSinglePropertyBidirectional( |
|
841 | |||
842 | /** |
||
843 | * To add a complex property to entity or complex type. |
||
844 | * |
||
845 | * @param ResourceType $targetResourceType The resource type to which the complex property needs to add |
||
846 | * @param string $name name of the complex property |
||
847 | * @param ResourceComplexType $complexResourceType complex resource type |
||
848 | * @param bool $isBag complex type is bag or not |
||
849 | * |
||
850 | * @return ResourceProperty |
||
851 | */ |
||
852 | public function addComplexProperty( |
||
883 | |||
884 | public function createSingleton($name, ResourceType $returnType, $functionName) |
||
908 | |||
909 | public function getSingletons() |
||
913 | |||
914 | public function callSingleton($name) |
||
923 | } |
||
924 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: