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 | public function actionMake() |
||
| 38 | { |
||
| 39 | self::$started = true; |
||
| 40 | $this->getRootDir(); |
||
| 41 | $this->includeMainConfig(); |
||
| 42 | $this->addAliases(); |
||
| 43 | $this->addAutoloader(); |
||
| 44 | $this->requireAll(); |
||
| 45 | $this->includeAll(); |
||
| 46 | $this->loadConfig(); |
||
| 47 | $this->includeMainConfig(); |
||
| 48 | } |
||
| 49 | |||
| 50 | public function includeMainConfig() |
||
| 51 | { |
||
| 52 | $this->takeConfig()->includeConfig('.hidev/config.yml'); |
||
| 53 | if (file_exists('.hidev/config-local.yml')) { |
||
| 54 | $this->takeConfig()->includeConfig('.hidev/config-local.yml'); |
||
| 55 | } |
||
| 56 | } |
||
| 57 | |||
| 58 | public function addAutoloader() |
||
| 59 | { |
||
| 60 | $autoloader = Yii::getAlias('@root/vendor/autoload.php'); |
||
| 61 | if (file_exists($autoloader)) { |
||
| 62 | spl_autoload_unregister(['Yii', 'autoload']); |
||
| 63 | require $autoloader; |
||
| 64 | spl_autoload_register(['Yii', 'autoload'], true, true); |
||
| 65 | } |
||
| 66 | } |
||
| 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 | public function addAliases() |
||
| 85 | { |
||
| 86 | Yii::setAlias('@root', $this->getRootDir()); |
||
| 87 | $config = $this->takeConfig()->rawItem('package'); |
||
| 88 | $alias = isset($config['namespace']) ? strtr($config['namespace'], '\\', '/') : ''; |
||
| 89 | if ($alias && !Yii::getAlias('@' . $alias, false)) { |
||
| 90 | $srcdir = Yii::getAlias('@root/' . ($config['src'] ?: 'src')); |
||
| 91 | Yii::setAlias($alias, $srcdir); |
||
|
|
|||
| 92 | } |
||
| 93 | $aliases = $this->takeConfig()->rawItem('aliases'); |
||
| 94 | if (!empty($aliases) && is_array($aliases)) { |
||
| 95 | foreach ($aliases as $alias => $path) { |
||
| 96 | if (!$this->hasAlias($alias)) { |
||
| 97 | Yii::setAlias($alias, $path); |
||
| 98 | } |
||
| 99 | } |
||
| 100 | } |
||
| 101 | } |
||
| 102 | |||
| 103 | public function hasAlias($alias, $exact = true) |
||
| 104 | { |
||
| 105 | $pos = strpos($alias, '/'); |
||
| 106 | |||
| 107 | return $pos === false ? isset(Yii::$aliases[$alias]) : isset(Yii::$aliases[substr($alias, 0, $pos)][$alias]); |
||
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Require all configured requires. |
||
| 112 | */ |
||
| 113 | protected function requireAll() |
||
| 114 | { |
||
| 115 | $plugins = $this->takeConfig()->rawItem('plugins'); |
||
| 116 | $vendors = []; |
||
| 117 | if ($plugins) { |
||
| 118 | $file = File::create('.hidev/composer.json'); |
||
| 119 | $data = ArrayHelper::merge($file->load(), ['require' => $plugins]); |
||
| 120 | if ($file->save($data) || !is_dir('.hidev/vendor')) { |
||
| 121 | $this->runAction('update'); |
||
| 122 | } |
||
| 123 | $vendors[] = '.hidev/vendor'; |
||
| 124 | } elseif ($this->needsComposerInstall()) { |
||
| 125 | if ($this->passthru('composer', ['install', '--ansi'])) { |
||
| 126 | throw new InvalidParamException('Failed initialize project with composer install'); |
||
| 127 | } |
||
| 128 | } |
||
| 129 | if (file_exists('vendor/hiqdev/composer-config-plugin-output/hidev.php')) { |
||
| 130 | $vendors[] = 'vendor'; |
||
| 131 | } |
||
| 132 | if (!empty($vendors)) { |
||
| 133 | foreach (array_unique($vendors) as $dir) { |
||
| 134 | $this->module->loadExtraVendor($dir); |
||
| 135 | } |
||
| 136 | } |
||
| 137 | } |
||
| 138 | |||
| 139 | public function needsComposerInstall() |
||
| 140 | { |
||
| 141 | if (file_exists('vendor')) { |
||
| 142 | return false; |
||
| 143 | } |
||
| 144 | if (!file_exists('composer.json')) { |
||
| 145 | return false; |
||
| 146 | } |
||
| 147 | $data = File::create('composer.json')->load(); |
||
| 148 | foreach (['require', 'require-dev'] as $key) { |
||
| 149 | if (isset($data[$key])) { |
||
| 150 | foreach ($data[$key] as $package => $version) { |
||
| 151 | list(, $name) = explode('/', $package); |
||
| 152 | if (strncmp($name, 'hidev-', 6) === 0) { |
||
| 153 | return true; |
||
| 154 | } |
||
| 155 | } |
||
| 156 | } |
||
| 157 | } |
||
| 158 | |||
| 159 | return false; |
||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Include all configs. |
||
| 164 | */ |
||
| 165 | public function includeAll() |
||
| 166 | { |
||
| 167 | $still = true; |
||
| 168 | while ($still) { |
||
| 169 | $still = false; |
||
| 170 | $include = $this->takeConfig()->rawItem('include'); |
||
| 171 | if ($include) { |
||
| 172 | foreach ($include as $path) { |
||
| 173 | $still = $still || $this->takeConfig()->includeConfig($path); |
||
| 174 | } |
||
| 175 | } |
||
| 176 | } |
||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Load project's config if configured. |
||
| 181 | */ |
||
| 182 | public function loadConfig() |
||
| 183 | { |
||
| 184 | $path = $this->takeConfig()->rawItem('config'); |
||
| 185 | if ($path) { |
||
| 186 | $this->module->loadExtraConfig($path); |
||
| 187 | } |
||
| 188 | } |
||
| 189 | |||
| 190 | public function setRootDir($value) |
||
| 194 | |||
| 195 | 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 | protected function findRootDir() |
||
| 210 | { |
||
| 211 | $configDir = '.hidev'; |
||
| 212 | for ($i = 0; $i < 9; ++$i) { |
||
| 213 | if (is_dir($configDir)) { |
||
| 214 | return getcwd(); |
||
| 215 | } |
||
| 216 | chdir('..'); |
||
| 217 | } |
||
| 218 | throw new InvalidParamException("Not a hidev project (or any of the parent directories).\nUse `hidev init` to initialize hidev project."); |
||
| 219 | } |
||
| 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.