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 |
||
| 18 | class QuantityValue extends DataValueObject { |
||
| 19 | |||
| 20 | /** |
||
| 21 | * The quantity's amount |
||
| 22 | * |
||
| 23 | * @var DecimalValue |
||
| 24 | */ |
||
| 25 | private $amount; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * The quantity's unit identifier (use "1" for unitless quantities). |
||
| 29 | * |
||
| 30 | * @var string |
||
| 31 | */ |
||
| 32 | private $unit; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * The quantity's upper bound |
||
| 36 | * |
||
| 37 | * @var DecimalValue |
||
| 38 | */ |
||
| 39 | private $upperBound; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * The quantity's lower bound |
||
| 43 | * |
||
| 44 | * @var DecimalValue |
||
| 45 | */ |
||
| 46 | private $lowerBound; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Constructs a new QuantityValue object, representing the given value. |
||
| 50 | * |
||
| 51 | * @since 0.1 |
||
| 52 | * |
||
| 53 | * @param DecimalValue $amount |
||
| 54 | * @param string $unit A unit identifier. Must not be empty, use "1" for unit-less quantities. |
||
| 55 | * @param DecimalValue $upperBound The upper bound of the quantity, inclusive. |
||
| 56 | * @param DecimalValue $lowerBound The lower bound of the quantity, inclusive. |
||
| 57 | * |
||
| 58 | * @throws IllegalValueException |
||
| 59 | */ |
||
| 60 | public function __construct( DecimalValue $amount, $unit, DecimalValue $upperBound, DecimalValue $lowerBound ) { |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Returns a QuantityValue representing the given amount. |
||
| 85 | * If no upper or lower bound is given, the amount is assumed to be absolutely exact, |
||
| 86 | * that is, the amount itself will be used as the upper and lower bound. |
||
| 87 | * |
||
| 88 | * This is a convenience wrapper around the constructor that accepts native values |
||
| 89 | * instead of DecimalValue objects. |
||
| 90 | * |
||
| 91 | * @note: if the amount or a bound is given as a string, the string must conform |
||
| 92 | * to the rules defined by @see DecimalValue. |
||
| 93 | * |
||
| 94 | * @since 0.1 |
||
| 95 | * |
||
| 96 | * @param string|int|float|DecimalValue $amount |
||
| 97 | * @param string $unit A unit identifier. Must not be empty, use "1" for unit-less quantities. |
||
| 98 | * @param string|int|float|DecimalValue|null $upperBound |
||
| 99 | * @param string|int|float|DecimalValue|null $lowerBound |
||
| 100 | * |
||
| 101 | * @return self |
||
| 102 | * @throws IllegalValueException |
||
| 103 | */ |
||
| 104 | public static function newFromNumber( $amount, $unit = '1', $upperBound = null, $lowerBound = null ) { |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Converts $number to a DecimalValue if possible and necessary. |
||
| 114 | * |
||
| 115 | * @note: if the $number is given as a string, it must conform to the rules |
||
| 116 | * defined by @see DecimalValue. |
||
| 117 | * |
||
| 118 | * @param string $name The variable name to use in exception messages |
||
| 119 | * @param string|int|float|DecimalValue|null $number |
||
| 120 | * @param DecimalValue|null $default |
||
| 121 | * |
||
| 122 | * @throws IllegalValueException |
||
| 123 | * @throws InvalidArgumentException |
||
| 124 | * @return DecimalValue |
||
| 125 | */ |
||
| 126 | private static function asDecimalValue( $name, $number, DecimalValue $default = null ) { |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @see Serializable::serialize |
||
| 152 | * |
||
| 153 | * @since 0.1 |
||
| 154 | * |
||
| 155 | * @return string |
||
| 156 | */ |
||
| 157 | public function serialize() { |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @see Serializable::unserialize |
||
| 168 | * |
||
| 169 | * @since 0.1 |
||
| 170 | * |
||
| 171 | * @param string $data |
||
| 172 | */ |
||
| 173 | public function unserialize( $data ) { |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @see DataValue::getType |
||
| 180 | * |
||
| 181 | * @since 0.1 |
||
| 182 | * |
||
| 183 | * @return string |
||
| 184 | */ |
||
| 185 | public static function getType() { |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @see DataValue::getSortKey |
||
| 191 | * |
||
| 192 | * @since 0.1 |
||
| 193 | * |
||
| 194 | * @return float |
||
| 195 | */ |
||
| 196 | public function getSortKey() { |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Returns the quantity object. |
||
| 202 | * @see DataValue::getValue |
||
| 203 | * |
||
| 204 | * @since 0.1 |
||
| 205 | * |
||
| 206 | * @return self |
||
| 207 | */ |
||
| 208 | public function getValue() { |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Returns the amount represented by this quantity. |
||
| 214 | * |
||
| 215 | * @since 0.1 |
||
| 216 | * |
||
| 217 | * @return DecimalValue |
||
| 218 | */ |
||
| 219 | public function getAmount() { |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Returns this quantity's upper bound. |
||
| 225 | * |
||
| 226 | * @since 0.1 |
||
| 227 | * |
||
| 228 | * @return DecimalValue |
||
| 229 | */ |
||
| 230 | public function getUpperBound() { |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Returns this quantity's lower bound. |
||
| 236 | * |
||
| 237 | * @since 0.1 |
||
| 238 | * |
||
| 239 | * @return DecimalValue |
||
| 240 | */ |
||
| 241 | public function getLowerBound() { |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Returns the size of the uncertainty interval. |
||
| 247 | * This can roughly be interpreted as "amount +/- uncertainty/2". |
||
| 248 | * |
||
| 249 | * The exact interpretation of the uncertainty interval is left to the concrete application or |
||
| 250 | * data point. For example, the uncertainty interval may be defined to be that part of a |
||
| 251 | * normal distribution that is required to cover the 95th percentile. |
||
| 252 | * |
||
| 253 | * @since 0.1 |
||
| 254 | * |
||
| 255 | * @return float |
||
| 256 | */ |
||
| 257 | public function getUncertainty() { |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Returns a DecimalValue representing the symmetrical offset to be applied |
||
| 263 | * to the raw amount for a rough representation of the uncertainty interval, |
||
| 264 | * as in "amount +/- offset". |
||
| 265 | * |
||
| 266 | * The offset is calculated as max( amount - lowerBound, upperBound - amount ). |
||
| 267 | * |
||
| 268 | * @since 0.1 |
||
| 269 | * |
||
| 270 | * @return DecimalValue |
||
| 271 | */ |
||
| 272 | public function getUncertaintyMargin() { |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Returns the order of magnitude of the uncertainty as the exponent of |
||
| 284 | * last significant digit in the amount-string. The value returned by this |
||
| 285 | * is suitable for use with @see DecimalMath::roundToExponent(). |
||
| 286 | * |
||
| 287 | * @example: if two digits after the decimal point are significant, this |
||
| 288 | * returns -2. |
||
| 289 | * |
||
| 290 | * @example: if the last two digits before the decimal point are insignificant, |
||
| 291 | * this returns 2. |
||
| 292 | * |
||
| 293 | * Note that this calculation assumes a symmetric uncertainty interval, |
||
| 294 | * and can be misleading. |
||
| 295 | * |
||
| 296 | * @since 0.1 |
||
| 297 | * |
||
| 298 | * @return int |
||
| 299 | */ |
||
| 300 | public function getOrderOfUncertainty() { |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Returns the number of significant figures in the amount-string, |
||
| 325 | * counting the decimal point, but not counting the leading sign. |
||
| 326 | * |
||
| 327 | * Note that this calculation assumes a symmetric uncertainty interval, and can be misleading |
||
| 328 | * |
||
| 329 | * @since 0.1 |
||
| 330 | * |
||
| 331 | * @return int |
||
| 332 | */ |
||
| 333 | public function getSignificantFigures() { |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Returns the unit held by this quantity. |
||
| 347 | * Unit-less quantities should use "1" as their unit. |
||
| 348 | * |
||
| 349 | * @since 0.1 |
||
| 350 | * |
||
| 351 | * @return string |
||
| 352 | */ |
||
| 353 | public function getUnit() { |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Returns a transformed value derived from this QuantityValue by applying |
||
| 359 | * the given transformation to the amount and the upper and lower bounds. |
||
| 360 | * The resulting amount and bounds are rounded to the significant number of |
||
| 361 | * digits. Note that for exact quantities (with at least one bound equal to |
||
| 362 | * the amount), no rounding is applied (since they are considered to have |
||
| 363 | * infinite precision). |
||
| 364 | * |
||
| 365 | * The transformation is provided as a callback, which must implement a |
||
| 366 | * monotonously increasing, fully differentiable function mapping a DecimalValue |
||
| 367 | * to a DecimalValue. Typically, it will be a linear transformation applying a |
||
| 368 | * factor and an offset. |
||
| 369 | * |
||
| 370 | * @param string $newUnit The unit of the transformed quantity. |
||
| 371 | * |
||
| 372 | * @param callable $transformation A callback that implements the desired transformation. |
||
| 373 | * The transformation will be called three times, once for the amount, once |
||
| 374 | * for the lower bound, and once for the upper bound. It must return a DecimalValue. |
||
| 375 | * The first parameter passed to $transformation is the DecimalValue to transform |
||
| 376 | * In addition, any extra parameters passed to transform() will be passed through |
||
| 377 | * to the transformation callback. |
||
| 378 | * |
||
| 379 | * @param mixed ... Any extra parameters will be passed to the $transformation function. |
||
| 380 | * |
||
| 381 | * @throws InvalidArgumentException |
||
| 382 | * @return self |
||
| 383 | */ |
||
| 384 | public function transform( $newUnit, $transformation ) { |
||
| 431 | |||
| 432 | public function __toString() { |
||
| 439 | |||
| 440 | /** |
||
| 441 | * @see DataValue::getArrayValue |
||
| 442 | * |
||
| 443 | * @since 0.1 |
||
| 444 | * |
||
| 445 | * @return array |
||
| 446 | */ |
||
| 447 | public function getArrayValue() { |
||
| 455 | |||
| 456 | /** |
||
| 457 | * Constructs a new instance of the DataValue from the provided data. |
||
| 458 | * This can round-trip with @see getArrayValue |
||
| 459 | * |
||
| 460 | * @since 0.1 |
||
| 461 | * |
||
| 462 | * @param mixed $data |
||
| 463 | * |
||
| 464 | * @return self |
||
| 465 | * @throws IllegalValueException |
||
| 466 | */ |
||
| 467 | public static function newFromArray( $data ) { |
||
| 477 | |||
| 478 | /** |
||
| 479 | * @see Comparable::equals |
||
| 480 | * |
||
| 481 | * @since 0.1 |
||
| 482 | * |
||
| 483 | * @param mixed $target |
||
| 484 | * |
||
| 485 | * @return bool |
||
| 486 | */ |
||
| 487 | public function equals( $target ) { |
||
| 495 | |||
| 496 | } |
||
| 497 |
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: