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( |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Returns a QuantityValue representing the given amount. |
||
| 91 | * If no upper or lower bound is given, the amount is assumed to be absolutely exact, |
||
| 92 | * that is, the amount itself will be used as the upper and lower bound. |
||
| 93 | * |
||
| 94 | * This is a convenience wrapper around the constructor that accepts native values |
||
| 95 | * instead of DecimalValue objects. |
||
| 96 | * |
||
| 97 | * @note: if the amount or a bound is given as a string, the string must conform |
||
| 98 | * to the rules defined by @see DecimalValue. |
||
| 99 | * |
||
| 100 | * @since 0.1 |
||
| 101 | * |
||
| 102 | * @param string|int|float|DecimalValue $amount |
||
| 103 | * @param string $unit A unit identifier. Must not be empty, use "1" for unit-less quantities. |
||
| 104 | * @param string|int|float|DecimalValue|null $upperBound |
||
| 105 | * @param string|int|float|DecimalValue|null $lowerBound |
||
| 106 | * |
||
| 107 | * @return QuantityValue |
||
| 108 | * @throws IllegalValueException |
||
| 109 | */ |
||
| 110 | public static function newFromNumber( $amount, $unit = '1', $upperBound = null, $lowerBound = null ) { |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @see newFromNumber |
||
| 120 | * |
||
| 121 | * @deprecated since 0.1, use newFromNumber instead |
||
| 122 | * |
||
| 123 | * @param string|int|float|DecimalValue $amount |
||
| 124 | * @param string $unit |
||
| 125 | * @param string|int|float|DecimalValue|null $upperBound |
||
| 126 | * @param string|int|float|DecimalValue|null $lowerBound |
||
| 127 | * |
||
| 128 | * @return QuantityValue |
||
| 129 | */ |
||
| 130 | public static function newFromDecimal( $amount, $unit = '1', $upperBound = null, $lowerBound = null ) { |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Converts $number to a DecimalValue if possible and necessary. |
||
| 136 | * |
||
| 137 | * @note: if the $number is given as a string, it must conform to the rules |
||
| 138 | * defined by @see DecimalValue. |
||
| 139 | * |
||
| 140 | * @param string $name The variable name to use in exception messages |
||
| 141 | * @param string|int|float|DecimalValue|null $number |
||
| 142 | * @param DecimalValue|null $default |
||
| 143 | * |
||
| 144 | * @throws IllegalValueException |
||
| 145 | * @throws InvalidArgumentException |
||
| 146 | * @return DecimalValue |
||
| 147 | */ |
||
| 148 | private static function asDecimalValue( $name, $number, DecimalValue $default = null ) { |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @see Serializable::serialize |
||
| 174 | * |
||
| 175 | * @since 0.1 |
||
| 176 | * |
||
| 177 | * @return string |
||
| 178 | */ |
||
| 179 | public function serialize() { |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @see Serializable::unserialize |
||
| 190 | * |
||
| 191 | * @since 0.1 |
||
| 192 | * |
||
| 193 | * @param string $data |
||
| 194 | */ |
||
| 195 | public function unserialize( $data ) { |
||
| 199 | |||
| 200 | /** |
||
| 201 | * @see DataValue::getType |
||
| 202 | * |
||
| 203 | * @since 0.1 |
||
| 204 | * |
||
| 205 | * @return string |
||
| 206 | */ |
||
| 207 | public static function getType() { |
||
| 210 | |||
| 211 | /** |
||
| 212 | * @see DataValue::getSortKey |
||
| 213 | * |
||
| 214 | * @since 0.1 |
||
| 215 | * |
||
| 216 | * @return float |
||
| 217 | */ |
||
| 218 | public function getSortKey() { |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Returns the quantity object. |
||
| 224 | * @see DataValue::getValue |
||
| 225 | * |
||
| 226 | * @since 0.1 |
||
| 227 | * |
||
| 228 | * @return QuantityValue |
||
| 229 | */ |
||
| 230 | public function getValue() { |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Returns the amount represented by this quantity. |
||
| 236 | * |
||
| 237 | * @since 0.1 |
||
| 238 | * |
||
| 239 | * @return DecimalValue |
||
| 240 | */ |
||
| 241 | public function getAmount() { |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Returns this quantity's upper bound. |
||
| 247 | * |
||
| 248 | * @since 0.1 |
||
| 249 | * |
||
| 250 | * @return DecimalValue|null |
||
| 251 | */ |
||
| 252 | public function getUpperBound() { |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Returns this quantity's lower bound. |
||
| 258 | * |
||
| 259 | * @since 0.1 |
||
| 260 | * |
||
| 261 | * @return DecimalValue|null |
||
| 262 | */ |
||
| 263 | public function getLowerBound() { |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Returns the size of the uncertainty interval. |
||
| 269 | * This can roughly be interpreted as "amount +/- uncertainty/2". |
||
| 270 | * |
||
| 271 | * The exact interpretation of the uncertainty interval is left to the concrete application or |
||
| 272 | * data point. For example, the uncertainty interval may be defined to be that part of a |
||
| 273 | * normal distribution that is required to cover the 95th percentile. |
||
| 274 | * |
||
| 275 | * @since 0.1 |
||
| 276 | * |
||
| 277 | * @return float |
||
| 278 | */ |
||
| 279 | public function getUncertainty() { |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Returns a DecimalValue representing the symmetrical offset to be applied |
||
| 285 | * to the raw amount for a rough representation of the uncertainty interval, |
||
| 286 | * as in "amount +/- offset". |
||
| 287 | * |
||
| 288 | * The offset is calculated as max( amount - lowerBound, upperBound - amount ). |
||
| 289 | * |
||
| 290 | * @since 0.1 |
||
| 291 | * |
||
| 292 | * @return DecimalValue |
||
| 293 | */ |
||
| 294 | public function getUncertaintyMargin() { |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Returns the order of magnitude of the uncertainty as the exponent of |
||
| 306 | * last significant digit in the amount-string. The value returned by this |
||
| 307 | * is suitable for use with @see DecimalMath::roundToExponent(). |
||
| 308 | * |
||
| 309 | * @example: if two digits after the decimal point are significant, this |
||
| 310 | * returns -2. |
||
| 311 | * |
||
| 312 | * @example: if the last two digits before the decimal point are insignificant, |
||
| 313 | * this returns 2. |
||
| 314 | * |
||
| 315 | * Note that this calculation assumes a symmetric uncertainty interval, |
||
| 316 | * and can be misleading. |
||
| 317 | * |
||
| 318 | * @since 0.1 |
||
| 319 | * |
||
| 320 | * @return int |
||
| 321 | */ |
||
| 322 | public function getOrderOfUncertainty() { |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Returns the number of significant figures in the amount-string, |
||
| 347 | * counting the decimal point, but not counting the leading sign. |
||
| 348 | * |
||
| 349 | * Note that this calculation assumes a symmetric uncertainty interval, and can be misleading |
||
| 350 | * |
||
| 351 | * @since 0.1 |
||
| 352 | * |
||
| 353 | * @return int |
||
| 354 | */ |
||
| 355 | public function getSignificantFigures() { |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Returns the unit held by this quantity. |
||
| 369 | * Unit-less quantities should use "1" as their unit. |
||
| 370 | * |
||
| 371 | * @since 0.1 |
||
| 372 | * |
||
| 373 | * @return string |
||
| 374 | */ |
||
| 375 | public function getUnit() { |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Returns a transformed value derived from this QuantityValue by applying |
||
| 381 | * the given transformation to the amount and the upper and lower bounds. |
||
| 382 | * The resulting amount and bounds are rounded to the significant number of |
||
| 383 | * digits. Note that for exact quantities (with at least one bound equal to |
||
| 384 | * the amount), no rounding is applied (since they are considered to have |
||
| 385 | * infinite precision). |
||
| 386 | * |
||
| 387 | * The transformation is provided as a callback, which must implement a |
||
| 388 | * monotonously increasing, fully differentiable function mapping a DecimalValue |
||
| 389 | * to a DecimalValue. Typically, it will be a linear transformation applying a |
||
| 390 | * factor and an offset. |
||
| 391 | * |
||
| 392 | * @param string $newUnit The unit of the transformed quantity. |
||
| 393 | * |
||
| 394 | * @param callable $transformation A callback that implements the desired transformation. |
||
| 395 | * The transformation will be called three times, once for the amount, once |
||
| 396 | * for the lower bound, and once for the upper bound. It must return a DecimalValue. |
||
| 397 | * The first parameter passed to $transformation is the DecimalValue to transform |
||
| 398 | * In addition, any extra parameters passed to transform() will be passed through |
||
| 399 | * to the transformation callback. |
||
| 400 | * |
||
| 401 | * @param mixed ... Any extra parameters will be passed to the $transformation function. |
||
| 402 | * |
||
| 403 | * @throws InvalidArgumentException |
||
| 404 | * @return QuantityValue |
||
| 405 | */ |
||
| 406 | public function transform( $newUnit, $transformation ) { |
||
| 453 | |||
| 454 | public function __toString() { |
||
| 462 | |||
| 463 | /** |
||
| 464 | * @see DataValue::getArrayValue |
||
| 465 | * |
||
| 466 | * @since 0.1 |
||
| 467 | * |
||
| 468 | * @return array |
||
| 469 | */ |
||
| 470 | public function getArrayValue() { |
||
| 478 | |||
| 479 | /** |
||
| 480 | * Constructs a new instance of the DataValue from the provided data. |
||
| 481 | * This can round-trip with @see getArrayValue |
||
| 482 | * |
||
| 483 | * @since 0.1 |
||
| 484 | * |
||
| 485 | * @param mixed $data |
||
| 486 | * |
||
| 487 | * @return QuantityValue |
||
| 488 | * @throws IllegalValueException |
||
| 489 | */ |
||
| 490 | public static function newFromArray( $data ) { |
||
| 500 | |||
| 501 | /** |
||
| 502 | * @see Comparable::equals |
||
| 503 | * |
||
| 504 | * @since 0.1 |
||
| 505 | * |
||
| 506 | * @param mixed $target |
||
| 507 | * |
||
| 508 | * @return bool |
||
| 509 | */ |
||
| 510 | public function equals( $target ) { |
||
| 518 | |||
| 519 | } |
||
| 520 |
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: