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 |
||
| 23 | class StartController extends CommonController |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * @var string absolute path to the project root directory |
||
| 27 | */ |
||
| 28 | protected $_rootDir; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var bool hidev already started flag |
||
| 32 | */ |
||
| 33 | public static $started = false; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Make action. |
||
| 37 | */ |
||
| 38 | 2 | public function actionMake() |
|
| 53 | |||
| 54 | 2 | public function includeMainConfig() |
|
| 58 | |||
| 59 | 2 | public function addAutoloader() |
|
| 68 | |||
| 69 | /** |
||
| 70 | * Update action. |
||
| 71 | * @return int exit code |
||
| 72 | */ |
||
| 73 | public function actionUpdate() |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Adds aliases: |
||
| 80 | * - @root alias to current project root dir |
||
| 81 | * - current package namespace for it could be used from hidev. |
||
| 82 | */ |
||
| 83 | 2 | public function addAliases() |
|
| 101 | |||
| 102 | 2 | public function hasAlias($alias, $exact = true) |
|
| 108 | |||
| 109 | /** |
||
| 110 | * Require all configured requires. |
||
| 111 | */ |
||
| 112 | 2 | protected function requireAll() |
|
| 137 | |||
| 138 | 2 | public function needsComposerInstall() |
|
| 160 | |||
| 161 | /** |
||
| 162 | * Include all configs. |
||
| 163 | */ |
||
| 164 | 2 | public function includeAll() |
|
| 177 | |||
| 178 | /** |
||
| 179 | * Load project's config if configured. |
||
| 180 | */ |
||
| 181 | 2 | public function loadConfig() |
|
| 188 | |||
| 189 | public function setRootDir($value) |
||
| 193 | |||
| 194 | 2 | public function getRootDir() |
|
| 202 | |||
| 203 | /** |
||
| 204 | * Chdirs to project's root by looking for config file in the current directory and up. |
||
| 205 | * @throws InvalidParamException when failed to find |
||
| 206 | * @return string path to the root directory of hidev project |
||
| 207 | */ |
||
| 208 | 2 | protected function findRootDir() |
|
| 219 | |||
| 220 | public function buildRootPath($subpath) |
||
| 224 | } |
||
| 225 |
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.