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; |
||
| 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; |
||
| 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 | 1 | public function __construct($project_path, ExtendedPdoInterface $db, ConsoleIO $io = null) |
|
| 85 | { |
||
| 86 | 1 | if ( !file_exists($project_path) || !is_dir($project_path) ) { |
|
| 87 | throw new \InvalidArgumentException('The project path doesn\'t exist.'); |
||
| 88 | } |
||
| 89 | |||
| 90 | 1 | $this->projectPath = $project_path; |
|
| 91 | 1 | $this->projectPathRegExp = '#^' . preg_quote($project_path, '#') . '/#'; |
|
| 92 | |||
| 93 | 1 | $this->db = $db; |
|
| 94 | 1 | $this->config = $this->getConfiguration(); |
|
| 95 | 1 | $this->io = $io; |
|
| 96 | |||
| 97 | 1 | $this->dataCollectors[] = new ClassDataCollector($db); |
|
| 98 | 1 | $this->dataCollectors[] = new ConstantDataCollector($db); |
|
| 99 | 1 | $this->dataCollectors[] = new FunctionDataCollector($db); |
|
| 100 | 1 | } |
|
| 101 | |||
| 102 | /** |
||
| 103 | * Returns database. |
||
| 104 | * |
||
| 105 | * @return ExtendedPdoInterface |
||
| 106 | */ |
||
| 107 | 8 | 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 | 1 | 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 | * Refreshes database silently. |
||
| 214 | * |
||
| 215 | * @return void |
||
| 216 | */ |
||
| 217 | 1 | public function silentRefresh() |
|
| 251 | |||
| 252 | /** |
||
| 253 | * Prints statistics about the code. |
||
| 254 | * |
||
| 255 | * @return array |
||
| 256 | */ |
||
| 257 | public function getStatistics() |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Processes file. |
||
| 274 | * |
||
| 275 | * @param string $file File. |
||
| 276 | * |
||
| 277 | * @return integer |
||
| 278 | */ |
||
| 279 | 1 | public function processFile($file) |
|
| 340 | |||
| 341 | /** |
||
| 342 | * Determines class locator. |
||
| 343 | * |
||
| 344 | * @return LocatorInterface |
||
| 345 | * @throws \LogicException When class locator from "class_locator" setting doesn't exist. |
||
| 346 | * @throws \LogicException When class locator from "class_locator" setting has non supported type. |
||
| 347 | */ |
||
| 348 | 1 | protected function detectClassLocator() |
|
| 386 | |||
| 387 | /** |
||
| 388 | * Processes the Finders configuration list. |
||
| 389 | * |
||
| 390 | * @return Finder[] |
||
| 391 | * @throws \LogicException If "finder" setting doesn't exist. |
||
| 392 | * @throws \LogicException If the configured method does not exist. |
||
| 393 | */ |
||
| 394 | 1 | protected function getFinders() |
|
| 436 | |||
| 437 | /** |
||
| 438 | * Resolves path within project. |
||
| 439 | * |
||
| 440 | * @param string $relative_path Relative path. |
||
| 441 | * |
||
| 442 | * @return string |
||
| 443 | */ |
||
| 444 | 1 | protected function resolveProjectPath($relative_path) |
|
| 448 | |||
| 449 | /** |
||
| 450 | * Removes project path from file path. |
||
| 451 | * |
||
| 452 | * @param string $absolute_path Absolute path. |
||
| 453 | * |
||
| 454 | * @return string |
||
| 455 | */ |
||
| 456 | 1 | protected function removeProjectPath($absolute_path) |
|
| 460 | |||
| 461 | /** |
||
| 462 | * Returns backwards compatibility checkers. |
||
| 463 | * |
||
| 464 | * @param CheckerFactory $factory Factory. |
||
| 465 | * |
||
| 466 | * @return AbstractChecker[] |
||
| 467 | */ |
||
| 468 | public function getBackwardsCompatibilityCheckers(CheckerFactory $factory) |
||
| 479 | |||
| 480 | /** |
||
| 481 | * Returns value of configuration setting. |
||
| 482 | * |
||
| 483 | * @param string $name Name. |
||
| 484 | * @param mixed|null $default Default value. |
||
| 485 | * |
||
| 486 | * @return mixed |
||
| 487 | */ |
||
| 488 | 1 | protected function getConfigSetting($name, $default = null) |
|
| 492 | |||
| 493 | } |
||
| 494 |