| Conditions | 10 |
| Paths | 19 |
| Total Lines | 33 |
| Code Lines | 18 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 26 | public function hashToFieldValue($fieldValue, array $context = array()) |
||
| 27 | { |
||
| 28 | if ($fieldValue === null) { |
||
| 29 | return new SelectionValue(); |
||
| 30 | } |
||
| 31 | |||
| 32 | // allow user to pass in a single value |
||
| 33 | if (is_string($fieldValue) || is_int($fieldValue)) { |
||
| 34 | $fieldValue = array($fieldValue); |
||
| 35 | } |
||
| 36 | |||
| 37 | // allow user to pass in selection values by name |
||
| 38 | $fieldSettings = null; |
||
| 39 | foreach($fieldValue as $key => $val) { |
||
|
|
|||
| 40 | if (is_string($val)) { |
||
| 41 | if (ctype_digit($val)) { |
||
| 42 | $fieldValue[$key] = (int)$val; |
||
| 43 | } else { |
||
| 44 | if ($fieldSettings === null) { |
||
| 45 | $fieldSettings = $this->loadFieldSettings($context['contentTypeIdentifier'], $context['fieldIdentifier']); |
||
| 46 | } |
||
| 47 | foreach($fieldSettings['options'] as $pos => $name) { |
||
| 48 | if ($name === $val) { |
||
| 49 | $fieldValue[$key] = $pos; |
||
| 50 | break; |
||
| 51 | } |
||
| 52 | } |
||
| 53 | } |
||
| 54 | } |
||
| 55 | } |
||
| 56 | |||
| 57 | return new SelectionValue($fieldValue); |
||
| 58 | } |
||
| 59 | |||
| 88 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.