| Conditions | 11 |
| Paths | 14 |
| Total Lines | 63 |
| Code Lines | 36 |
| Lines | 47 |
| Ratio | 74.6 % |
| 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 |
||
| 19 | public function validateField(Structure $document, $fieldName, array $params) |
||
| 20 | { |
||
| 21 | $value = $document->get($fieldName); |
||
| 22 | |||
| 23 | if (!$value) { |
||
| 24 | return; |
||
| 25 | } |
||
| 26 | |||
| 27 | $length = mb_strlen($value); |
||
| 28 | |||
| 29 | // check if field is of specified length |
||
| 30 | View Code Duplication | if (isset($params['is'])) { |
|
|
|
|||
| 31 | if ($length === $params['is']) { |
||
| 32 | return; |
||
| 33 | } |
||
| 34 | |||
| 35 | if (!isset($params['message'])) { |
||
| 36 | $params['message'] = sprintf( |
||
| 37 | 'Field "%s" length not equal to %s in model %s', |
||
| 38 | $fieldName, |
||
| 39 | $params['is'], |
||
| 40 | get_called_class() |
||
| 41 | ); |
||
| 42 | } |
||
| 43 | |||
| 44 | $document->addError($fieldName, $this->getName(), $params['message']); |
||
| 45 | return; |
||
| 46 | } |
||
| 47 | |||
| 48 | // check if fied is shorter than required |
||
| 49 | View Code Duplication | if (isset($params['min'])) { |
|
| 50 | if ($length < $params['min']) { |
||
| 51 | if (!isset($params['messageTooShort'])) { |
||
| 52 | $params['messageTooShort'] = sprintf( |
||
| 53 | 'Field "%s" length is shorter than %s in model %s', |
||
| 54 | $fieldName, |
||
| 55 | $params['min'], |
||
| 56 | get_called_class() |
||
| 57 | ); |
||
| 58 | } |
||
| 59 | |||
| 60 | $document->addError($fieldName, $this->getName(), $params['messageTooShort']); |
||
| 61 | return; |
||
| 62 | } |
||
| 63 | } |
||
| 64 | |||
| 65 | // check if fied is longer than required |
||
| 66 | View Code Duplication | if (isset($params['max'])) { |
|
| 67 | if ($length > $params['max']) { |
||
| 68 | if (!isset($params['messageTooLong'])) { |
||
| 69 | $params['messageTooLong'] = sprintf( |
||
| 70 | 'Field "%s" length is longer than %s in model %s', |
||
| 71 | $fieldName, |
||
| 72 | $params['max'], |
||
| 73 | get_called_class() |
||
| 74 | ); |
||
| 75 | } |
||
| 76 | |||
| 77 | $document->addError($fieldName, $this->getName(), $params['messageTooLong']); |
||
| 78 | return; |
||
| 79 | } |
||
| 80 | } |
||
| 81 | } |
||
| 82 | } |
||
| 83 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.