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:
| 1 | <?php |
||
| 18 | class Bpack247 |
||
| 19 | { |
||
| 20 | // URL for the api |
||
| 21 | const API_URL = 'http://www.bpack247.be/BpostRegistrationWebserviceREST/servicecontroller.svc'; |
||
| 22 | |||
| 23 | // current version |
||
| 24 | const VERSION = '3.0.0'; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * The account id |
||
| 28 | * |
||
| 29 | * @var string |
||
| 30 | */ |
||
| 31 | private $accountId; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * A cURL instance |
||
| 35 | * |
||
| 36 | * @var resource |
||
| 37 | */ |
||
| 38 | private $curl; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * The passPhrase |
||
| 42 | * |
||
| 43 | * @var string |
||
| 44 | */ |
||
| 45 | private $passPhrase; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * The port to use. |
||
| 49 | * |
||
| 50 | * @var int |
||
| 51 | */ |
||
| 52 | private $port; |
||
|
|
|||
| 53 | |||
| 54 | /** |
||
| 55 | * The timeout |
||
| 56 | * |
||
| 57 | * @var int |
||
| 58 | */ |
||
| 59 | private $timeOut = 30; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * The user agent |
||
| 63 | * |
||
| 64 | * @var string |
||
| 65 | */ |
||
| 66 | private $userAgent; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Make the call |
||
| 70 | * |
||
| 71 | * @param string $url The URL to call. |
||
| 72 | * @param string $body The data to pass. |
||
| 73 | * @param string $method The HTTP-method to use. |
||
| 74 | * @return \SimpleXMLElement |
||
| 75 | * @throws BpostApiBusinessException |
||
| 76 | * @throws BpostApiSystemException |
||
| 77 | * @throws BpostCurlException |
||
| 78 | * @throws BpostInvalidResponseException |
||
| 79 | */ |
||
| 80 | private function doCall($url, $body = null, $method = 'GET') |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Generate the secret string for the Authorization header |
||
| 154 | * |
||
| 155 | * @return string |
||
| 156 | */ |
||
| 157 | private function getAuthorizationHeader() |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Set the timeout |
||
| 164 | * After this time the request will stop. You should handle any errors triggered by this. |
||
| 165 | * |
||
| 166 | * @param int $seconds The timeout in seconds. |
||
| 167 | */ |
||
| 168 | public function setTimeOut($seconds) |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Get the timeout that will be used |
||
| 175 | * |
||
| 176 | * @return int |
||
| 177 | */ |
||
| 178 | public function getTimeOut() |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Get the useragent that will be used. |
||
| 185 | * Our version will be prepended to yours. |
||
| 186 | * It will look like: "PHP Bpost/<version> <your-user-agent>" |
||
| 187 | * |
||
| 188 | * @return string |
||
| 189 | */ |
||
| 190 | public function getUserAgent() |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Set the user-agent for you application |
||
| 197 | * It will be appended to ours, the result will look like: "PHP Bpost/<version> <your-user-agent>" |
||
| 198 | * |
||
| 199 | * @param string $userAgent Your user-agent, it should look like <app-name>/<app-version>. |
||
| 200 | */ |
||
| 201 | public function setUserAgent($userAgent) |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Create Bpost instance |
||
| 208 | * |
||
| 209 | * @param string $accountId |
||
| 210 | * @param string $passPhrase |
||
| 211 | */ |
||
| 212 | public function __construct($accountId, $passPhrase) |
||
| 217 | |||
| 218 | // webservice methods |
||
| 219 | /** |
||
| 220 | * @param Customer $customer |
||
| 221 | * |
||
| 222 | * @return \SimpleXMLElement |
||
| 223 | * @throws BpostApiBusinessException |
||
| 224 | * @throws BpostApiSystemException |
||
| 225 | * @throws BpostCurlException |
||
| 226 | * @throws BpostInvalidResponseException |
||
| 227 | */ |
||
| 228 | public function createMember(Customer $customer) |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Retrieve member information |
||
| 251 | * |
||
| 252 | * @param string $id |
||
| 253 | * |
||
| 254 | * @return Customer |
||
| 255 | * @throws BpostApiBusinessException |
||
| 256 | * @throws BpostApiSystemException |
||
| 257 | * @throws BpostCurlException |
||
| 258 | * @throws BpostInvalidResponseException |
||
| 259 | * @throws Exception\XmlException\BpostXmlNoUserIdFoundException |
||
| 260 | */ |
||
| 261 | public function getMember($id) |
||
| 269 | } |
||
| 270 |
This check marks private properties in classes that are never used. Those properties can be removed.