| 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 |
||
| 61 | public function setValue($value): void |
||
| 62 | { |
||
| 63 | if ($this->is('radio')) { |
||
| 64 | $value = $value ?? $this->attr('value'); |
||
| 65 | } |
||
| 66 | |||
| 67 | if (null === $value) { |
||
| 68 | throw new \InvalidArgumentException('Value required for select form fields.'); |
||
| 69 | } |
||
| 70 | |||
| 71 | if (\is_array($value) && !$this->isMultiple()) { |
||
| 72 | throw new \InvalidArgumentException('Value for a single select form field cannot be an array.'); |
||
| 73 | } |
||
| 74 | |||
| 75 | if (\is_array($value) && $this->isMultiple() && $this->dom->crawler() instanceof PantherCrawler) { |
||
| 76 | // todo have a separate PantherCrawler to avoid the "inner" check above |
||
| 77 | // with panther, unselect all before setting (otherwise existing selections will remain) |
||
| 78 | (new WebDriverSelect($this->dom->getElement(0)))->deselectAll(); |
||
| 79 | } |
||
| 80 | |||
| 81 | try { |
||
| 82 | $this->inner->setValue($value); |
||
| 83 | } catch (NoSuchElementException $e) { |
||
| 84 | // try selecting by visible text |
||
| 85 | $select = new WebDriverSelect($this->dom->getElement(0)); |
||
| 86 | |||
| 87 | foreach ((array) $value as $item) { |
||
| 88 | $select->selectByVisibleText($item); |
||
| 89 | } |
||
| 148 |