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:
Complex classes like Pool often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Pool, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class Pool extends Client |
||
12 | { |
||
13 | /** |
||
14 | * Setting default config options |
||
15 | * Overriden from NetworkClient::getConfigDefaults |
||
16 | * @return array|bool |
||
17 | */ |
||
18 | protected function getConfigDefaults() |
||
31 | |||
32 | /** |
||
33 | * Perform a HEAD request |
||
34 | * @param string $url |
||
35 | * @param array $params |
||
36 | * @param callable $resultcb |
||
37 | * @call ( url $url, array $params ) |
||
38 | * @call ( url $url, callable $resultcb ) |
||
39 | * @callback $resultcb ( Connection $conn, boolean $success ) |
||
40 | */ |
||
41 | View Code Duplication | public function head($url, $params) |
|
66 | |||
67 | |||
68 | /** |
||
69 | * Perform a GET request |
||
70 | * @param string $url |
||
71 | * @param array $params |
||
72 | * @param callable $resultcb |
||
73 | * @call ( url $url, array $params ) |
||
74 | * @call ( url $url, callable $resultcb ) |
||
75 | * @callback $resultcb ( Connection $conn, boolean $success ) |
||
76 | */ |
||
77 | View Code Duplication | public function get($url, $params) |
|
102 | |||
103 | /** |
||
104 | * Perform a POST request |
||
105 | * @param string $url |
||
106 | * @param array $data |
||
107 | * @param array $params |
||
108 | * @param callable $resultcb |
||
109 | * @call ( url $url, array $data, array $params ) |
||
110 | * @call ( url $url, array $data, callable $resultcb ) |
||
111 | * @callback $resultcb ( Connection $conn, boolean $success ) |
||
112 | */ |
||
113 | public function post($url, $data, $params) |
||
142 | |||
143 | /** |
||
144 | * Builds URL from array |
||
145 | * @param string $mixed |
||
146 | * @call ( string $str ) |
||
147 | * @call ( array $mixed ) |
||
148 | * @return string|false |
||
149 | */ |
||
150 | public static function buildUrl($mixed) |
||
178 | |||
179 | /** |
||
180 | * Parse URL |
||
181 | * @param string $mixed Look Pool::buildUrl() |
||
182 | * @call ( string $str ) |
||
183 | * @call ( array $mixed ) |
||
184 | * @return array|bool |
||
185 | */ |
||
186 | public static function parseUrl($mixed) |
||
202 | } |
||
203 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.