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 |
||
| 46 | class FixtureController extends Controller |
||
| 47 | { |
||
| 48 | use FixtureTrait; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var string controller default action ID. |
||
| 52 | */ |
||
| 53 | public $defaultAction = 'load'; |
||
| 54 | /** |
||
| 55 | * @var string default namespace to search fixtures in |
||
| 56 | */ |
||
| 57 | public $namespace = 'tests\unit\fixtures'; |
||
| 58 | /** |
||
| 59 | * @var array global fixtures that should be applied when loading and unloading. By default it is set to `InitDbFixture` |
||
| 60 | * that disables and enables integrity check, so your data can be safely loaded. |
||
| 61 | */ |
||
| 62 | public $globalFixtures = [ |
||
| 63 | InitDbFixture::class, |
||
| 64 | ]; |
||
| 65 | |||
| 66 | |||
| 67 | /** |
||
| 68 | * @inheritdoc |
||
| 69 | */ |
||
| 70 | public function options($actionID) |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @inheritdoc |
||
| 79 | * @since 2.0.8 |
||
| 80 | */ |
||
| 81 | public function optionAliases() |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Loads the specified fixture data. |
||
| 91 | * |
||
| 92 | * For example, |
||
| 93 | * |
||
| 94 | * ``` |
||
| 95 | * # load the fixture data specified by User and UserProfile. |
||
| 96 | * # any existing fixture data will be removed first |
||
| 97 | * yii fixture/load "User, UserProfile" |
||
| 98 | * |
||
| 99 | * # load all available fixtures found under 'tests\unit\fixtures' |
||
| 100 | * yii fixture/load "*" |
||
| 101 | * |
||
| 102 | * # load all fixtures except User and UserProfile |
||
| 103 | * yii fixture/load "*, -User, -UserProfile" |
||
| 104 | * ``` |
||
| 105 | * |
||
| 106 | * @param array $fixturesInput |
||
| 107 | * @return int return code |
||
| 108 | * @throws Exception if the specified fixture does not exist. |
||
| 109 | */ |
||
| 110 | 7 | public function actionLoad(array $fixturesInput = []) |
|
| 169 | |||
| 170 | /** |
||
| 171 | * Unloads the specified fixtures. |
||
| 172 | * |
||
| 173 | * For example, |
||
| 174 | * |
||
| 175 | * ``` |
||
| 176 | * # unload the fixture data specified by User and UserProfile. |
||
| 177 | * yii fixture/unload "User, UserProfile" |
||
| 178 | * |
||
| 179 | * # unload all fixtures found under 'tests\unit\fixtures' |
||
| 180 | * yii fixture/unload "*" |
||
| 181 | * |
||
| 182 | * # unload all fixtures except User and UserProfile |
||
| 183 | * yii fixture/unload "*, -User, -UserProfile" |
||
| 184 | * ``` |
||
| 185 | * |
||
| 186 | * @param array $fixturesInput |
||
| 187 | * @return int return code |
||
| 188 | * @throws Exception if the specified fixture does not exist. |
||
| 189 | */ |
||
| 190 | 6 | public function actionUnload(array $fixturesInput = []) |
|
| 235 | |||
| 236 | /** |
||
| 237 | * Notifies user that fixtures were successfully loaded. |
||
| 238 | * @param array $fixtures |
||
| 239 | */ |
||
| 240 | 6 | private function notifyLoaded($fixtures) |
|
| 246 | |||
| 247 | /** |
||
| 248 | * Notifies user that there are no fixtures to load according input conditions. |
||
| 249 | * @param array $foundFixtures array of found fixtures |
||
| 250 | * @param array $except array of names of fixtures that should not be loaded |
||
| 251 | */ |
||
| 252 | public function notifyNothingToLoad($foundFixtures, $except) |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Notifies user that there are no fixtures to unload according input conditions. |
||
| 271 | * @param array $foundFixtures array of found fixtures |
||
| 272 | * @param array $except array of names of fixtures that should not be loaded |
||
| 273 | */ |
||
| 274 | public function notifyNothingToUnload($foundFixtures, $except) |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Notifies user that fixtures were successfully unloaded. |
||
| 293 | * @param array $fixtures |
||
| 294 | */ |
||
| 295 | 5 | private function notifyUnloaded($fixtures) |
|
| 301 | |||
| 302 | /** |
||
| 303 | * Notifies user that fixtures were not found under fixtures path. |
||
| 304 | * @param array $fixtures |
||
| 305 | */ |
||
| 306 | 2 | private function notifyNotFound($fixtures) |
|
| 314 | |||
| 315 | /** |
||
| 316 | * Prompts user with confirmation if fixtures should be loaded. |
||
| 317 | * @param array $fixtures |
||
| 318 | * @param array $except |
||
| 319 | * @return bool |
||
| 320 | */ |
||
| 321 | 6 | private function confirmLoad($fixtures, $except) |
|
| 346 | |||
| 347 | /** |
||
| 348 | * Prompts user with confirmation for fixtures that should be unloaded. |
||
| 349 | * @param array $fixtures |
||
| 350 | * @param array $except |
||
| 351 | * @return bool |
||
| 352 | */ |
||
| 353 | 5 | private function confirmUnload($fixtures, $except) |
|
| 375 | |||
| 376 | /** |
||
| 377 | * Outputs data to the console as a list. |
||
| 378 | * @param array $data |
||
| 379 | */ |
||
| 380 | 13 | private function outputList($data) |
|
| 386 | |||
| 387 | /** |
||
| 388 | * Checks if needed to apply all fixtures. |
||
| 389 | * @param string $fixture |
||
| 390 | * @return bool |
||
| 391 | */ |
||
| 392 | 13 | public function needToApplyAll($fixture) |
|
| 396 | |||
| 397 | /** |
||
| 398 | * Finds fixtures to be loaded, for example "User", if no fixtures were specified then all of them |
||
| 399 | * will be searching by suffix "Fixture.php". |
||
| 400 | * @param array $fixtures fixtures to be loaded |
||
| 401 | * @return array Array of found fixtures. These may differ from input parameter as not all fixtures may exists. |
||
| 402 | */ |
||
| 403 | 13 | private function findFixtures(array $fixtures = []) |
|
| 427 | |||
| 428 | /** |
||
| 429 | * Calculates fixture's name |
||
| 430 | * Basically, strips [[getFixturePath()]] and `Fixture.php' suffix from fixture's full path. |
||
| 431 | * @see getFixturePath() |
||
| 432 | * @param string $fullFixturePath Full fixture path |
||
| 433 | * @return string Relative fixture name |
||
| 434 | */ |
||
| 435 | 11 | private function getFixtureRelativeName($fullFixturePath) |
|
| 445 | |||
| 446 | /** |
||
| 447 | * Returns valid fixtures config that can be used to load them. |
||
| 448 | * @param array $fixtures fixtures to configure |
||
| 449 | * @return array |
||
| 450 | */ |
||
| 451 | 11 | private function getFixturesConfig($fixtures) |
|
| 470 | |||
| 471 | /** |
||
| 472 | * Filters fixtures by splitting them in two categories: one that should be applied and not. |
||
| 473 | * |
||
| 474 | * If fixture is prefixed with "-", for example "-User", that means that fixture should not be loaded, |
||
| 475 | * if it is not prefixed it is considered as one to be loaded. Returns array: |
||
| 476 | * |
||
| 477 | * ```php |
||
| 478 | * [ |
||
| 479 | * 'apply' => [ |
||
| 480 | * 'User', |
||
| 481 | * ... |
||
| 482 | * ], |
||
| 483 | * 'except' => [ |
||
| 484 | * 'Custom', |
||
| 485 | * ... |
||
| 486 | * ], |
||
| 487 | * ] |
||
| 488 | * ``` |
||
| 489 | * @param array $fixtures |
||
| 490 | * @return array fixtures array with 'apply' and 'except' elements. |
||
| 491 | */ |
||
| 492 | 13 | private function filterFixtures($fixtures) |
|
| 509 | |||
| 510 | /** |
||
| 511 | * Returns fixture path that determined on fixtures namespace. |
||
| 512 | * @throws InvalidConfigException if fixture namespace is invalid |
||
| 513 | * @return string fixture path |
||
| 514 | */ |
||
| 515 | 13 | private function getFixturePath() |
|
| 523 | } |
||
| 524 |
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.