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 |
||
| 27 | class AuthnetApiFactory |
||
| 28 | {
|
||
| 29 | /** |
||
| 30 | * @const Indicates use of Authorize.Net's production server |
||
| 31 | */ |
||
| 32 | const USE_PRODUCTION_SERVER = 0; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @const Indicates use of the development server |
||
| 36 | */ |
||
| 37 | const USE_DEVELOPMENT_SERVER = 1; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @const Indicates use of the Akamai endpoint |
||
| 41 | */ |
||
| 42 | const USE_AKAMAI_SERVER = 2; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Validates the Authorize.Net credentials and returns a Request object to be used to make an API call |
||
| 46 | * |
||
| 47 | * @param string $login Authorize.Net API Login ID |
||
| 48 | * @param string $transaction_key Authorize.Net API Transaction Key |
||
| 49 | * @param integer $server ID of which server to use (optional) |
||
| 50 | * @return object \JohnConde\Authnet\AuthnetJson |
||
| 51 | * @throws \JohnConde\Authnet\AuthnetInvalidCredentialsException |
||
| 52 | */ |
||
| 53 | public static function getJsonApiHandler($login, $transaction_key, $server = self::USE_AKAMAI_SERVER) |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Gets the API endpoint to be used for a JSON API call |
||
| 77 | * |
||
| 78 | * @param integer $server ID of which server to use |
||
| 79 | * @return string The URL endpoint the request is to be sent to |
||
| 80 | * @throws \JohnConde\Authnet\AuthnetInvalidServerException |
||
| 81 | */ |
||
| 82 | protected static function getWebServiceURL($server) |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Validates the Authorize.Net credentials and returns a SIM object to be used to make a SIM API call |
||
| 101 | * |
||
| 102 | * @param string $login Authorize.Net API Login ID |
||
| 103 | * @param string $transaction_key Authorize.Net API Transaction Key |
||
| 104 | * @param integer $server ID of which server to use (optional) |
||
| 105 | * @return object \JohnConde\Authnet\AuthnetSim |
||
| 106 | * @throws \JohnConde\Authnet\AuthnetInvalidCredentialsException |
||
| 107 | */ |
||
| 108 | public static function getSimHandler($login, $transaction_key, $server = self::USE_PRODUCTION_SERVER) |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Gets the API endpoint to be used for a SIM API call |
||
| 123 | * |
||
| 124 | * @param integer $server ID of which server to use |
||
| 125 | * @return string The URL endpoint the request is to be sent to |
||
| 126 | * @throws \JohnConde\Authnet\AuthnetInvalidServerException |
||
| 127 | */ |
||
| 128 | View Code Duplication | protected static function getSimURL($server) |
|
| 141 | |||
| 142 | /** |
||
| 143 | * Validates the Authorize.Net credentials and returns a Webhooks Request object to be used to make an Webhook call |
||
| 144 | * |
||
| 145 | * @param string $login Authorize.Net API Login ID |
||
| 146 | * @param string $transaction_key Authorize.Net API Transaction Key |
||
| 147 | * @param integer $server ID of which server to use (optional) |
||
| 148 | * @return object \JohnConde\Authnet\AuthnetJson |
||
| 149 | * @throws \JohnConde\Authnet\AuthnetInvalidCredentialsException |
||
| 150 | */ |
||
| 151 | public static function getWebhooksHandler($login, $transaction_key, $server = self::USE_PRODUCTION_SERVER) |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Gets the API endpoint to be used for a SIM API call |
||
| 178 | * |
||
| 179 | * @param integer $server ID of which server to use |
||
| 180 | * @return string The URL endpoint the request is to be sent to |
||
| 181 | * @throws \JohnConde\Authnet\AuthnetInvalidServerException |
||
| 182 | */ |
||
| 183 | View Code Duplication | protected static function getWebhooksURL($server) |
|
| 196 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.