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 | * Perform a HEAD request |
||
| 15 | * @param string $url |
||
| 16 | * @param array $params |
||
| 17 | * @param callable $resultcb |
||
|
|
|||
| 18 | * @call ( url $url, array $params ) |
||
| 19 | * @call ( url $url, callable $resultcb ) |
||
| 20 | * @callback $resultcb ( Connection $conn, boolean $success ) |
||
| 21 | */ |
||
| 22 | View Code Duplication | public function head($url, $params) |
|
| 47 | |||
| 48 | /** |
||
| 49 | * Parse URL |
||
| 50 | * @param string $mixed Look Pool::buildUrl() |
||
| 51 | * @call ( string $str ) |
||
| 52 | * @call ( array $mixed ) |
||
| 53 | * @return array|bool |
||
| 54 | */ |
||
| 55 | public static function parseUrl($mixed) |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Builds URL from array |
||
| 77 | * @param string $mixed |
||
| 78 | * @call ( string $str ) |
||
| 79 | * @call ( array $mixed ) |
||
| 80 | * @return string|false |
||
| 81 | */ |
||
| 82 | public static function buildUrl($mixed) |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Perform a GET request |
||
| 113 | * @param string $url |
||
| 114 | * @param array $params |
||
| 115 | * @param callable $resultcb |
||
| 116 | * @call ( url $url, array $params ) |
||
| 117 | * @call ( url $url, callable $resultcb ) |
||
| 118 | * @callback $resultcb ( Connection $conn, boolean $success ) |
||
| 119 | */ |
||
| 120 | View Code Duplication | public function get($url, $params) |
|
| 145 | |||
| 146 | /** |
||
| 147 | * Perform a POST request |
||
| 148 | * @param string $url |
||
| 149 | * @param array $data |
||
| 150 | * @param array $params |
||
| 151 | * @param callable $resultcb |
||
| 152 | * @param \Closure $beforeConnect |
||
| 153 | * @call ( url $url, array $data, array $params ) |
||
| 154 | * @call ( url $url, array $data, callable $resultcb ) |
||
| 155 | * @callback $resultcb ( Connection $conn, boolean $success ) |
||
| 156 | */ |
||
| 157 | public function post($url, $data, $params, \Closure $beforeConnect = null) |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Setting default config options |
||
| 189 | * Overriden from NetworkClient::getConfigDefaults |
||
| 190 | * @return array|bool |
||
| 191 | */ |
||
| 192 | protected function getConfigDefaults() |
||
| 205 | } |
||
| 206 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.