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 UriProcessorNew 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 UriProcessorNew, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 30 | class UriProcessorNew implements IUriProcessor |
||
| 31 | { |
||
| 32 | /** |
||
| 33 | * Description of the OData request that a client has submitted. |
||
| 34 | * |
||
| 35 | * @var RequestDescription |
||
| 36 | */ |
||
| 37 | private $request; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Holds reference to the data service instance. |
||
| 41 | * |
||
| 42 | * @var IService |
||
| 43 | */ |
||
| 44 | private $service; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Holds reference to the wrapper over IDSMP and IDSQP implementation. |
||
| 48 | * |
||
| 49 | * @var ProvidersWrapper |
||
| 50 | */ |
||
| 51 | private $providers; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Holds reference to request expander. |
||
| 55 | * |
||
| 56 | * @var RequestExpander |
||
| 57 | */ |
||
| 58 | private $expander; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Constructs a new instance of UriProcessor. |
||
| 62 | * |
||
| 63 | * @param IService $service Reference to the data service instance |
||
| 64 | */ |
||
| 65 | private function __construct(IService $service) |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Process the resource path and query options of client's request uri. |
||
| 80 | * |
||
| 81 | * @param IService $service Reference to the data service instance |
||
| 82 | * |
||
| 83 | * @throws ODataException |
||
| 84 | * |
||
| 85 | * @return IUriProcessor |
||
| 86 | */ |
||
| 87 | public static function process(IService $service) |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Gets reference to the request submitted by client. |
||
| 108 | * |
||
| 109 | * @return RequestDescription |
||
| 110 | */ |
||
| 111 | public function getRequest() |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Gets reference to the request submitted by client. |
||
| 118 | * |
||
| 119 | * @return ProvidersWrapper |
||
| 120 | */ |
||
| 121 | public function getProviders() |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Gets the data service instance. |
||
| 128 | * |
||
| 129 | * @return IService |
||
| 130 | */ |
||
| 131 | public function getService() |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Gets the request expander instance. |
||
| 138 | * |
||
| 139 | * @return RequestExpander |
||
| 140 | */ |
||
| 141 | public function getExpander() |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Execute the client submitted request against the data source. |
||
| 148 | */ |
||
| 149 | public function execute() |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Execute the client submitted request against the data source (GET). |
||
| 182 | */ |
||
| 183 | protected function executeGet() |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Execute the client submitted request against the data source (DELETE). |
||
| 217 | */ |
||
| 218 | protected function executeDelete() |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Execute the client submitted request against the data source (PUT). |
||
| 231 | */ |
||
| 232 | protected function executePut() |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Execute the client submitted request against the data source (POST). |
||
| 258 | */ |
||
| 259 | protected function executePost() |
||
| 279 | |||
| 280 | /** |
||
| 281 | * @return null|SegmentDescriptor |
||
| 282 | */ |
||
| 283 | protected function getFinalEffectiveSegment() |
||
| 293 | |||
| 294 | /** |
||
| 295 | * @param $resourceSet |
||
| 296 | * @param $keyDescriptor |
||
| 297 | * @param $requestMethod |
||
| 298 | * @throws ODataException |
||
| 299 | */ |
||
| 300 | View Code Duplication | protected function checkUriValidForSuppliedVerb($resourceSet, $keyDescriptor, $requestMethod) |
|
| 309 | |||
| 310 | /** |
||
| 311 | * @param $segment |
||
| 312 | */ |
||
| 313 | private function executeGetSingleton($segment) |
||
| 319 | |||
| 320 | /** |
||
| 321 | * @param $segment |
||
| 322 | */ |
||
| 323 | private function executeGetResource($segment) |
||
| 347 | |||
| 348 | /** |
||
| 349 | * @param $segment |
||
| 350 | */ |
||
| 351 | private function executeGetLink($segment) |
||
| 357 | } |
||
| 358 |
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: