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 |
||
| 12 | class SimpleMetadataProvider implements IMetadataProvider |
||
| 13 | { |
||
| 14 | protected $resourceSets = []; |
||
| 15 | protected $resourceTypes = []; |
||
| 16 | protected $associationSets = []; |
||
| 17 | protected $containerName; |
||
| 18 | protected $namespaceName; |
||
| 19 | public $mappedDetails = null; |
||
| 20 | |||
| 21 | //Begin Implementation of IMetadataProvider |
||
| 22 | |||
| 23 | /** |
||
| 24 | * get the Container name for the data source. |
||
| 25 | * |
||
| 26 | * @return string container name |
||
| 27 | */ |
||
| 28 | public function getContainerName() |
||
| 32 | |||
| 33 | /** |
||
| 34 | * get Namespace name for the data source. |
||
| 35 | * |
||
| 36 | * @return string namespace |
||
| 37 | */ |
||
| 38 | public function getContainerNamespace() |
||
| 42 | |||
| 43 | /** |
||
| 44 | * get all entity set information. |
||
| 45 | * |
||
| 46 | * @return ResourceSet[] |
||
| 47 | */ |
||
| 48 | public function getResourceSets($params = null) |
||
| 75 | |||
| 76 | /** |
||
| 77 | * get all resource types in the data source. |
||
| 78 | * |
||
| 79 | * @return ResourceType[] |
||
| 80 | */ |
||
| 81 | public function getTypes() |
||
| 85 | |||
| 86 | /** |
||
| 87 | * get a resource set based on the specified resource set name. |
||
| 88 | * |
||
| 89 | * @param string $name Name of the resource set |
||
| 90 | * |
||
| 91 | * @return ResourceSet|null resource set with the given name if found else NULL |
||
| 92 | */ |
||
| 93 | public function resolveResourceSet($name) |
||
| 99 | |||
| 100 | /** |
||
| 101 | * get a resource type based on the resource type name. |
||
| 102 | * |
||
| 103 | * @param string $name Name of the resource type |
||
| 104 | * |
||
| 105 | * @return ResourceType|null resource type with the given resource type name if found else NULL |
||
| 106 | */ |
||
| 107 | public function resolveResourceType($name) |
||
| 113 | |||
| 114 | /** |
||
| 115 | * get a resource set based on the specified resource association set name. |
||
| 116 | * |
||
| 117 | * @param string $name Name of the resource assocation set |
||
| 118 | * |
||
| 119 | * @return ResourceAssociationSet|null resource association set with the given name if found else NULL |
||
| 120 | */ |
||
| 121 | public function resolveAssociationSet($name) |
||
| 127 | |||
| 128 | /** |
||
| 129 | * The method must return a collection of all the types derived from |
||
| 130 | * $resourceType The collection returned should NOT include the type |
||
| 131 | * passed in as a parameter. |
||
| 132 | * |
||
| 133 | * @param ResourceType $resourceType Resource to get derived resource types from |
||
| 134 | * |
||
| 135 | * @return ResourceType[] |
||
| 136 | */ |
||
| 137 | public function getDerivedTypes(ResourceType $resourceType) |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @param ResourceType $resourceType Resource to check for derived resource types |
||
| 144 | * |
||
| 145 | * @return bool true if $resourceType represents an Entity Type which has derived Entity Types, else false |
||
| 146 | */ |
||
| 147 | public function hasDerivedTypes(ResourceType $resourceType) |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Gets the ResourceAssociationSet instance for the given source |
||
| 154 | * association end. |
||
| 155 | * |
||
| 156 | * @param ResourceSet $sourceResourceSet Resource set |
||
| 157 | * of the source |
||
| 158 | * association end |
||
| 159 | * @param ResourceType $sourceResourceType Resource type of the source |
||
| 160 | * association end |
||
| 161 | * @param ResourceProperty $targetResourceProperty Resource property of |
||
| 162 | * the source |
||
| 163 | * association end |
||
| 164 | * |
||
| 165 | * @return ResourceAssociationSet|null |
||
| 166 | */ |
||
| 167 | public function getResourceAssociationSet(ResourceSet $sourceResourceSet, ResourceType $sourceResourceType, ResourceProperty $targetResourceProperty) |
||
| 203 | |||
| 204 | //End Implementation of IMetadataProvider |
||
| 205 | |||
| 206 | /** |
||
| 207 | * @param string $containerName container name for the datasource |
||
| 208 | * @param string $namespaceName namespace for the datasource |
||
| 209 | */ |
||
| 210 | public function __construct($containerName, $namespaceName) |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Add an entity type. |
||
| 218 | * |
||
| 219 | * @param \ReflectionClass $refClass reflection class of the entity |
||
| 220 | * @param string $name name of the entity |
||
| 221 | * @param string $namespace namespace of the data source |
||
| 222 | * |
||
| 223 | * @throws InvalidOperationException when the name is already in use |
||
| 224 | * |
||
| 225 | * @return ResourceType |
||
| 226 | */ |
||
| 227 | public function addEntityType(\ReflectionClass $refClass, $name, $namespace = null) |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Add a complex type. |
||
| 234 | * |
||
| 235 | * @param \ReflectionClass $refClass reflection class of the complex entity type |
||
| 236 | * @param string $name name of the entity |
||
| 237 | * @param string $namespace namespace of the data source |
||
| 238 | * @param ResourceType $baseResourceType base resource type |
||
| 239 | * |
||
| 240 | * @throws InvalidOperationException when the name is already in use |
||
| 241 | * |
||
| 242 | * @return ResourceType |
||
| 243 | */ |
||
| 244 | public function addComplexType(\ReflectionClass $refClass, $name, $namespace = null, $baseResourceType = null) |
||
| 248 | |||
| 249 | /** |
||
| 250 | * @param string $name name of the resource set |
||
| 251 | * @param ResourceType $resourceType resource type |
||
| 252 | * |
||
| 253 | * @throws InvalidOperationException |
||
| 254 | * |
||
| 255 | * @return ResourceSet |
||
| 256 | */ |
||
| 257 | public function addResourceSet($name, ResourceType $resourceType) |
||
| 271 | |||
| 272 | /** |
||
| 273 | * To add a Key-primitive property to a resouce (Complex/Entuty). |
||
| 274 | * |
||
| 275 | * @param ResourceType $resourceType resource type to which key property |
||
| 276 | * is to be added |
||
| 277 | * @param string $name name of the key property |
||
| 278 | * @param TypeCode $typeCode type of the key property |
||
| 279 | */ |
||
| 280 | public function addKeyProperty($resourceType, $name, $typeCode) |
||
| 284 | |||
| 285 | /** |
||
| 286 | * To add a NonKey-primitive property (Complex/Entity). |
||
| 287 | * |
||
| 288 | * @param ResourceType $resourceType resource type to which key property |
||
| 289 | * is to be added |
||
| 290 | * @param string $name name of the key property |
||
| 291 | * @param TypeCode $typeCode type of the key property |
||
| 292 | * @param bool $isBag property is bag or not |
||
| 293 | */ |
||
| 294 | public function addPrimitiveProperty($resourceType, $name, $typeCode, $isBag = false) |
||
| 298 | |||
| 299 | /** |
||
| 300 | * To add a non-key etag property. |
||
| 301 | * |
||
| 302 | * @param ResourceType $resourceType resource type to which key property |
||
| 303 | * is to be added |
||
| 304 | * @param string $name name of the property |
||
| 305 | * @param TypeCode $typeCode type of the etag property |
||
| 306 | */ |
||
| 307 | public function addETagProperty($resourceType, $name, $typeCode) |
||
| 311 | |||
| 312 | /** |
||
| 313 | * To add a resource reference property. |
||
| 314 | * |
||
| 315 | * @param ResourceType $resourceType The resource type to add the resource |
||
| 316 | * reference property to |
||
| 317 | * @param string $name The name of the property to add |
||
| 318 | * @param ResourceSet $targetResourceSet The resource set the resource reference |
||
| 319 | * property points to |
||
| 320 | */ |
||
| 321 | public function addResourceReferenceProperty($resourceType, $name, $targetResourceSet) |
||
| 330 | |||
| 331 | /** |
||
| 332 | * To add a resource set reference property. |
||
| 333 | * |
||
| 334 | * @param ResourceType $resourceType The resource type to add the |
||
| 335 | * resource reference set property to |
||
| 336 | * @param string $name The name of the property to add |
||
| 337 | * @param ResourceSet $targetResourceSet The resource set the resource |
||
| 338 | * reference set property points to |
||
| 339 | */ |
||
| 340 | public function addResourceSetReferenceProperty($resourceType, $name, $targetResourceSet) |
||
| 349 | |||
| 350 | /** |
||
| 351 | * To add a complex property to entity or complex type. |
||
| 352 | * |
||
| 353 | * @param ResourceType $resourceType The resource type to which the |
||
| 354 | * complex property needs to add |
||
| 355 | * @param string $name name of the complex property |
||
| 356 | * @param ResourceType $complexResourceType complex resource type |
||
| 357 | * @param bool $isBag complex type is bag or not |
||
| 358 | * |
||
| 359 | * @return ResourceProperty |
||
| 360 | */ |
||
| 361 | public function addComplexProperty($resourceType, $name, $complexResourceType, $isBag = false) |
||
| 381 | |||
| 382 | /** |
||
| 383 | * To add a Key/NonKey-primitive property to a resource (complex/entity). |
||
| 384 | * |
||
| 385 | * @param ResourceType $resourceType Resource type |
||
| 386 | * @param string $name name of the property |
||
| 387 | * @param TypeCode $typeCode type of property |
||
| 388 | * @param bool $isKey property is key or not |
||
| 389 | * @param bool $isBag property is bag or not |
||
| 390 | * @param bool $isETagProperty property is etag or not |
||
| 391 | */ |
||
| 392 | private function _addPrimitivePropertyInternal($resourceType, $name, $typeCode, $isKey = false, $isBag = false, $isETagProperty = false) |
||
| 416 | |||
| 417 | /** |
||
| 418 | * To add a navigation property (resource set or resource reference) |
||
| 419 | * to a resource type. |
||
| 420 | * |
||
| 421 | * @param ResourceType $resourceType The resource type to add |
||
| 422 | * the resource reference |
||
| 423 | * or resource |
||
| 424 | * reference set property to |
||
| 425 | * @param string $name The name of the |
||
| 426 | * property to add |
||
| 427 | * @param ResourceSet $targetResourceSet The resource set the |
||
| 428 | * resource reference |
||
| 429 | * or reference |
||
| 430 | * set property |
||
| 431 | * points to |
||
| 432 | * @param ResourcePropertyKind $resourcePropertyKind The property kind |
||
| 433 | */ |
||
| 434 | private function _addReferencePropertyInternal( |
||
| 471 | |||
| 472 | /** |
||
| 473 | * @param \ReflectionClass $refClass |
||
| 474 | * @param string $name |
||
| 475 | * @param string|null $namespace |
||
| 476 | * @param $typeKind |
||
| 477 | * @param null|ResourceType $baseResourceType |
||
| 478 | * |
||
| 479 | * @throws InvalidOperationException |
||
| 480 | * |
||
| 481 | * @return ResourceType |
||
| 482 | */ |
||
| 483 | private function createResourceType( |
||
| 500 | |||
| 501 | /** |
||
| 502 | * @param string $name |
||
| 503 | * @param ResourceType $resourceType |
||
| 504 | * |
||
| 505 | * @throws InvalidOperationException |
||
| 506 | */ |
||
| 507 | private function checkInstanceProperty($name, ResourceType $resourceType) |
||
| 524 | } |
||
| 525 |
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: