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 |
||
| 15 | View Code Duplication | abstract class TemplateCompiler |
|
|
|
|||
| 16 | { |
||
| 17 | /** |
||
| 18 | * Compile keyboard. |
||
| 19 | * |
||
| 20 | * @param Keyboard $keyboard |
||
| 21 | * @param array $args |
||
| 22 | * |
||
| 23 | * @return mixed |
||
| 24 | */ |
||
| 25 | abstract protected function compileKeyboard(Keyboard $keyboard, array $args); |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Compile payload button. |
||
| 29 | * |
||
| 30 | * @param PayloadButton $button |
||
| 31 | * @param array $args |
||
| 32 | * |
||
| 33 | * @return mixed |
||
| 34 | */ |
||
| 35 | abstract protected function compilePayloadButton(PayloadButton $button, array $args); |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Compile reply button. |
||
| 39 | * |
||
| 40 | * @param ReplyButton $button |
||
| 41 | * @param array $args |
||
| 42 | * |
||
| 43 | * @return mixed |
||
| 44 | */ |
||
| 45 | abstract protected function compileReplyButton(ReplyButton $button, array $args); |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Compile url button. |
||
| 49 | * |
||
| 50 | * @param UrlButton $button |
||
| 51 | * @param array $args |
||
| 52 | * |
||
| 53 | * @return mixed |
||
| 54 | */ |
||
| 55 | abstract protected function compileUrlButton(UrlButton $button, array $args); |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Compile template. |
||
| 59 | * |
||
| 60 | * @param Template $template |
||
| 61 | * @param array $args |
||
| 62 | * |
||
| 63 | * @return mixed |
||
| 64 | */ |
||
| 65 | public function compile(Template $template, array $args = []) |
||
| 97 | } |
||
| 98 |
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.