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 |
||
| 30 | class KnowledgeBase |
||
| 31 | { |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Project path. |
||
| 35 | * |
||
| 36 | * @var string |
||
| 37 | */ |
||
| 38 | protected $projectPath = ''; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Regular expression for removing project path. |
||
| 42 | * |
||
| 43 | * @var string |
||
| 44 | */ |
||
| 45 | protected $projectPathRegExp = ''; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Database. |
||
| 49 | * |
||
| 50 | * @var ExtendedPdoInterface |
||
| 51 | */ |
||
| 52 | protected $db; |
||
|
1 ignored issue
–
show
|
|||
| 53 | |||
| 54 | /** |
||
| 55 | * Config |
||
| 56 | * |
||
| 57 | * @var array |
||
| 58 | */ |
||
| 59 | protected $config = array(); |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Data collectors. |
||
| 63 | * |
||
| 64 | * @var AbstractDataCollector[] |
||
| 65 | */ |
||
| 66 | protected $dataCollectors = array(); |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Console IO. |
||
| 70 | * |
||
| 71 | * @var ConsoleIO |
||
| 72 | */ |
||
| 73 | protected $io; |
||
|
1 ignored issue
–
show
|
|||
| 74 | |||
| 75 | /** |
||
| 76 | * Creates knowledge base instance. |
||
| 77 | * |
||
| 78 | * @param string $project_path Project path. |
||
| 79 | * @param ExtendedPdoInterface $db Database. |
||
| 80 | * @param ConsoleIO $io Console IO. |
||
| 81 | * |
||
| 82 | * @throws \InvalidArgumentException When project path doesn't exist. |
||
| 83 | */ |
||
| 84 | public function __construct($project_path, ExtendedPdoInterface $db, ConsoleIO $io = null) |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Returns database. |
||
| 104 | * |
||
| 105 | * @return ExtendedPdoInterface |
||
| 106 | */ |
||
| 107 | public function getDatabase() |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Returns project configuration. |
||
| 114 | * |
||
| 115 | * @return array |
||
| 116 | * @throws \LogicException When configuration file is not found. |
||
| 117 | * @throws \LogicException When configuration file isn't in JSON format. |
||
| 118 | */ |
||
| 119 | protected function getConfiguration() |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Refreshes database. |
||
| 140 | * |
||
| 141 | * @return void |
||
| 142 | * @throws \LogicException When "$this->io" wasn't set upfront. |
||
| 143 | */ |
||
| 144 | public function refresh() |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Prints statistics about the code. |
||
| 214 | * |
||
| 215 | * @return array |
||
| 216 | */ |
||
| 217 | public function getStatistics() |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Processes file. |
||
| 234 | * |
||
| 235 | * @param string $file File. |
||
| 236 | * |
||
| 237 | * @return integer |
||
| 238 | */ |
||
| 239 | public function processFile($file) |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Determines class locator. |
||
| 302 | * |
||
| 303 | * @return LocatorInterface |
||
| 304 | * @throws \LogicException When class locator from "class_locator" setting doesn't exist. |
||
| 305 | * @throws \LogicException When class locator from "class_locator" setting has non supported type. |
||
| 306 | */ |
||
| 307 | protected function detectClassLocator() |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Processes the Finders configuration list. |
||
| 348 | * |
||
| 349 | * @return Finder[] |
||
| 350 | * @throws \LogicException If "finder" setting doesn't exist. |
||
| 351 | * @throws \LogicException If the configured method does not exist. |
||
| 352 | */ |
||
| 353 | protected function getFinders() |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Resolves path within project. |
||
| 398 | * |
||
| 399 | * @param string $relative_path Relative path. |
||
| 400 | * |
||
| 401 | * @return string |
||
| 402 | */ |
||
| 403 | protected function resolveProjectPath($relative_path) |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Removes project path from file path. |
||
| 410 | * |
||
| 411 | * @param string $absolute_path Absolute path. |
||
| 412 | * |
||
| 413 | * @return string |
||
| 414 | */ |
||
| 415 | protected function removeProjectPath($absolute_path) |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Returns backwards compatibility checkers. |
||
| 422 | * |
||
| 423 | * @param CheckerFactory $factory Factory. |
||
| 424 | * |
||
| 425 | * @return AbstractChecker[] |
||
| 426 | */ |
||
| 427 | public function getBackwardsCompatibilityCheckers(CheckerFactory $factory) |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Returns value of configuration setting. |
||
| 441 | * |
||
| 442 | * @param string $name Name. |
||
| 443 | * @param mixed|null $default Default value. |
||
| 444 | * |
||
| 445 | * @return mixed |
||
| 446 | */ |
||
| 447 | protected function getConfigSetting($name, $default = null) |
||
| 451 | |||
| 452 | } |
||
| 453 |
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.