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 |
||
29 | View Code Duplication | class RaycasterComponent extends ComponentAbstract implements RaycasterCMPTIF |
|
|
|||
30 | { |
||
31 | |||
32 | /** |
||
33 | * Initialize Component |
||
34 | * |
||
35 | * {@inheritdoc} |
||
36 | * |
||
37 | * @return bool |
||
38 | */ |
||
39 | 3 | public function initializeComponent(): bool |
|
44 | |||
45 | /** |
||
46 | * Maximum distance |
||
47 | * |
||
48 | * Maximum distance under which resulting entities are returned. Cannot be lower then near. |
||
49 | * |
||
50 | * @param string $distance |
||
51 | * @return RaycasterCMPTIF |
||
52 | */ |
||
53 | 1 | public function far(string $distance = 'Infinity'): RaycasterCMPTIF |
|
58 | |||
59 | /** |
||
60 | * Number of milliseconds |
||
61 | * |
||
62 | * Number of milliseconds to wait in between each intersection test. Lower number is better for faster updates. |
||
63 | * Higher number is better for performance. |
||
64 | * |
||
65 | * @param int $ms |
||
66 | * @return RaycasterCMPTIF |
||
67 | */ |
||
68 | 2 | public function interval(int $ms = 100): RaycasterCMPTIF |
|
73 | |||
74 | /** |
||
75 | * Minimum distance |
||
76 | * |
||
77 | * Minimum distance over which resuilting entities are returned. Cannot be lower than 0. |
||
78 | * |
||
79 | * @param int $distance |
||
80 | * @return RaycasterCMPTIF |
||
81 | */ |
||
82 | 1 | public function near(int $distance = 0): RaycasterCMPTIF |
|
87 | |||
88 | /** |
||
89 | * Query selector |
||
90 | * |
||
91 | * Query selector to pick which objects to test for intersection. If not specified, all entities will be tested. |
||
92 | * |
||
93 | * @param null|string $selector |
||
94 | * @return RaycasterCMPTIF |
||
95 | */ |
||
96 | 1 | public function objects(string $selector = null): RaycasterCMPTIF |
|
101 | |||
102 | /** |
||
103 | * Recursive |
||
104 | * |
||
105 | * Checks all children of objects if set. Else only checks intersections with root objects. |
||
106 | * |
||
107 | * @param bool $r |
||
108 | * @return RaycasterCMPTIF |
||
109 | */ |
||
110 | 1 | public function recursive(bool $r = true): RaycasterCMPTIF |
|
115 | } |
||
116 |
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.