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 |
||
| 16 | class SimpleMetadataProvider implements IMetadataProvider |
||
| 17 | { |
||
| 18 | public $OdataEntityMap = []; |
||
| 19 | protected $resourceSets = []; |
||
| 20 | protected $resourceTypes = []; |
||
| 21 | protected $associationSets = []; |
||
| 22 | protected $containerName; |
||
| 23 | protected $namespaceName; |
||
| 24 | private $metadataManager; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @param string $containerName container name for the datasource |
||
| 28 | * @param string $namespaceName namespace for the datasource |
||
| 29 | */ |
||
| 30 | public function __construct($containerName, $namespaceName) |
||
| 36 | |||
| 37 | //Begin Implementation of IMetadataProvider |
||
| 38 | |||
| 39 | public function getXML() |
||
| 43 | |||
| 44 | /** |
||
| 45 | * get the Container name for the data source. |
||
| 46 | * |
||
| 47 | * @return string container name |
||
| 48 | */ |
||
| 49 | public function getContainerName() |
||
| 53 | |||
| 54 | /** |
||
| 55 | * get Namespace name for the data source. |
||
| 56 | * |
||
| 57 | * @return string namespace |
||
| 58 | */ |
||
| 59 | public function getContainerNamespace() |
||
| 63 | |||
| 64 | /** |
||
| 65 | * get all entity set information. |
||
| 66 | * |
||
| 67 | * @return ResourceSet[] |
||
| 68 | */ |
||
| 69 | public function getResourceSets($params = null) |
||
| 96 | |||
| 97 | /** |
||
| 98 | * get all resource types in the data source. |
||
| 99 | * |
||
| 100 | * @return ResourceType[] |
||
| 101 | */ |
||
| 102 | public function getTypes() |
||
| 106 | |||
| 107 | /** |
||
| 108 | * get a resource set based on the specified resource set name. |
||
| 109 | * |
||
| 110 | * @param string $name Name of the resource set |
||
| 111 | * |
||
| 112 | * @return ResourceSet|null resource set with the given name if found else NULL |
||
| 113 | */ |
||
| 114 | public function resolveResourceSet($name) |
||
| 121 | |||
| 122 | /** |
||
| 123 | * get a resource type based on the resource type name. |
||
| 124 | * |
||
| 125 | * @param string $name Name of the resource type |
||
| 126 | * |
||
| 127 | * @return ResourceType|null resource type with the given resource type name if found else NULL |
||
| 128 | */ |
||
| 129 | public function resolveResourceType($name) |
||
| 136 | |||
| 137 | /** |
||
| 138 | * get a resource set based on the specified resource association set name. |
||
| 139 | * |
||
| 140 | * @param string $name Name of the resource assocation set |
||
| 141 | * |
||
| 142 | * @return ResourceAssociationSet|null resource association set with the given name if found else NULL |
||
| 143 | */ |
||
| 144 | public function resolveAssociationSet($name) |
||
| 151 | |||
| 152 | /* |
||
| 153 | * Get number of association sets hooked up |
||
| 154 | */ |
||
| 155 | public function getAssociationCount() |
||
| 159 | |||
| 160 | /** |
||
| 161 | * The method must return a collection of all the types derived from |
||
| 162 | * $resourceType The collection returned should NOT include the type |
||
| 163 | * passed in as a parameter. |
||
| 164 | * |
||
| 165 | * @param ResourceEntityType $resourceType Resource to get derived resource types from |
||
| 166 | * |
||
| 167 | * @return ResourceType[] |
||
| 168 | */ |
||
| 169 | public function getDerivedTypes(ResourceEntityType $resourceType) |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @param ResourceType $resourceType Resource to check for derived resource types |
||
| 176 | * |
||
| 177 | * @return bool true if $resourceType represents an Entity Type which has derived Entity Types, else false |
||
| 178 | */ |
||
| 179 | public function hasDerivedTypes(ResourceEntityType $resourceType) |
||
| 183 | |||
| 184 | //End Implementation of IMetadataProvider |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Gets the ResourceAssociationSet instance for the given source |
||
| 188 | * association end. |
||
| 189 | * |
||
| 190 | * @param ResourceSet $sourceResourceSet Resource set |
||
| 191 | * of the source |
||
| 192 | * association end |
||
| 193 | * @param ResourceType $sourceResourceType Resource type of the source |
||
| 194 | * association end |
||
| 195 | * @param ResourceProperty $targetResourceProperty Resource property of |
||
| 196 | * the source |
||
| 197 | * association end |
||
| 198 | * |
||
| 199 | * @return ResourceAssociationSet|null |
||
| 200 | */ |
||
| 201 | public function getResourceAssociationSet( |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Add an entity type. |
||
| 243 | * |
||
| 244 | * @param \ReflectionClass $refClass reflection class of the entity |
||
| 245 | * @param string $name name of the entity |
||
| 246 | * @return ResourceType when the name is already in use |
||
| 247 | * |
||
| 248 | * @throws InvalidOperationException when the name is already in use |
||
| 249 | * @internal param string $namespace namespace of the data source |
||
| 250 | * |
||
| 251 | */ |
||
| 252 | public function addEntityType(\ReflectionClass $refClass, $name) |
||
| 253 | { |
||
| 254 | return $this->createResourceType($refClass, $name, ResourceTypeKind::ENTITY); |
||
| 255 | } |
||
| 256 | |||
| 257 | /** |
||
| 258 | * @param \ReflectionClass $refClass |
||
| 259 | * @param string $name |
||
| 260 | * @param $typeKind |
||
| 261 | * @return ResourceType |
||
| 262 | * @throws InvalidOperationException |
||
| 263 | * @internal param null|string $namespace |
||
| 264 | * @internal param null|ResourceType $baseResourceType |
||
| 265 | * |
||
| 266 | */ |
||
| 267 | private function createResourceType( |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Add a complex type. |
||
| 297 | * |
||
| 298 | * @param \ReflectionClass $refClass reflection class of the complex entity type |
||
| 299 | * @param string $name name of the entity |
||
| 300 | * @return ResourceType when the name is already in use |
||
| 301 | * |
||
| 302 | * @throws InvalidOperationException when the name is already in use |
||
| 303 | * @internal param string $namespace namespace of the data source |
||
| 304 | * @internal param ResourceType $baseResourceType base resource type |
||
| 305 | * |
||
| 306 | */ |
||
| 307 | public function addComplexType(\ReflectionClass $refClass, $name) |
||
| 308 | { |
||
| 309 | return $this->createResourceType($refClass, $name, ResourceTypeKind::COMPLEX); |
||
| 310 | } |
||
| 311 | |||
| 312 | /** |
||
| 313 | * @param string $name name of the resource set |
||
| 314 | * @param ResourceEntityType $resourceType resource type |
||
| 315 | * |
||
| 316 | * @throws InvalidOperationException |
||
| 317 | * |
||
| 318 | * @return ResourceSet |
||
| 319 | */ |
||
| 320 | public function addResourceSet($name, ResourceEntityType $resourceType) |
||
| 334 | |||
| 335 | /** |
||
| 336 | * To add a Key-primitive property to a resource (Complex/Entity). |
||
| 337 | * |
||
| 338 | * @param ResourceType $resourceType resource type to which key property |
||
| 339 | * is to be added |
||
| 340 | * @param string $name name of the key property |
||
| 341 | * @param TypeCode $typeCode type of the key property |
||
| 342 | */ |
||
| 343 | public function addKeyProperty($resourceType, $name, $typeCode) |
||
| 347 | |||
| 348 | /** |
||
| 349 | * To add a Key/NonKey-primitive property to a resource (complex/entity). |
||
| 350 | * |
||
| 351 | * @param ResourceType $resourceType Resource type |
||
| 352 | * @param string $name name of the property |
||
| 353 | * @param TypeCode $typeCode type of property |
||
| 354 | * @param bool $isKey property is key or not |
||
| 355 | * @param bool $isBag property is bag or not |
||
| 356 | * @param bool $isETagProperty property is etag or not |
||
| 357 | */ |
||
| 358 | private function _addPrimitivePropertyInternal( |
||
| 405 | |||
| 406 | /** |
||
| 407 | * @param string $name |
||
| 408 | * @param ResourceType $resourceType |
||
| 409 | * |
||
| 410 | * @throws InvalidOperationException |
||
| 411 | */ |
||
| 412 | private function checkInstanceProperty($name, ResourceType $resourceType) |
||
| 429 | |||
| 430 | /** |
||
| 431 | * To add a NonKey-primitive property (Complex/Entity). |
||
| 432 | * |
||
| 433 | * @param ResourceType $resourceType resource type to which key property |
||
| 434 | * is to be added |
||
| 435 | * @param string $name name of the key property |
||
| 436 | * @param TypeCode $typeCode type of the key property |
||
| 437 | * @param bool $isBag property is bag or not |
||
| 438 | */ |
||
| 439 | public function addPrimitiveProperty($resourceType, $name, $typeCode, $isBag = false) |
||
| 443 | |||
| 444 | /** |
||
| 445 | * To add a non-key etag property. |
||
| 446 | * |
||
| 447 | * @param ResourceType $resourceType resource type to which key property |
||
| 448 | * is to be added |
||
| 449 | * @param string $name name of the property |
||
| 450 | * @param TypeCode $typeCode type of the etag property |
||
| 451 | */ |
||
| 452 | public function addETagProperty($resourceType, $name, $typeCode) |
||
| 456 | |||
| 457 | /** |
||
| 458 | * To add a resource reference property. |
||
| 459 | * |
||
| 460 | * @param ResourceEntityType $resourceType The resource type to add the resource |
||
| 461 | * reference property to |
||
| 462 | * @param string $name The name of the property to add |
||
| 463 | * @param ResourceSet $targetResourceSet The resource set the resource reference |
||
| 464 | * property points to |
||
| 465 | */ |
||
| 466 | public function addResourceReferenceProperty($resourceType, $name, $targetResourceSet) |
||
| 475 | |||
| 476 | /** |
||
| 477 | * To add a 1:N resource reference property. |
||
| 478 | * |
||
| 479 | * @param ResourceType $sourceResourceType The resource type to add the resource |
||
| 480 | * reference property from |
||
| 481 | * @param ResourceType $targetResourceType The resource type to add the resource |
||
| 482 | * reference property to |
||
| 483 | * @param string $sourceProperty The name of the property to add, on source type |
||
| 484 | * @param string $targetProperty The name of the property to add, on target type |
||
| 485 | */ |
||
| 486 | public function addResourceReferencePropertyBidirectional( |
||
| 501 | |||
| 502 | /** |
||
| 503 | * To add a navigation property (resource set or resource reference) |
||
| 504 | * to a resource type. |
||
| 505 | * |
||
| 506 | * @param ResourceEntityType $sourceResourceType The resource type to add |
||
| 507 | * the resource reference |
||
| 508 | * or resource |
||
| 509 | * reference set property to |
||
| 510 | * @param string $name The name of the |
||
| 511 | * property to add |
||
| 512 | * @param ResourceSet $targetResourceSet The resource set the |
||
| 513 | * resource reference |
||
| 514 | * or reference |
||
| 515 | * set property |
||
| 516 | * points to |
||
| 517 | * @param ResourcePropertyKind $resourcePropertyKind The property kind |
||
| 518 | */ |
||
| 519 | private function _addReferencePropertyInternal( |
||
| 566 | |||
| 567 | /** |
||
| 568 | * To add a navigation property (resource set or resource reference) |
||
| 569 | * to a resource type. |
||
| 570 | * |
||
| 571 | * @param ResourceEntityType $sourceResourceType The source resource type to add |
||
| 572 | * the resource reference |
||
| 573 | * or resource reference set property to |
||
| 574 | * @param ResourceEntityType $targetResourceType The target resource type to add |
||
| 575 | * the resource reference |
||
| 576 | * or resource reference set property to |
||
| 577 | * @param string $sourceProperty The name of the |
||
| 578 | * property to add to source type |
||
| 579 | * @param string $targetProperty The name of the |
||
| 580 | * property to add to target type |
||
| 581 | * @param ResourcePropertyKind $sourcePropertyKind The property kind on the source type |
||
| 582 | * @param ResourcePropertyKind $targetPropertyKind The property kind on the target type |
||
| 583 | */ |
||
| 584 | private function _addReferencePropertyInternalBidirectional( |
||
| 665 | |||
| 666 | /** |
||
| 667 | * To add a resource set reference property. |
||
| 668 | * |
||
| 669 | * @param ResourceEntityType $resourceType The resource type to add the |
||
| 670 | * resource reference set property to |
||
| 671 | * @param string $name The name of the property to add |
||
| 672 | * @param ResourceSet $targetResourceSet The resource set the resource |
||
| 673 | * reference set property points to |
||
| 674 | */ |
||
| 675 | public function addResourceSetReferenceProperty(ResourceEntityType $resourceType, $name, $targetResourceSet) |
||
| 684 | |||
| 685 | /** |
||
| 686 | * To add a M:N resource reference property. |
||
| 687 | * |
||
| 688 | * @param ResourceEntityType $sourceResourceType The resource type to add the resource |
||
| 689 | * reference property from |
||
| 690 | * @param ResourceEntityType $targetResourceType The resource type to add the resource |
||
| 691 | * reference property to |
||
| 692 | * @param string $sourceProperty The name of the property to add, on source type |
||
| 693 | * @param string $targetProperty The name of the property to add, on target type |
||
| 694 | */ |
||
| 695 | public function addResourceSetReferencePropertyBidirectional( |
||
| 710 | |||
| 711 | /** |
||
| 712 | * To add a 1-1 resource reference. |
||
| 713 | * |
||
| 714 | * @param ResourceEntityType $sourceResourceType The resource type to add the resource |
||
| 715 | * reference property from |
||
| 716 | * @param ResourceEntityType $targetResourceType The resource type to add the resource |
||
| 717 | * reference property to |
||
| 718 | * @param string $sourceProperty The name of the property to add, on source type |
||
| 719 | * @param string $targetProperty The name of the property to add, on target type |
||
| 720 | */ |
||
| 721 | public function addResourceReferenceSinglePropertyBidirectional( |
||
| 736 | |||
| 737 | /** |
||
| 738 | * To add a complex property to entity or complex type. |
||
| 739 | * |
||
| 740 | * @param ResourceType $targetResourceType The resource type to which the complex property needs to add |
||
| 741 | * @param string $name name of the complex property |
||
| 742 | * @param ResourceComplexType $complexResourceType complex resource type |
||
| 743 | * @param bool $isBag complex type is bag or not |
||
| 744 | * |
||
| 745 | * @return ResourceProperty |
||
| 746 | */ |
||
| 747 | public function addComplexProperty( |
||
| 778 | } |
||
| 779 |
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: