| Conditions | 18 |
| Paths | 189 |
| Total Lines | 65 |
| Code Lines | 36 |
| 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 |
||
| 84 | public function php($data) |
||
| 85 | { |
||
| 86 | $valid = true; |
||
| 87 | $fields = $this->form->Fields(); |
||
| 88 | |||
| 89 | foreach ($fields as $field) { |
||
| 90 | $valid = ($field->validate($this) && $valid); |
||
| 91 | } |
||
| 92 | |||
| 93 | if (!$this->required) { |
||
| 94 | return $valid; |
||
| 95 | } |
||
| 96 | |||
| 97 | foreach ($this->required as $fieldName) { |
||
| 98 | if (!$fieldName) { |
||
| 99 | continue; |
||
| 100 | } |
||
| 101 | |||
| 102 | if ($fieldName instanceof FormField) { |
||
| 103 | $formField = $fieldName; |
||
| 104 | $fieldName = $fieldName->getName(); |
||
| 105 | } else { |
||
| 106 | $formField = $fields->dataFieldByName($fieldName); |
||
| 107 | } |
||
| 108 | |||
| 109 | // submitted data for file upload fields come back as an array |
||
| 110 | $value = isset($data[$fieldName]) ? $data[$fieldName] : null; |
||
| 111 | |||
| 112 | if (is_array($value)) { |
||
| 113 | if ($formField instanceof FileField && isset($value['error']) && $value['error']) { |
||
| 114 | $error = true; |
||
| 115 | } else { |
||
| 116 | $error = (count($value)) ? false : true; |
||
| 117 | } |
||
| 118 | } else { |
||
| 119 | // assume a string or integer |
||
| 120 | $error = (strlen($value)) ? false : true; |
||
| 121 | } |
||
| 122 | |||
| 123 | if ($formField && $error) { |
||
| 124 | $errorMessage = _t( |
||
| 125 | 'SilverStripe\\Forms\\Form.FIELDISREQUIRED', |
||
| 126 | '{name} is required', |
||
| 127 | [ |
||
| 128 | 'name' => strip_tags( |
||
| 129 | '"' . ($formField->Title() ? $formField->Title() : $fieldName) . '"' |
||
| 130 | ) |
||
| 131 | ] |
||
| 132 | ); |
||
| 133 | |||
| 134 | if ($msg = $formField->getCustomValidationMessage()) { |
||
| 135 | $errorMessage = $msg; |
||
| 136 | } |
||
| 137 | |||
| 138 | $this->validationError( |
||
| 139 | $fieldName, |
||
| 140 | $errorMessage, |
||
| 141 | "required" |
||
| 142 | ); |
||
| 143 | |||
| 144 | $valid = false; |
||
| 145 | } |
||
| 146 | } |
||
| 147 | |||
| 148 | return $valid; |
||
| 149 | } |
||
| 227 |