| Conditions | 11 |
| Paths | 10 |
| Total Lines | 62 |
| Code Lines | 34 |
| 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 |
||
| 19 | public function validateField(Structure $document, $fieldName, array $params) |
||
| 20 | { |
||
| 21 | $value = $document->get($fieldName); |
||
| 22 | |||
| 23 | // check only if set |
||
| 24 | if (!$value) { |
||
| 25 | return; |
||
| 26 | } |
||
| 27 | |||
| 28 | // check if url valid |
||
| 29 | $isValidUrl = (bool) filter_var($value, FILTER_VALIDATE_URL); |
||
| 30 | if (!$isValidUrl) { |
||
| 31 | if (!isset($params['message'])) { |
||
| 32 | $params['message'] = 'Value of field "' |
||
| 33 | . $fieldName |
||
| 34 | . '" is not valid url in model ' |
||
| 35 | . get_called_class(); |
||
| 36 | } |
||
| 37 | |||
| 38 | $document->addError($fieldName, $this->getName(), $params['message']); |
||
| 39 | return; |
||
| 40 | } |
||
| 41 | |||
| 42 | // ping not required - so url is valid |
||
| 43 | if (empty($params['ping'])) { |
||
| 44 | return; |
||
| 45 | } |
||
| 46 | |||
| 47 | // ping required |
||
| 48 | $dnsRecordExists = dns_get_record(parse_url($value, PHP_URL_HOST)); |
||
| 49 | |||
| 50 | // network not allowed |
||
| 51 | if ($dnsRecordExists === false) { |
||
| 52 | throw new \RuntimeException('Error getting DNS record to validated url'); |
||
| 53 | } |
||
| 54 | |||
| 55 | // empty array - host not found |
||
| 56 | if (is_array($dnsRecordExists) && empty($dnsRecordExists)) { |
||
| 57 | if (!isset($params['message'])) { |
||
| 58 | $params['message'] = 'Value of field "' |
||
| 59 | . $fieldName |
||
| 60 | . '" is valid url but host is unreachable in model ' |
||
| 61 | . get_called_class(); |
||
| 62 | } |
||
| 63 | |||
| 64 | $document->addError($fieldName, $this->getName(), $params['message']); |
||
| 65 | return; |
||
| 66 | } |
||
| 67 | |||
| 68 | if ($this->isUrlAccessible($value)) { |
||
| 69 | return; |
||
| 70 | } |
||
| 71 | |||
| 72 | if (!isset($params['message'])) { |
||
| 73 | $params['message'] = 'Value of field "' |
||
| 74 | . $fieldName |
||
| 75 | . '" is valid url but page not found ' |
||
| 76 | . get_called_class(); |
||
| 77 | } |
||
| 78 | |||
| 79 | $document->addError($fieldName, $this->getName(), $params['message']); |
||
| 80 | } |
||
| 81 | |||
| 104 |