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 | /** |
||
| 25 | * @var string absolute path to the project root directory |
||
| 26 | */ |
||
| 27 | protected $_rootDir; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var bool hidev already started flag |
||
| 31 | */ |
||
| 32 | public static $started = false; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Make action. |
||
| 36 | */ |
||
| 37 | 2 | public function actionMake() |
|
| 49 | |||
| 50 | 2 | public function includeMainConfig() |
|
| 51 | { |
||
| 52 | 2 | $this->takeConfig()->includeConfig('.hidev/config.yml'); |
|
| 53 | 2 | if (file_exists('.hidev/config-local.yml')) { |
|
| 54 | $this->takeConfig()->includeConfig('.hidev/config-local.yml'); |
||
| 55 | } |
||
| 56 | 2 | } |
|
| 57 | |||
| 58 | 2 | public function addAutoloader() |
|
| 59 | { |
||
| 60 | 2 | $autoloader = Yii::getAlias('@root/vendor/autoload.php'); |
|
| 61 | 2 | if (file_exists($autoloader)) { |
|
| 62 | spl_autoload_unregister(['Yii', 'autoload']); |
||
| 63 | require $autoloader; |
||
| 64 | spl_autoload_register(['Yii', 'autoload'], true, true); |
||
| 65 | } |
||
| 66 | 2 | } |
|
| 67 | |||
| 68 | /** |
||
| 69 | * Update action. |
||
| 70 | * @return int exit code |
||
| 71 | */ |
||
| 72 | public function actionUpdate() |
||
| 73 | { |
||
| 74 | if (file_exists('.hidev/composer.json')) { |
||
| 75 | return $this->passthru('composer', ['update', '-d', '.hidev', '--prefer-source', '--ansi']); |
||
| 76 | } |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Adds aliases: |
||
| 81 | * - @root alias to current project root dir |
||
| 82 | * - current package namespace for it could be used from hidev. |
||
| 83 | */ |
||
| 84 | 2 | public function addAliases() |
|
| 85 | { |
||
| 86 | 2 | Yii::setAlias('@root', $this->getRootDir()); |
|
| 87 | 2 | $config = $this->takeConfig()->rawItem('package'); |
|
| 88 | 2 | $alias = isset($config['namespace']) ? strtr($config['namespace'], '\\', '/') : ''; |
|
| 89 | 2 | if ($alias && !Yii::getAlias('@' . $alias, false)) { |
|
| 90 | $srcdir = Yii::getAlias('@root/' . ($config['src'] ?: 'src')); |
||
| 91 | Yii::setAlias($alias, $srcdir); |
||
|
|
|||
| 92 | } |
||
| 93 | 2 | $aliases = $this->takeConfig()->rawItem('aliases'); |
|
| 94 | 2 | if (!empty($aliases) && is_array($aliases)) { |
|
| 95 | 2 | foreach ($aliases as $alias => $path) { |
|
| 96 | 2 | if (!$this->hasAlias($alias)) { |
|
| 97 | Yii::setAlias($alias, $path); |
||
| 98 | } |
||
| 99 | 2 | } |
|
| 100 | 2 | } |
|
| 101 | 2 | } |
|
| 102 | |||
| 103 | 2 | public function hasAlias($alias, $exact = true) |
|
| 109 | |||
| 110 | /** |
||
| 111 | * Require all configured requires. |
||
| 112 | */ |
||
| 113 | 2 | protected function requireAll() |
|
| 138 | |||
| 139 | 2 | public function needsComposerInstall() |
|
| 161 | |||
| 162 | /** |
||
| 163 | * Include all configs. |
||
| 164 | */ |
||
| 165 | 2 | public function includeAll() |
|
| 178 | |||
| 179 | /** |
||
| 180 | * Load project's config if configured. |
||
| 181 | */ |
||
| 182 | 2 | public function loadConfig() |
|
| 189 | |||
| 190 | public function setRootDir($value) |
||
| 194 | |||
| 195 | 2 | public function getRootDir() |
|
| 203 | |||
| 204 | /** |
||
| 205 | * Chdirs to project's root by looking for config file in the current directory and up. |
||
| 206 | * @throws InvalidParamException when failed to find |
||
| 207 | * @return string path to the root directory of hidev project |
||
| 208 | */ |
||
| 209 | 2 | protected function findRootDir() |
|
| 220 | |||
| 221 | public function buildRootPath($subpath) |
||
| 225 | } |
||
| 226 |
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.