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\Contracts\Response |
||
| 74 | */ |
||
| 75 | public function debug($sMessage) |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Used internally to generate the response headers |
||
| 83 | * |
||
| 84 | * @return void |
||
| 85 | */ |
||
| 86 | public function sendHeaders() |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Return the output, generated from the commands added to the response, that will be sent to the browser |
||
| 100 | * |
||
| 101 | * @return string |
||
| 102 | */ |
||
| 103 | public function getOutput() |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Print the output, generated from the commands added to the response, that will be sent to the browser |
||
| 113 | * |
||
| 114 | * @return void |
||
| 115 | */ |
||
| 116 | public function printOutput() |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Merge the response commands from the specified <Response> object with |
||
| 123 | * the response commands in this <Response> object |
||
| 124 | * |
||
| 125 | * @param ResponseContract $mCommands The <Response> object |
||
| 126 | * @param boolean $bBefore Add the new commands to the beginning of the list |
||
| 127 | * |
||
| 128 | * @return void |
||
| 129 | */ |
||
| 130 | public function appendResponse(ResponseContract $mCommands, $bBefore = false) |
||
| 134 | } |
||
| 135 |
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.