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 |
||
| 23 | class Sample extends ServiceAbstractBase |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * Simple ping |
||
| 27 | * |
||
| 28 | * @SWG\Get( |
||
| 29 | * path="/sample/ping", |
||
| 30 | * tags={"zz_sample"}, |
||
| 31 | * @SWG\Response( |
||
| 32 | * response=200, |
||
| 33 | * description="The object", |
||
| 34 | * @SWG\Schema( |
||
| 35 | * required={"result"}, |
||
| 36 | * @SWG\Property(property="result", type="string") |
||
| 37 | * ) |
||
| 38 | * ) |
||
| 39 | * ) |
||
| 40 | * @param HttpResponse $response |
||
| 41 | * @param HttpRequest $request |
||
| 42 | */ |
||
| 43 | public function getPing($response, $request) |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Get the rows from the Dummy table (used in the example) |
||
| 52 | * @SWG\Get( |
||
| 53 | * path="/sample/dummy/{field}", |
||
| 54 | * tags={"zz_sample"}, |
||
| 55 | * @SWG\Parameter( |
||
| 56 | * name="field", |
||
| 57 | * in="path", |
||
| 58 | * description="The field content to be returned", |
||
| 59 | * required=true, |
||
| 60 | * type="string" |
||
| 61 | * ), |
||
| 62 | * @SWG\Response( |
||
| 63 | * response=200, |
||
| 64 | * description="The object", |
||
| 65 | * @SWG\Schema(type="array", @SWG\Items(ref="#/definitions/Dummy")) |
||
| 66 | * ), |
||
| 67 | * @SWG\Response( |
||
| 68 | * response=404, |
||
| 69 | * description="Not found", |
||
| 70 | * @SWG\Schema(ref="#/definitions/error") |
||
| 71 | * ), |
||
| 72 | * @SWG\Response( |
||
| 73 | * response=500, |
||
| 74 | * description="Erro Geral", |
||
| 75 | * @SWG\Schema(ref="#/definitions/error") |
||
| 76 | * ) |
||
| 77 | * ) |
||
| 78 | * |
||
| 79 | * @param HttpResponse $response |
||
| 80 | * @param HttpRequest $request |
||
| 81 | * @throws ConfigNotFoundException |
||
| 82 | * @throws EnvironmentException |
||
| 83 | * @throws Error404Exception |
||
| 84 | * @throws InvalidArgumentException |
||
| 85 | * @throws KeyNotFoundException |
||
| 86 | * @throws ReflectionException |
||
| 87 | * @throws \ByJG\MicroOrm\Exception\InvalidArgumentException |
||
| 88 | * @throws \ByJG\Serializer\Exception\InvalidArgumentException |
||
| 89 | */ |
||
| 90 | View Code Duplication | public function getDummy($response, $request) |
|
| 103 | |||
| 104 | /** |
||
| 105 | * Save data content in the table Dummy |
||
| 106 | * @SWG\Post( |
||
| 107 | * path="/sample/dummy", |
||
| 108 | * tags={"zz_sample"}, |
||
| 109 | * @SWG\Parameter( |
||
| 110 | * name="body", |
||
| 111 | * in="body", |
||
| 112 | * description="The dummy data", |
||
| 113 | * required=true, |
||
| 114 | * @SWG\Schema(ref="#/definitions/Dummy") |
||
| 115 | * ), |
||
| 116 | * @SWG\Response( |
||
| 117 | * response=200, |
||
| 118 | * description="The object", |
||
| 119 | * ), |
||
| 120 | * @SWG\Response( |
||
| 121 | * response=500, |
||
| 122 | * description="Erro Geral", |
||
| 123 | * @SWG\Schema(ref="#/definitions/error") |
||
| 124 | * ) |
||
| 125 | * ) |
||
| 126 | * |
||
| 127 | * @param HttpResponse $response |
||
| 128 | * @param HttpRequest $request |
||
| 129 | * @throws ConfigNotFoundException |
||
| 130 | * @throws EnvironmentException |
||
| 131 | * @throws InvalidArgumentException |
||
| 132 | * @throws KeyNotFoundException |
||
| 133 | * @throws OrmBeforeInvalidException |
||
| 134 | * @throws OrmInvalidFieldsException |
||
| 135 | * @throws \ByJG\MicroOrm\Exception\InvalidArgumentException |
||
| 136 | * @throws \ByJG\Serializer\Exception\InvalidArgumentException |
||
| 137 | * @throws ReflectionException |
||
| 138 | */ |
||
| 139 | public function postDummy($response, $request) |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Get the rows from the DummyHex table by ID |
||
| 151 | * @SWG\Get( |
||
| 152 | * path="/sample/dummyhex/{id}", |
||
| 153 | * tags={"zz_sample"}, |
||
| 154 | * @SWG\Parameter( |
||
| 155 | * name="id", |
||
| 156 | * in="path", |
||
| 157 | * description="The field content to be returned", |
||
| 158 | * required=true, |
||
| 159 | * type="string" |
||
| 160 | * ), |
||
| 161 | * @SWG\Response( |
||
| 162 | * response=200, |
||
| 163 | * description="The object", |
||
| 164 | * @SWG\Schema(ref="#/definitions/DummyHex") |
||
| 165 | * ), |
||
| 166 | * @SWG\Response( |
||
| 167 | * response=404, |
||
| 168 | * description="Not found", |
||
| 169 | * @SWG\Schema(ref="#/definitions/error") |
||
| 170 | * ), |
||
| 171 | * @SWG\Response( |
||
| 172 | * response=500, |
||
| 173 | * description="Erro Geral", |
||
| 174 | * @SWG\Schema(ref="#/definitions/error") |
||
| 175 | * ) |
||
| 176 | * ) |
||
| 177 | * |
||
| 178 | * @param HttpResponse $response |
||
| 179 | * @param HttpRequest $request |
||
| 180 | * @throws ConfigNotFoundException |
||
| 181 | * @throws EnvironmentException |
||
| 182 | * @throws Error404Exception |
||
| 183 | * @throws InvalidArgumentException |
||
| 184 | * @throws KeyNotFoundException |
||
| 185 | * @throws \ByJG\MicroOrm\Exception\InvalidArgumentException |
||
| 186 | * @throws \ByJG\Serializer\Exception\InvalidArgumentException |
||
| 187 | * @throws ReflectionException |
||
| 188 | */ |
||
| 189 | View Code Duplication | public function getDummyHex($response, $request) |
|
| 202 | |||
| 203 | /** |
||
| 204 | * Save data content in the table Dummy Hex |
||
| 205 | * @SWG\Post( |
||
| 206 | * path="/sample/dummyhex", |
||
| 207 | * tags={"zz_sample"}, |
||
| 208 | * @SWG\Parameter( |
||
| 209 | * name="body", |
||
| 210 | * in="body", |
||
| 211 | * description="The dummy data", |
||
| 212 | * required=true, |
||
| 213 | * @SWG\Schema(ref="#/definitions/DummyHex") |
||
| 214 | * ), |
||
| 215 | * @SWG\Response( |
||
| 216 | * response=200, |
||
| 217 | * description="The object", |
||
| 218 | * @SWG\Schema(ref="#/definitions/DummyHex") |
||
| 219 | * ), |
||
| 220 | * @SWG\Response( |
||
| 221 | * response=500, |
||
| 222 | * description="Erro Geral", |
||
| 223 | * @SWG\Schema(ref="#/definitions/error") |
||
| 224 | * ) |
||
| 225 | * ) |
||
| 226 | * |
||
| 227 | * @param HttpResponse $response |
||
| 228 | * @param HttpRequest $request |
||
| 229 | * @throws ConfigNotFoundException |
||
| 230 | * @throws EnvironmentException |
||
| 231 | * @throws InvalidArgumentException |
||
| 232 | * @throws KeyNotFoundException |
||
| 233 | * @throws OrmBeforeInvalidException |
||
| 234 | * @throws OrmInvalidFieldsException |
||
| 235 | * @throws \ByJG\MicroOrm\Exception\InvalidArgumentException |
||
| 236 | * @throws \ByJG\Serializer\Exception\InvalidArgumentException |
||
| 237 | * @throws ReflectionException |
||
| 238 | */ |
||
| 239 | public function postDummyHex($response, $request) |
||
| 252 | } |
||
| 253 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.