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 |
||
| 17 | class StreamProviderWrapper |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * Holds reference to the data service instance. |
||
| 21 | * |
||
| 22 | * @var IService |
||
| 23 | */ |
||
| 24 | private $_service; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Holds reference to the implementation of IStreamProvider or IStreamProvider2. |
||
| 28 | * |
||
| 29 | * |
||
| 30 | * @var IStreamProvider|IStreamProvider2 |
||
| 31 | */ |
||
| 32 | private $_streamProvider; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Used to check whether interface implementation modified response content type header or not. |
||
| 36 | * |
||
| 37 | * |
||
| 38 | * @var string|null |
||
| 39 | */ |
||
| 40 | private $_responseContentType; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Used to check whether interface implementation modified ETag header or not. |
||
| 44 | * |
||
| 45 | * @var string|null |
||
| 46 | */ |
||
| 47 | private $_responseETag; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * To set reference to the data service instance. |
||
| 51 | * |
||
| 52 | * @param IService $service The data service instance |
||
| 53 | */ |
||
| 54 | public function setService(IService $service) |
||
| 58 | |||
| 59 | /** |
||
| 60 | * To get stream associated with the given media resource. |
||
| 61 | * |
||
| 62 | * @param object $entity The media resource |
||
| 63 | * @param ResourceStreamInfo $resourceStreamInfo This will be null if media |
||
| 64 | * resource is MLE, if media |
||
| 65 | * resource is named |
||
| 66 | * stream then will be the |
||
| 67 | * ResourceStreamInfo instance |
||
| 68 | * holding the details of |
||
| 69 | * named stream |
||
| 70 | * |
||
| 71 | * @return string|null |
||
| 72 | */ |
||
| 73 | public function getReadStream($entity, $resourceStreamInfo) |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Gets the IANA content type (aka media type) of the stream associated with |
||
| 133 | * the specified media resource. |
||
| 134 | * |
||
| 135 | * @param object $entity The entity instance |
||
| 136 | * (media resource) associated with |
||
| 137 | * the stream for which the content |
||
| 138 | * type is to be obtained |
||
| 139 | * @param ResourceStreamInfo $resourceStreamInfo This will be null if |
||
| 140 | * media resource is MLE, |
||
| 141 | * if media resource is named |
||
| 142 | * stream then will be the |
||
| 143 | * ResourceStreamInfo instance |
||
| 144 | * holding the details of |
||
| 145 | * named stream |
||
| 146 | * |
||
| 147 | * @return string|null |
||
| 148 | */ |
||
| 149 | View Code Duplication | public function getStreamContentType($entity, $resourceStreamInfo) |
|
| 150 | { |
||
| 151 | $contentType = null; |
||
| 152 | $this->_saveContentTypeAndETag(); |
||
| 153 | $opContext = $this->_service->getOperationContext(); |
||
| 154 | if (is_null($resourceStreamInfo)) { |
||
| 155 | $this->_loadAndValidateStreamProvider(); |
||
| 156 | $contentType = $this->_streamProvider->getStreamContentType($entity, $opContext); |
||
| 157 | if (is_null($contentType)) { |
||
| 158 | throw new InvalidOperationException( |
||
| 159 | Messages::streamProviderWrapperGetStreamContentTypeReturnsEmptyOrNull() |
||
| 160 | ); |
||
| 161 | } |
||
| 162 | } else { |
||
| 163 | $this->_loadAndValidateStreamProvider2(); |
||
| 164 | assert($this->_streamProvider instanceof IStreamProvider2); |
||
| 165 | $contentType = $this->_streamProvider->getStreamContentType2($entity, $resourceStreamInfo, $opContext); |
||
| 166 | } |
||
| 167 | |||
| 168 | $this->_verifyContentTypeOrETagModified('IDSSP::getStreamContentType'); |
||
| 169 | |||
| 170 | return $contentType; |
||
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Get the ETag of the stream associated with the entity specified. |
||
| 175 | * |
||
| 176 | * @param object $entity The entity instance |
||
| 177 | * (media resource) associated |
||
| 178 | * with the stream for which |
||
| 179 | * the etag is to be obtained |
||
| 180 | * @param ResourceStreamInfo $resourceStreamInfo This will be null if |
||
| 181 | * media resource is MLE, |
||
| 182 | * if media resource is named |
||
| 183 | * stream then will be the |
||
| 184 | * ResourceStreamInfo |
||
| 185 | * instance holding the |
||
| 186 | * details of named stream |
||
| 187 | * |
||
| 188 | * @throws InvalidOperationException |
||
| 189 | * |
||
| 190 | * @return string Etag |
||
| 191 | */ |
||
| 192 | View Code Duplication | public function getStreamETag($entity, $resourceStreamInfo) |
|
| 193 | { |
||
| 194 | $eTag = null; |
||
| 195 | $this->_saveContentTypeAndETag(); |
||
| 196 | $opContext = $this->_service->getOperationContext(); |
||
| 197 | if (is_null($resourceStreamInfo)) { |
||
| 198 | $this->_loadAndValidateStreamProvider(); |
||
| 199 | $eTag = $this->_streamProvider->getStreamETag($entity, $opContext); |
||
| 200 | } else { |
||
| 201 | $this->_loadAndValidateStreamProvider2(); |
||
| 202 | assert($this->_streamProvider instanceof IStreamProvider2); |
||
| 203 | $eTag = $this->_streamProvider->getStreamETag2($entity, $resourceStreamInfo, $opContext); |
||
| 204 | } |
||
| 205 | |||
| 206 | $this->_verifyContentTypeOrETagModified('IDSSP::getStreamETag'); |
||
| 207 | if (!self::isETagValueValid($eTag, true)) { |
||
| 208 | throw new InvalidOperationException( |
||
| 209 | Messages::streamProviderWrapperGetStreamETagReturnedInvalidETagFormat() |
||
| 210 | ); |
||
| 211 | } |
||
| 212 | |||
| 213 | return $eTag; |
||
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Gets the URI clients should use when making retrieve (ie. GET) requests |
||
| 218 | * to the stream. |
||
| 219 | * |
||
| 220 | * @param object $entity The entity instance |
||
| 221 | * associated with the |
||
| 222 | * stream for which a |
||
| 223 | * read stream URI is to |
||
| 224 | * be obtained |
||
| 225 | * @param ResourceStreamInfo $resourceStreamInfo This will be null |
||
| 226 | * if media resource |
||
| 227 | * is MLE, if media |
||
| 228 | * resource is named |
||
| 229 | * stream then will be |
||
| 230 | * the ResourceStreamInfo |
||
| 231 | * instance holding the |
||
| 232 | * details of named stream |
||
| 233 | * @param string $mediaLinkEntryUri MLE uri |
||
| 234 | * |
||
| 235 | * @return string |
||
| 236 | * |
||
| 237 | * @throws InvalidOperationException |
||
| 238 | */ |
||
| 239 | public function getReadStreamUri( |
||
| 240 | $entity, |
||
| 241 | $resourceStreamInfo, |
||
| 242 | $mediaLinkEntryUri |
||
| 243 | ) { |
||
| 244 | $readStreamUri = null; |
||
| 245 | $this->_saveContentTypeAndETag(); |
||
| 246 | $opContext = $this->_service->getOperationContext(); |
||
| 247 | if (is_null($resourceStreamInfo)) { |
||
| 248 | $this->_loadAndValidateStreamProvider(); |
||
| 249 | $readStreamUri = $this->_streamProvider->getReadStreamUri($entity, $opContext); |
||
| 250 | } else { |
||
| 251 | $this->_loadAndValidateStreamProvider2(); |
||
| 252 | assert($this->_streamProvider instanceof IStreamProvider2); |
||
| 253 | $readStreamUri = $this->_streamProvider->getReadStreamUri2($entity, $resourceStreamInfo, $opContext); |
||
| 254 | } |
||
| 255 | |||
| 256 | $this->_verifyContentTypeOrETagModified('IDSSP::getReadStreamUri'); |
||
| 257 | if (!is_null($readStreamUri)) { |
||
| 258 | try { |
||
| 259 | new \POData\Common\Url($readStreamUri); |
||
| 260 | } catch (\POData\Common\UrlFormatException $ex) { |
||
| 261 | throw new InvalidOperationException( |
||
| 262 | Messages::streamProviderWrapperGetReadStreamUriMustReturnAbsoluteUriOrNull() |
||
| 263 | ); |
||
| 264 | } |
||
| 265 | } else { |
||
| 266 | if (is_null($resourceStreamInfo)) { |
||
| 267 | // For MLEs the content src attribute is |
||
| 268 | //required so we cannot return null. |
||
| 269 | $readStreamUri = $this->getDefaultStreamEditMediaUri($mediaLinkEntryUri, null); |
||
| 270 | } |
||
| 271 | } |
||
| 272 | |||
| 273 | // Note if readStreamUri is null, the self link for the |
||
| 274 | // named stream will be omitted. |
||
| 275 | return $readStreamUri; |
||
| 276 | } |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Checks the given value is a valid eTag. |
||
| 280 | * |
||
| 281 | * @param string $etag eTag to validate |
||
| 282 | * @param bool $allowStrongEtag True if strong eTag is allowed |
||
| 283 | * False otherwise |
||
| 284 | * |
||
| 285 | * @return bool |
||
| 286 | */ |
||
| 287 | public static function isETagValueValid($etag, $allowStrongEtag) |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Get ETag header value from request header. |
||
| 324 | * |
||
| 325 | * @param mixed &$eTag On return, this parameter will hold |
||
| 326 | * value of IfMatch or IfNoneMatch |
||
| 327 | * header, if this header is absent then |
||
| 328 | * this parameter will hold NULL |
||
| 329 | * @param mixed &$checkETagForEquality On return, this parameter will hold |
||
| 330 | * true if IfMatch is present, false if |
||
| 331 | * IfNoneMatch header is present, null |
||
| 332 | * otherwise |
||
| 333 | */ |
||
| 334 | private function _getETagFromHeaders(&$eTag, &$checkETagForEquality) |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Validates that an implementation of IStreamProvider exists and |
||
| 358 | * load it. |
||
| 359 | * |
||
| 360 | * |
||
| 361 | * @throws ODataException |
||
| 362 | */ |
||
| 363 | private function _loadAndValidateStreamProvider() |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Validates that an implementation of IStreamProvider2 exists and |
||
| 378 | * load it. |
||
| 379 | * |
||
| 380 | * |
||
| 381 | * @throws ODataException |
||
| 382 | */ |
||
| 383 | private function _loadAndValidateStreamProvider2() |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Ask data service to load stream provider instance. |
||
| 408 | * |
||
| 409 | * |
||
| 410 | * @throws ODataException |
||
| 411 | */ |
||
| 412 | private function _loadStreamProvider() |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Construct the default edit media uri from the given media link entry uri. |
||
| 426 | * |
||
| 427 | * @param string $mediaLinkEntryUri Uri to the media link entry |
||
| 428 | * @param ResourceStreamInfo|null $resourceStreamInfo Stream info instance, if its |
||
| 429 | * null default stream is assumed |
||
| 430 | * |
||
| 431 | * @return string Uri to the media resource |
||
| 432 | */ |
||
| 433 | public function getDefaultStreamEditMediaUri($mediaLinkEntryUri, $resourceStreamInfo) |
||
| 440 | |||
| 441 | /** |
||
| 442 | * Save value of content type and etag headers before invoking implementor |
||
| 443 | * methods. |
||
| 444 | */ |
||
| 445 | private function _saveContentTypeAndETag() |
||
| 450 | |||
| 451 | /** |
||
| 452 | * Check whether implementor modified content type or etag header |
||
| 453 | * if so throw InvalidOperationException. |
||
| 454 | * |
||
| 455 | * @param string $methodName NAme of the method |
||
| 456 | * |
||
| 457 | * @throws InvalidOperationException |
||
| 458 | */ |
||
| 459 | private function _verifyContentTypeOrETagModified($methodName) |
||
| 469 | } |
||
| 470 |
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: