| Conditions | 10 |
| Paths | 16 |
| Total Lines | 28 |
| Code Lines | 14 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 38 | public function setValue($value): void |
||
| 39 | { |
||
| 40 | if ($this->is('radio')) { |
||
| 41 | $value = $value ?? $this->attr('value'); |
||
| 42 | } |
||
| 43 | |||
| 44 | if (null === $value) { |
||
| 45 | throw new \InvalidArgumentException('Value required for select form fields.'); |
||
| 46 | } |
||
| 47 | |||
| 48 | if (\is_array($value) && !$this->isMultiple()) { |
||
| 49 | throw new \InvalidArgumentException('Value for a single select form field cannot be an array.'); |
||
| 50 | } |
||
| 51 | |||
| 52 | if (\is_array($value) && $this->isMultiple() && $this->dom->inner() instanceof PantherCrawler) { |
||
| 53 | // todo have a separate PantherCrawler to avoid the "inner" check above |
||
| 54 | // with panther, unselect all before setting (otherwise existing selections will remain) |
||
| 55 | (new WebDriverSelect($this->dom->getElement(0)))->deselectAll(); |
||
| 56 | } |
||
| 57 | |||
| 58 | try { |
||
| 59 | $this->inner->setValue($value); |
||
| 60 | } catch (NoSuchElementException $e) { |
||
| 61 | // try selecting by visible text |
||
| 62 | $select = new WebDriverSelect($this->dom->getElement(0)); |
||
| 63 | |||
| 64 | foreach ((array) $value as $item) { |
||
| 65 | $select->selectByVisibleText($item); |
||
| 66 | } |
||
| 78 |