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); |
||
8 | final class HighPrecisionScheduler implements SchedulerInterface |
||
9 | { |
||
10 | /** @var LoopInterface */ |
||
11 | private $loop; |
||
12 | |||
13 | /** @var bool */ |
||
14 | private $useHighResolution = false; |
||
15 | |||
16 | /** @var callable[] */ |
||
17 | private $ticks = []; |
||
18 | |||
19 | /** @var TimerInterface */ |
||
20 | private $timer; |
||
21 | |||
22 | /** |
||
23 | * @param LoopInterface $loop |
||
24 | */ |
||
25 | 2 | public function __construct(LoopInterface $loop) |
|
32 | |||
33 | 2 | public function schedule(callable $tick): void |
|
41 | |||
42 | 2 | private function time(): float |
|
46 | |||
47 | 2 | private function hasDrifted(float $time): bool |
|
51 | |||
52 | 2 | private function tick(): void |
|
64 | |||
65 | 2 | private function align(): void |
|
103 | } |
||
104 |
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.