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 |
||
25 | class PlaylistBusinessLayer extends BusinessLayer { |
||
26 | |||
27 | private $logger; |
||
28 | |||
29 | public function __construct(PlaylistMapper $playlistMapper, Logger $logger) { |
||
33 | |||
34 | /** |
||
35 | * Return a playlist |
||
36 | * @param int $playlistId the id of the playlist |
||
37 | * @param string $userId the name of the user |
||
38 | * @return Playlist playlist |
||
39 | */ |
||
40 | public function find($playlistId, $userId) { |
||
43 | |||
44 | /** |
||
45 | * Returns all playlists |
||
46 | * @param string $userId the name of the user |
||
47 | * @return Playlist[] playlists |
||
48 | */ |
||
49 | public function findAll($userId) { |
||
52 | |||
53 | View Code Duplication | public function addTracks($trackIds, $playlistId, $userId) { |
|
60 | |||
61 | View Code Duplication | public function removeTracks($trackIds, $playlistId, $userId) { |
|
68 | |||
69 | public function create($name, $userId) { |
||
76 | |||
77 | public function rename($name, $playlistId, $userId) { |
||
83 | |||
84 | /** |
||
85 | * removes tracks from all available playlists |
||
86 | * @param int[] $trackIds array of all track IDs to remove |
||
87 | */ |
||
88 | public function removeTracksFromAllLists($trackIds) { |
||
99 | } |
||
100 |
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.