Conditions | 23 |
Paths | 18 |
Total Lines | 68 |
Code Lines | 39 |
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 |
||
17 | public static function normalizeConstraints($constraints) { |
||
18 | $new_constraints = array(); |
||
19 | |||
20 | foreach (array_reverse($constraints) as $constraint) { |
||
21 | $start_date = $constraint->getStartDate(); |
||
22 | $end_date = $constraint->getEndDate(); |
||
23 | |||
24 | if (get_class($constraint) == 'Roomify\Bat\Constraint\MinMaxDaysConstraint') { |
||
25 | $split_constraint = NULL; |
||
26 | |||
27 | if (!empty($new_constraints)) { |
||
28 | foreach ($new_constraints as $new_constraint) { |
||
29 | if (get_class($new_constraint) == 'Roomify\Bat\Constraint\MinMaxDaysConstraint') { |
||
30 | $new_start_date = $new_constraint->getStartDate(); |
||
31 | $new_end_date = $new_constraint->getEndDate(); |
||
32 | |||
33 | if ($constraint->getMinDays() && $new_constraint->getMinDays() || |
||
34 | ($constraint->getMaxDays() && $new_constraint->getMaxDays())) { |
||
35 | if ($start_date >= $new_start_date && $start_date <= $new_end_date) { |
||
36 | $constraint->setStartDate(clone($new_end_date)->add(new \DateInterval('P1D'))); |
||
37 | } |
||
38 | elseif ($end_date >= $new_start_date && $end_date <= $new_end_date) { |
||
39 | $constraint->setEndDate(clone($new_start_date)->sub(new \DateInterval('P1D'))); |
||
40 | } |
||
41 | elseif ($start_date < $new_start_date && $end_date > $new_end_date) { |
||
42 | if ($constraint->getEndDate() > $new_start_date) { |
||
43 | $constraint->setEndDate(clone($new_start_date)->sub(new \DateInterval('P1D'))); |
||
44 | } |
||
45 | |||
46 | if ($split_constraint == NULL) { |
||
47 | $split_start_date = clone($new_end_date)->add(new \DateInterval('P1D')); |
||
48 | $split_end_date = $end_date; |
||
49 | |||
50 | $split_constraint = new MinMaxDaysConstraint($constraint->getUnits(), $constraint->getMinDays(), $constraint->getMaxDays(), $split_start_date, $split_end_date , $constraint->getCheckinDay()); |
||
51 | } |
||
52 | else { |
||
53 | $split_start_date = $split_constraint->getStartDate(); |
||
54 | $split_end_date = $split_constraint->getEndDate(); |
||
55 | |||
56 | if ($split_start_date < $new_end_date) { |
||
57 | $split_constraint->setStartDate(clone($new_end_date)->add(new \DateInterval('P1D'))); |
||
58 | } |
||
59 | if ($split_end_date < $new_start_date) { |
||
60 | $split_constraint->setEndDate(clone($new_start_date)->sub(new \DateInterval('P1D'))); |
||
61 | } |
||
62 | } |
||
63 | } |
||
64 | } |
||
65 | } |
||
66 | } |
||
67 | } |
||
68 | |||
69 | if ($split_constraint != NULL) { |
||
70 | $new_constraints[] = $split_constraint; |
||
71 | } |
||
72 | } |
||
73 | |||
74 | $new_constraints[] = $constraint; |
||
75 | } |
||
76 | |||
77 | foreach ($new_constraints as $i => $constraint) { |
||
78 | if ($constraint->getStartDate() > $constraint->getEndDate()) { |
||
79 | unset($new_constraints[$i]); |
||
80 | } |
||
81 | } |
||
82 | |||
83 | return $new_constraints; |
||
84 | } |
||
85 | |||
87 |