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 |
||
8 | class Pool extends Server |
||
9 | { |
||
10 | use \PHPDaemon\Traits\EventHandlers; |
||
11 | |||
12 | /** |
||
13 | * @var array |
||
14 | */ |
||
15 | public $routes = []; |
||
16 | |||
17 | /** |
||
18 | * Binary packet type |
||
19 | */ |
||
20 | const BINARY = 'BINARY'; |
||
21 | |||
22 | /** |
||
23 | * String packet type |
||
24 | */ |
||
25 | const STRING = 'STRING'; |
||
26 | |||
27 | /** |
||
28 | * Setting default config options |
||
29 | * Overriden from ConnectionPool::getConfigDefaults |
||
30 | * @return array|bool |
||
31 | */ |
||
32 | protected function getConfigDefaults() |
||
51 | |||
52 | /** |
||
53 | * Sets an array of options associated to the route |
||
54 | * @param string $path Route name. |
||
55 | * @param array $opts Options |
||
56 | * @return boolean Success. |
||
57 | */ |
||
58 | View Code Duplication | public function setRouteOptions($path, $opts) |
|
68 | |||
69 | |||
70 | /** |
||
71 | * Return options by route |
||
72 | * @param string $path Route name |
||
73 | * @return array Options |
||
74 | */ |
||
75 | public function getRouteOptions($path) |
||
83 | |||
84 | /** |
||
85 | * Adds a route if it doesn't exist already. |
||
86 | * @param string $path Route name. |
||
87 | * @param callable $cb Route's callback. |
||
88 | * @callback $cb ( ) |
||
89 | * @return boolean Success. |
||
90 | */ |
||
91 | View Code Duplication | public function addRoute($path, $cb) |
|
101 | |||
102 | /** |
||
103 | * @TODO |
||
104 | * @param string $path |
||
105 | * @param object $client |
||
106 | * @param boolean $withoutCustomTransport |
||
107 | * @return mixed |
||
108 | */ |
||
109 | public function getRoute($path, $client, $withoutCustomTransport = false) |
||
149 | |||
150 | /** |
||
151 | * Force add/replace a route. |
||
152 | * @param string $path Path |
||
153 | * @param callable $cb Callback |
||
154 | * @callback $cb ( ) |
||
155 | * @return boolean Success |
||
156 | */ |
||
157 | public function setRoute($path, $cb) |
||
163 | |||
164 | /** |
||
165 | * Removes a route. |
||
166 | * @param string $path Route name |
||
167 | * @return boolean Success |
||
168 | */ |
||
169 | public function removeRoute($path) |
||
178 | |||
179 | /** |
||
180 | * Checks if route exists |
||
181 | * @param string $path Route name |
||
182 | * @return boolean Exists? |
||
183 | */ |
||
184 | public function routeExists($path) |
||
189 | } |
||
190 |
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.