Complex classes like FixtureController 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 FixtureController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 44 | class FixtureController extends Controller |
||
| 45 | { |
||
| 46 | use FixtureTrait; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var string controller default action ID. |
||
| 50 | */ |
||
| 51 | public $defaultAction = 'load'; |
||
| 52 | /** |
||
| 53 | * @var string default namespace to search fixtures in |
||
| 54 | */ |
||
| 55 | public $namespace = 'tests\unit\fixtures'; |
||
| 56 | /** |
||
| 57 | * @var array global fixtures that should be applied when loading and unloading. By default it is set to `InitDbFixture` |
||
| 58 | * that disables and enables integrity check, so your data can be safely loaded. |
||
| 59 | */ |
||
| 60 | public $globalFixtures = [ |
||
| 61 | 'yii\test\InitDb', |
||
| 62 | ]; |
||
| 63 | |||
| 64 | |||
| 65 | /** |
||
| 66 | * @inheritdoc |
||
| 67 | */ |
||
| 68 | public function options($actionID) |
||
| 69 | { |
||
| 70 | return array_merge(parent::options($actionID), [ |
||
| 71 | 'namespace', 'globalFixtures' |
||
| 72 | ]); |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @inheritdoc |
||
| 77 | * @since 2.0.8 |
||
| 78 | */ |
||
| 79 | public function optionAliases() |
||
| 80 | { |
||
| 81 | return array_merge(parent::optionAliases(), [ |
||
| 82 | 'g' => 'globalFixtures', |
||
| 83 | 'n' => 'namespace', |
||
| 84 | ]); |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Loads the specified fixture data. |
||
| 89 | * For example, |
||
| 90 | * |
||
| 91 | * ``` |
||
| 92 | * # load the fixture data specified by User and UserProfile. |
||
| 93 | * # any existing fixture data will be removed first |
||
| 94 | * yii fixture/load "User, UserProfile" |
||
| 95 | * |
||
| 96 | * # load all available fixtures found under 'tests\unit\fixtures' |
||
| 97 | * yii fixture/load "*" |
||
| 98 | * |
||
| 99 | * # load all fixtures except User and UserProfile |
||
| 100 | * yii fixture/load "*, -User, -UserProfile" |
||
| 101 | * ``` |
||
| 102 | * |
||
| 103 | * @param array $fixturesInput |
||
| 104 | * @return int return code |
||
| 105 | * @throws Exception if the specified fixture does not exist. |
||
| 106 | */ |
||
| 107 | 6 | public function actionLoad(array $fixturesInput = []) |
|
| 108 | { |
||
| 109 | 6 | if ($fixturesInput === []) { |
|
| 110 | $this->stdout($this->getHelpSummary() . "\n"); |
||
| 111 | |||
| 112 | $helpCommand = Console::ansiFormat('yii help fixture', [Console::FG_CYAN]); |
||
| 113 | $this->stdout("Use $helpCommand to get usage info.\n"); |
||
| 114 | |||
| 115 | return self::EXIT_CODE_NORMAL; |
||
| 116 | } |
||
| 117 | |||
| 118 | 6 | $filtered = $this->filterFixtures($fixturesInput); |
|
| 119 | 6 | $except = $filtered['except']; |
|
| 120 | |||
| 121 | 6 | if (!$this->needToApplyAll($fixturesInput[0])) { |
|
| 122 | 4 | $fixtures = $filtered['apply']; |
|
| 123 | |||
| 124 | 4 | $foundFixtures = $this->findFixtures($fixtures); |
|
| 125 | 4 | $notFoundFixtures = array_diff($fixtures, $foundFixtures); |
|
| 126 | |||
| 127 | 4 | if ($notFoundFixtures) { |
|
|
|
|||
| 128 | 1 | $this->notifyNotFound($notFoundFixtures); |
|
| 129 | 1 | } |
|
| 130 | 4 | } else { |
|
| 131 | 2 | $foundFixtures = $this->findFixtures(); |
|
| 132 | } |
||
| 133 | |||
| 134 | 6 | $fixturesToLoad = array_diff($foundFixtures, $except); |
|
| 135 | |||
| 136 | 6 | if (!$foundFixtures) { |
|
| 137 | 1 | throw new Exception( |
|
| 138 | 1 | "No files were found for: \"" . implode(', ', $fixturesInput) . "\".\n" . |
|
| 139 | 1 | "Check that files exist under fixtures path: \n\"" . $this->getFixturePath() . "\"." |
|
| 140 | 1 | ); |
|
| 141 | } |
||
| 142 | |||
| 143 | 5 | if (!$fixturesToLoad) { |
|
| 144 | 1 | $this->notifyNothingToLoad($foundFixtures, $except); |
|
| 145 | 1 | return static::EXIT_CODE_NORMAL; |
|
| 146 | } |
||
| 147 | |||
| 148 | 4 | if (!$this->confirmLoad($fixturesToLoad, $except)) { |
|
| 149 | return static::EXIT_CODE_NORMAL; |
||
| 150 | } |
||
| 151 | |||
| 152 | 4 | $fixtures = $this->getFixturesConfig(array_merge($this->globalFixtures, $fixturesToLoad)); |
|
| 153 | |||
| 154 | 4 | if (!$fixtures) { |
|
| 155 | throw new Exception('No fixtures were found in namespace: "' . $this->namespace . '"' . ''); |
||
| 156 | } |
||
| 157 | |||
| 158 | 4 | $fixturesObjects = $this->createFixtures($fixtures); |
|
| 159 | |||
| 160 | 4 | $this->unloadFixtures($fixturesObjects); |
|
| 161 | 4 | $this->loadFixtures($fixturesObjects); |
|
| 162 | 4 | $this->notifyLoaded($fixtures); |
|
| 163 | |||
| 164 | 4 | return static::EXIT_CODE_NORMAL; |
|
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Unloads the specified fixtures. |
||
| 169 | * For example, |
||
| 170 | * |
||
| 171 | * ``` |
||
| 172 | * # unload the fixture data specified by User and UserProfile. |
||
| 173 | * yii fixture/unload "User, UserProfile" |
||
| 174 | * |
||
| 175 | * # unload all fixtures found under 'tests\unit\fixtures' |
||
| 176 | * yii fixture/unload "*" |
||
| 177 | * |
||
| 178 | * # unload all fixtures except User and UserProfile |
||
| 179 | * yii fixture/unload "*, -User, -UserProfile" |
||
| 180 | * ``` |
||
| 181 | * |
||
| 182 | * @param array $fixturesInput |
||
| 183 | * @return int return code |
||
| 184 | * @throws Exception if the specified fixture does not exist. |
||
| 185 | */ |
||
| 186 | 6 | public function actionUnload(array $fixturesInput = []) |
|
| 187 | { |
||
| 188 | 6 | $filtered = $this->filterFixtures($fixturesInput); |
|
| 189 | 6 | $except = $filtered['except']; |
|
| 190 | |||
| 191 | 6 | if (!$this->needToApplyAll($fixturesInput[0])) { |
|
| 192 | 4 | $fixtures = $filtered['apply']; |
|
| 193 | |||
| 194 | 4 | $foundFixtures = $this->findFixtures($fixtures); |
|
| 195 | 4 | $notFoundFixtures = array_diff($fixtures, $foundFixtures); |
|
| 196 | |||
| 197 | 4 | if ($notFoundFixtures) { |
|
| 198 | 1 | $this->notifyNotFound($notFoundFixtures); |
|
| 199 | 1 | } |
|
| 200 | 4 | } else { |
|
| 201 | 2 | $foundFixtures = $this->findFixtures(); |
|
| 202 | } |
||
| 203 | |||
| 204 | 6 | $fixturesToUnload = array_diff($foundFixtures, $except); |
|
| 205 | |||
| 206 | 6 | if (!$foundFixtures) { |
|
| 207 | 1 | throw new Exception( |
|
| 208 | 1 | "No files were found for: \"" . implode(', ', $fixturesInput) . "\".\n" . |
|
| 209 | 1 | "Check that files exist under fixtures path: \n\"" . $this->getFixturePath() . "\"." |
|
| 210 | 1 | ); |
|
| 211 | } |
||
| 212 | |||
| 213 | 5 | if (!$fixturesToUnload) { |
|
| 214 | 1 | $this->notifyNothingToUnload($foundFixtures, $except); |
|
| 215 | 1 | return static::EXIT_CODE_NORMAL; |
|
| 216 | } |
||
| 217 | |||
| 218 | 4 | if (!$this->confirmUnload($fixturesToUnload, $except)) { |
|
| 219 | return static::EXIT_CODE_NORMAL; |
||
| 220 | } |
||
| 221 | |||
| 222 | 4 | $fixtures = $this->getFixturesConfig(array_merge($this->globalFixtures, $fixturesToUnload)); |
|
| 223 | |||
| 224 | 4 | if (!$fixtures) { |
|
| 225 | throw new Exception('No fixtures were found in namespace: ' . $this->namespace . '".'); |
||
| 226 | } |
||
| 227 | |||
| 228 | 4 | $this->unloadFixtures($this->createFixtures($fixtures)); |
|
| 229 | 4 | $this->notifyUnloaded($fixtures); |
|
| 230 | 4 | } |
|
| 231 | |||
| 232 | /** |
||
| 233 | * Notifies user that fixtures were successfully loaded. |
||
| 234 | * @param array $fixtures |
||
| 235 | */ |
||
| 236 | 4 | private function notifyLoaded($fixtures) |
|
| 242 | |||
| 243 | /** |
||
| 244 | * Notifies user that there are no fixtures to load according input conditions |
||
| 245 | * @param array $foundFixtures array of found fixtures |
||
| 246 | * @param array $except array of names of fixtures that should not be loaded |
||
| 247 | */ |
||
| 248 | 1 | public function notifyNothingToLoad($foundFixtures, $except) |
|
| 264 | |||
| 265 | /** |
||
| 266 | * Notifies user that there are no fixtures to unload according input conditions |
||
| 267 | * @param array $foundFixtures array of found fixtures |
||
| 268 | * @param array $except array of names of fixtures that should not be loaded |
||
| 269 | */ |
||
| 270 | 1 | public function notifyNothingToUnload($foundFixtures, $except) |
|
| 286 | |||
| 287 | /** |
||
| 288 | * Notifies user that fixtures were successfully unloaded. |
||
| 289 | * @param array $fixtures |
||
| 290 | */ |
||
| 291 | 4 | private function notifyUnloaded($fixtures) |
|
| 297 | |||
| 298 | /** |
||
| 299 | * Notifies user that fixtures were not found under fixtures path. |
||
| 300 | * @param array $fixtures |
||
| 301 | */ |
||
| 302 | 2 | private function notifyNotFound($fixtures) |
|
| 310 | |||
| 311 | /** |
||
| 312 | * Prompts user with confirmation if fixtures should be loaded. |
||
| 313 | * @param array $fixtures |
||
| 314 | * @param array $except |
||
| 315 | * @return bool |
||
| 316 | */ |
||
| 317 | 4 | private function confirmLoad($fixtures, $except) |
|
| 318 | { |
||
| 319 | 4 | $this->stdout("Fixtures namespace is: \n", Console::FG_YELLOW); |
|
| 320 | 4 | $this->stdout("\t" . $this->namespace . "\n\n", Console::FG_GREEN); |
|
| 321 | |||
| 322 | 4 | if (count($this->globalFixtures)) { |
|
| 323 | 1 | $this->stdout("Global fixtures will be used:\n\n", Console::FG_YELLOW); |
|
| 324 | 1 | $this->outputList($this->globalFixtures); |
|
| 325 | 1 | } |
|
| 326 | |||
| 327 | 4 | if (count($fixtures)) { |
|
| 328 | 4 | $this->stdout("\nFixtures below will be loaded:\n\n", Console::FG_YELLOW); |
|
| 329 | 4 | $this->outputList($fixtures); |
|
| 330 | 4 | } |
|
| 331 | |||
| 332 | 4 | if (count($except)) { |
|
| 333 | 2 | $this->stdout("\nFixtures that will NOT be loaded: \n\n", Console::FG_YELLOW); |
|
| 334 | 2 | $this->outputList($except); |
|
| 335 | 2 | } |
|
| 336 | |||
| 337 | 4 | $this->stdout("\nBe aware that:\n", Console::BOLD); |
|
| 338 | 4 | $this->stdout("Applying leads to purging of certain data in the database!\n", Console::FG_RED); |
|
| 339 | |||
| 340 | 4 | return $this->confirm("\nLoad above fixtures?"); |
|
| 341 | } |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Prompts user with confirmation for fixtures that should be unloaded. |
||
| 345 | * @param array $fixtures |
||
| 346 | * @param array $except |
||
| 347 | * @return bool |
||
| 348 | */ |
||
| 349 | 4 | private function confirmUnload($fixtures, $except) |
|
| 371 | |||
| 372 | /** |
||
| 373 | * Outputs data to the console as a list. |
||
| 374 | * @param array $data |
||
| 375 | */ |
||
| 376 | 12 | private function outputList($data) |
|
| 382 | |||
| 383 | /** |
||
| 384 | * Checks if needed to apply all fixtures. |
||
| 385 | * @param string $fixture |
||
| 386 | * @return bool |
||
| 387 | */ |
||
| 388 | 12 | public function needToApplyAll($fixture) |
|
| 392 | |||
| 393 | /** |
||
| 394 | * Finds fixtures to be loaded, for example "User", if no fixtures were specified then all of them |
||
| 395 | * will be searching by suffix "Fixture.php". |
||
| 396 | * @param array $fixtures fixtures to be loaded |
||
| 397 | * @return array Array of found fixtures. These may differ from input parameter as not all fixtures may exists. |
||
| 398 | */ |
||
| 399 | 12 | private function findFixtures(array $fixtures = []) |
|
| 423 | |||
| 424 | /** |
||
| 425 | * Returns valid fixtures config that can be used to load them. |
||
| 426 | * @param array $fixtures fixtures to configure |
||
| 427 | * @return array |
||
| 428 | */ |
||
| 429 | 8 | private function getFixturesConfig($fixtures) |
|
| 444 | |||
| 445 | /** |
||
| 446 | * Filters fixtures by splitting them in two categories: one that should be applied and not. |
||
| 447 | * If fixture is prefixed with "-", for example "-User", that means that fixture should not be loaded, |
||
| 448 | * if it is not prefixed it is considered as one to be loaded. Returns array: |
||
| 449 | * |
||
| 450 | * ```php |
||
| 451 | * [ |
||
| 452 | * 'apply' => [ |
||
| 453 | * 'User', |
||
| 454 | * ... |
||
| 455 | * ], |
||
| 456 | * 'except' => [ |
||
| 457 | * 'Custom', |
||
| 458 | * ... |
||
| 459 | * ], |
||
| 460 | * ] |
||
| 461 | * ``` |
||
| 462 | * @param array $fixtures |
||
| 463 | * @return array fixtures array with 'apply' and 'except' elements. |
||
| 464 | */ |
||
| 465 | 12 | private function filterFixtures($fixtures) |
|
| 482 | |||
| 483 | /** |
||
| 484 | * Returns fixture path that determined on fixtures namespace. |
||
| 485 | * @throws InvalidConfigException if fixture namespace is invalid |
||
| 486 | * @return string fixture path |
||
| 487 | */ |
||
| 488 | 12 | private function getFixturePath() |
|
| 496 | } |
||
| 497 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.