| Conditions | 11 |
| Paths | 6 |
| Total Lines | 36 |
| Code Lines | 23 |
| 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 |
||
| 97 | public function validate($validator) { |
||
| 98 | $valid = false; |
||
| 99 | $source = $this->getSourceAsArray(); |
||
| 100 | $disabled = $this->getDisabledItems(); |
||
| 101 | |||
| 102 | if ($this->value) { |
||
| 103 | foreach ($source as $value => $title) { |
||
| 104 | if (is_array($title) && array_key_exists($this->value, $title)) { |
||
| 105 | // Check that the set value is not in the list of disabled items |
||
| 106 | if (!isset($disabled[$value]) || !in_array($this->value, $disabled[$value])) { |
||
| 107 | $valid = true; |
||
| 108 | } |
||
| 109 | // Check that the value matches and is not disabled |
||
| 110 | } elseif($this->value == $value && !in_array($this->value, $disabled)) { |
||
| 111 | $valid = true; |
||
| 112 | } |
||
| 113 | } |
||
| 114 | } elseif ($this->getHasEmptyDefault()) { |
||
| 115 | $valid = true; |
||
| 116 | } |
||
| 117 | |||
| 118 | if (!$valid) { |
||
| 119 | $validator->validationError( |
||
| 120 | $this->name, |
||
| 121 | _t( |
||
| 122 | 'DropdownField.SOURCE_VALIDATION', |
||
| 123 | "Please select a value within the list provided. {value} is not a valid option", |
||
| 124 | array('value' => $this->value) |
||
| 125 | ), |
||
| 126 | "validation" |
||
| 127 | ); |
||
| 128 | return false; |
||
| 129 | } |
||
| 130 | |||
| 131 | return true; |
||
| 132 | } |
||
| 133 | |||
| 135 |