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 | public function trackByReference(string $reference) |
||
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 | * |
||
333 | * @return SMSAResponse |
||
334 | */ |
||
335 | public function awbPDF(string $awb): SMSAResponse |
||
353 | |||
354 | /** |
||
355 | * Parse the cities xml response into array |
||
356 | * |
||
357 | * @param string $citiesResult |
||
358 | * |
||
359 | * @return array Array of cities with its names and route code |
||
360 | */ |
||
361 | protected function parseCityResult(string $citiesResult): array |
||
375 | |||
376 | /** |
||
377 | * Parse retails xml response into an array |
||
378 | * |
||
379 | * @param string $retailsResult |
||
380 | * |
||
381 | * @return array array of retails with their details |
||
382 | */ |
||
383 | private function parseRetailsResult(string $retailsResult): array |
||
404 | |||
405 | /** |
||
406 | * Parse a tracking xml response |
||
407 | * |
||
408 | * @param string $result |
||
409 | * |
||
410 | * @return array Details of the tracking |
||
411 | */ |
||
412 | private function parseTrackResult(string $result): array |
||
431 | |||
432 | /** |
||
433 | * Return a successful response with the data. |
||
434 | * |
||
435 | * @param $type string The type/function of the SOAP Api used. |
||
436 | * @param $payload AbstractStructBase The object that was sent to the SoapClient |
||
437 | * @param $data mixed |
||
438 | * |
||
439 | * @return SMSAResponse |
||
440 | */ |
||
441 | View Code Duplication | protected function successResponse($type, $payload, $data): SMSAResponse |
|
455 | |||
456 | /** |
||
457 | * Send a Response with failed request. |
||
458 | * |
||
459 | * @param $type string The type/function of the SOAP Api used. |
||
460 | * @param $payload AbstractStructBase The object that was sent to the SoapClient |
||
461 | * |
||
462 | * @throws \Alhoqbani\SmsaWebService\Exceptions\RequestError |
||
463 | * |
||
464 | * @return SMSAResponse |
||
465 | */ |
||
466 | protected function failedRequest($type, $payload) |
||
487 | |||
488 | /** |
||
489 | * Return a failed response. (Usually, wrong data was sent to SMSA Api) |
||
490 | * |
||
491 | * @param $type string The type/function of the SOAP Api used |
||
492 | * @param $payload AbstractStructBase The object that was sent to the SoapClient |
||
493 | * @param $error string The error message from SMSA. |
||
494 | * |
||
495 | * @throws FailedResponse |
||
496 | * |
||
497 | * @return SMSAResponse |
||
498 | */ |
||
499 | View Code Duplication | protected function failedResponse($type, $payload, $error) |
|
517 | |||
518 | protected function shouldUseExceptions() |
||
522 | |||
523 | /** |
||
524 | * To handle response error from Smsa Soap server |
||
525 | * |
||
526 | * @param SMSAResponse $response |
||
527 | * |
||
528 | * @throws RequestError |
||
529 | */ |
||
530 | protected function throwRequestError(SMSAResponse $response): void |
||
541 | } |
||
542 |
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.