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 | public function __construct($project_path, ExtendedPdoInterface $db, ConsoleIO $io = null) |
||
101 | |||
102 | /** |
||
103 | * Returns database. |
||
104 | * |
||
105 | * @return ExtendedPdoInterface |
||
106 | */ |
||
107 | 8 | public function getDatabase() |
|
108 | { |
||
109 | 8 | return $this->db; |
|
110 | } |
||
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 | * Refreshes database silently. |
||
214 | * |
||
215 | * @return void |
||
216 | */ |
||
217 | 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 | 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 | 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 | protected function getFinders() |
||
436 | |||
437 | /** |
||
438 | * Resolves path within project. |
||
439 | * |
||
440 | * @param string $relative_path Relative path. |
||
441 | * |
||
442 | * @return string |
||
443 | */ |
||
444 | 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 | 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 backwards compatibility ignore rules. |
||
482 | * |
||
483 | * @return array |
||
484 | */ |
||
485 | public function getBackwardsCompatibilityIgnoreRules() |
||
486 | { |
||
487 | return $this->getConfigSetting('bc_ignore', array()); |
||
488 | } |
||
489 | |||
490 | /** |
||
491 | * Returns value of configuration setting. |
||
492 | * |
||
493 | * @param string $name Name. |
||
494 | * @param mixed|null $default Default value. |
||
495 | * |
||
496 | * @return mixed |
||
497 | */ |
||
498 | protected function getConfigSetting($name, $default = null) |
||
502 | |||
503 | } |
||
504 |