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 |
||
8 | final class Synchronizer |
||
9 | { |
||
10 | /** |
||
11 | * @var CanManagePlaylists The target. |
||
12 | */ |
||
13 | private $target; |
||
14 | |||
15 | /** |
||
16 | * Synchronizer constructor. |
||
17 | * |
||
18 | * @param CanManagePlaylists $target The target. |
||
19 | */ |
||
20 | 6 | public function __construct(CanManagePlaylists $target) |
|
24 | |||
25 | /** |
||
26 | * Sync a track collection to a playlist. |
||
27 | * |
||
28 | * @param string $name The name of the target playlist. |
||
29 | * @param TrackCollection $source The track collection to sync to the playlist. |
||
30 | * @param SyncProgressCallback|null $callback The progress callback. |
||
31 | * |
||
32 | * @return SyncResult |
||
33 | */ |
||
34 | 6 | public function syncToPlaylist($name, TrackCollection $source, SyncProgressCallback $callback = null) |
|
42 | |||
43 | /** |
||
44 | * Assert that the playlist name is valid. |
||
45 | * |
||
46 | * @param string $name |
||
47 | */ |
||
48 | 6 | View Code Duplication | private function assertValidPlaylistName($name) |
57 | |||
58 | /** |
||
59 | * Get the playlist id of a playlist. |
||
60 | * |
||
61 | * If the playlist not exists, the playlist will be created. |
||
62 | * |
||
63 | * @param string $name |
||
64 | * |
||
65 | * @return string The playlist id. |
||
66 | */ |
||
67 | 3 | private function getPlaylistId($name) |
|
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.