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 | ||
| 13 | class SimpleMetadataProvider implements IMetadataProvider | ||
| 14 | { | ||
| 15 | public $OdataEntityMap = []; | ||
| 16 | protected $resourceSets = []; | ||
| 17 | protected $resourceTypes = []; | ||
| 18 | protected $associationSets = []; | ||
| 19 | protected $containerName; | ||
| 20 | protected $namespaceName; | ||
| 21 | private $metadataManager; | ||
| 22 | |||
| 23 | /** | ||
| 24 | * @param string $containerName container name for the datasource | ||
| 25 | * @param string $namespaceName namespace for the datasource | ||
| 26 | */ | ||
| 27 | public function __construct($containerName, $namespaceName) | ||
| 28 |     { | ||
| 29 | $this->containerName = $containerName; | ||
| 30 | $this->namespaceName = $namespaceName; | ||
| 31 | $this->metadataManager = new MetadataManager($namespaceName, $containerName); | ||
| 32 | } | ||
| 33 | |||
| 34 | //Begin Implementation of IMetadataProvider | ||
| 35 | |||
| 36 | public function getXML() | ||
| 40 | |||
| 41 | /** | ||
| 42 | * get the Container name for the data source. | ||
| 43 | * | ||
| 44 | * @return string container name | ||
| 45 | */ | ||
| 46 | public function getContainerName() | ||
| 50 | |||
| 51 | /** | ||
| 52 | * get Namespace name for the data source. | ||
| 53 | * | ||
| 54 | * @return string namespace | ||
| 55 | */ | ||
| 56 | public function getContainerNamespace() | ||
| 60 | |||
| 61 | /** | ||
| 62 | * get all entity set information. | ||
| 63 | * | ||
| 64 | * @return ResourceSet[] | ||
| 65 | */ | ||
| 66 | public function getResourceSets($params = null) | ||
| 93 | |||
| 94 | /** | ||
| 95 | * get all resource types in the data source. | ||
| 96 | * | ||
| 97 | * @return ResourceType[] | ||
| 98 | */ | ||
| 99 | public function getTypes() | ||
| 103 | |||
| 104 | /** | ||
| 105 | * get a resource set based on the specified resource set name. | ||
| 106 | * | ||
| 107 | * @param string $name Name of the resource set | ||
| 108 | * | ||
| 109 | * @return ResourceSet|null resource set with the given name if found else NULL | ||
| 110 | */ | ||
| 111 | public function resolveResourceSet($name) | ||
| 118 | |||
| 119 | /** | ||
| 120 | * get a resource type based on the resource type name. | ||
| 121 | * | ||
| 122 | * @param string $name Name of the resource type | ||
| 123 | * | ||
| 124 | * @return ResourceType|null resource type with the given resource type name if found else NULL | ||
| 125 | */ | ||
| 126 | public function resolveResourceType($name) | ||
| 133 | |||
| 134 | /** | ||
| 135 | * get a resource set based on the specified resource association set name. | ||
| 136 | * | ||
| 137 | * @param string $name Name of the resource assocation set | ||
| 138 | * | ||
| 139 | * @return ResourceAssociationSet|null resource association set with the given name if found else NULL | ||
| 140 | */ | ||
| 141 | public function resolveAssociationSet($name) | ||
| 148 | |||
| 149 | /* | ||
| 150 | * Get number of association sets hooked up | ||
| 151 | */ | ||
| 152 | public function getAssociationCount() | ||
| 156 | |||
| 157 | /** | ||
| 158 | * The method must return a collection of all the types derived from | ||
| 159 | * $resourceType The collection returned should NOT include the type | ||
| 160 | * passed in as a parameter. | ||
| 161 | * | ||
| 162 | * @param ResourceType $resourceType Resource to get derived resource types from | ||
| 163 | * | ||
| 164 | * @return ResourceType[] | ||
| 165 | */ | ||
| 166 | public function getDerivedTypes(ResourceType $resourceType) | ||
| 170 | |||
| 171 | /** | ||
| 172 | * @param ResourceType $resourceType Resource to check for derived resource types | ||
| 173 | * | ||
| 174 | * @return bool true if $resourceType represents an Entity Type which has derived Entity Types, else false | ||
| 175 | */ | ||
| 176 | public function hasDerivedTypes(ResourceType $resourceType) | ||
| 180 | |||
| 181 | //End Implementation of IMetadataProvider | ||
| 182 | |||
| 183 | /** | ||
| 184 | * Gets the ResourceAssociationSet instance for the given source | ||
| 185 | * association end. | ||
| 186 | * | ||
| 187 | * @param ResourceSet $sourceResourceSet Resource set | ||
| 188 | * of the source | ||
| 189 | * association end | ||
| 190 | * @param ResourceType $sourceResourceType Resource type of the source | ||
| 191 | * association end | ||
| 192 | * @param ResourceProperty $targetResourceProperty Resource property of | ||
| 193 | * the source | ||
| 194 | * association end | ||
| 195 | * | ||
| 196 | * @return ResourceAssociationSet|null | ||
| 197 | */ | ||
| 198 | public function getResourceAssociationSet( | ||
| 237 | |||
| 238 | /** | ||
| 239 | * Add an entity type. | ||
| 240 | * | ||
| 241 | * @param \ReflectionClass $refClass reflection class of the entity | ||
| 242 | * @param string $name name of the entity | ||
| 243 | * @param string $namespace namespace of the data source | ||
| 244 | * | ||
| 245 | * @throws InvalidOperationException when the name is already in use | ||
| 246 | * | ||
| 247 | * @return ResourceType | ||
| 248 | */ | ||
| 249 | public function addEntityType(\ReflectionClass $refClass, $name, $namespace = null) | ||
| 253 | |||
| 254 | /** | ||
| 255 | * @param \ReflectionClass $refClass | ||
| 256 | * @param string $name | ||
| 257 | * @param string|null $namespace | ||
| 258 | * @param $typeKind | ||
| 259 | * @param null|ResourceType $baseResourceType | ||
| 260 | * | ||
| 261 | * @throws InvalidOperationException | ||
| 262 | * | ||
| 263 | * @return ResourceType | ||
| 264 | */ | ||
| 265 | private function createResourceType( | ||
| 287 | |||
| 288 | /** | ||
| 289 | * Add a complex type. | ||
| 290 | * | ||
| 291 | * @param \ReflectionClass $refClass reflection class of the complex entity type | ||
| 292 | * @param string $name name of the entity | ||
| 293 | * @param string $namespace namespace of the data source | ||
| 294 | * @param ResourceType $baseResourceType base resource type | ||
| 295 | * | ||
| 296 | * @throws InvalidOperationException when the name is already in use | ||
| 297 | * | ||
| 298 | * @return ResourceType | ||
| 299 | */ | ||
| 300 | public function addComplexType(\ReflectionClass $refClass, $name, $namespace = null, $baseResourceType = null) | ||
| 304 | |||
| 305 | /** | ||
| 306 | * @param string $name name of the resource set | ||
| 307 | * @param ResourceType $resourceType resource type | ||
| 308 | * | ||
| 309 | * @throws InvalidOperationException | ||
| 310 | * | ||
| 311 | * @return ResourceSet | ||
| 312 | */ | ||
| 313 | public function addResourceSet($name, ResourceType $resourceType) | ||
| 327 | |||
| 328 | /** | ||
| 329 | * To add a Key-primitive property to a resource (Complex/Entity). | ||
| 330 | * | ||
| 331 | * @param ResourceType $resourceType resource type to which key property | ||
| 332 | * is to be added | ||
| 333 | * @param string $name name of the key property | ||
| 334 | * @param TypeCode $typeCode type of the key property | ||
| 335 | */ | ||
| 336 | public function addKeyProperty($resourceType, $name, $typeCode) | ||
| 340 | |||
| 341 | /** | ||
| 342 | * To add a Key/NonKey-primitive property to a resource (complex/entity). | ||
| 343 | * | ||
| 344 | * @param ResourceType $resourceType Resource type | ||
| 345 | * @param string $name name of the property | ||
| 346 | * @param TypeCode $typeCode type of property | ||
| 347 | * @param bool $isKey property is key or not | ||
| 348 | * @param bool $isBag property is bag or not | ||
| 349 | * @param bool $isETagProperty property is etag or not | ||
| 350 | */ | ||
| 351 | private function _addPrimitivePropertyInternal( | ||
| 352 | $resourceType, | ||
| 353 | $name, | ||
| 354 | $typeCode, | ||
| 355 | $isKey = false, | ||
| 356 | $isBag = false, | ||
| 357 | $isETagProperty = false | ||
| 358 |     ) { | ||
| 359 | $this->checkInstanceProperty($name, $resourceType); | ||
| 360 | |||
| 361 | // check that property and resource name don't up and collide - would violate OData spec | ||
| 362 |         if (strtolower($name) == strtolower($resourceType->getName())) { | ||
| 363 | throw new InvalidOperationException( | ||
| 364 | 'Property name must be different from resource name.' | ||
| 365 | ); | ||
| 366 | } | ||
| 367 | |||
| 368 | $primitiveResourceType = ResourceType::getPrimitiveResourceType($typeCode); | ||
|  | |||
| 369 | |||
| 370 |         if ($isETagProperty && $isBag) { | ||
| 371 | throw new InvalidOperationException( | ||
| 372 | 'Only primitve property can be etag property, bag property cannot be etag property.' | ||
| 373 | ); | ||
| 374 | } | ||
| 375 | |||
| 376 | $kind = $isKey ? ResourcePropertyKind::PRIMITIVE | ResourcePropertyKind::KEY : ResourcePropertyKind::PRIMITIVE; | ||
| 377 |         if ($isBag) { | ||
| 378 | $kind = $kind | ResourcePropertyKind::BAG; | ||
| 379 | } | ||
| 380 | |||
| 381 |         if ($isETagProperty) { | ||
| 382 | $kind = $kind | ResourcePropertyKind::ETAG; | ||
| 383 | } | ||
| 384 | |||
| 385 | $resourceProperty = new ResourceProperty($name, null, $kind, $primitiveResourceType); | ||
| 386 | $resourceType->addProperty($resourceProperty); | ||
| 387 |         if (array_key_exists($resourceType->getFullName(), $this->OdataEntityMap)) { | ||
| 388 | $this->metadataManager->addPropertyToEntityType( | ||
| 389 | $this->OdataEntityMap[$resourceType->getFullName()], | ||
| 390 | $name, | ||
| 391 | $primitiveResourceType->getFullName(), | ||
| 392 | null, | ||
| 393 | false, | ||
| 394 | $isKey | ||
| 395 | ); | ||
| 396 | } | ||
| 397 | } | ||
| 398 | |||
| 399 | /** | ||
| 400 | * @param string $name | ||
| 401 | * @param ResourceType $resourceType | ||
| 402 | * | ||
| 403 | * @throws InvalidOperationException | ||
| 404 | */ | ||
| 405 | private function checkInstanceProperty($name, ResourceType $resourceType) | ||
| 422 | |||
| 423 | /** | ||
| 424 | * To add a NonKey-primitive property (Complex/Entity). | ||
| 425 | * | ||
| 426 | * @param ResourceType $resourceType resource type to which key property | ||
| 427 | * is to be added | ||
| 428 | * @param string $name name of the key property | ||
| 429 | * @param TypeCode $typeCode type of the key property | ||
| 430 | * @param bool $isBag property is bag or not | ||
| 431 | */ | ||
| 432 | public function addPrimitiveProperty($resourceType, $name, $typeCode, $isBag = false) | ||
| 436 | |||
| 437 | /** | ||
| 438 | * To add a non-key etag property. | ||
| 439 | * | ||
| 440 | * @param ResourceType $resourceType resource type to which key property | ||
| 441 | * is to be added | ||
| 442 | * @param string $name name of the property | ||
| 443 | * @param TypeCode $typeCode type of the etag property | ||
| 444 | */ | ||
| 445 | public function addETagProperty($resourceType, $name, $typeCode) | ||
| 449 | |||
| 450 | /** | ||
| 451 | * To add a resource reference property. | ||
| 452 | * | ||
| 453 | * @param ResourceType $resourceType The resource type to add the resource | ||
| 454 | * reference property to | ||
| 455 | * @param string $name The name of the property to add | ||
| 456 | * @param ResourceSet $targetResourceSet The resource set the resource reference | ||
| 457 | * property points to | ||
| 458 | */ | ||
| 459 | public function addResourceReferenceProperty($resourceType, $name, $targetResourceSet) | ||
| 468 | |||
| 469 | /** | ||
| 470 | * To add a 1:N resource reference property. | ||
| 471 | * | ||
| 472 | * @param ResourceType $sourceResourceType The resource type to add the resource | ||
| 473 | * reference property from | ||
| 474 | * @param ResourceType $targetResourceType The resource type to add the resource | ||
| 475 | * reference property to | ||
| 476 | * @param string $sourceProperty The name of the property to add, on source type | ||
| 477 | * @param string $targetProperty The name of the property to add, on target type | ||
| 478 | */ | ||
| 479 | public function addResourceReferencePropertyBidirectional( | ||
| 494 | |||
| 495 | /** | ||
| 496 | * To add a navigation property (resource set or resource reference) | ||
| 497 | * to a resource type. | ||
| 498 | * | ||
| 499 | * @param ResourceType $sourceResourceType The resource type to add | ||
| 500 | * the resource reference | ||
| 501 | * or resource | ||
| 502 | * reference set property to | ||
| 503 | * @param string $name The name of the | ||
| 504 | * property to add | ||
| 505 | * @param ResourceSet $targetResourceSet The resource set the | ||
| 506 | * resource reference | ||
| 507 | * or reference | ||
| 508 | * set property | ||
| 509 | * points to | ||
| 510 | * @param ResourcePropertyKind $resourcePropertyKind The property kind | ||
| 511 | */ | ||
| 512 | private function _addReferencePropertyInternal( | ||
| 559 | |||
| 560 | /** | ||
| 561 | * To add a navigation property (resource set or resource reference) | ||
| 562 | * to a resource type. | ||
| 563 | * | ||
| 564 | * @param ResourceType $sourceResourceType The source resource type to add | ||
| 565 | * the resource reference | ||
| 566 | * or resource reference set property to | ||
| 567 | * @param ResourceType $targetResourceType The target resource type to add | ||
| 568 | * the resource reference | ||
| 569 | * or resource reference set property to | ||
| 570 | * @param string $sourceProperty The name of the | ||
| 571 | * property to add to source type | ||
| 572 | * @param string $targetProperty The name of the | ||
| 573 | * property to add to target type | ||
| 574 | * @param ResourcePropertyKind $sourcePropertyKind The property kind on the source type | ||
| 575 | * @param ResourcePropertyKind $targetPropertyKind The property kind on the target type | ||
| 576 | */ | ||
| 577 | private function _addReferencePropertyInternalBidirectional( | ||
| 578 | ResourceType $sourceResourceType, | ||
| 579 | ResourceType $targetResourceType, | ||
| 580 | $sourceProperty, | ||
| 581 | $targetProperty, | ||
| 582 | $sourcePropertyKind, | ||
| 583 | $targetPropertyKind | ||
| 584 |     ) { | ||
| 585 |         if (!is_string($sourceProperty) || !is_string($targetProperty)) { | ||
| 586 |             throw new InvalidOperationException("Source and target properties must both be strings"); | ||
| 587 | } | ||
| 588 | |||
| 589 | $this->checkInstanceProperty($sourceProperty, $sourceResourceType); | ||
| 590 | $this->checkInstanceProperty($targetProperty, $targetResourceType); | ||
| 591 | |||
| 592 | // check that property and resource name don't up and collide - would violate OData spec | ||
| 593 |         if (strtolower($sourceProperty) == strtolower($sourceResourceType->getName())) { | ||
| 594 | throw new InvalidOperationException( | ||
| 595 | 'Source property name must be different from source resource name.' | ||
| 596 | ); | ||
| 597 | } | ||
| 598 |         if (strtolower($targetProperty) == strtolower($targetResourceType->getName())) { | ||
| 599 | throw new InvalidOperationException( | ||
| 600 | 'Target property name must be different from target resource name.' | ||
| 601 | ); | ||
| 602 | } | ||
| 603 | |||
| 604 | //Create instance of AssociationSet for this relationship | ||
| 605 | $sourceResourceSet = $sourceResourceType->getCustomState(); | ||
| 606 |         if (!$sourceResourceSet instanceof ResourceSet) { | ||
| 607 | throw new InvalidOperationException( | ||
| 608 | 'Failed to retrieve the custom state from ' | ||
| 609 | . $sourceResourceType->getName() | ||
| 610 | ); | ||
| 611 | } | ||
| 612 | $targetResourceSet = $targetResourceType->getCustomState(); | ||
| 613 |         if (!$targetResourceSet instanceof ResourceSet) { | ||
| 614 | throw new InvalidOperationException( | ||
| 615 | 'Failed to retrieve the custom state from ' | ||
| 616 | . $targetResourceType->getName() | ||
| 617 | ); | ||
| 618 | } | ||
| 619 | |||
| 620 | //Customer_Orders_Orders, Order_Customer_Customers | ||
| 621 | $fwdSetKey = ResourceAssociationSet::keyName($sourceResourceType, $sourceProperty, $targetResourceSet); | ||
| 622 | $revSetKey = ResourceAssociationSet::keyName($targetResourceType, $targetProperty, $sourceResourceSet); | ||
| 623 |         if (isset($this->associationSets[$fwdSetKey]) && $this->associationSets[$revSetKey]) { | ||
| 624 | return; | ||
| 625 | } | ||
| 626 | |||
| 627 | $sourceResourceProperty = new ResourceProperty($sourceProperty, null, $sourcePropertyKind, $targetResourceType); | ||
| 628 | $sourceResourceType->addProperty($sourceResourceProperty, false); | ||
| 629 | $targetResourceProperty = new ResourceProperty($targetProperty, null, $targetPropertyKind, $sourceResourceType); | ||
| 630 | $targetResourceType->addProperty($targetResourceProperty, false); | ||
| 631 | |||
| 632 | |||
| 633 | $fwdSet = new ResourceAssociationSet( | ||
| 634 | $fwdSetKey, | ||
| 635 | new ResourceAssociationSetEnd($sourceResourceSet, $sourceResourceType, $sourceResourceProperty), | ||
| 636 | new ResourceAssociationSetEnd($targetResourceSet, $targetResourceType, $targetResourceProperty) | ||
| 637 | ); | ||
| 638 | $revSet = new ResourceAssociationSet( | ||
| 639 | $revSetKey, | ||
| 640 | new ResourceAssociationSetEnd($targetResourceSet, $targetResourceType, $targetResourceProperty), | ||
| 641 | new ResourceAssociationSetEnd($sourceResourceSet, $sourceResourceType, $sourceResourceProperty) | ||
| 642 | ); | ||
| 643 | $sourceName = $sourceResourceType->getFullName(); | ||
| 644 | $targetName = $targetResourceType->getFullName(); | ||
| 645 | $sourceMult = $sourcePropertyKind == ResourcePropertyKind::RESOURCESET_REFERENCE ? '*' : '0..1'; | ||
| 646 | $targetMult = $targetPropertyKind == ResourcePropertyKind::RESOURCESET_REFERENCE ? '*' : '0..1'; | ||
| 647 | $this->metadataManager->addNavigationPropertyToEntityType( | ||
| 648 | $this->OdataEntityMap[$sourceName], | ||
| 649 | $sourceMult, | ||
| 650 | $sourceProperty, | ||
| 651 | $this->OdataEntityMap[$targetName], | ||
| 652 | $targetMult, | ||
| 653 | $targetProperty | ||
| 654 | ); | ||
| 655 | $this->associationSets[$fwdSetKey] = $fwdSet; | ||
| 656 | $this->associationSets[$revSetKey] = $revSet; | ||
| 657 | } | ||
| 658 | |||
| 659 | /** | ||
| 660 | * To add a resource set reference property. | ||
| 661 | * | ||
| 662 | * @param ResourceType $resourceType The resource type to add the | ||
| 663 | * resource reference set property to | ||
| 664 | * @param string $name The name of the property to add | ||
| 665 | * @param ResourceSet $targetResourceSet The resource set the resource | ||
| 666 | * reference set property points to | ||
| 667 | */ | ||
| 668 | public function addResourceSetReferenceProperty($resourceType, $name, $targetResourceSet) | ||
| 677 | |||
| 678 | /** | ||
| 679 | * To add a M:N resource reference property. | ||
| 680 | * | ||
| 681 | * @param ResourceType $sourceResourceType The resource type to add the resource | ||
| 682 | * reference property from | ||
| 683 | * @param ResourceType $targetResourceType The resource type to add the resource | ||
| 684 | * reference property to | ||
| 685 | * @param string $sourceProperty The name of the property to add, on source type | ||
| 686 | * @param string $targetProperty The name of the property to add, on target type | ||
| 687 | */ | ||
| 688 | public function addResourceSetReferencePropertyBidirectional( | ||
| 703 | |||
| 704 | /** | ||
| 705 | * To add a 1-1 resource reference. | ||
| 706 | * | ||
| 707 | * @param ResourceType $sourceResourceType The resource type to add the resource | ||
| 708 | * reference property from | ||
| 709 | * @param ResourceType $targetResourceType The resource type to add the resource | ||
| 710 | * reference property to | ||
| 711 | * @param string $sourceProperty The name of the property to add, on source type | ||
| 712 | * @param string $targetProperty The name of the property to add, on target type | ||
| 713 | */ | ||
| 714 | public function addResourceReferenceSinglePropertyBidirectional( | ||
| 729 | |||
| 730 | /** | ||
| 731 | * To add a complex property to entity or complex type. | ||
| 732 | * | ||
| 733 | * @param ResourceType $resourceType The resource type to which the | ||
| 734 | * complex property needs to add | ||
| 735 | * @param string $name name of the complex property | ||
| 736 | * @param ResourceType $complexResourceType complex resource type | ||
| 737 | * @param bool $isBag complex type is bag or not | ||
| 738 | * | ||
| 739 | * @return ResourceProperty | ||
| 740 | */ | ||
| 741 | public function addComplexProperty($resourceType, $name, $complexResourceType, $isBag = false) | ||
| 768 | } | ||
| 769 | 
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: