Complex classes like CoverFishHelper 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 CoverFishHelper, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class CoverFishHelper |
||
| 17 | { |
||
| 18 | const PHPUNIT_ID_INTERFACE = 'PHPUnit_Framework_Test', |
||
| 19 | PHPUNIT_ID_CLASS = 'PHPUnit_Framework_TestCase'; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @param string $file |
||
| 23 | * |
||
| 24 | * @return bool |
||
| 25 | */ |
||
| 26 | public function checkFileExist($file) |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @param $param |
||
| 33 | * |
||
| 34 | * @return bool |
||
| 35 | */ |
||
| 36 | public function checkParamNotEmpty($param) |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @param string $namespace |
||
| 43 | * |
||
| 44 | * @return string |
||
| 45 | */ |
||
| 46 | public function getClassNameFromClassFQN($namespace) |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @param string $fileNameAndPath |
||
| 53 | * |
||
| 54 | * @return string |
||
| 55 | */ |
||
| 56 | public function getFileNameFromPath($fileNameAndPath) |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @param string $namespace |
||
| 63 | * |
||
| 64 | * @return string |
||
| 65 | */ |
||
| 66 | public function getPathFromFileNameAndPath($namespace) |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @param string $fqn |
||
| 73 | * @param string $delimiter |
||
| 74 | * |
||
| 75 | * @return string |
||
| 76 | */ |
||
| 77 | public function getLastItemInFQNBlock($fqn, $delimiter) |
||
| 85 | |||
| 86 | /** |
||
| 87 | * check for className in use statements, return className on missing use statement |
||
| 88 | * |
||
| 89 | * @param string $coverClassName |
||
| 90 | * @param array|null $usedClasses |
||
| 91 | * |
||
| 92 | * @return string |
||
| 93 | */ |
||
| 94 | public function getClassFromUse($coverClassName, $usedClasses) |
||
| 109 | |||
| 110 | /** |
||
| 111 | * return all in file use statement defined classes |
||
| 112 | * |
||
| 113 | * @param string $classFile absolute path of readable class file |
||
| 114 | * |
||
| 115 | * @return array |
||
| 116 | */ |
||
| 117 | public function getUsedClassesInClass($classFile) |
||
| 128 | |||
| 129 | /** |
||
| 130 | * return loc of given test method |
||
| 131 | * |
||
| 132 | * @param array $methodData |
||
| 133 | * |
||
| 134 | * @return int |
||
| 135 | */ |
||
| 136 | public function getLocOfTestMethod(array $methodData) |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @param string $path |
||
| 147 | * |
||
| 148 | * @return string|false |
||
| 149 | */ |
||
| 150 | public function checkPath($path) |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @param string $fileOrPath |
||
| 159 | * |
||
| 160 | * @return bool |
||
| 161 | */ |
||
| 162 | public function checkFileOrPath($fileOrPath) |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @param string $file absolute path of readable class file |
||
| 173 | * |
||
| 174 | * @return null|string |
||
| 175 | */ |
||
| 176 | public function getFileContent($file) |
||
| 184 | |||
| 185 | /** |
||
| 186 | * in case of (wrong) multiple annotation, get the last defined coversDefaultClass from class docBlock |
||
| 187 | * |
||
| 188 | * @param array $coversDefaultClass |
||
| 189 | * |
||
| 190 | * @return string |
||
| 191 | */ |
||
| 192 | public function getCoversDefaultClassUsable(array $coversDefaultClass) |
||
| 200 | |||
| 201 | /** |
||
| 202 | * fetch annotation key value(s) as array from corresponding class docBlock directly |
||
| 203 | * |
||
| 204 | * @param string $docBlock |
||
| 205 | * @param string $key |
||
| 206 | * |
||
| 207 | * @return array |
||
| 208 | */ |
||
| 209 | public function getAnnotationByKey($docBlock, $key) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @param string $key |
||
| 222 | * @param array $classData |
||
| 223 | * |
||
| 224 | * @return string |
||
| 225 | */ |
||
| 226 | public function getAttributeByKey($key, array $classData) |
||
| 234 | |||
| 235 | /** |
||
| 236 | * @param string $inputPath |
||
| 237 | * |
||
| 238 | * @return string |
||
| 239 | */ |
||
| 240 | public function getRegexPath($inputPath) |
||
| 244 | |||
| 245 | /** |
||
| 246 | * @param string $docBlock |
||
| 247 | * |
||
| 248 | * @return array |
||
| 249 | */ |
||
| 250 | public function parseCoverAnnotationDocBlock($docBlock) |
||
| 270 | |||
| 271 | public function isValidTestMethod($methodSignature) |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Check if a class extends or implements a specific class/interface |
||
| 285 | * |
||
| 286 | * @param string $classFQN The class name of the object to compare to |
||
| 287 | * |
||
| 288 | * @return bool |
||
| 289 | */ |
||
| 290 | public function isValidTestClass($classFQN) |
||
| 314 | |||
| 315 | /** |
||
| 316 | * check if class got fully qualified name |
||
| 317 | * |
||
| 318 | * @param string $class |
||
| 319 | * |
||
| 320 | * @return bool |
||
| 321 | */ |
||
| 322 | public function checkClassHasFQN($class) |
||
| 328 | |||
| 329 | /** |
||
| 330 | * @param $string |
||
| 331 | * |
||
| 332 | * @return bool |
||
| 333 | */ |
||
| 334 | public function stringToBool($string) |
||
| 338 | } |