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 |
||
| 41 | class RemoteRequest extends RemoteRequestBuilder { |
||
| 42 | |||
| 43 | |||
| 44 | /** |
||
| 45 | * @param AppService $remote |
||
| 46 | */ |
||
| 47 | View Code Duplication | public function save(AppService $remote): void { |
|
| 56 | |||
| 57 | |||
| 58 | /** |
||
| 59 | * @param AppService $remote |
||
| 60 | */ |
||
| 61 | View Code Duplication | public function update(AppService $remote) { |
|
| 71 | |||
| 72 | |||
| 73 | /** |
||
| 74 | * @param AppService $remote |
||
| 75 | */ |
||
| 76 | View Code Duplication | public function updateInstance(AppService $remote) { |
|
| 84 | |||
| 85 | |||
| 86 | /** |
||
| 87 | * @param AppService $remote |
||
| 88 | */ |
||
| 89 | View Code Duplication | public function updateHref(AppService $remote) { |
|
| 97 | |||
| 98 | |||
| 99 | /** |
||
| 100 | * @param string $host |
||
| 101 | * |
||
| 102 | * @return AppService |
||
| 103 | * @throws RowNotFoundException |
||
| 104 | */ |
||
| 105 | public function getFromInstance(string $host): AppService { |
||
| 106 | $qb = $this->getRemoteSelectSql(); |
||
| 107 | $qb->limitToInstance($host); |
||
| 108 | |||
| 109 | return $this->getItemFromRequest($qb); |
||
| 110 | } |
||
| 111 | |||
| 112 | |||
| 113 | /** |
||
| 114 | * @param string $href |
||
| 115 | * |
||
| 116 | * @return AppService |
||
| 117 | * @throws RowNotFoundException |
||
| 118 | */ |
||
| 119 | public function getFromHref(string $href): AppService { |
||
| 125 | |||
| 126 | |||
| 127 | } |
||
| 128 | |||
| 129 |
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.