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 |
||
9 | class IntervalManager |
||
10 | { |
||
11 | private $sizeForSeeking; |
||
12 | private $sizeForJobDistribution; |
||
13 | |||
14 | 4 | public function __construct(int $sizeForSeeking, int $sizeForJobDistribution) |
|
19 | |||
20 | /** |
||
21 | * Divides interval into parts for job distribution. |
||
22 | * Always includes first and last part without from/until restrictions to handle new added IDs |
||
23 | * |
||
24 | * @param Interval $idRange |
||
25 | * @return Generator|Interval[] |
||
26 | */ |
||
27 | 4 | public function divideForJobDistribution(Interval $idRange): Generator |
|
47 | |||
48 | /** |
||
49 | * Divides interval into smaller ones which could be used for in-memory seeking. |
||
50 | * If interval is already small enough, null is returned |
||
51 | * |
||
52 | * @param Interval $interval |
||
53 | * @return Interval[]|Generator |
||
54 | */ |
||
55 | 3 | public function divideForSeeking(Interval $interval): Generator |
|
69 | } |
||
70 |
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.