Conditions | 14 |
Paths | 10 |
Total Lines | 52 |
Code Lines | 36 |
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 |
||
26 | public function validate($value, Constraint $constraint): void |
||
27 | { |
||
28 | if (!$constraint instanceof DateAfter) { |
||
29 | throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\DateAfter'); |
||
30 | } |
||
31 | |||
32 | if (!\is_object($value)) { |
||
33 | return; |
||
34 | } |
||
35 | |||
36 | try { |
||
37 | $firstDate = $this->getFieldValue($value, $constraint->firstField); |
||
38 | $secondDate = $this->getFieldValue($value, $constraint->secondField); |
||
39 | } catch (NoSuchPropertyException $e) { |
||
40 | throw new InvalidArgumentException($e->getMessage()); |
||
41 | } |
||
42 | |||
43 | if (!$constraint->required && !$firstDate && !$secondDate) { |
||
44 | return; |
||
45 | } |
||
46 | |||
47 | if (!$firstDate && $secondDate) { |
||
48 | $this->context |
||
49 | ->buildViolation($constraint->emptyMessage) |
||
50 | ->setParameter('%emptyField%', $constraint->firstField) |
||
51 | ->setParameter('%field%', $constraint->secondField) |
||
52 | ->atPath($constraint->firstField) |
||
53 | ->addViolation(); |
||
54 | |||
55 | return; |
||
56 | } elseif ($firstDate && !$secondDate) { |
||
57 | $this->context |
||
58 | ->buildViolation($constraint->emptyMessage) |
||
59 | ->setParameter('%emptyField%', $constraint->secondField) |
||
60 | ->setParameter('%field%', $constraint->firstField) |
||
61 | ->atPath($constraint->secondField) |
||
62 | ->addViolation(); |
||
63 | |||
64 | return; |
||
65 | } |
||
66 | |||
67 | if (!$firstDate instanceof \DateTime || !$secondDate instanceof \DateTime) { |
||
68 | throw new UnexpectedTypeException($value, \DateTime::class); |
||
69 | } |
||
70 | |||
71 | if ($firstDate > $secondDate) { |
||
72 | $this->context |
||
73 | ->buildViolation($constraint->message) |
||
74 | ->setParameter('%firstField%', $constraint->firstField) |
||
75 | ->setParameter('%secondField%', $constraint->secondField) |
||
76 | ->atPath($constraint->secondField) |
||
77 | ->addViolation(); |
||
78 | } |
||
94 |