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 |
||
13 | abstract class AbstractTask implements TaskInterface |
||
14 | { |
||
15 | const STATUS_ABORTED = 2; |
||
16 | const STATUS_FINISHED = 1; |
||
17 | const STATUS_NOT_STARTED = 0; |
||
18 | const STATUS_RUNNING = 3; |
||
19 | |||
20 | /** |
||
21 | * @var int |
||
22 | */ |
||
23 | private $parentProcessId; |
||
24 | |||
25 | /** |
||
26 | * @var int |
||
27 | */ |
||
28 | private $startTime = 0; |
||
29 | |||
30 | /** |
||
31 | * @var int |
||
32 | */ |
||
33 | private $status = self::STATUS_NOT_STARTED; |
||
34 | |||
35 | /** |
||
36 | * @return int |
||
37 | */ |
||
38 | public function getRunTime() |
||
42 | |||
43 | /** |
||
44 | * @return int |
||
45 | */ |
||
46 | public function getMemoryUsage() |
||
50 | |||
51 | /** |
||
52 | * @return int |
||
53 | */ |
||
54 | public function getParentProcessId() |
||
58 | |||
59 | /** |
||
60 | * @return int |
||
61 | */ |
||
62 | public function getProcessId() |
||
66 | |||
67 | /** |
||
68 | * @return bool |
||
69 | */ |
||
70 | public function isAborted() |
||
74 | |||
75 | /** |
||
76 | * @return bool |
||
77 | */ |
||
78 | public function isFinished() |
||
82 | |||
83 | /** |
||
84 | * @return bool |
||
85 | */ |
||
86 | public function isNotStarted() |
||
90 | |||
91 | /** |
||
92 | * @return bool |
||
93 | */ |
||
94 | public function isRunning() |
||
98 | |||
99 | public function markAsAborted() |
||
103 | |||
104 | public function markAsFinished() |
||
108 | |||
109 | public function markAsRunning() |
||
113 | |||
114 | /** |
||
115 | * @param int $parentProcessId |
||
116 | */ |
||
117 | public function setParentProcessId($parentProcessId) |
||
121 | |||
122 | /** |
||
123 | * @param int $timestamp |
||
124 | */ |
||
125 | public function setStartTime($timestamp) |
||
129 | |||
130 | protected function dispatchSignal() |
||
134 | |||
135 | /** |
||
136 | * @param $nameOfSignalHandlerMethod |
||
137 | * @throws InvalidArgumentException |
||
138 | * @codeCoverageIgnore |
||
139 | */ |
||
140 | protected function setUpSignalHandling($nameOfSignalHandlerMethod) |
||
166 | } |
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.