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) |
||
| 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) |
||
| 132 | |||
| 133 | /** |
||
| 134 | * get a resource set based on the specified resource association set name. |
||
| 135 | * |
||
| 136 | * @param string $name Name of the resource assocation set |
||
| 137 | * |
||
| 138 | * @return ResourceAssociationSet|null resource association set with the given name if found else NULL |
||
| 139 | */ |
||
| 140 | public function resolveAssociationSet($name) |
||
| 147 | |||
| 148 | /** |
||
| 149 | * The method must return a collection of all the types derived from |
||
| 150 | * $resourceType The collection returned should NOT include the type |
||
| 151 | * passed in as a parameter. |
||
| 152 | * |
||
| 153 | * @param ResourceType $resourceType Resource to get derived resource types from |
||
| 154 | * |
||
| 155 | * @return ResourceType[] |
||
| 156 | */ |
||
| 157 | public function getDerivedTypes(ResourceType $resourceType) |
||
| 161 | |||
| 162 | /** |
||
| 163 | * @param ResourceType $resourceType Resource to check for derived resource types |
||
| 164 | * |
||
| 165 | * @return bool true if $resourceType represents an Entity Type which has derived Entity Types, else false |
||
| 166 | */ |
||
| 167 | public function hasDerivedTypes(ResourceType $resourceType) |
||
| 171 | |||
| 172 | //End Implementation of IMetadataProvider |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Gets the ResourceAssociationSet instance for the given source |
||
| 176 | * association end. |
||
| 177 | * |
||
| 178 | * @param ResourceSet $sourceResourceSet Resource set |
||
| 179 | * of the source |
||
| 180 | * association end |
||
| 181 | * @param ResourceType $sourceResourceType Resource type of the source |
||
| 182 | * association end |
||
| 183 | * @param ResourceProperty $targetResourceProperty Resource property of |
||
| 184 | * the source |
||
| 185 | * association end |
||
| 186 | * |
||
| 187 | * @return ResourceAssociationSet|null |
||
| 188 | */ |
||
| 189 | public function getResourceAssociationSet( |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Add an entity type. |
||
| 231 | * |
||
| 232 | * @param \ReflectionClass $refClass reflection class of the entity |
||
| 233 | * @param string $name name of the entity |
||
| 234 | * @param string $namespace namespace of the data source |
||
| 235 | * |
||
| 236 | * @throws InvalidOperationException when the name is already in use |
||
| 237 | * |
||
| 238 | * @return ResourceType |
||
| 239 | */ |
||
| 240 | public function addEntityType(\ReflectionClass $refClass, $name, $namespace = null) |
||
| 244 | |||
| 245 | /** |
||
| 246 | * @param \ReflectionClass $refClass |
||
| 247 | * @param string $name |
||
| 248 | * @param string|null $namespace |
||
| 249 | * @param $typeKind |
||
| 250 | * @param null|ResourceType $baseResourceType |
||
| 251 | * |
||
| 252 | * @throws InvalidOperationException |
||
| 253 | * |
||
| 254 | * @return ResourceType |
||
| 255 | */ |
||
| 256 | private function createResourceType( |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Add a complex type. |
||
| 281 | * |
||
| 282 | * @param \ReflectionClass $refClass reflection class of the complex entity type |
||
| 283 | * @param string $name name of the entity |
||
| 284 | * @param string $namespace namespace of the data source |
||
| 285 | * @param ResourceType $baseResourceType base resource type |
||
| 286 | * |
||
| 287 | * @throws InvalidOperationException when the name is already in use |
||
| 288 | * |
||
| 289 | * @return ResourceType |
||
| 290 | */ |
||
| 291 | public function addComplexType(\ReflectionClass $refClass, $name, $namespace = null, $baseResourceType = null) |
||
| 295 | |||
| 296 | /** |
||
| 297 | * @param string $name name of the resource set |
||
| 298 | * @param ResourceType $resourceType resource type |
||
| 299 | * |
||
| 300 | * @throws InvalidOperationException |
||
| 301 | * |
||
| 302 | * @return ResourceSet |
||
| 303 | */ |
||
| 304 | public function addResourceSet($name, ResourceType $resourceType) |
||
| 318 | |||
| 319 | /** |
||
| 320 | * To add a Key-primitive property to a resource (Complex/Entity). |
||
| 321 | * |
||
| 322 | * @param ResourceType $resourceType resource type to which key property |
||
| 323 | * is to be added |
||
| 324 | * @param string $name name of the key property |
||
| 325 | * @param TypeCode $typeCode type of the key property |
||
| 326 | */ |
||
| 327 | public function addKeyProperty($resourceType, $name, $typeCode) |
||
| 331 | |||
| 332 | /** |
||
| 333 | * To add a Key/NonKey-primitive property to a resource (complex/entity). |
||
| 334 | * |
||
| 335 | * @param ResourceType $resourceType Resource type |
||
| 336 | * @param string $name name of the property |
||
| 337 | * @param TypeCode $typeCode type of property |
||
| 338 | * @param bool $isKey property is key or not |
||
| 339 | * @param bool $isBag property is bag or not |
||
| 340 | * @param bool $isETagProperty property is etag or not |
||
| 341 | */ |
||
| 342 | private function _addPrimitivePropertyInternal( |
||
| 382 | |||
| 383 | /** |
||
| 384 | * @param string $name |
||
| 385 | * @param ResourceType $resourceType |
||
| 386 | * |
||
| 387 | * @throws InvalidOperationException |
||
| 388 | */ |
||
| 389 | private function checkInstanceProperty($name, ResourceType $resourceType) |
||
| 406 | |||
| 407 | /** |
||
| 408 | * To add a NonKey-primitive property (Complex/Entity). |
||
| 409 | * |
||
| 410 | * @param ResourceType $resourceType resource type to which key property |
||
| 411 | * is to be added |
||
| 412 | * @param string $name name of the key property |
||
| 413 | * @param TypeCode $typeCode type of the key property |
||
| 414 | * @param bool $isBag property is bag or not |
||
| 415 | */ |
||
| 416 | public function addPrimitiveProperty($resourceType, $name, $typeCode, $isBag = false) |
||
| 420 | |||
| 421 | /** |
||
| 422 | * To add a non-key etag property. |
||
| 423 | * |
||
| 424 | * @param ResourceType $resourceType resource type to which key property |
||
| 425 | * is to be added |
||
| 426 | * @param string $name name of the property |
||
| 427 | * @param TypeCode $typeCode type of the etag property |
||
| 428 | */ |
||
| 429 | public function addETagProperty($resourceType, $name, $typeCode) |
||
| 433 | |||
| 434 | /** |
||
| 435 | * To add a resource reference property. |
||
| 436 | * |
||
| 437 | * @param ResourceType $resourceType The resource type to add the resource |
||
| 438 | * reference property to |
||
| 439 | * @param string $name The name of the property to add |
||
| 440 | * @param ResourceSet $targetResourceSet The resource set the resource reference |
||
| 441 | * property points to |
||
| 442 | */ |
||
| 443 | public function addResourceReferenceProperty($resourceType, $name, $targetResourceSet) |
||
| 452 | |||
| 453 | /** |
||
| 454 | * To add a navigation property (resource set or resource reference) |
||
| 455 | * to a resource type. |
||
| 456 | * |
||
| 457 | * @param ResourceType $resourceType The resource type to add |
||
| 458 | * the resource reference |
||
| 459 | * or resource |
||
| 460 | * reference set property to |
||
| 461 | * @param string $name The name of the |
||
| 462 | * property to add |
||
| 463 | * @param ResourceSet $targetResourceSet The resource set the |
||
| 464 | * resource reference |
||
| 465 | * or reference |
||
| 466 | * set property |
||
| 467 | * points to |
||
| 468 | * @param ResourcePropertyKind $resourcePropertyKind The property kind |
||
| 469 | */ |
||
| 470 | private function _addReferencePropertyInternal( |
||
| 519 | |||
| 520 | /** |
||
| 521 | * To add a resource set reference property. |
||
| 522 | * |
||
| 523 | * @param ResourceType $resourceType The resource type to add the |
||
| 524 | * resource reference set property to |
||
| 525 | * @param string $name The name of the property to add |
||
| 526 | * @param ResourceSet $targetResourceSet The resource set the resource |
||
| 527 | * reference set property points to |
||
| 528 | */ |
||
| 529 | public function addResourceSetReferenceProperty($resourceType, $name, $targetResourceSet) |
||
| 538 | |||
| 539 | /** |
||
| 540 | * To add a complex property to entity or complex type. |
||
| 541 | * |
||
| 542 | * @param ResourceType $resourceType The resource type to which the |
||
| 543 | * complex property needs to add |
||
| 544 | * @param string $name name of the complex property |
||
| 545 | * @param ResourceType $complexResourceType complex resource type |
||
| 546 | * @param bool $isBag complex type is bag or not |
||
| 547 | * |
||
| 548 | * @return ResourceProperty |
||
| 549 | */ |
||
| 550 | public function addComplexProperty($resourceType, $name, $complexResourceType, $isBag = false) |
||
| 577 | } |
||
| 578 |
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: