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 |
||
23 | class Table |
||
24 | { |
||
25 | use TagsTrait; |
||
26 | |||
27 | const SPINNER = "⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏"; |
||
28 | |||
29 | /** @var Pool */ |
||
30 | private $processPool; |
||
31 | /** @var string[] */ |
||
32 | private $rows = []; |
||
33 | /** @var Exception[] */ |
||
34 | private $exceptions; |
||
35 | /** @var DiffConsoleOutput */ |
||
36 | private $output; |
||
37 | /** @var TerminalInterface */ |
||
38 | private $terminal; |
||
39 | /** @var bool */ |
||
40 | private $showOutput = true; |
||
41 | /** @var bool */ |
||
42 | private $showSummary = true; |
||
43 | |||
44 | /** |
||
45 | * Table constructor. |
||
46 | * |
||
47 | * @param OutputInterface $output |
||
48 | * @param Pool|null $pool |
||
49 | */ |
||
50 | 15 | View Code Duplication | public function __construct(OutputInterface $output, Pool $pool = null) |
62 | |||
63 | /** |
||
64 | * @param array $data |
||
65 | * @param string $status |
||
66 | * @param float $duration |
||
67 | * @param string $extra |
||
68 | * |
||
69 | * @return string |
||
70 | */ |
||
71 | 12 | private function formatRow(array $data, $status, $duration, $extra = '') |
|
77 | |||
78 | /** |
||
79 | * @param Process $process |
||
80 | * @param array $data |
||
81 | */ |
||
82 | 12 | public function add(Process $process, array $data = []) |
|
123 | |||
124 | /** |
||
125 | * @return string |
||
126 | */ |
||
127 | 2 | private function getSummary() |
|
144 | |||
145 | /** |
||
146 | * Render a specific row |
||
147 | * |
||
148 | * @param int $row |
||
149 | */ |
||
150 | 12 | private function render($row = 0) |
|
159 | |||
160 | /** |
||
161 | * @param float $checkInterval |
||
162 | * |
||
163 | * @return bool true if all processes were successful |
||
164 | * @throws Exception |
||
165 | */ |
||
166 | 12 | public function run($checkInterval = Pool::CHECK_INTERVAL) |
|
186 | |||
187 | /** |
||
188 | * @return bool |
||
189 | */ |
||
190 | 1 | public function isShowOutput() |
|
194 | |||
195 | /** |
||
196 | * @param bool $showOutput |
||
197 | * |
||
198 | * @return $this |
||
199 | */ |
||
200 | 11 | public function setShowOutput($showOutput) |
|
205 | |||
206 | /** |
||
207 | * @return bool |
||
208 | */ |
||
209 | 1 | public function isShowSummary() |
|
213 | |||
214 | /** |
||
215 | * @param bool $showSummary |
||
216 | * |
||
217 | * @return $this |
||
218 | */ |
||
219 | 13 | public function setShowSummary($showSummary) |
|
224 | } |
||
225 |
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.