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 |
||
20 | class FormContext extends RawFormContext |
||
21 | { |
||
22 | /** |
||
23 | * @param string $value |
||
24 | * Typed text. |
||
25 | * @param string $selector |
||
26 | * Selector of the field. |
||
27 | * @param int $option |
||
28 | * An option number. Will be selected from loaded variants. |
||
29 | * |
||
30 | * @throws \InvalidArgumentException |
||
31 | * When $option is less than zero. |
||
32 | * @throws NoSuchElement |
||
33 | * When autocomplete list was not loaded. |
||
34 | * @throws \RuntimeException |
||
35 | * When neither option was not loaded. |
||
36 | * @throws \OverflowException |
||
37 | * When $option is more than variants are available. |
||
38 | * @throws \Exception |
||
39 | * When value was not changed. |
||
40 | * |
||
41 | * @Then /^(?:|I )typed "([^"]*)" in the "([^"]*)" field and chose (\d+) option from autocomplete variants$/ |
||
42 | */ |
||
43 | public function choseOptionFromAutocompleteVariants($value, $selector, $option) |
||
88 | |||
89 | /** |
||
90 | * Use the current user data for filling fields. |
||
91 | * |
||
92 | * @example |
||
93 | * Then I fill "First name" with value of field "First name" of current user |
||
94 | * And fill "field_last_name[und][0]" with value of field "field_user_last_name" of current user |
||
95 | * |
||
96 | * @param string $field |
||
97 | * The name of field to fill in. HTML Label, name or ID can be user as selector. |
||
98 | * @param string $user_field |
||
99 | * The name of field from which the data will taken. Drupal label or machine name can be used as selector. |
||
100 | * |
||
101 | * @throws \InvalidArgumentException |
||
102 | * @throws \UnexpectedValueException |
||
103 | * @throws \Exception |
||
104 | * @throws NoSuchElement |
||
105 | * When field cannot be found. |
||
106 | * |
||
107 | * @Then /^(?:I )fill "([^"]*)" with value of field "([^"]*)" of current user$/ |
||
108 | */ |
||
109 | public function fillInWithValueOfFieldOfCurrentUser($field, $user_field) |
||
131 | |||
132 | /** |
||
133 | * @param string $action |
||
134 | * Can be "check" or "uncheck". |
||
135 | * @param TableNode $checkboxes |
||
136 | * Table with one row of checkboxes selectors. |
||
137 | * |
||
138 | * @example |
||
139 | * I uncheck the boxes: |
||
140 | * | Consumer Products | |
||
141 | * | Financial Services | |
||
142 | * |
||
143 | * @example |
||
144 | * I check the boxes: |
||
145 | * | Consumer Products | |
||
146 | * | Financial Services | |
||
147 | * |
||
148 | * @Given /^(?:|I )(?:|un)check the boxes:/ |
||
149 | */ |
||
150 | public function checkboxAction($action, TableNode $checkboxes) |
||
158 | |||
159 | /** |
||
160 | * This method was defined and used instead of "assertSelectRadioById", |
||
161 | * because the field label can contain too long value and better to use |
||
162 | * another selector instead of label. |
||
163 | * |
||
164 | * @see MinkContext::assertSelectRadioById() |
||
165 | * |
||
166 | * @param string $customized |
||
167 | * Can be an empty string or " customized". |
||
168 | * @param string $selector |
||
169 | * Field selector. |
||
170 | * |
||
171 | * @throws NoSuchElement |
||
172 | * When radio button was not found. |
||
173 | * @throws \Exception |
||
174 | * |
||
175 | * @Given /^(?:|I )check the(| customized) "([^"]*)" radio button$/ |
||
176 | */ |
||
177 | public function radioAction($customized, $selector) |
||
200 | |||
201 | /** |
||
202 | * @param string $selector |
||
203 | * @param string $value |
||
204 | * |
||
205 | * @throws NoSuchElement |
||
206 | * |
||
207 | * @When /^(?:|I )fill "([^"]*)" with "([^"]*)"$/ |
||
208 | */ |
||
209 | public function fillField($selector, $value) |
||
213 | |||
214 | /** |
||
215 | * @param TableNode $fields |
||
216 | * | Field locator | Value | |
||
217 | * |
||
218 | * @throws NoSuchElement |
||
219 | * |
||
220 | * @When /^(?:|I )fill the following:$/ |
||
221 | */ |
||
222 | public function fillFields(TableNode $fields) |
||
228 | |||
229 | /** |
||
230 | * @param string $file |
||
231 | * Path to a file. Relative to the directory specified in "files_path" in behat.yml. |
||
232 | * @param string $selector |
||
233 | * Field selector (label|id|name). |
||
234 | * |
||
235 | * @throws \Exception |
||
236 | * @throws NoSuchElement |
||
237 | * |
||
238 | * @Given /^(?:|I )attach file "([^"]*)" to "([^"]*)"$/ |
||
239 | */ |
||
240 | public function attachFile($file, $selector) |
||
256 | |||
257 | /** |
||
258 | * @param string $selector |
||
259 | * @param TableNode $values |
||
260 | * |
||
261 | * @throws ElementNotFoundException |
||
262 | * @throws \Exception |
||
263 | * @throws NoSuchElement |
||
264 | * |
||
265 | * @Given /^(?:|I )select the following in "([^"]*)" hierarchical select:$/ |
||
266 | */ |
||
267 | public function setValueForHierarchicalSelect($selector, TableNode $values) |
||
310 | |||
311 | /** |
||
312 | * Check that an image was uploaded and can be viewed on the page. |
||
313 | * |
||
314 | * @throws \Exception |
||
315 | * @throws FileNotFoundException |
||
316 | * |
||
317 | * @Then /^(?:|I )should see the thumbnail$/ |
||
318 | */ |
||
319 | public function shouldSeeThumbnail() |
||
345 | |||
346 | /** |
||
347 | * @param string $option |
||
348 | * @param string $selector |
||
349 | * |
||
350 | * @Then /^(?:|I )pick "([^"]*)" from "([^"]*)"$/ |
||
351 | */ |
||
352 | public function selectFrom($option, $selector) |
||
356 | |||
357 | /** |
||
358 | * @example |
||
359 | * And pick the following: |
||
360 | * | Entity Reference | Type of new field | |
||
361 | * | Inline entity form - Multiple values | Widget for new field | |
||
362 | * |
||
363 | * @param TableNode $rows |
||
364 | * |
||
365 | * @Then /^(?:|I )pick the following:$/ |
||
366 | */ |
||
367 | public function selectFromFollowing(TableNode $rows) |
||
373 | |||
374 | /** |
||
375 | * @example |
||
376 | * And check that "Users" field has "admin" value |
||
377 | * And check that "Users" field has not "customer" value |
||
378 | * |
||
379 | * @Then /^(?:|I )check that "([^"]*)" field has(| not) "([^"]*)" value$/ |
||
380 | */ |
||
381 | public function assertTextualField($selector, $not, $expected) |
||
385 | |||
386 | /** |
||
387 | * @example |
||
388 | * And check that "User" is selected in "Apply to" select |
||
389 | * And check that "Product(s)" is not selected in "Apply to" select |
||
390 | * |
||
391 | * @Then /^(?:|I )check that "([^"]*)" is(| not) selected in "([^"]*)" select$/ |
||
392 | */ |
||
393 | public function assertSelectableField($expected, $not, $selector) |
||
397 | |||
398 | /** |
||
399 | * @example |
||
400 | * And check that "Order discount" is checked |
||
401 | * And check that "Product discount" is not checked |
||
402 | * |
||
403 | * @Then /^(?:|I )check that "([^"]*)" is(| not) checked$/ |
||
404 | */ |
||
405 | public function assertCheckableField($selector, $not) |
||
409 | |||
410 | /** |
||
411 | * @param string $date |
||
412 | * @param string $selector |
||
413 | * |
||
414 | * @Then /^(?:|I )choose "([^"]*)" in "([^"]*)" datepicker$/ |
||
415 | * @Then /^(?:|I )set the "([^"]*)" for "([^"]*)" datepicker$/ |
||
416 | */ |
||
417 | public function setDate($date, $selector) |
||
421 | |||
422 | /** |
||
423 | * @param string $selector |
||
424 | * @param string $date |
||
425 | * |
||
426 | * @Then /^(?:|I )check that "([^"]*)" datepicker contains "([^"]*)" date$/ |
||
427 | */ |
||
428 | public function isDateSelected($selector, $date) |
||
432 | |||
433 | /** |
||
434 | * @param string $date |
||
435 | * @param string $selector |
||
436 | * |
||
437 | * @Then /^(?:|I )check that "([^"]*)" is available for "([^"]*)" datepicker$/ |
||
438 | */ |
||
439 | public function isDateAvailable($date, $selector) |
||
443 | } |
||
444 |
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.