Conditions | 13 |
Paths | 12 |
Total Lines | 53 |
Lines | 22 |
Ratio | 41.51 % |
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 |
||
34 | public function validate($value, Constraint $constraint) |
||
35 | { |
||
36 | if (!$constraint instanceof Email) { |
||
37 | throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Email'); |
||
38 | } |
||
39 | |||
40 | if ($constraint->strict) { |
||
41 | $baseEmailValidator = new BaseEmailValidator($constraint->strict); |
||
42 | $baseEmailValidator->initialize($this->context); |
||
43 | $baseEmailValidator->validate($value, $constraint); |
||
44 | |||
45 | return; |
||
46 | } |
||
47 | |||
48 | if (null === $value || '' === $value) { |
||
49 | return; |
||
50 | } |
||
51 | |||
52 | if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) { |
||
53 | throw new UnexpectedTypeException($value, 'string'); |
||
54 | } |
||
55 | |||
56 | $value = (string) $value; |
||
57 | |||
58 | $noRfcValidator = new NoRFCEmailValidator(); |
||
59 | View Code Duplication | if (!$noRfcValidator->isValid($value)) { |
|
60 | $this->context->buildViolation($constraint->message) |
||
61 | ->setParameter('{{ value }}', $this->formatValue($value)) |
||
62 | ->setCode(Email::INVALID_FORMAT_ERROR) |
||
63 | ->addViolation(); |
||
64 | } |
||
65 | |||
66 | $host = (string) substr($value, strrpos($value, '@') + 1); |
||
67 | |||
68 | // Check for host DNS resource records |
||
69 | View Code Duplication | if ($constraint->checkMX) { |
|
70 | if (!$this->checkMX($host)) { |
||
71 | $this->context->buildViolation($constraint->message) |
||
72 | ->setParameter('{{ value }}', $this->formatValue($value)) |
||
73 | ->setCode(Email::MX_CHECK_FAILED_ERROR) |
||
74 | ->addViolation(); |
||
75 | } |
||
76 | |||
77 | return; |
||
78 | } |
||
79 | |||
80 | View Code Duplication | if ($constraint->checkHost && !$this->checkHost($host)) { |
|
81 | $this->context->buildViolation($constraint->message) |
||
82 | ->setParameter('{{ value }}', $this->formatValue($value)) |
||
83 | ->setCode(Email::HOST_CHECK_FAILED_ERROR) |
||
84 | ->addViolation(); |
||
85 | } |
||
86 | } |
||
87 | |||
113 |