| Conditions | 10 |
| Paths | 18 |
| Total Lines | 38 |
| Code Lines | 21 |
| 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 |
||
| 17 | public function php($data) |
||
| 18 | { |
||
| 19 | $valid = true; |
||
| 20 | $fields = $this->form->Fields(); |
||
| 21 | |||
| 22 | // Check field validation |
||
| 23 | foreach ($fields as $field) { |
||
| 24 | $valid = ($field->validate($this) && $valid); |
||
| 25 | } |
||
| 26 | if (!$valid) { |
||
| 27 | return $valid; |
||
| 28 | } |
||
| 29 | $hasSelectField = $fields->dataFieldByName('Campaign') !== null; |
||
| 30 | $hasNewField = $fields->dataFieldByName('NewTitle') !== null; |
||
| 31 | |||
| 32 | // Guess best field to display error against |
||
| 33 | if ((!empty($data['AddNewSelect']) || !$hasSelectField) && $hasNewField) { |
||
| 34 | // Error: - Enter a title, either if the checkbox is checked, or no option dropdown is present |
||
| 35 | // but only if NewTitle is set and empty |
||
| 36 | if (empty($data['NewTitle'])) { |
||
| 37 | $valid = false; |
||
| 38 | $this->result->addFieldError( |
||
| 39 | 'NewTitle', |
||
| 40 | _t(__CLASS__ . '.NEW_TITLE_EMPTY', 'Please enter a title for the new campaign') |
||
| 41 | ); |
||
| 42 | } |
||
| 43 | } elseif ($hasSelectField) { |
||
| 44 | // Error: - Campaign dropdown is posted but empty |
||
| 45 | if (empty($data['Campaign'])) { |
||
| 46 | $valid = false; |
||
| 47 | $this->result->addFieldError( |
||
| 48 | 'Campaign', |
||
| 49 | _t(__CLASS__ . '.SELECT_CAMPAIGN_EMPTY', 'Please select a campaign') |
||
| 50 | ); |
||
| 51 | } |
||
| 52 | } |
||
| 53 | |||
| 54 | return $valid; |
||
| 55 | } |
||
| 57 |