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 |
||
18 | class SimplifyBearing implements SimplifyInterface |
||
19 | { |
||
20 | /** |
||
21 | * @var float |
||
22 | */ |
||
23 | private $bearingAngle; |
||
24 | |||
25 | /** |
||
26 | * SimplifyBearing constructor. |
||
27 | * |
||
28 | * @param float $bearingAngle |
||
29 | */ |
||
30 | public function __construct(float $bearingAngle) |
||
34 | |||
35 | /** |
||
36 | * @param Polyline $polyline |
||
37 | * |
||
38 | * @return Polyline |
||
39 | * @throws RuntimeException |
||
40 | */ |
||
41 | View Code Duplication | public function simplify(Polyline $polyline): Polyline |
|
51 | |||
52 | /** |
||
53 | * Simplifies the given polyline |
||
54 | * |
||
55 | * 1. calculate the bearing angle between the first two points p1 and p2: b1 |
||
56 | * 2. calculate the bearing angle between the next two points p2 and p3: b2 |
||
57 | * 3. calculate the difference between b1 and b2: deltaB; if deltaB is |
||
58 | * smaller than the threshold angle, remove the middle point p2 |
||
59 | * 4. start again at (1.) as long as the polyline contains more points |
||
60 | * |
||
61 | * This method will be merged with `simplify()` in the next major release. |
||
62 | * |
||
63 | * @param GeometryInterface $geometry |
||
64 | * |
||
65 | * @return GeometryInterface |
||
66 | */ |
||
67 | public function simplifyGeometry(GeometryInterface $geometry): GeometryInterface |
||
120 | } |
||
121 |
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.