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 |
||
| 20 | class TaskLinkEventBuilder extends BaseEventBuilder |
||
| 21 | { |
||
| 22 | protected $taskLinkId = 0; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Set taskLinkId. |
||
| 26 | * |
||
| 27 | * @param int $taskLinkId |
||
| 28 | * |
||
| 29 | * @return $this |
||
| 30 | */ |
||
| 31 | public function withTaskLinkId($taskLinkId) |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Build event data. |
||
| 40 | * |
||
| 41 | * @return TaskLinkEvent|null |
||
| 42 | */ |
||
| 43 | View Code Duplication | public function buildEvent() |
|
| 58 | |||
| 59 | /** |
||
| 60 | * Get event title with author. |
||
| 61 | * |
||
| 62 | * @param string $author |
||
| 63 | * @param string $eventName |
||
| 64 | * @param array $eventData |
||
| 65 | * |
||
| 66 | * @return string |
||
| 67 | */ |
||
| 68 | public function buildTitleWithAuthor($author, $eventName, array $eventData) |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Get event title without author. |
||
| 81 | * |
||
| 82 | * @param string $eventName |
||
| 83 | * @param array $eventData |
||
| 84 | * |
||
| 85 | * @return string |
||
| 86 | */ |
||
| 87 | public function buildTitleWithoutAuthor($eventName, array $eventData) |
||
| 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.