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 Smsa 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 Smsa, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
29 | class Smsa |
||
30 | { |
||
31 | |||
32 | /** |
||
33 | * Weather to throw exceptions when there is a request error or a failed response from SMSA. |
||
34 | * |
||
35 | * @var bool |
||
36 | */ |
||
37 | public $shouldUseExceptions = true; |
||
38 | |||
39 | /** |
||
40 | * @var \Alhoqbani\SmsaWebService\Soap\Service |
||
41 | */ |
||
42 | protected $service; |
||
43 | |||
44 | /** |
||
45 | * @var string |
||
46 | */ |
||
47 | private $passKey; |
||
48 | |||
49 | /** |
||
50 | * Create a new Smsa Instance |
||
51 | * |
||
52 | * @param string $passKey The pass key to the api provided by Smsa. |
||
53 | * @param array|null $options WSDL option to be passed to SoapClient. |
||
54 | * @param AbstractSoapClientBase $service The service which provide SMSA api methods. |
||
55 | */ |
||
56 | public function __construct( |
||
70 | |||
71 | /** |
||
72 | * Fetch all cities that has SMSAExpress locations |
||
73 | * |
||
74 | * @throws RequestError |
||
75 | * |
||
76 | * @return SMSAResponse with array of cities with their route code. |
||
77 | */ |
||
78 | View Code Duplication | public function cities(): SMSAResponse |
|
94 | |||
95 | /** |
||
96 | * Fetch all retails in a particular city. |
||
97 | * The city code is the three letters route code |
||
98 | * For example: RUH, JED |
||
99 | * |
||
100 | * @param $cityCode |
||
101 | * |
||
102 | * @throws RequestError |
||
103 | * |
||
104 | * @return SMSAResponse with array of retails in given city. |
||
105 | */ |
||
106 | View Code Duplication | public function retailsIn($cityCode): SMSAResponse |
|
122 | |||
123 | /** |
||
124 | * Fetch all Smsa Express retails |
||
125 | * |
||
126 | * @throws RequestError |
||
127 | * |
||
128 | * @return SMSAResponse with list of all retails with details |
||
129 | */ |
||
130 | View Code Duplication | public function retails(): SMSAResponse |
|
146 | |||
147 | /** |
||
148 | * Track a shipment by its awb |
||
149 | * |
||
150 | * @param $awb |
||
151 | * |
||
152 | * @throws \Alhoqbani\SmsaWebService\Exceptions\RequestError |
||
153 | * @throws \Alhoqbani\SmsaWebService\Exceptions\FailedResponse |
||
154 | * |
||
155 | * @return SMSAResponse |
||
156 | */ |
||
157 | public function track($awb): SMSAResponse |
||
185 | |||
186 | /** |
||
187 | * Get the real-time tracking information of the shipment |
||
188 | * |
||
189 | * @param string $reference Customer reference number |
||
190 | * |
||
191 | * @todo This is not working |
||
192 | * |
||
193 | * @throws FailedResponse |
||
194 | * @throws RequestError |
||
195 | * |
||
196 | * @return SMSAResponse |
||
197 | */ |
||
198 | View Code Duplication | public function trackByReference(string $reference) |
|
199 | { |
||
200 | $result = $this->service->getTrackingByRef( |
||
201 | $payload = new GetTrackingByRef($reference, $this->passKey) |
||
202 | ); |
||
203 | |||
204 | if (false === $result) { |
||
205 | return $this->failedRequest('GetTrackingwithRef', $payload); |
||
206 | } |
||
207 | |||
208 | $data = $result->getGetTrackingByRefResult(); |
||
209 | |||
210 | if (is_null($data)) { |
||
211 | return $this->failedResponse('GetTrackingwithRef', $payload, 'Shipment was not found'); |
||
212 | } |
||
213 | |||
214 | return $this->successResponse( |
||
215 | 'GetTrackingwithRef', |
||
216 | $payload, |
||
217 | $data->getAny() |
||
218 | ); |
||
219 | } |
||
220 | |||
221 | /** |
||
222 | * @param $awb |
||
223 | * |
||
224 | * @throws FailedResponse |
||
225 | * @throws RequestError |
||
226 | * |
||
227 | * @return SMSAResponse |
||
228 | */ |
||
229 | View Code Duplication | public function status($awb): SMSAResponse |
|
255 | |||
256 | /** |
||
257 | * Cancel a shipment by AWB |
||
258 | * |
||
259 | * @param $awb string |
||
260 | * @param $reason string |
||
261 | * |
||
262 | * @throws FailedResponse |
||
263 | * @throws RequestError |
||
264 | * |
||
265 | * @return SMSAResponse |
||
266 | */ |
||
267 | View Code Duplication | public function cancel($awb, $reason): SMSAResponse |
|
293 | |||
294 | /** |
||
295 | * Create a new shipment |
||
296 | * |
||
297 | * @param Shipment $shipment |
||
298 | * |
||
299 | * @throws FailedResponse |
||
300 | * @throws RequestError |
||
301 | * |
||
302 | * @return SMSAResponse |
||
303 | */ |
||
304 | public function createShipment(Shipment $shipment): SMSAResponse |
||
324 | |||
325 | /** |
||
326 | * Get AWB in PDF for printing |
||
327 | * This method can be used to get the AWB Copy in PDF format for printing and labeling on shipment. |
||
328 | * |
||
329 | * @param $awb string |
||
330 | * |
||
331 | * @throws RequestError |
||
332 | * @throws FailedResponse |
||
333 | * |
||
334 | * @return SMSAResponse |
||
335 | */ |
||
336 | View Code Duplication | public function awbPDF(string $awb): SMSAResponse |
|
358 | |||
359 | /** |
||
360 | * Parse the cities xml response into array |
||
361 | * |
||
362 | * @param string $citiesResult |
||
363 | * |
||
364 | * @return array Array of cities with its names and route code |
||
365 | */ |
||
366 | protected function parseCityResult(string $citiesResult): array |
||
380 | |||
381 | /** |
||
382 | * Parse retails xml response into an array |
||
383 | * |
||
384 | * @param string $retailsResult |
||
385 | * |
||
386 | * @return array array of retails with their details |
||
387 | */ |
||
388 | private function parseRetailsResult(string $retailsResult): array |
||
409 | |||
410 | /** |
||
411 | * Parse a tracking xml response |
||
412 | * |
||
413 | * @param string $result |
||
414 | * |
||
415 | * @return array Details of the tracking |
||
416 | */ |
||
417 | private function parseTrackResult(string $result): array |
||
436 | |||
437 | /** |
||
438 | * Return a successful response with the data. |
||
439 | * |
||
440 | * @param $type string The type/function of the SOAP Api used. |
||
441 | * @param $payload AbstractStructBase The object that was sent to the SoapClient |
||
442 | * @param $data mixed |
||
443 | * |
||
444 | * @return SMSAResponse |
||
445 | */ |
||
446 | View Code Duplication | protected function successResponse($type, $payload, $data): SMSAResponse |
|
460 | |||
461 | /** |
||
462 | * Send a Response with failed request. |
||
463 | * |
||
464 | * @param $type string The type/function of the SOAP Api used. |
||
465 | * @param $payload AbstractStructBase The object that was sent to the SoapClient |
||
466 | * |
||
467 | * @throws \Alhoqbani\SmsaWebService\Exceptions\RequestError |
||
468 | * |
||
469 | * @return SMSAResponse |
||
470 | */ |
||
471 | protected function failedRequest($type, $payload) |
||
492 | |||
493 | /** |
||
494 | * Return a failed response. (Usually, wrong data was sent to SMSA Api) |
||
495 | * |
||
496 | * @param $type string The type/function of the SOAP Api used |
||
497 | * @param $payload AbstractStructBase The object that was sent to the SoapClient |
||
498 | * @param $error string The error message from SMSA. |
||
499 | * |
||
500 | * @throws FailedResponse |
||
501 | * |
||
502 | * @return SMSAResponse |
||
503 | */ |
||
504 | View Code Duplication | protected function failedResponse($type, $payload, $error) |
|
522 | |||
523 | protected function shouldUseExceptions() |
||
527 | |||
528 | /** |
||
529 | * To handle response error from Smsa Soap server |
||
530 | * |
||
531 | * @param SMSAResponse $response |
||
532 | * |
||
533 | * @throws RequestError |
||
534 | */ |
||
535 | protected function throwRequestError(SMSAResponse $response): void |
||
546 | } |
||
547 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.