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 |
||
| 38 | class Params |
||
| 39 | { |
||
| 40 | /** |
||
| 41 | * For injecting a custom Request Creator |
||
| 42 | * |
||
| 43 | * @var RequestCreatorInterface |
||
| 44 | */ |
||
| 45 | public $requestCreator; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * For injecting a custom session handler |
||
| 49 | * |
||
| 50 | * @var Session\Handler\HandlerInterface |
||
| 51 | */ |
||
| 52 | public $sessionHandler; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * For injecting a custom response handler |
||
| 56 | * |
||
| 57 | * @var ResponseHandlerInterface |
||
| 58 | */ |
||
| 59 | public $responseHandler; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Parameters for authenticating to the Amadeus Web Services |
||
| 63 | * |
||
| 64 | * @var Params\AuthParams |
||
| 65 | */ |
||
| 66 | public $authParams; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Parameters required to create the Session Handler |
||
| 70 | * |
||
| 71 | * @var Params\SessionHandlerParams |
||
| 72 | */ |
||
| 73 | public $sessionHandlerParams; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Parameters required to create the Request Creator |
||
| 77 | * |
||
| 78 | * @var Params\RequestCreatorParams |
||
| 79 | */ |
||
| 80 | public $requestCreatorParams; |
||
| 81 | |||
| 82 | |||
| 83 | /** |
||
| 84 | * @param array $params |
||
| 85 | */ |
||
| 86 | public function __construct($params = []) |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Load parameters from an associative array |
||
| 93 | * |
||
| 94 | * @param array $params |
||
| 95 | * @return void |
||
| 96 | */ |
||
| 97 | protected function loadFromArray(array $params) { |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Load Request Creator |
||
| 110 | * |
||
| 111 | * @param array $params |
||
| 112 | * @return void |
||
| 113 | */ |
||
| 114 | protected function loadRequestCreator($params) |
||
| 120 | |||
| 121 | |||
| 122 | /** |
||
| 123 | * Load Session Handler |
||
| 124 | * |
||
| 125 | * @param array $params |
||
| 126 | * @return void |
||
| 127 | */ |
||
| 128 | protected function loadSessionHandler($params) |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Load Response Handler |
||
| 137 | * |
||
| 138 | * @param array $params |
||
| 139 | * @return void |
||
| 140 | */ |
||
| 141 | protected function loadResponseHandler($params) |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Load Authentication Parameters |
||
| 150 | * |
||
| 151 | * @param array $params |
||
| 152 | * @return void |
||
| 153 | */ |
||
| 154 | View Code Duplication | protected function loadAuthParams($params) |
|
| 164 | |||
| 165 | /** |
||
| 166 | * Load Session Handler Parameters |
||
| 167 | * |
||
| 168 | * @param array $params |
||
| 169 | * @return void |
||
| 170 | */ |
||
| 171 | View Code Duplication | protected function loadSessionHandlerParams($params) |
|
| 181 | |||
| 182 | /** |
||
| 183 | * Load Request Creator Parameters |
||
| 184 | * |
||
| 185 | * @param array $params |
||
| 186 | * @return void |
||
| 187 | */ |
||
| 188 | View Code Duplication | protected function loadRequestCreatorParams($params) |
|
| 198 | } |
||
| 199 |
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.