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 LogoType implements CreatableFromArray |
||
11 | { |
||
12 | /** |
||
13 | * @var string |
||
14 | */ |
||
15 | private $content; |
||
16 | |||
17 | /** |
||
18 | * @var string |
||
19 | */ |
||
20 | private $fileType; |
||
21 | |||
22 | /** |
||
23 | * @return string |
||
24 | */ |
||
25 | public function getContent(): string |
||
29 | |||
30 | /** |
||
31 | * @param string $content |
||
32 | * |
||
33 | * @return LogoType |
||
34 | */ |
||
35 | 1 | public function withContent(string $content) |
|
42 | |||
43 | /** |
||
44 | * @return string |
||
45 | */ |
||
46 | public function getFileType(): string |
||
50 | |||
51 | /** |
||
52 | * @param string $fileType |
||
53 | * |
||
54 | * @return LogoType |
||
55 | */ |
||
56 | 1 | public function withFileType(string $fileType) |
|
63 | |||
64 | /** |
||
65 | * Create an API response object from the HTTP response from the API server. |
||
66 | * |
||
67 | * @param array $data |
||
68 | * |
||
69 | * @return self |
||
70 | * |
||
71 | * @throws ValidationException |
||
72 | */ |
||
73 | 2 | public static function createFromArray(array $data) |
|
85 | |||
86 | /** |
||
87 | * @return array |
||
88 | */ |
||
89 | 1 | public function toArray() |
|
101 | } |
||
102 |
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.