Conditions | 17 |
Paths | 12 |
Total Lines | 77 |
Code Lines | 55 |
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 |
||
27 | public function validate($value, Constraint $constraint): void |
||
28 | { |
||
29 | if (!$constraint instanceof BatchTimeAfter) { |
||
30 | throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\BatchTimeAfter'); |
||
31 | } |
||
32 | |||
33 | if (!\is_object($value)) { |
||
34 | return; |
||
35 | } |
||
36 | |||
37 | try { |
||
38 | $firstDate = $this->getFieldValue($value, $constraint->firstField); |
||
39 | } catch (NoSuchPropertyException $e) { |
||
40 | throw new InvalidArgumentException($e->getMessage()); |
||
41 | } |
||
42 | |||
43 | try { |
||
44 | $secondDate = $this->getFieldValue($value, $constraint->secondField); |
||
45 | } catch (NoSuchPropertyException $e) { |
||
46 | throw new InvalidArgumentException($e->getMessage()); |
||
47 | } |
||
48 | |||
49 | if (!$constraint->required && !$firstDate && !$secondDate) { |
||
50 | return; |
||
51 | } |
||
52 | |||
53 | if (!$firstDate && $secondDate) { |
||
54 | $this->context |
||
55 | ->buildViolation($constraint->emptyMessage) |
||
56 | ->setParameter('%emptyField%', $constraint->firstField) |
||
57 | ->setParameter('%field%', $constraint->secondField) |
||
58 | ->atPath($constraint->firstField) |
||
59 | ->addViolation(); |
||
60 | |||
61 | return; |
||
62 | } elseif ($firstDate && !$secondDate) { |
||
63 | $this->context |
||
64 | ->buildViolation($constraint->emptyMessage) |
||
65 | ->setParameter('%emptyField%', $constraint->secondField) |
||
66 | ->setParameter('%field%', $constraint->firstField) |
||
67 | ->atPath($constraint->secondField) |
||
68 | ->addViolation(); |
||
69 | |||
70 | return; |
||
71 | } |
||
72 | |||
73 | if (!$firstDate instanceof BatchTime || !$secondDate instanceof BatchTime) { |
||
74 | throw new UnexpectedTypeException($value, BatchTime::class); |
||
75 | } |
||
76 | |||
77 | if (!$firstDate->getTime()) { |
||
78 | $this->context |
||
79 | ->buildViolation($constraint->emptyMessage) |
||
80 | ->setParameter('%emptyField%', $constraint->firstField) |
||
81 | ->setParameter('%field%', $constraint->secondField) |
||
82 | ->atPath($constraint->firstField) |
||
83 | ->addViolation(); |
||
84 | |||
85 | return; |
||
86 | } elseif (!$secondDate->getTime()) { |
||
87 | $this->context |
||
88 | ->buildViolation($constraint->emptyMessage) |
||
89 | ->setParameter('%emptyField%', $constraint->secondField) |
||
90 | ->setParameter('%field%', $constraint->firstField) |
||
91 | ->atPath($constraint->secondField) |
||
92 | ->addViolation(); |
||
93 | |||
94 | return; |
||
95 | } |
||
96 | |||
97 | if ($firstDate->getSeconds() > $secondDate->getSeconds()) { |
||
98 | $this->context |
||
99 | ->buildViolation($constraint->message) |
||
100 | ->setParameter('%firstField%', $constraint->firstField) |
||
101 | ->setParameter('%secondField%', $constraint->secondField) |
||
102 | ->atPath($constraint->secondField) |
||
103 | ->addViolation(); |
||
104 | } |
||
120 |