Complex classes like FormContext 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 FormContext, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class FormContext extends RawFormContext |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * @param string $value |
||
| 26 | * Typed text. |
||
| 27 | * @param string $selector |
||
| 28 | * Selector of the field. |
||
| 29 | * @param int $option |
||
| 30 | * An option number. Will be selected from loaded variants. |
||
| 31 | * |
||
| 32 | * @throws \InvalidArgumentException |
||
| 33 | * When $option is less than zero. |
||
| 34 | * @throws NoSuchElement |
||
| 35 | * When autocomplete list was not loaded. |
||
| 36 | * @throws \RuntimeException |
||
| 37 | * When neither option was not loaded. |
||
| 38 | * @throws \OverflowException |
||
| 39 | * When $option is more than variants are available. |
||
| 40 | * @throws \Exception |
||
| 41 | * When value was not changed. |
||
| 42 | * |
||
| 43 | * @Then /^(?:|I )typed "([^"]*)" in the "([^"]*)" field and chose (\d+) option from autocomplete variants$/ |
||
| 44 | */ |
||
| 45 | public function choseOptionFromAutocompleteVariants($value, $selector, $option) |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Use the current user data for filling fields. |
||
| 93 | * |
||
| 94 | * @example |
||
| 95 | * Then I fill "First name" with value of field "First name" of current user |
||
| 96 | * And fill "field_last_name[und][0]" with value of field "field_user_last_name" of current user |
||
| 97 | * |
||
| 98 | * @param string $field |
||
| 99 | * The name of field to fill in. HTML Label, name or ID can be user as selector. |
||
| 100 | * @param string $user_field |
||
| 101 | * The name of field from which the data will taken. Drupal label or machine name can be used as selector. |
||
| 102 | * |
||
| 103 | * @throws \InvalidArgumentException |
||
| 104 | * @throws \UnexpectedValueException |
||
| 105 | * @throws \Exception |
||
| 106 | * @throws NoSuchElement |
||
| 107 | * When field cannot be found. |
||
| 108 | * |
||
| 109 | * @Then /^(?:I )fill "([^"]*)" with value of field "([^"]*)" of current user$/ |
||
| 110 | */ |
||
| 111 | public function fillInWithValueOfFieldOfCurrentUser($field, $user_field) |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @param string $action |
||
| 136 | * Can be "check" or "uncheck". |
||
| 137 | * @param TableNode $checkboxes |
||
| 138 | * Table with one row of checkboxes selectors. |
||
| 139 | * |
||
| 140 | * @example |
||
| 141 | * I uncheck the boxes: |
||
| 142 | * | Consumer Products | |
||
| 143 | * | Financial Services | |
||
| 144 | * |
||
| 145 | * @example |
||
| 146 | * I check the boxes: |
||
| 147 | * | Consumer Products | |
||
| 148 | * | Financial Services | |
||
| 149 | * |
||
| 150 | * @Given /^(?:|I )(?:|un)check the boxes:/ |
||
| 151 | */ |
||
| 152 | public function checkboxAction($action, TableNode $checkboxes) |
||
| 160 | |||
| 161 | /** |
||
| 162 | * This method was defined and used instead of "assertSelectRadioById", |
||
| 163 | * because the field label can contain too long value and better to use |
||
| 164 | * another selector instead of label. |
||
| 165 | * |
||
| 166 | * @see MinkContext::assertSelectRadioById() |
||
| 167 | * |
||
| 168 | * @param string $customized |
||
| 169 | * Can be an empty string or " customized". |
||
| 170 | * @param string $selector |
||
| 171 | * Field selector. |
||
| 172 | * |
||
| 173 | * @throws NoSuchElement |
||
| 174 | * When radio button was not found. |
||
| 175 | * @throws \Exception |
||
| 176 | * |
||
| 177 | * @Given /^(?:|I )check the(| customized) "([^"]*)" radio button$/ |
||
| 178 | */ |
||
| 179 | public function radioAction($customized, $selector) |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @param string $selector |
||
| 205 | * @param string $value |
||
| 206 | * |
||
| 207 | * @throws NoSuchElement |
||
| 208 | * |
||
| 209 | * @When /^(?:|I )fill "([^"]*)" with "([^"]*)"$/ |
||
| 210 | */ |
||
| 211 | public function fillField($selector, $value) |
||
| 215 | |||
| 216 | /** |
||
| 217 | * @param TableNode $fields |
||
| 218 | * | Field locator | Value | |
||
| 219 | * |
||
| 220 | * @throws NoSuchElement |
||
| 221 | * |
||
| 222 | * @When /^(?:|I )fill the following:$/ |
||
| 223 | */ |
||
| 224 | public function fillFields(TableNode $fields) |
||
| 230 | |||
| 231 | /** |
||
| 232 | * @param string $file |
||
| 233 | * Path to a file. Relative to the directory specified in "files_path" in behat.yml. |
||
| 234 | * @param string $selector |
||
| 235 | * Field selector (label|id|name). |
||
| 236 | * |
||
| 237 | * @throws \Exception |
||
| 238 | * @throws NoSuchElement |
||
| 239 | * |
||
| 240 | * @Given /^(?:|I )attach file "([^"]*)" to "([^"]*)"$/ |
||
| 241 | */ |
||
| 242 | public function attachFile($file, $selector) |
||
| 258 | |||
| 259 | /** |
||
| 260 | * @param string $selector |
||
| 261 | * @param TableNode $values |
||
| 262 | * |
||
| 263 | * @throws ElementNotFoundException |
||
| 264 | * @throws \Exception |
||
| 265 | * @throws NoSuchElement |
||
| 266 | * |
||
| 267 | * @Given /^(?:|I )select the following in "([^"]*)" hierarchical select:$/ |
||
| 268 | */ |
||
| 269 | public function setValueForHierarchicalSelect($selector, TableNode $values) |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Check that an image was uploaded and can be viewed on the page. |
||
| 315 | * |
||
| 316 | * @throws \Exception |
||
| 317 | * @throws FileNotFoundException |
||
| 318 | * |
||
| 319 | * @Then /^(?:|I )should see the thumbnail$/ |
||
| 320 | */ |
||
| 321 | public function shouldSeeThumbnail() |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Check that the page have no error messages and fields - error classes. |
||
| 350 | * |
||
| 351 | * @throws \InvalidArgumentException |
||
| 352 | * @throws \RuntimeException |
||
| 353 | * @throws \Exception |
||
| 354 | * |
||
| 355 | * @Then /^(?:|I )should see no errors$/ |
||
| 356 | */ |
||
| 357 | public function shouldSeeNoErrors() |
||
| 390 | |||
| 391 | /** |
||
| 392 | * @param string $option |
||
| 393 | * @param string $selector |
||
| 394 | * |
||
| 395 | * @Then /^(?:|I )pick "([^"]*)" from "([^"]*)"$/ |
||
| 396 | */ |
||
| 397 | public function selectFrom($option, $selector) |
||
| 401 | |||
| 402 | /** |
||
| 403 | * @example |
||
| 404 | * And pick the following: |
||
| 405 | * | Entity Reference | Type of new field | |
||
| 406 | * | Inline entity form - Multiple values | Widget for new field | |
||
| 407 | * |
||
| 408 | * @param TableNode $rows |
||
| 409 | * |
||
| 410 | * @Then /^(?:|I )pick the following:$/ |
||
| 411 | */ |
||
| 412 | public function selectFromFollowing(TableNode $rows) |
||
| 418 | |||
| 419 | /** |
||
| 420 | * @example |
||
| 421 | * And check that "Users" field has "admin" value |
||
| 422 | * And check that "Users" field has not "customer" value |
||
| 423 | * |
||
| 424 | * @Then /^(?:|I )check that "([^"]*)" field has(| not) "([^"]*)" value$/ |
||
| 425 | */ |
||
| 426 | public function assertTextualField($selector, $not, $expected) |
||
| 430 | |||
| 431 | /** |
||
| 432 | * @example |
||
| 433 | * And check that "User" is selected in "Apply to" select |
||
| 434 | * And check that "Product(s)" is not selected in "Apply to" select |
||
| 435 | * |
||
| 436 | * @Then /^(?:|I )check that "([^"]*)" is(| not) selected in "([^"]*)" select$/ |
||
| 437 | */ |
||
| 438 | public function assertSelectableField($expected, $not, $selector) |
||
| 442 | |||
| 443 | /** |
||
| 444 | * @example |
||
| 445 | * And check that "Order discount" is checked |
||
| 446 | * And check that "Product discount" is not checked |
||
| 447 | * |
||
| 448 | * @Then /^(?:|I )check that "([^"]*)" is(| not) checked$/ |
||
| 449 | */ |
||
| 450 | public function assertCheckableField($selector, $not) |
||
| 454 | |||
| 455 | /** |
||
| 456 | * @param string $date |
||
| 457 | * @param string $selector |
||
| 458 | * |
||
| 459 | * @Then /^(?:|I )choose "([^"]*)" in "([^"]*)" datepicker$/ |
||
| 460 | * @Then /^(?:|I )set the "([^"]*)" for "([^"]*)" datepicker$/ |
||
| 461 | */ |
||
| 462 | public function setDate($date, $selector) |
||
| 466 | |||
| 467 | /** |
||
| 468 | * @param string $selector |
||
| 469 | * @param string $date |
||
| 470 | * |
||
| 471 | * @Then /^(?:|I )check that "([^"]*)" datepicker contains "([^"]*)" date$/ |
||
| 472 | */ |
||
| 473 | public function isDateSelected($selector, $date) |
||
| 477 | |||
| 478 | /** |
||
| 479 | * @param string $date |
||
| 480 | * @param string $selector |
||
| 481 | * |
||
| 482 | * @Then /^(?:|I )check that "([^"]*)" is available for "([^"]*)" datepicker$/ |
||
| 483 | */ |
||
| 484 | public function isDateAvailable($date, $selector) |
||
| 488 | } |
||
| 489 |