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 |
||
| 7 | class UploadResponse implements ResponseContract |
||
| 8 | { |
||
| 9 | use \Jaxon\Features\Config; |
||
| 10 | |||
| 11 | /** |
||
| 12 | * The response type |
||
| 13 | * |
||
| 14 | * @var string |
||
| 15 | */ |
||
| 16 | private $sContentType = 'text/html'; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * The path to the uploaded file |
||
| 20 | * |
||
| 21 | * @var string |
||
| 22 | */ |
||
| 23 | private $sUploadedFile = ''; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * The error message |
||
| 27 | * |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | private $sErrorMessage = ''; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Get the content type, which is always set to 'text/json' |
||
| 34 | * |
||
| 35 | * @return string |
||
| 36 | */ |
||
| 37 | public function getContentType() |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Set the path to the uploaded file |
||
| 44 | */ |
||
| 45 | public function setUploadedFile($sUploadedFile) |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Set the error message |
||
| 52 | */ |
||
| 53 | public function setErrorMessage($sErrorMessage) |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Get the configured character encoding |
||
| 60 | * |
||
| 61 | * @return string |
||
| 62 | */ |
||
| 63 | public function getCharacterEncoding() |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Add a command to display a debug message to the user |
||
| 70 | * |
||
| 71 | * @param string $sMessage The message to be displayed |
||
| 72 | * |
||
| 73 | * @return \Jaxon\Plugin\Response |
||
| 74 | */ |
||
| 75 | public function debug($sMessage) |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Used internally to generate the response headers |
||
| 82 | * |
||
| 83 | * @return void |
||
| 84 | */ |
||
| 85 | public function sendHeaders() |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Return the output, generated from the commands added to the response, that will be sent to the browser |
||
| 99 | * |
||
| 100 | * @return string |
||
| 101 | */ |
||
| 102 | public function getOutput() |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Print the output, generated from the commands added to the response, that will be sent to the browser |
||
| 112 | * |
||
| 113 | * @return void |
||
| 114 | */ |
||
| 115 | public function printOutput() |
||
| 119 | } |
||
| 120 |
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.