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 |
||
7 | final class TrackCollection implements Iterator |
||
8 | { |
||
9 | private $tracks; |
||
10 | |||
11 | private $position = 0; |
||
12 | |||
13 | /** |
||
14 | * TrackCollection constructor. |
||
15 | * |
||
16 | * @param array $tracks An array of TrackInfo objects. |
||
17 | */ |
||
18 | 6 | public function __construct(array $tracks = []) |
|
23 | |||
24 | 6 | View Code Duplication | private function assertThatTheseAreAllTracks(array $tracks) |
36 | |||
37 | public function current() |
||
41 | |||
42 | public function next() |
||
46 | |||
47 | public function key() |
||
51 | |||
52 | public function valid() |
||
56 | |||
57 | public function rewind() |
||
61 | |||
62 | /** |
||
63 | * Create a sync progress. |
||
64 | * |
||
65 | * @param SyncProgressCallback|null $callback |
||
66 | * |
||
67 | * @return SyncProgress |
||
68 | */ |
||
69 | 3 | public function createSyncProgress(SyncProgressCallback $callback = null) |
|
75 | } |
||
76 |
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.