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 |
||
| 33 | View Code Duplication | class JavaScriptContent extends TextContent { |
|
|
|
|||
| 34 | |||
| 35 | /** |
||
| 36 | * @var bool|Title|null |
||
| 37 | */ |
||
| 38 | private $redirectTarget = false; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @param string $text JavaScript code. |
||
| 42 | * @param string $modelId the content model name |
||
| 43 | */ |
||
| 44 | public function __construct( $text, $modelId = CONTENT_MODEL_JAVASCRIPT ) { |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Returns a Content object with pre-save transformations applied using |
||
| 50 | * Parser::preSaveTransform(). |
||
| 51 | * |
||
| 52 | * @param Title $title |
||
| 53 | * @param User $user |
||
| 54 | * @param ParserOptions $popts |
||
| 55 | * |
||
| 56 | * @return JavaScriptContent |
||
| 57 | */ |
||
| 58 | public function preSaveTransform( Title $title, User $user, ParserOptions $popts ) { |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @return string JavaScript wrapped in a <pre> tag. |
||
| 71 | */ |
||
| 72 | protected function getHtml() { |
||
| 80 | |||
| 81 | /** |
||
| 82 | * If this page is a redirect, return the content |
||
| 83 | * if it should redirect to $target instead |
||
| 84 | * |
||
| 85 | * @param Title $target |
||
| 86 | * @return JavaScriptContent |
||
| 87 | */ |
||
| 88 | public function updateRedirect( Title $target ) { |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @return Title|null |
||
| 98 | */ |
||
| 99 | public function getRedirectTarget() { |
||
| 122 | |||
| 123 | } |
||
| 124 |
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.