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 |
||
12 | View Code Duplication | final class ArtistInfoBuilder |
|
|
|||
13 | { |
||
14 | /** |
||
15 | * @var array |
||
16 | */ |
||
17 | private $query; |
||
18 | |||
19 | private function __construct() |
||
23 | |||
24 | /** |
||
25 | * @param string $artist |
||
26 | * |
||
27 | * @return ArtistInfoBuilder |
||
28 | */ |
||
29 | public static function forArtist(string $artist): self |
||
37 | |||
38 | /** |
||
39 | * @param string $mbid |
||
40 | * |
||
41 | * @return ArtistInfoBuilder |
||
42 | */ |
||
43 | public static function forMbid(string $mbid): self |
||
51 | |||
52 | /** |
||
53 | * @param bool $autocorrect |
||
54 | * |
||
55 | * @return ArtistInfoBuilder |
||
56 | */ |
||
57 | public function autocorrect(bool $autocorrect): self |
||
63 | |||
64 | /** |
||
65 | * @param string $name |
||
66 | * |
||
67 | * @return ArtistInfoBuilder |
||
68 | */ |
||
69 | public function language(string $name): self |
||
75 | |||
76 | /** |
||
77 | * @param string $name |
||
78 | * |
||
79 | * @return ArtistInfoBuilder |
||
80 | */ |
||
81 | public function forUsername(string $name): self |
||
87 | |||
88 | /** |
||
89 | * @return array |
||
90 | */ |
||
91 | public function getQuery(): array |
||
95 | } |
||
96 |
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.