Complex classes like KnowledgeBase 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 KnowledgeBase, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | class KnowledgeBase |
||
| 29 | { |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Project path. |
||
| 33 | * |
||
| 34 | * @var string |
||
| 35 | */ |
||
| 36 | protected $projectPath = ''; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Regular expression for removing project path. |
||
| 40 | * |
||
| 41 | * @var string |
||
| 42 | */ |
||
| 43 | protected $projectPathRegExp = ''; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Database. |
||
| 47 | * |
||
| 48 | * @var ExtendedPdoInterface |
||
| 49 | */ |
||
| 50 | protected $db; |
||
|
1 ignored issue
–
show
|
|||
| 51 | |||
| 52 | /** |
||
| 53 | * Config |
||
| 54 | * |
||
| 55 | * @var array |
||
| 56 | */ |
||
| 57 | protected $config = array(); |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Data collectors. |
||
| 61 | * |
||
| 62 | * @var AbstractDataCollector[] |
||
| 63 | */ |
||
| 64 | protected $dataCollectors = array(); |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Console IO. |
||
| 68 | * |
||
| 69 | * @var ConsoleIO |
||
| 70 | */ |
||
| 71 | protected $io; |
||
|
1 ignored issue
–
show
|
|||
| 72 | |||
| 73 | /** |
||
| 74 | * Creates knowledge base instance. |
||
| 75 | * |
||
| 76 | * @param string $project_path Project path. |
||
| 77 | * @param ExtendedPdoInterface $db Database. |
||
| 78 | * @param ConsoleIO $io Console IO. |
||
| 79 | * |
||
| 80 | * @throws \InvalidArgumentException When project path doesn't exist. |
||
| 81 | */ |
||
| 82 | public function __construct($project_path, ExtendedPdoInterface $db, ConsoleIO $io = null) |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Returns database. |
||
| 102 | * |
||
| 103 | * @return ExtendedPdoInterface |
||
| 104 | */ |
||
| 105 | public function getDatabase() |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Returns project configuration. |
||
| 112 | * |
||
| 113 | * @return array |
||
| 114 | * @throws \LogicException When configuration file is not found. |
||
| 115 | * @throws \LogicException When configuration file isn't in JSON format. |
||
| 116 | */ |
||
| 117 | protected function getConfiguration() |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Refreshes database. |
||
| 138 | * |
||
| 139 | * @return void |
||
| 140 | * @throws \LogicException When "$this->io" wasn't set upfront. |
||
| 141 | */ |
||
| 142 | public function refresh() |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Prints statistics about the code. |
||
| 213 | * |
||
| 214 | * @return array |
||
| 215 | */ |
||
| 216 | public function getStatistics() |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Processes file. |
||
| 233 | * |
||
| 234 | * @param string $file File. |
||
| 235 | * |
||
| 236 | * @return integer |
||
| 237 | */ |
||
| 238 | public function processFile($file) |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Determines class locator. |
||
| 301 | * |
||
| 302 | * @return LocatorInterface |
||
| 303 | * @throws \LogicException When class locator from "class_locator" setting doesn't exist. |
||
| 304 | * @throws \LogicException When class locator from "class_locator" setting has non supported type. |
||
| 305 | */ |
||
| 306 | protected function detectClassLocator() |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Processes the Finders configuration list. |
||
| 346 | * |
||
| 347 | * @return Finder[] |
||
| 348 | * @throws \LogicException If "finder" setting doesn't exist. |
||
| 349 | * @throws \LogicException If the configured method does not exist. |
||
| 350 | */ |
||
| 351 | protected function getFinders() |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Resolves path within project. |
||
| 394 | * |
||
| 395 | * @param string $relative_path Relative path. |
||
| 396 | * |
||
| 397 | * @return string |
||
| 398 | */ |
||
| 399 | protected function resolveProjectPath($relative_path) |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Removes project path from file path. |
||
| 406 | * |
||
| 407 | * @param string $absolute_path Absolute path. |
||
| 408 | * |
||
| 409 | * @return string |
||
| 410 | */ |
||
| 411 | protected function removeProjectPath($absolute_path) |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Finds backward compatibility breaks. |
||
| 418 | * |
||
| 419 | * @param ExtendedPdoInterface $source_db Source database. |
||
| 420 | * |
||
| 421 | * @return array |
||
| 422 | */ |
||
| 423 | public function getBackwardsCompatibilityBreaks(ExtendedPdoInterface $source_db) |
||
| 436 | |||
| 437 | } |
||
| 438 |
Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.