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 declare(strict_types = 1); |
||
9 | View Code Duplication | class StorySummary |
|
|
|||
10 | { |
||
11 | /** |
||
12 | * The path to the individual story resource. |
||
13 | * |
||
14 | * @var string |
||
15 | */ |
||
16 | private $resourceURI; |
||
17 | |||
18 | /** |
||
19 | * The canonical name of the story. |
||
20 | * |
||
21 | * @var string |
||
22 | */ |
||
23 | private $name; |
||
24 | |||
25 | /** |
||
26 | * The type of the story (interior or cover). |
||
27 | * |
||
28 | * @var string |
||
29 | */ |
||
30 | private $type; |
||
31 | |||
32 | /** |
||
33 | * @return string |
||
34 | */ |
||
35 | public function getResourceURI() |
||
39 | |||
40 | /** |
||
41 | * @param string $resourceURI |
||
42 | */ |
||
43 | public function setResourceURI(string $resourceURI) |
||
47 | |||
48 | /** |
||
49 | * @return string |
||
50 | */ |
||
51 | public function getName() |
||
55 | |||
56 | /** |
||
57 | * @param string $name |
||
58 | */ |
||
59 | public function setName(string $name) |
||
63 | |||
64 | /** |
||
65 | * @return string |
||
66 | */ |
||
67 | public function getType() |
||
71 | |||
72 | /** |
||
73 | * @param string $type |
||
74 | */ |
||
75 | public function setType(string $type) |
||
79 | } |
||
80 |
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.