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() |
|
| 39 | { |
||
| 40 | 2 | self::$started = true; |
|
| 41 | 2 | $this->getRootDir(); |
|
| 42 | 2 | $this->includeMainConfig(); |
|
| 43 | 2 | $this->addAliases(); |
|
| 44 | 2 | $this->addAutoloader(); |
|
| 45 | 2 | $this->requireAll(); |
|
| 46 | 2 | $this->includeAll(); |
|
| 47 | 2 | $this->loadConfig(); |
|
| 48 | 2 | $this->includeMainConfig(); |
|
| 49 | 2 | } |
|
| 50 | |||
| 51 | 2 | public function includeMainConfig() |
|
| 52 | { |
||
| 53 | 2 | $this->takeConfig()->includeConfig('.hidev/config.yml'); |
|
| 54 | 2 | if (file_exists('.hidev/config-local.yml')) { |
|
| 55 | $this->takeConfig()->includeConfig('.hidev/config-local.yml'); |
||
| 56 | } |
||
| 57 | 2 | } |
|
| 58 | |||
| 59 | 2 | public function addAutoloader() |
|
| 60 | { |
||
| 61 | 2 | $autoloader = Yii::getAlias('@root/vendor/autoload.php'); |
|
| 62 | 2 | if (file_exists($autoloader)) { |
|
| 63 | spl_autoload_unregister(['Yii', 'autoload']); |
||
| 64 | require $autoloader; |
||
| 65 | spl_autoload_register(['Yii', 'autoload'], true, true); |
||
| 66 | } |
||
| 67 | 2 | } |
|
| 68 | |||
| 69 | /** |
||
| 70 | * Update action. |
||
| 71 | * @return int exit code |
||
| 72 | */ |
||
| 73 | public function actionUpdate() |
||
| 74 | { |
||
| 75 | if (file_exists('.hidev/composer.json')) { |
||
| 76 | return $this->passthru('composer', ['update', '-d', '.hidev', '--prefer-source', '--ansi']); |
||
| 77 | } |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Adds aliases: |
||
| 82 | * - @root alias to current project root dir |
||
| 83 | * - current package namespace for it could be used from hidev. |
||
| 84 | */ |
||
| 85 | 2 | public function addAliases() |
|
| 86 | { |
||
| 87 | 2 | Yii::setAlias('@root', $this->getRootDir()); |
|
| 88 | 2 | $config = $this->takeConfig()->rawItem('package'); |
|
| 89 | 2 | $alias = isset($config['namespace']) ? strtr($config['namespace'], '\\', '/') : ''; |
|
| 90 | 2 | if ($alias && !Yii::getAlias('@' . $alias, false)) { |
|
| 91 | $srcdir = Yii::getAlias('@root/' . ($config['src'] ?: 'src')); |
||
| 92 | Yii::setAlias($alias, $srcdir); |
||
|
|
|||
| 93 | } |
||
| 94 | 2 | $aliases = $this->takeConfig()->rawItem('aliases'); |
|
| 95 | 2 | if (!empty($aliases) && is_array($aliases)) { |
|
| 96 | 2 | foreach ($aliases as $alias => $path) { |
|
| 97 | 2 | if (!$this->hasAlias($alias)) { |
|
| 98 | Yii::setAlias($alias, $path); |
||
| 99 | } |
||
| 100 | } |
||
| 101 | } |
||
| 102 | 2 | } |
|
| 103 | |||
| 104 | 2 | public function hasAlias($alias, $exact = true) |
|
| 110 | |||
| 111 | /** |
||
| 112 | * Require all configured requires. |
||
| 113 | */ |
||
| 114 | 2 | protected function requireAll() |
|
| 115 | { |
||
| 140 | |||
| 141 | 2 | public function needsComposerInstall() |
|
| 163 | |||
| 164 | /** |
||
| 165 | * Include all configs. |
||
| 166 | */ |
||
| 167 | 2 | public function includeAll() |
|
| 180 | |||
| 181 | /** |
||
| 182 | * Load project's config if configured. |
||
| 183 | */ |
||
| 184 | 2 | public function loadConfig() |
|
| 191 | |||
| 192 | public function setRootDir($value) |
||
| 196 | |||
| 197 | 2 | public function getRootDir() |
|
| 205 | |||
| 206 | /** |
||
| 207 | * Chdirs to project's root by looking for config file in the current directory and up. |
||
| 208 | * @throws InvalidParamException when failed to find |
||
| 209 | * @return string path to the root directory of hidev project |
||
| 210 | */ |
||
| 211 | 2 | protected function findRootDir() |
|
| 222 | |||
| 223 | public function buildRootPath($subpath) |
||
| 227 | } |
||
| 228 |
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.