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 |
||
27 | class Uptime implements PluginInterface |
||
28 | { |
||
29 | /** |
||
30 | * Seconds in a day |
||
31 | */ |
||
32 | const DAY_IN_SECONDS = 86400; |
||
33 | |||
34 | /** |
||
35 | * @var Node |
||
36 | */ |
||
37 | private $node; |
||
38 | |||
39 | /** |
||
40 | * Cached configuration state |
||
41 | * |
||
42 | * @var Configuration |
||
43 | */ |
||
44 | private $configuration; |
||
45 | |||
46 | /** |
||
47 | * Start time of this instance |
||
48 | * |
||
49 | * @var int |
||
50 | */ |
||
51 | private $startTime = 0; |
||
52 | |||
53 | /** |
||
54 | * Save the time this instance started |
||
55 | */ |
||
56 | 8 | public function __construct() |
|
57 | { |
||
58 | 8 | $this->startTime = time(); |
|
59 | 8 | } |
|
60 | |||
61 | /** |
||
62 | * {@inheritdoc} |
||
63 | */ |
||
64 | 8 | public function setNode(Node $node) |
|
65 | { |
||
66 | 8 | $this->node = $node; |
|
67 | 8 | } |
|
68 | |||
69 | /** |
||
70 | * {@inheritdoc} |
||
71 | */ |
||
72 | 1 | public function getSlug(): string |
|
73 | { |
||
74 | 1 | return 'uptime'; |
|
75 | } |
||
76 | |||
77 | /** |
||
78 | * {@inheritdoc} |
||
79 | */ |
||
80 | 1 | public function getCategorySlug(): string |
|
84 | |||
85 | /** |
||
86 | * {@inheritdoc} |
||
87 | */ |
||
88 | 2 | View Code Duplication | public function getConfiguration(): PromiseInterface |
104 | |||
105 | /** |
||
106 | * {@inheritdoc} |
||
107 | */ |
||
108 | 2 | public function getValues(): PromiseInterface |
|
114 | |||
115 | /** |
||
116 | * @return Metric |
||
117 | */ |
||
118 | 2 | private function getUptimeValue(): Metric |
|
126 | |||
127 | /** |
||
128 | * {@inheritdoc} |
||
129 | */ |
||
130 | 1 | public function getCapabilities(): array |
|
134 | } |
||
135 |
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.