Complex classes like QuantityValue 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 QuantityValue, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class QuantityValue extends DataValueObject { |
||
20 | |||
21 | /** |
||
22 | * The quantity's amount |
||
23 | * |
||
24 | * @var DecimalValue |
||
25 | */ |
||
26 | private $amount; |
||
27 | |||
28 | /** |
||
29 | * The quantity's unit identifier (use "1" for unitless quantities). |
||
30 | * |
||
31 | * @var string |
||
32 | */ |
||
33 | private $unit; |
||
34 | |||
35 | /** |
||
36 | * The quantity's upper bound |
||
37 | * |
||
38 | * @var DecimalValue|null |
||
39 | */ |
||
40 | private $upperBound; |
||
41 | |||
42 | /** |
||
43 | * The quantity's lower bound |
||
44 | * |
||
45 | * @var DecimalValue|null |
||
46 | */ |
||
47 | private $lowerBound; |
||
48 | |||
49 | /** |
||
50 | * Constructs a new QuantityValue object, representing the given value. |
||
51 | * |
||
52 | * @since 0.1 |
||
53 | * |
||
54 | * @param DecimalValue $amount |
||
55 | * @param string $unit A unit identifier. Must not be empty, use "1" for unit-less quantities. |
||
56 | * @param DecimalValue|null $upperBound The upper bound of the quantity, inclusive. |
||
57 | * @param DecimalValue|null $lowerBound The lower bound of the quantity, inclusive. |
||
58 | * |
||
59 | * @throws IllegalValueException |
||
60 | */ |
||
61 | public function __construct( |
||
92 | |||
93 | /** |
||
94 | * Returns a QuantityValue representing the given amount. |
||
95 | * If no upper or lower bound is given, the amount is assumed to be absolutely exact, |
||
96 | * that is, the amount itself will be used as the upper and lower bound. |
||
97 | * |
||
98 | * This is a convenience wrapper around the constructor that accepts native values |
||
99 | * instead of DecimalValue objects. |
||
100 | * |
||
101 | * @note: if the amount or a bound is given as a string, the string must conform |
||
102 | * to the rules defined by @see DecimalValue. |
||
103 | * |
||
104 | * @since 0.1 |
||
105 | * |
||
106 | * @param string|int|float|DecimalValue $amount |
||
107 | * @param string $unit A unit identifier. Must not be empty, use "1" for unit-less quantities. |
||
108 | * @param string|int|float|DecimalValue|null $upperBound |
||
109 | * @param string|int|float|DecimalValue|null $lowerBound |
||
110 | * |
||
111 | * @return self |
||
112 | * @throws IllegalValueException |
||
113 | */ |
||
114 | public static function newFromNumber( $amount, $unit = '1', $upperBound = null, $lowerBound = null ) { |
||
127 | |||
128 | /** |
||
129 | * @see newFromNumber |
||
130 | * |
||
131 | * @deprecated since 0.1, use newFromNumber instead |
||
132 | * |
||
133 | * @param string|int|float|DecimalValue $amount |
||
134 | * @param string $unit |
||
135 | * @param string|int|float|DecimalValue|null $upperBound |
||
136 | * @param string|int|float|DecimalValue|null $lowerBound |
||
137 | * |
||
138 | * @return self |
||
139 | */ |
||
140 | public static function newFromDecimal( $amount, $unit = '1', $upperBound = null, $lowerBound = null ) { |
||
141 | return self::newFromNumber( $amount, $unit, $upperBound, $lowerBound ); |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * Converts $number to a DecimalValue if possible and necessary. |
||
146 | * |
||
147 | * @note: if the $number is given as a string, it must conform to the rules |
||
148 | * defined by @see DecimalValue. |
||
149 | * |
||
150 | * @param string $name The variable name to use in exception messages |
||
151 | * @param string|int|float|DecimalValue $number |
||
152 | * |
||
153 | * @throws IllegalValueException |
||
154 | * @throws InvalidArgumentException |
||
155 | * @return DecimalValue |
||
156 | */ |
||
157 | private static function asDecimalValue( $name, $number ) { |
||
158 | if ( !is_string( $name ) ) { |
||
159 | throw new InvalidArgumentException( '$name must be a string' ); |
||
160 | } |
||
161 | |||
162 | if ( $number instanceof DecimalValue ) { |
||
163 | // nothing to do |
||
164 | } elseif ( is_int( $number ) || is_float( $number ) || is_string( $number ) ) { |
||
165 | $number = new DecimalValue( $number ); |
||
166 | } else { |
||
167 | throw new IllegalValueException( '$' . $name . ' must be a string, int, or float' ); |
||
168 | } |
||
169 | |||
170 | return $number; |
||
171 | } |
||
172 | |||
173 | /** |
||
174 | * @see Serializable::serialize |
||
175 | * |
||
176 | * @since 0.1 |
||
177 | * |
||
178 | * @return string |
||
179 | */ |
||
180 | public function serialize() { |
||
181 | return serialize( array( |
||
182 | $this->amount, |
||
183 | $this->unit, |
||
184 | $this->upperBound, |
||
185 | $this->lowerBound, |
||
186 | ) ); |
||
187 | } |
||
188 | |||
189 | /** |
||
190 | * @see Serializable::unserialize |
||
191 | * |
||
192 | * @since 0.1 |
||
193 | * |
||
194 | * @param string $data |
||
195 | */ |
||
196 | public function unserialize( $data ) { |
||
197 | list( $amount, $unit, $upperBound, $lowerBound ) = unserialize( $data ); |
||
198 | $this->__construct( $amount, $unit, $upperBound, $lowerBound ); |
||
199 | } |
||
200 | |||
201 | /** |
||
202 | * @see DataValue::getType |
||
203 | * |
||
204 | * @since 0.1 |
||
205 | * |
||
206 | * @return string |
||
207 | */ |
||
208 | public static function getType() { |
||
209 | return 'quantity'; |
||
210 | } |
||
211 | |||
212 | /** |
||
213 | * @see DataValue::getSortKey |
||
214 | * |
||
215 | * @since 0.1 |
||
216 | * |
||
217 | * @return float |
||
218 | */ |
||
219 | public function getSortKey() { |
||
220 | return $this->amount->getValueFloat(); |
||
221 | } |
||
222 | |||
223 | /** |
||
224 | * Returns the quantity object. |
||
225 | * @see DataValue::getValue |
||
226 | * |
||
227 | * @since 0.1 |
||
228 | * |
||
229 | * @return self |
||
230 | */ |
||
231 | public function getValue() { |
||
232 | return $this; |
||
233 | } |
||
234 | |||
235 | /** |
||
236 | * Returns the amount represented by this quantity. |
||
237 | * |
||
238 | * @since 0.1 |
||
239 | * |
||
240 | * @return DecimalValue |
||
241 | */ |
||
242 | public function getAmount() { |
||
243 | return $this->amount; |
||
244 | } |
||
245 | |||
246 | /** |
||
247 | * @since 0.8 |
||
248 | * |
||
249 | * @return bool |
||
250 | */ |
||
251 | public function hasBounds() { |
||
252 | return $this->upperBound !== null; |
||
253 | } |
||
254 | |||
255 | /** |
||
256 | * Returns this quantity's upper bound. |
||
257 | * |
||
258 | * @since 0.1 |
||
259 | * |
||
260 | * @return DecimalValue|null |
||
261 | */ |
||
262 | public function getUpperBound() { |
||
265 | |||
266 | /** |
||
267 | * Returns this quantity's lower bound. |
||
268 | * |
||
269 | * @since 0.1 |
||
270 | * |
||
271 | * @return DecimalValue|null |
||
272 | */ |
||
273 | public function getLowerBound() { |
||
274 | return $this->lowerBound; |
||
276 | |||
277 | /** |
||
278 | * Returns the size of the uncertainty interval. |
||
279 | * This can roughly be interpreted as "amount +/- uncertainty/2". |
||
280 | * |
||
281 | * The exact interpretation of the uncertainty interval is left to the concrete application or |
||
282 | * data point. For example, the uncertainty interval may be defined to be that part of a |
||
283 | * normal distribution that is required to cover the 95th percentile. |
||
284 | * |
||
285 | * @since 0.1 |
||
286 | * |
||
287 | * @return float|null |
||
288 | */ |
||
289 | public function getUncertainty() { |
||
294 | |||
295 | /** |
||
296 | * Returns a DecimalValue representing the symmetrical offset to be applied |
||
297 | * to the raw amount for a rough representation of the uncertainty interval, |
||
298 | * as in "amount +/- offset". |
||
299 | * |
||
300 | * The offset is calculated as max( amount - lowerBound, upperBound - amount ). |
||
301 | * |
||
302 | * @since 0.1 |
||
303 | * |
||
304 | * @return DecimalValue|null |
||
305 | */ |
||
306 | public function getUncertaintyMargin() { |
||
319 | |||
320 | /** |
||
321 | * Returns the order of magnitude of the uncertainty as the exponent of |
||
322 | * last significant digit in the amount-string. The value returned by this |
||
323 | * is suitable for use with @see DecimalMath::roundToExponent(). |
||
324 | * |
||
325 | * @example: if two digits after the decimal point are significant, this |
||
326 | * returns -2. |
||
327 | * |
||
328 | * @example: if the last two digits before the decimal point are insignificant, |
||
329 | * this returns 2. |
||
330 | * |
||
331 | * Note that this calculation assumes a symmetric uncertainty interval, |
||
332 | * and can be misleading. |
||
333 | * |
||
334 | * @since 0.1 |
||
335 | * |
||
336 | * @return int|null |
||
337 | */ |
||
338 | public function getOrderOfUncertainty() { |
||
364 | |||
365 | /** |
||
366 | * Returns the number of significant figures in the amount-string, |
||
367 | * counting the decimal point, but not counting the leading sign. |
||
368 | * |
||
369 | * Note that this calculation assumes a symmetric uncertainty interval, and can be misleading |
||
370 | * |
||
371 | * @since 0.1 |
||
372 | * |
||
373 | * @return int|null |
||
374 | */ |
||
375 | public function getSignificantFigures() { |
||
390 | |||
391 | /** |
||
392 | * Returns the unit held by this quantity. |
||
393 | * Unit-less quantities should use "1" as their unit. |
||
394 | * |
||
395 | * @since 0.1 |
||
396 | * |
||
397 | * @return string |
||
398 | */ |
||
399 | public function getUnit() { |
||
402 | |||
403 | /** |
||
404 | * Returns a transformed value derived from this QuantityValue by applying |
||
405 | * the given transformation to the amount and the upper and lower bounds. |
||
406 | * The resulting amount and bounds are rounded to the significant number of |
||
407 | * digits. Note that for exact quantities (with at least one bound equal to |
||
408 | * the amount), no rounding is applied (since they are considered to have |
||
409 | * infinite precision). |
||
410 | * |
||
411 | * The transformation is provided as a callback, which must implement a |
||
412 | * monotonously increasing, fully differentiable function mapping a DecimalValue |
||
413 | * to a DecimalValue. Typically, it will be a linear transformation applying a |
||
414 | * factor and an offset. |
||
415 | * |
||
416 | * @param string $newUnit The unit of the transformed quantity. |
||
417 | * |
||
418 | * @param callable $transformation A callback that implements the desired transformation. |
||
419 | * The transformation will be called three times, once for the amount, once |
||
420 | * for the lower bound, and once for the upper bound. It must return a DecimalValue. |
||
421 | * The first parameter passed to $transformation is the DecimalValue to transform |
||
422 | * In addition, any extra parameters passed to transform() will be passed through |
||
423 | * to the transformation callback. |
||
424 | * |
||
425 | * @param mixed ... Any extra parameters will be passed to the $transformation function. |
||
426 | * |
||
427 | * @throws InvalidArgumentException |
||
428 | * @return self |
||
429 | */ |
||
430 | public function transform( $newUnit, $transformation ) { |
||
485 | |||
486 | public function __toString() { |
||
493 | |||
494 | /** |
||
495 | * @see DataValue::getArrayValue |
||
496 | * |
||
497 | * @since 0.1 |
||
498 | * |
||
499 | * @return string[] |
||
500 | */ |
||
501 | public function getArrayValue() { |
||
514 | |||
515 | /** |
||
516 | * Constructs a new instance of the DataValue from the provided data. |
||
517 | * This can round-trip with @see getArrayValue |
||
518 | * |
||
519 | * @since 0.1 |
||
520 | * |
||
521 | * @param mixed $data |
||
522 | * |
||
523 | * @return self |
||
524 | * @throws IllegalValueException |
||
525 | */ |
||
526 | public static function newFromArray( $data ) { |
||
536 | |||
537 | /** |
||
538 | * @see Comparable::equals |
||
539 | * |
||
540 | * @since 0.1 |
||
541 | * |
||
542 | * @param mixed $target |
||
543 | * |
||
544 | * @return bool |
||
545 | */ |
||
546 | public function equals( $target ) { |
||
554 | |||
555 | } |
||
556 |
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: