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 StreamProviderWrapper 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 StreamProviderWrapper, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | class StreamProviderWrapper |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * Holds reference to the data service instance. |
||
| 24 | * |
||
| 25 | * @var IService |
||
| 26 | */ |
||
| 27 | private $_service; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Holds reference to the implementation of IStreamProvider or IStreamProvider2. |
||
| 31 | * |
||
| 32 | * |
||
| 33 | * @var IStreamProvider|IStreamProvider2 |
||
| 34 | */ |
||
| 35 | private $_streamProvider; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Used to check whether interface implementation modified response content type header or not. |
||
| 39 | * |
||
| 40 | * |
||
| 41 | * @var string|null |
||
| 42 | */ |
||
| 43 | private $_responseContentType; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Used to check whether interface implementation modified ETag header or not. |
||
| 47 | * |
||
| 48 | * @var string|null |
||
| 49 | */ |
||
| 50 | private $_responseETag; |
||
| 51 | |||
| 52 | |||
| 53 | /** |
||
| 54 | * To set reference to the data service instance. |
||
| 55 | * |
||
| 56 | * @param IService $service The data service instance. |
||
| 57 | * |
||
| 58 | * @return void |
||
| 59 | */ |
||
| 60 | public function setService(IService $service) |
||
| 64 | |||
| 65 | /** |
||
| 66 | * To get stream associated with the given media resource. |
||
| 67 | * |
||
| 68 | * @param object $entity The media resource. |
||
| 69 | * @param ResourceStreamInfo $resourceStreamInfo This will be null if media |
||
| 70 | * resource is MLE, if media |
||
| 71 | * resource is named |
||
| 72 | * stream then will be the |
||
| 73 | * ResourceStreamInfo instance |
||
| 74 | * holding the details of |
||
| 75 | * named stream. |
||
| 76 | * |
||
| 77 | * @return string|null |
||
| 78 | */ |
||
| 79 | public function getReadStream($entity, $resourceStreamInfo) |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Gets the IANA content type (aka media type) of the stream associated with |
||
| 140 | * the specified media resource. |
||
| 141 | * |
||
| 142 | * @param object $entity The entity instance |
||
| 143 | * (media resource) associated with |
||
| 144 | * the stream for which the content |
||
| 145 | * type is to be obtained. |
||
| 146 | * @param ResourceStreamInfo $resourceStreamInfo This will be null if |
||
| 147 | * media resource is MLE, |
||
| 148 | * if media resource is named |
||
| 149 | * stream then will be the |
||
| 150 | * ResourceStreamInfo instance |
||
| 151 | * holding the details of |
||
| 152 | * named stream. |
||
| 153 | * |
||
| 154 | * @return string|null |
||
| 155 | */ |
||
| 156 | View Code Duplication | public function getStreamContentType($entity, $resourceStreamInfo) |
|
| 183 | |||
| 184 | /** |
||
| 185 | * Get the ETag of the stream associated with the entity specified. |
||
| 186 | * |
||
| 187 | * @param object $entity The entity instance |
||
| 188 | * (media resource) associated |
||
| 189 | * with the stream for which |
||
| 190 | * the etag is to be obtained. |
||
| 191 | * @param ResourceStreamInfo $resourceStreamInfo This will be null if |
||
| 192 | * media resource is MLE, |
||
| 193 | * if media resource is named |
||
| 194 | * stream then will be the |
||
| 195 | * ResourceStreamInfo |
||
| 196 | * instance holding the |
||
| 197 | * details of named stream. |
||
| 198 | * |
||
| 199 | * @throws InvalidOperationException |
||
| 200 | * @return String Etag |
||
| 201 | */ |
||
| 202 | View Code Duplication | public function getStreamETag($entity, $resourceStreamInfo) |
|
| 230 | |||
| 231 | /** |
||
| 232 | * Gets the URI clients should use when making retrieve (ie. GET) requests |
||
| 233 | * to the stream. |
||
| 234 | * |
||
| 235 | * @param object $entity The entity instance |
||
| 236 | * associated with the |
||
| 237 | * stream for which a |
||
| 238 | * read stream URI is to |
||
| 239 | * be obtained. |
||
| 240 | * @param ResourceStreamInfo $resourceStreamInfo This will be null |
||
| 241 | * if media resource |
||
| 242 | * is MLE, if media |
||
| 243 | * resource is named |
||
| 244 | * stream then will be |
||
| 245 | * the ResourceStreamInfo |
||
| 246 | * instance holding the |
||
| 247 | * details of named stream. |
||
| 248 | * @param string $mediaLinkEntryUri MLE uri. |
||
| 249 | * |
||
| 250 | * @return string |
||
| 251 | * |
||
| 252 | * @throws InvalidOperationException |
||
| 253 | */ |
||
| 254 | public function getReadStreamUri($entity, $resourceStreamInfo, |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Checks the given value is a valid eTag. |
||
| 302 | * |
||
| 303 | * @param string $etag eTag to validate. |
||
| 304 | * @param boolean $allowStrongEtag True if strong eTag is allowed |
||
| 305 | * False otherwise. |
||
| 306 | * |
||
| 307 | * @return boolean |
||
| 308 | */ |
||
| 309 | public static function isETagValueValid($etag, $allowStrongEtag) |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Get ETag header value from request header. |
||
| 347 | * |
||
| 348 | * @param mixed &$eTag On return, this parameter will hold |
||
| 349 | * value of IfMatch or IfNoneMatch |
||
| 350 | * header, if this header is absent then |
||
| 351 | * this parameter will hold NULL. |
||
| 352 | * @param mixed &$checkETagForEquality On return, this parameter will hold |
||
| 353 | * true if IfMatch is present, false if |
||
| 354 | * IfNoneMatch header is present, null |
||
| 355 | * otherwise. |
||
| 356 | * |
||
| 357 | * @return void |
||
| 358 | */ |
||
| 359 | private function _getETagFromHeaders(&$eTag, &$checkETagForEquality) |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Validates that an implementation of IStreamProvider exists and |
||
| 381 | * load it. |
||
| 382 | * |
||
| 383 | * @return void |
||
| 384 | * |
||
| 385 | * @throws ODataException |
||
| 386 | */ |
||
| 387 | private function _loadAndValidateStreamProvider() |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Validates that an implementation of IStreamProvider2 exists and |
||
| 401 | * load it. |
||
| 402 | * |
||
| 403 | * @return void |
||
| 404 | * |
||
| 405 | * @throws ODataException |
||
| 406 | */ |
||
| 407 | private function _loadAndValidateStreamProvider2() |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Ask data service to load stream provider instance. |
||
| 434 | * |
||
| 435 | * @return void |
||
| 436 | * |
||
| 437 | * @throws ODataException |
||
| 438 | */ |
||
| 439 | private function _loadStreamProvider() |
||
| 464 | |||
| 465 | /** |
||
| 466 | * Construct the default edit media uri from the given media link entry uri. |
||
| 467 | * |
||
| 468 | * @param string $mediaLinkEntryUri Uri to the media link entry. |
||
| 469 | * @param ResourceStreamInfo $resourceStreamInfo Stream info instance, if its |
||
| 470 | * null default stream is assumed. |
||
| 471 | * |
||
| 472 | * @return string Uri to the media resource. |
||
| 473 | */ |
||
| 474 | public function getDefaultStreamEditMediaUri($mediaLinkEntryUri, $resourceStreamInfo) |
||
| 482 | |||
| 483 | /** |
||
| 484 | * Save value of content type and etag headers before invoking implementor |
||
| 485 | * methods. |
||
| 486 | * |
||
| 487 | * @return void |
||
| 488 | */ |
||
| 489 | private function _saveContentTypeAndETag() |
||
| 494 | |||
| 495 | /** |
||
| 496 | * Check whether implementor modified content type or etag header |
||
| 497 | * if so throw InvalidOperationException. |
||
| 498 | * |
||
| 499 | * @param string $methodName NAme of the method |
||
| 500 | * |
||
| 501 | * @return void |
||
| 502 | * |
||
| 503 | * @throws InvalidOperationException |
||
| 504 | */ |
||
| 505 | private function _verifyContentTypeOrETagModified($methodName) |
||
| 515 | } |
||
| 516 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: