Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like IntervalComparator often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use IntervalComparator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class IntervalComparator |
||
17 | { |
||
18 | /** |
||
19 | * @var ComparableIntervalInterface |
||
20 | */ |
||
21 | private $interval; |
||
22 | |||
23 | /** |
||
24 | * @param ComparableIntervalInterface $interval |
||
25 | */ |
||
26 | 63 | public function __construct(ComparableIntervalInterface $interval) |
|
30 | |||
31 | /** |
||
32 | * Checks if this Interval is equal to the specified interval. |
||
33 | * |
||
34 | * @param ComparableIntervalInterface $interval |
||
35 | * |
||
36 | * @return bool |
||
37 | */ |
||
38 | public function equal(ComparableIntervalInterface $interval) |
||
39 | { |
||
40 | return |
||
41 | $this->interval->startPoint()->eq($interval->startPoint()) && |
||
|
|||
42 | $this->interval->endPoint()->eq($interval->endPoint()) && |
||
43 | $this->interval->type()->equal($interval->type()) |
||
44 | ; |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * Does this interval contain the specified point. |
||
49 | * |
||
50 | * @param IntervalPointInterface $point |
||
51 | * |
||
52 | * @return bool |
||
53 | */ |
||
54 | 7 | public function contains(IntervalPointInterface $point) |
|
66 | |||
67 | /** |
||
68 | * Does this interval intersect the specified interval. |
||
69 | * |
||
70 | * @param ComparableIntervalInterface $interval |
||
71 | * |
||
72 | * @return bool |
||
73 | */ |
||
74 | 8 | public function intersects(ComparableIntervalInterface $interval) |
|
75 | { |
||
76 | if ( |
||
77 | 8 | $this->interval->startPoint()->gt($interval->endPoint()) || |
|
78 | 8 | $this->interval->endPoint()->lt($interval->startPoint()) |
|
79 | ) { |
||
80 | 2 | return false; |
|
81 | } |
||
82 | |||
83 | 6 | View Code Duplication | if ($this->interval->startPoint()->eq($interval->endPoint())) { |
84 | 3 | return !$this->interval->type()->startExcluded() && !$interval->type()->endExcluded(); |
|
85 | } |
||
86 | |||
87 | 3 | View Code Duplication | if ($this->interval->endPoint()->eq($interval->startPoint())) { |
88 | 3 | return !$this->interval->type()->endExcluded() && !$interval->type()->startExcluded(); |
|
89 | } |
||
90 | |||
91 | return true; |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * Gets the intersection between this interval and another interval. |
||
96 | * |
||
97 | * @param ComparableIntervalInterface $interval |
||
98 | * |
||
99 | * @return ComparableIntervalInterface|null |
||
100 | */ |
||
101 | 12 | public function intersection(ComparableIntervalInterface $interval) |
|
102 | { |
||
103 | // intervals is not intersect or impossible create interval from one point |
||
104 | if ( |
||
105 | 12 | $this->interval->startPoint()->gte($interval->endPoint()) || |
|
106 | 12 | $this->interval->endPoint()->lte($interval->startPoint()) |
|
107 | ) { |
||
108 | // ignore closed intervals: |
||
109 | // [a, b] | [b, c] = [b, b] |
||
110 | 4 | return null; |
|
111 | } |
||
112 | |||
113 | 8 | $type = IntervalType::TYPE_CLOSED; |
|
114 | |||
115 | 8 | View Code Duplication | if ($this->interval->startPoint()->lt($interval->startPoint())) { |
116 | 4 | $start = $interval->startPoint(); |
|
117 | 4 | if ($interval->type()->startExcluded()) { |
|
118 | 4 | $type |= IntervalType::TYPE_START_EXCLUDED; |
|
119 | } |
||
120 | } else { |
||
121 | 4 | $start = $this->interval->startPoint(); |
|
122 | 4 | if ($this->interval->type()->startExcluded()) { |
|
123 | 2 | $type |= IntervalType::TYPE_START_EXCLUDED; |
|
124 | } |
||
125 | } |
||
126 | |||
127 | 8 | View Code Duplication | if ($this->interval->endPoint()->gt($interval->endPoint())) { |
128 | 4 | $end = $interval->endPoint(); |
|
129 | 4 | if ($interval->type()->endExcluded()) { |
|
130 | 4 | $type |= IntervalType::TYPE_END_EXCLUDED; |
|
131 | } |
||
132 | } else { |
||
133 | 4 | $end = $this->interval->endPoint(); |
|
134 | 4 | if ($this->interval->type()->endExcluded()) { |
|
135 | 2 | $type |= IntervalType::TYPE_END_EXCLUDED; |
|
136 | } |
||
137 | } |
||
138 | |||
139 | 8 | return $this->interval |
|
140 | 8 | ->withStart($start) |
|
141 | 8 | ->withEnd($end) |
|
142 | 8 | ->withType(IntervalType::create($type)); |
|
143 | } |
||
144 | |||
145 | /** |
||
146 | * Gets the covered interval between this Interval and another interval. |
||
147 | * |
||
148 | * @param ComparableIntervalInterface $interval |
||
149 | * |
||
150 | * @return ComparableIntervalInterface |
||
151 | */ |
||
152 | public function cover(ComparableIntervalInterface $interval) |
||
153 | { |
||
154 | $type = IntervalType::TYPE_CLOSED; |
||
155 | |||
156 | View Code Duplication | if ($this->interval->startPoint()->lt($interval->startPoint())) { |
|
157 | $start = $this->interval->startPoint(); |
||
158 | if ($this->interval->type()->startExcluded()) { |
||
159 | $type |= IntervalType::TYPE_START_EXCLUDED; |
||
160 | } |
||
161 | } else { |
||
162 | $start = $interval->startPoint(); |
||
163 | if ($interval->type()->startExcluded()) { |
||
164 | $type |= IntervalType::TYPE_START_EXCLUDED; |
||
165 | } |
||
166 | } |
||
167 | |||
168 | View Code Duplication | if ($this->interval->endPoint()->gt($interval->endPoint())) { |
|
169 | $end = $this->interval->endPoint(); |
||
170 | if ($this->interval->type()->endExcluded()) { |
||
171 | $type |= IntervalType::TYPE_END_EXCLUDED; |
||
172 | } |
||
173 | } else { |
||
174 | $end = $interval->endPoint(); |
||
175 | if ($interval->type()->endExcluded()) { |
||
176 | $type |= IntervalType::TYPE_END_EXCLUDED; |
||
177 | } |
||
178 | } |
||
179 | |||
180 | return $this->interval |
||
181 | ->withStart($start) |
||
182 | ->withEnd($end) |
||
183 | ->withType(IntervalType::create($type)); |
||
184 | } |
||
185 | |||
186 | /** |
||
187 | * Gets the gap between this interval and another interval. |
||
188 | * |
||
189 | * @param ComparableIntervalInterface $interval |
||
190 | * |
||
191 | * @return ComparableIntervalInterface|null |
||
192 | */ |
||
193 | public function gap(ComparableIntervalInterface $interval) |
||
194 | { |
||
195 | View Code Duplication | if ($this->interval->startPoint()->gt($interval->endPoint())) { |
|
196 | $type = IntervalType::TYPE_CLOSED; |
||
197 | |||
198 | if (!$interval->type()->endExcluded()) { // invert exclude |
||
199 | $type |= IntervalType::TYPE_START_EXCLUDED; |
||
200 | } |
||
201 | |||
202 | if (!$this->interval->type()->startExcluded()) { // invert exclude |
||
203 | $type |= IntervalType::TYPE_END_EXCLUDED; |
||
204 | } |
||
205 | |||
206 | return $this->interval |
||
207 | ->withStart($interval->endPoint()) |
||
208 | ->withEnd($this->interval->startPoint()) |
||
209 | ->withType(IntervalType::create($type)); |
||
210 | } |
||
211 | |||
212 | View Code Duplication | if ($interval->startPoint()->gt($this->interval->endPoint())) { |
|
213 | $type = IntervalType::TYPE_CLOSED; |
||
214 | |||
215 | if (!$this->interval->type()->endExcluded()) { // invert exclude |
||
216 | $type |= IntervalType::TYPE_START_EXCLUDED; |
||
217 | } |
||
218 | |||
219 | if (!$interval->type()->startExcluded()) { // invert exclude |
||
220 | $type |= IntervalType::TYPE_END_EXCLUDED; |
||
221 | } |
||
222 | |||
223 | return $this->interval |
||
224 | ->withStart($this->interval->endPoint()) |
||
225 | ->withEnd($interval->startPoint()) |
||
226 | ->withType(IntervalType::create($type)); |
||
227 | } |
||
228 | |||
229 | return null; // no gap |
||
230 | } |
||
231 | |||
232 | /** |
||
233 | * Does this interval abuts with the interval specified. |
||
234 | * |
||
235 | * @param ComparableIntervalInterface $interval |
||
236 | * |
||
237 | * @return bool |
||
238 | */ |
||
239 | public function abuts(ComparableIntervalInterface $interval) |
||
240 | { |
||
241 | return |
||
242 | $interval->endPoint()->eq($this->interval->startPoint()) || |
||
243 | $this->interval->endPoint()->eq($interval->startPoint()) |
||
244 | ; |
||
245 | } |
||
246 | |||
247 | /** |
||
248 | * Joins the interval between the adjacent. |
||
249 | * |
||
250 | * @param ComparableIntervalInterface $interval |
||
251 | * |
||
252 | * @return ComparableIntervalInterface|null |
||
253 | */ |
||
254 | public function join(ComparableIntervalInterface $interval) |
||
262 | |||
263 | /** |
||
264 | * Gets the union between this interval and another interval. |
||
265 | * |
||
266 | * @param ComparableIntervalInterface $interval |
||
267 | * |
||
268 | * @return ComparableIntervalInterface|null |
||
269 | */ |
||
270 | public function union(ComparableIntervalInterface $interval) |
||
278 | |||
279 | /** |
||
280 | * The point is before the interval. |
||
281 | * |
||
282 | * @param IntervalPointInterface $point |
||
283 | * |
||
284 | * @return bool |
||
285 | */ |
||
286 | public function before(IntervalPointInterface $point) |
||
294 | |||
295 | /** |
||
296 | * The point is after the interval. |
||
297 | * |
||
298 | * @param IntervalPointInterface $point |
||
299 | * |
||
300 | * @return bool |
||
301 | */ |
||
302 | public function after(IntervalPointInterface $point) |
||
310 | } |
||
311 |
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: