Conditions | 19 |
Paths | 27 |
Total Lines | 77 |
Lines | 29 |
Ratio | 37.66 % |
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 |
||
28 | public function validate($value, Constraint $constraint) |
||
29 | { |
||
30 | if (!$constraint instanceof Email) { |
||
31 | throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Email'); |
||
32 | } |
||
33 | |||
34 | if (null === $value || '' === $value) { |
||
35 | return; |
||
36 | } |
||
37 | |||
38 | if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) { |
||
39 | throw new UnexpectedTypeException($value, 'string'); |
||
40 | } |
||
41 | |||
42 | $value = (string)$value; |
||
43 | |||
44 | if (null === $constraint->strict) { |
||
45 | $constraint->strict = false; |
||
46 | } |
||
47 | |||
48 | if ($constraint->strict) { |
||
49 | if (!class_exists('\Egulias\EmailValidator\EmailValidator')) { |
||
50 | throw new RuntimeException('Strict email validation requires egulias/email-validator ~1.2|~2.0'); |
||
51 | } |
||
52 | |||
53 | $strictValidator = new \Egulias\EmailValidator\EmailValidator(); |
||
54 | |||
55 | if (interface_exists(EmailValidation::class) && !$strictValidator->isValid($value, new NoRFCWarningsValidation())) { |
||
56 | $this->context->buildViolation($constraint->message) |
||
57 | ->setParameter('{{ value }}', $this->formatValue($value)) |
||
58 | ->setCode(Email::INVALID_FORMAT_ERROR) |
||
59 | ->addViolation(); |
||
60 | |||
61 | return; |
||
62 | } elseif (!interface_exists(EmailValidation::class) && !$strictValidator->isValid($value, false, true)) { |
||
63 | $this->context->buildViolation($constraint->message) |
||
64 | ->setParameter('{{ value }}', $this->formatValue($value)) |
||
65 | ->setCode(Email::INVALID_FORMAT_ERROR) |
||
66 | ->addViolation(); |
||
67 | |||
68 | return; |
||
69 | } |
||
70 | View Code Duplication | } else { |
|
71 | |||
72 | $validator = new NoRFCEmailValidator(); |
||
73 | |||
74 | if (!$validator->isValid($value)) { |
||
75 | $this->context->buildViolation($constraint->message) |
||
76 | ->setParameter('{{ value }}', $this->formatValue($value)) |
||
77 | ->setCode(Email::INVALID_FORMAT_ERROR) |
||
78 | ->addViolation(); |
||
79 | |||
80 | return; |
||
81 | } |
||
82 | } |
||
83 | |||
84 | $host = (string)substr($value, strrpos($value, '@') + 1); |
||
85 | |||
86 | // Check for host DNS resource records |
||
87 | View Code Duplication | if ($constraint->checkMX) { |
|
88 | if (!$this->checkMX($host)) { |
||
89 | $this->context->buildViolation($constraint->message) |
||
90 | ->setParameter('{{ value }}', $this->formatValue($value)) |
||
91 | ->setCode(Email::MX_CHECK_FAILED_ERROR) |
||
92 | ->addViolation(); |
||
93 | } |
||
94 | |||
95 | return; |
||
96 | } |
||
97 | |||
98 | View Code Duplication | if ($constraint->checkHost && !$this->checkHost($host)) { |
|
99 | $this->context->buildViolation($constraint->message) |
||
100 | ->setParameter('{{ value }}', $this->formatValue($value)) |
||
101 | ->setCode(Email::HOST_CHECK_FAILED_ERROR) |
||
102 | ->addViolation(); |
||
103 | } |
||
104 | } |
||
105 | |||
131 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: