Complex classes like Real 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 Real, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class Real implements NumberInterface, MathAwareInterface |
||
11 | { |
||
12 | protected $value; |
||
13 | |||
14 | /** |
||
15 | * Returns a Real object given a PHP native float as parameter. |
||
16 | * |
||
17 | * @param float $number |
||
|
|||
18 | * |
||
19 | * @return static |
||
20 | */ |
||
21 | 5 | public static function fromNative() |
|
27 | |||
28 | /** |
||
29 | * Returns a Real object given a PHP native float as parameter. |
||
30 | * |
||
31 | * @param float $number |
||
32 | */ |
||
33 | 149 | public function __construct($value) |
|
43 | |||
44 | /** |
||
45 | * Returns the native value of the real number |
||
46 | * |
||
47 | * @return float |
||
48 | */ |
||
49 | 140 | public function toNative() |
|
53 | |||
54 | /** |
||
55 | * Tells whether two Real are equal by comparing their values |
||
56 | * |
||
57 | * @param ValueObjectInterface $real |
||
58 | * |
||
59 | * @return bool |
||
60 | */ |
||
61 | 15 | public function sameValueAs(ValueObjectInterface $real) |
|
69 | |||
70 | /** |
||
71 | * Returns the integer part of the Real number as a Integer |
||
72 | * |
||
73 | * @param RoundingMode $rounding_mode Rounding mode of the conversion. Defaults to RoundingMode::HALF_UP. |
||
74 | * |
||
75 | * @return Integer |
||
76 | */ |
||
77 | 2 | public function toInteger(RoundingMode $rounding_mode = NULL) |
|
89 | |||
90 | /** |
||
91 | * Returns the absolute integer part of the Real number as a Natural |
||
92 | * |
||
93 | * @param RoundingMode $rounding_mode Rounding mode of the conversion. Defaults to RoundingMode::HALF_UP. |
||
94 | * |
||
95 | * @return Natural |
||
96 | */ |
||
97 | 1 | public function toNatural(RoundingMode $rounding_mode = NULL) |
|
105 | |||
106 | /** |
||
107 | * Returns the string representation of the real value |
||
108 | * |
||
109 | * @return string |
||
110 | */ |
||
111 | 26 | public function __toString() |
|
115 | |||
116 | static public function pi() |
||
120 | |||
121 | /** |
||
122 | * @param \ValueObjects\Number\MathAwareInterface $number |
||
123 | * |
||
124 | * @return MathAwareInterface |
||
125 | */ |
||
126 | 2 | public function add(MathAwareInterface $number) |
|
130 | |||
131 | /** |
||
132 | * @param \ValueObjects\Number\MathAwareInterface $number |
||
133 | * |
||
134 | * @return MathAwareInterface |
||
135 | */ |
||
136 | 2 | public function subtract(MathAwareInterface $number) |
|
141 | |||
142 | /** |
||
143 | * @param \ValueObjects\Number\MathAwareInterface $number |
||
144 | * |
||
145 | * @return MathAwareInterface |
||
146 | */ |
||
147 | 3 | public function multiply(MathAwareInterface $number) |
|
152 | |||
153 | /** |
||
154 | * @param \ValueObjects\Number\MathAwareInterface $number |
||
155 | * |
||
156 | * @return MathAwareInterface |
||
157 | */ |
||
158 | 3 | public function divide(MathAwareInterface $number) |
|
164 | |||
165 | /** |
||
166 | * @param \ValueObjects\Number\MathAwareInterface $number |
||
167 | * |
||
168 | * @return MathAwareInterface |
||
169 | */ |
||
170 | 1 | public function toPower(MathAwareInterface $number) |
|
175 | |||
176 | /** |
||
177 | * @return MathAwareInterface |
||
178 | */ |
||
179 | 1 | public function square() |
|
183 | |||
184 | /** |
||
185 | * @return MathAwareInterface |
||
186 | */ |
||
187 | 1 | public function squareRoot() |
|
191 | |||
192 | |||
193 | /** |
||
194 | * @return MathAwareInterface |
||
195 | */ |
||
196 | 1 | public function cube() |
|
201 | |||
202 | /** |
||
203 | * @return bool |
||
204 | */ |
||
205 | 2 | public function isEven() |
|
209 | |||
210 | /** |
||
211 | * @return bool |
||
212 | */ |
||
213 | 1 | public function isOdd() |
|
217 | |||
218 | /** |
||
219 | * @return MathAwareInterface |
||
220 | */ |
||
221 | 1 | public function factorial() |
|
232 | |||
233 | /** |
||
234 | * @param \ValueObjects\Number\MathAwareInterface $number |
||
235 | * |
||
236 | * @return bool |
||
237 | */ |
||
238 | 2 | public function isGreaterThan(MathAwareInterface $number) |
|
242 | |||
243 | /** |
||
244 | * @param \ValueObjects\Number\MathAwareInterface $number |
||
245 | * |
||
246 | * @return bool |
||
247 | */ |
||
248 | 1 | public function isGreaterOrEqual(MathAwareInterface $number) |
|
253 | |||
254 | /** |
||
255 | * @param \ValueObjects\Number\MathAwareInterface $number |
||
256 | * |
||
257 | * @return bool |
||
258 | */ |
||
259 | 2 | public function isLessThan(MathAwareInterface $number) |
|
264 | |||
265 | /** |
||
266 | * @param \ValueObjects\Number\MathAwareInterface $number |
||
267 | * |
||
268 | * @return bool |
||
269 | */ |
||
270 | 1 | public function isLessOrEqual(MathAwareInterface $number) |
|
275 | |||
276 | /** |
||
277 | * @param \ValueObjects\Number\MathAwareInterface $number |
||
278 | * |
||
279 | * @return MathAwareInterface |
||
280 | */ |
||
281 | 1 | public function modulo(MathAwareInterface $number) |
|
285 | |||
286 | /** |
||
287 | * @return bool |
||
288 | */ |
||
289 | 1 | public function isZero() |
|
293 | |||
294 | /** |
||
295 | * @return bool |
||
296 | */ |
||
297 | 1 | public function isPositive() |
|
302 | |||
303 | /** |
||
304 | * @return bool |
||
305 | */ |
||
306 | 2 | public function isNegative() |
|
310 | |||
311 | /** |
||
312 | * @return MathAwareInterface |
||
313 | */ |
||
314 | 1 | public function digits() |
|
318 | |||
319 | /** |
||
320 | * @return MathAwareInterface |
||
321 | */ |
||
322 | 2 | public function inverse() |
|
327 | |||
328 | 1 | public function increment() |
|
332 | |||
333 | 1 | public function decrement() |
|
338 | |||
339 | 1 | public function by10() |
|
344 | |||
345 | 1 | public function by100() |
|
350 | |||
351 | /** |
||
352 | * @return StringLiteral |
||
353 | */ |
||
354 | 1 | public function scientific() |
|
358 | |||
359 | 1 | public static function zero() |
|
363 | |||
364 | public static function random($min = NULL, $max = NULL) |
||
368 | |||
369 | 1 | public function absolute() |
|
377 | |||
378 | |||
379 | |||
380 | /** |
||
381 | * @return float |
||
382 | */ |
||
383 | 1 | function jsonSerialize() |
|
387 | |||
388 | |||
389 | } |
||
390 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.