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 |
||
31 | class SinchExceptionResolver |
||
32 | { |
||
33 | /** |
||
34 | * Create appropriate Sinch exception |
||
35 | * |
||
36 | * @param ClientException $e Exception |
||
37 | * |
||
38 | * @return \Exception|\Fresh\SinchBundle\Exception\SinchException |
||
39 | */ |
||
40 | public static function createAppropriateSinchException(ClientException $e) |
||
74 | |||
75 | /** |
||
76 | * Get Sinch exception for bad request |
||
77 | * |
||
78 | * @param int $errorCode Sinch error code |
||
79 | * @param string $errorMessage Sinch error message |
||
80 | * |
||
81 | * @return \Fresh\SinchBundle\Exception\SinchException|null |
||
82 | */ |
||
83 | View Code Duplication | private static function getSinchExceptionForBadRequest($errorCode, $errorMessage) |
|
101 | |||
102 | /** |
||
103 | * Get Sinch exception for unauthorized |
||
104 | * |
||
105 | * @param int $errorCode Sinch error code |
||
106 | * @param string $errorMessage Sinch error message |
||
107 | * |
||
108 | * @return \Fresh\SinchBundle\Exception\SinchException|null |
||
109 | */ |
||
110 | View Code Duplication | private static function getSinchExceptionForUnauthorized($errorCode, $errorMessage) |
|
120 | |||
121 | /** |
||
122 | * Get Sinch exception for payment required |
||
123 | * |
||
124 | * Sinch returns 402 code when application run out of money |
||
125 | * |
||
126 | * @param int $errorCode Sinch error code |
||
127 | * @param string $errorMessage Sinch error message |
||
128 | * |
||
129 | * @return \Fresh\SinchBundle\Exception\SinchException|null |
||
130 | */ |
||
131 | View Code Duplication | private static function getSinchExceptionForPaymentRequired($errorCode, $errorMessage) |
|
141 | |||
142 | /** |
||
143 | * Get Sinch exception for forbidden |
||
144 | * |
||
145 | * @param int $errorCode Sinch error code |
||
146 | * @param string $errorMessage Sinch error message |
||
147 | * |
||
148 | * @return \Fresh\SinchBundle\Exception\SinchException|null |
||
149 | */ |
||
150 | View Code Duplication | private static function getSinchExceptionForForbidden($errorCode, $errorMessage) |
|
169 | |||
170 | /** |
||
171 | * Get Sinch exception for internal server error |
||
172 | * |
||
173 | * @param int $errorCode Sinch error code |
||
174 | * @param string $errorMessage Sinch error message |
||
175 | * |
||
176 | * @return \Fresh\SinchBundle\Exception\SinchException|null |
||
177 | */ |
||
178 | View Code Duplication | private static function getSinchExceptionForInternalServerError($errorCode, $errorMessage) |
|
188 | |||
189 | // endregion |
||
190 | } |
||
191 |
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.