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 |
||
11 | trait KeyboardTrait { |
||
12 | |||
13 | /** |
||
14 | * Does some normalization for the char we want to do keyboard events with. |
||
15 | * @param $char |
||
16 | * @throws DriverException |
||
17 | * @return string |
||
18 | */ |
||
19 | protected function normalizeCharForKeyEvent($char) { |
||
34 | |||
35 | /** |
||
36 | * Does some control and normalization for the key event modifier |
||
37 | * @param $modifier |
||
38 | * @return string |
||
39 | * @throws DriverException |
||
40 | */ |
||
41 | protected function keyEventModifierControl($modifier) { |
||
51 | |||
52 | /** |
||
53 | * Send a key-down event to the browser element |
||
54 | * @param $xpath |
||
55 | * @param $char |
||
56 | * @param string $modifier |
||
57 | * @throws DriverException |
||
58 | */ |
||
59 | View Code Duplication | public function keyDown($xpath, $char, $modifier = null) { |
|
65 | |||
66 | /** |
||
67 | * @param string $xpath |
||
68 | * @param string $char |
||
69 | * @param string $modifier |
||
70 | * @throws DriverException |
||
71 | */ |
||
72 | View Code Duplication | public function keyPress($xpath, $char, $modifier = null) { |
|
78 | |||
79 | /** |
||
80 | * Pressed up specific keyboard key. |
||
81 | * |
||
82 | * @param string $xpath |
||
83 | * @param string|integer $char could be either char ('b') or char-code (98) |
||
84 | * @param string $modifier keyboard modifier (could be 'ctrl', 'alt', 'shift' or 'meta') |
||
85 | * |
||
86 | * @throws DriverException When the operation cannot be done |
||
87 | */ |
||
88 | View Code Duplication | public function keyUp($xpath, $char, $modifier = null) { |
|
95 | } |
||
96 |
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.