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 SnapshotsController extends AbstractController |
||
24 | { |
||
25 | /** |
||
26 | * List of snapshots. |
||
27 | * |
||
28 | * @param SnapshotService $service |
||
29 | * @param Timestamps $timestamps |
||
30 | * @param Names $names |
||
31 | * @return string |
||
32 | */ |
||
33 | public function indexAction(SnapshotService $service, Timestamps $timestamps, Names $names) |
||
43 | |||
44 | /** |
||
45 | * View snapshot. |
||
46 | * |
||
47 | * @param SnapshotService $service |
||
48 | * @param Timestamps $timestamps |
||
49 | * @return string |
||
50 | */ |
||
51 | View Code Duplication | public function viewAction(SnapshotService $service, Timestamps $timestamps) |
|
67 | |||
68 | /** |
||
69 | * View last snapshot incident source. |
||
70 | * |
||
71 | * @param SnapshotService $service |
||
72 | * @return string |
||
73 | */ |
||
74 | public function iframeAction(SnapshotService $service) |
||
87 | |||
88 | /** |
||
89 | * Remove all snapshots with all incident records. |
||
90 | * |
||
91 | * @param SnapshotService $service |
||
92 | * @return array|\Psr\Http\Message\ResponseInterface |
||
93 | */ |
||
94 | public function removeAllAction(SnapshotService $service) |
||
112 | |||
113 | /** |
||
114 | * Remove single snapshot with all incident records. |
||
115 | * |
||
116 | * @param SnapshotService $service |
||
117 | * @param ServerRequestInterface $request |
||
118 | * @return array|\Psr\Http\Message\ResponseInterface |
||
119 | */ |
||
120 | View Code Duplication | public function removeAction(SnapshotService $service, ServerRequestInterface $request) |
|
145 | |||
146 | /** |
||
147 | * Build redirect URI for removal operation. |
||
148 | * |
||
149 | * @param ServerRequestInterface $request |
||
150 | * @return \Psr\Http\Message\UriInterface |
||
151 | */ |
||
152 | View Code Duplication | protected function removeBackURI(ServerRequestInterface $request) |
|
163 | } |
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.