Complex classes like StartController 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 StartController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class StartController extends CommonController |
||
23 | { |
||
24 | const MAIN_CONFIG = '.hidev/config.yml'; |
||
25 | |||
26 | /** |
||
27 | * @var string absolute path to the project root directory |
||
28 | */ |
||
29 | protected $_rootDir; |
||
30 | |||
31 | /** |
||
32 | * @var bool hidev already started flag |
||
33 | */ |
||
34 | public static $started = false; |
||
35 | |||
36 | /** |
||
37 | * Make action. |
||
38 | */ |
||
39 | 3 | public function actionMake() |
|
51 | |||
52 | 3 | public function addAutoloader() |
|
61 | |||
62 | /** |
||
63 | * Update action. |
||
64 | * @return int exit code |
||
65 | */ |
||
66 | public function actionUpdate() |
||
70 | |||
71 | /** |
||
72 | * Adds aliases: |
||
73 | * - @root alias to current project root dir |
||
74 | * - current package namespace for it could be used from hidev. |
||
75 | */ |
||
76 | 3 | public function addAliases() |
|
94 | |||
95 | public function hasAlias($alias, $exact = true) |
||
101 | |||
102 | /** |
||
103 | * Require all configured requires. |
||
104 | */ |
||
105 | 3 | protected function requireAll() |
|
129 | |||
130 | 3 | public function needsComposerInstall() |
|
152 | |||
153 | /** |
||
154 | * Include all configs. |
||
155 | */ |
||
156 | 3 | public function includeAll() |
|
169 | |||
170 | /** |
||
171 | * Load project's config if configured. |
||
172 | */ |
||
173 | 3 | public function loadConfig() |
|
180 | |||
181 | public function setRootDir($value) |
||
185 | |||
186 | 3 | public function getRootDir() |
|
194 | |||
195 | /** |
||
196 | * Chdirs to project's root by looking for config file in the current directory and up. |
||
197 | * @throws InvalidParamException when failed to find |
||
198 | * @return string path to the root directory of hidev project |
||
199 | */ |
||
200 | 3 | protected function findRootDir() |
|
211 | |||
212 | public function buildRootPath($subpath) |
||
216 | } |
||
217 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.