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 PrestashopWebServiceLibrary 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 PrestashopWebServiceLibrary, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class PrestashopWebServiceLibrary |
||
| 13 | { |
||
| 14 | /** @var string Shop URL */ |
||
| 15 | protected $url; |
||
| 16 | |||
| 17 | /** @var string Authentification key */ |
||
| 18 | protected $key; |
||
| 19 | |||
| 20 | /** @var boolean is debug activated */ |
||
| 21 | protected $debug; |
||
| 22 | |||
| 23 | /** @var string PS version */ |
||
| 24 | protected $version; |
||
| 25 | |||
| 26 | /** @var boolean Are we running in a console */ |
||
| 27 | protected $runningInConsole; |
||
| 28 | |||
| 29 | /** @var array compatible versions of PrestaShop WebService */ |
||
| 30 | const PS_COMPATIBLE_VERSION_MIN = '1.4.0.0'; |
||
| 31 | const PS_COMPATIBLE_VERSION_MAX = '1.7.99.99'; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * PrestaShopWebService constructor. Throw an exception when CURL is not installed/activated |
||
| 35 | * <code> |
||
| 36 | * <?php |
||
| 37 | * require_once('./PrestaShopWebService.php'); |
||
| 38 | * try |
||
| 39 | * { |
||
| 40 | * $ws = new PrestaShopWebService('http://mystore.com/', 'ZQ88PRJX5VWQHCWE4EE7SQ7HPNX00RAJ', false); |
||
| 41 | * // Now we have a WebService object to play with |
||
| 42 | * } |
||
| 43 | * catch (PrestashopWebServiceException $ex) |
||
| 44 | * { |
||
| 45 | * echo 'Error : '.$ex->getMessage(); |
||
| 46 | * } |
||
| 47 | * ?> |
||
| 48 | * </code> |
||
| 49 | * @param string $url Root URL for the shop |
||
| 50 | * @param string $key Authentification key |
||
| 51 | * @param mixed $debug Debug mode Activated (true) or deactivated (false) |
||
| 52 | * @throws PrestashopWebServiceException |
||
| 53 | */ |
||
| 54 | public function __construct($url, $key, $debug = true) |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Take the status code and throw an exception if the server didn't return 200 or 201 code |
||
| 71 | * @param int $status_code Status code of an HTTP return |
||
| 72 | * @return boolean |
||
| 73 | * @throws PrestashopWebServiceException |
||
| 74 | */ |
||
| 75 | protected function checkRequest($request) |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Throws exception if prestashop version is not supported |
||
| 106 | * @param int $version The prestashop version |
||
| 107 | * @throws PrestashopWebServiceException |
||
| 108 | */ |
||
| 109 | public function isPrestashopVersionSupported($version) |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Prepares and validate a CURL request to PrestaShop WebService. Can throw exception. |
||
| 122 | * @param string $url Resource name |
||
| 123 | * @param mixed $curl_params CURL parameters (sent to curl_set_opt) |
||
| 124 | * @return array status_code, response |
||
| 125 | * @throws PrestashopWebServiceException |
||
| 126 | */ |
||
| 127 | protected function executeRequest($url, $curl_params = []) |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Executes the CURL request to PrestaShop WebService. |
||
| 203 | * @param string $url Resource name |
||
| 204 | * @param mixed $options CURL parameters (sent to curl_setopt_array) |
||
| 205 | * @return array response, info |
||
| 206 | */ |
||
| 207 | protected function executeCurl($url, array $options = []) |
||
| 227 | |||
| 228 | public function printDebug($title, $content) |
||
| 244 | |||
| 245 | public function getVersion() |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Load XML from string. Can throw exception |
||
| 252 | * @param string $response String from a CURL response |
||
| 253 | * @param boolean $suppressExceptions Whether to throw exceptions on errors |
||
| 254 | * @return SimpleXMLElement status_code, response |
||
| 255 | * @throws PrestashopWebServiceException |
||
| 256 | */ |
||
| 257 | protected function parseXML($response, $suppressExceptions = false) |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Add (POST) a resource |
||
| 282 | * <p>Unique parameter must take : <br><br> |
||
| 283 | * 'resource' => Resource name<br> |
||
| 284 | * 'postXml' => Full XML string to add resource<br><br> |
||
| 285 | * Examples are given in the tutorial</p> |
||
| 286 | * @param array $options |
||
| 287 | * @return SimpleXMLElement status_code, response |
||
| 288 | * @throws PrestashopWebServiceException |
||
| 289 | */ |
||
| 290 | public function add($options) |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Retrieve (GET) a resource |
||
| 314 | * <p>Unique parameter must take : <br><br> |
||
| 315 | * 'url' => Full URL for a GET request of WebService (ex: http://mystore.com/api/customers/1/)<br> |
||
| 316 | * OR<br> |
||
| 317 | * 'resource' => Resource name,<br> |
||
| 318 | * 'id' => ID of a resource you want to get<br><br> |
||
| 319 | * </p> |
||
| 320 | * <code> |
||
| 321 | * <?php |
||
| 322 | * require_once('./PrestaShopWebService.php'); |
||
| 323 | * try |
||
| 324 | * { |
||
| 325 | * $ws = new PrestaShopWebService('http://mystore.com/', 'ZQ88PRJX5VWQHCWE4EE7SQ7HPNX00RAJ', false); |
||
| 326 | * $xml = $ws->get(array('resource' => 'orders', 'id' => 1)); |
||
| 327 | * // Here in $xml, a SimpleXMLElement object you can parse |
||
| 328 | * foreach ($xml->children()->children() as $attName => $attValue) |
||
| 329 | * echo $attName.' = '.$attValue.'<br />'; |
||
| 330 | * } |
||
| 331 | * catch (PrestashopWebServiceException $ex) |
||
| 332 | * { |
||
| 333 | * echo 'Error : '.$ex->getMessage(); |
||
| 334 | * } |
||
| 335 | * ?> |
||
| 336 | * </code> |
||
| 337 | * @param array $options Array representing resource to get. |
||
| 338 | * @return SimpleXMLElement status_code, response |
||
| 339 | * @throws PrestashopWebServiceException |
||
| 340 | */ |
||
| 341 | public function get($options) |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Head method (HEAD) a resource |
||
| 375 | * |
||
| 376 | * @param array $options Array representing resource for head request. |
||
| 377 | * @return SimpleXMLElement status_code, response |
||
| 378 | * @throws PrestashopWebServiceException |
||
| 379 | */ |
||
| 380 | public function head($options) |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Edit (PUT) a resource |
||
| 412 | * <p>Unique parameter must take : <br><br> |
||
| 413 | * 'resource' => Resource name ,<br> |
||
| 414 | * 'id' => ID of a resource you want to edit,<br> |
||
| 415 | * 'putXml' => Modified XML string of a resource<br><br> |
||
| 416 | * Examples are given in the tutorial</p> |
||
| 417 | * @param array $options Array representing resource to edit. |
||
| 418 | * @return SimpleXMLElement |
||
| 419 | * @throws PrestashopWebServiceException |
||
| 420 | */ |
||
| 421 | public function edit($options) |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Delete (DELETE) a resource. |
||
| 450 | * Unique parameter must take : <br><br> |
||
| 451 | * 'resource' => Resource name<br> |
||
| 452 | * 'id' => ID or array which contains IDs of a resource(s) you want to delete<br><br> |
||
| 453 | * <code> |
||
| 454 | * <?php |
||
| 455 | * require_once('./PrestaShopWebService.php'); |
||
| 456 | * try |
||
| 457 | * { |
||
| 458 | * $ws = new PrestaShopWebService('http://mystore.com/', 'ZQ88PRJX5VWQHCWE4EE7SQ7HPNX00RAJ', false); |
||
| 459 | * $xml = $ws->delete(array('resource' => 'orders', 'id' => 1)); |
||
| 460 | * // Following code will not be executed if an exception is thrown. |
||
| 461 | * echo 'Successfully deleted.'; |
||
| 462 | * } |
||
| 463 | * catch (PrestashopWebServiceException $ex) |
||
| 464 | * { |
||
| 465 | * echo 'Error : '.$ex->getMessage(); |
||
| 466 | * } |
||
| 467 | * ?> |
||
| 468 | * </code> |
||
| 469 | * @param array $options Array representing resource to delete. |
||
| 470 | * @return bool |
||
| 471 | * @throws PrestashopWebServiceException |
||
| 472 | */ |
||
| 473 | public function delete($options) |
||
| 494 | } |
||
| 495 |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: