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 |
||
10 | class Musixmatch extends RESTfulService |
||
11 | { |
||
12 | /** |
||
13 | * Construct an instance of Musixmatch service. |
||
14 | * |
||
15 | * @param string $key The Musixmatch API key |
||
16 | * @param Client|null $client The Guzzle HTTP client |
||
17 | */ |
||
18 | View Code Duplication | public function __construct($key = null, Client $client = null) |
|
27 | |||
28 | /** |
||
29 | * Determine if our application is using Musixmatch. |
||
30 | * |
||
31 | * @return bool |
||
32 | */ |
||
33 | public function enabled() |
||
37 | |||
38 | /** |
||
39 | * @param App\Models\Song $song |
||
40 | * |
||
41 | * @return mixed|null |
||
42 | */ |
||
43 | public function searchLyricsRelatedToSong(Song $song) |
||
58 | |||
59 | /** |
||
60 | * @param string $name |
||
61 | * @param string $artistName |
||
62 | * |
||
63 | * @return mixed|false |
||
64 | */ |
||
65 | public function search(string $name, string $artistName = null) |
||
87 | |||
88 | /** |
||
89 | * @param string $response |
||
90 | * |
||
91 | * @return mixed |
||
92 | */ |
||
93 | private function jsonpDecode($response) |
||
97 | |||
98 | /** |
||
99 | * @param JSON $response |
||
100 | * |
||
101 | * @return string|null |
||
102 | */ |
||
103 | private function getLyrics($response) |
||
116 | } |
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.