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 ) { |
||
| 61 | if ( $lowerBound->compare( $amount ) > 0 ) { |
||
|
|
|||
| 62 | throw new IllegalValueException( '$lowerBound ' . $lowerBound->getValue() . ' must be <= $amount ' . $amount->getValue() ); |
||
| 63 | } |
||
| 64 | |||
| 65 | if ( $upperBound->compare( $amount ) < 0 ) { |
||
| 66 | throw new IllegalValueException( '$upperBound ' . $upperBound->getValue() . ' must be >= $amount ' . $amount->getValue() ); |
||
| 67 | } |
||
| 68 | |||
| 69 | if ( !is_string( $unit ) ) { |
||
| 70 | throw new IllegalValueException( '$unit needs to be a string, not ' . gettype( $unit ) ); |
||
| 71 | } |
||
| 72 | |||
| 73 | if ( $unit === '' ) { |
||
| 74 | throw new IllegalValueException( '$unit can not be an empty string (use "1" for unit-less quantities)' ); |
||
| 75 | } |
||
| 76 | |||
| 77 | $this->amount = $amount; |
||
| 78 | $this->unit = $unit; |
||
| 79 | $this->upperBound = $upperBound; |
||
| 80 | $this->lowerBound = $lowerBound; |
||
| 81 | } |
||
| 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 ) { |
||
| 105 | $amount = self::asDecimalValue( 'amount', $amount ); |
||
| 106 | $upperBound = self::asDecimalValue( 'upperBound', $upperBound, $amount ); |
||
| 107 | $lowerBound = self::asDecimalValue( 'lowerBound', $lowerBound, $amount ); |
||
| 108 | |||
| 109 | return new self( $amount, $unit, $upperBound, $lowerBound ); |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @see newFromNumber |
||
| 114 | * |
||
| 115 | * @deprecated since 0.1, use newFromNumber instead |
||
| 116 | * |
||
| 117 | * @param string|int|float|DecimalValue $amount |
||
| 118 | * @param string $unit |
||
| 119 | * @param string|int|float|DecimalValue|null $upperBound |
||
| 120 | * @param string|int|float|DecimalValue|null $lowerBound |
||
| 121 | * |
||
| 122 | * @return self |
||
| 123 | */ |
||
| 124 | public static function newFromDecimal( $amount, $unit = '1', $upperBound = null, $lowerBound = null ) { |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Converts $number to a DecimalValue if possible and necessary. |
||
| 130 | * |
||
| 131 | * @note: if the $number is given as a string, it must conform to the rules |
||
| 132 | * defined by @see DecimalValue. |
||
| 133 | * |
||
| 134 | * @param string $name The variable name to use in exception messages |
||
| 135 | * @param string|int|float|DecimalValue|null $number |
||
| 136 | * @param DecimalValue|null $default |
||
| 137 | * |
||
| 138 | * @throws IllegalValueException |
||
| 139 | * @throws InvalidArgumentException |
||
| 140 | * @return DecimalValue |
||
| 141 | */ |
||
| 142 | private static function asDecimalValue( $name, $number, DecimalValue $default = null ) { |
||
| 143 | if ( !is_string( $name ) ) { |
||
| 144 | throw new InvalidArgumentException( '$name must be a string' ); |
||
| 145 | } |
||
| 146 | |||
| 147 | if ( $number === null ) { |
||
| 148 | if ( $default === null ) { |
||
| 149 | throw new InvalidArgumentException( '$' . $name . ' must not be null' ); |
||
| 150 | } |
||
| 151 | |||
| 152 | $number = $default; |
||
| 153 | } |
||
| 154 | |||
| 155 | if ( $number instanceof DecimalValue ) { |
||
| 156 | // nothing to do |
||
| 157 | } elseif ( is_int( $number ) || is_float( $number ) || is_string( $number ) ) { |
||
| 158 | $number = new DecimalValue( $number ); |
||
| 159 | } else { |
||
| 160 | throw new IllegalValueException( '$' . $name . ' must be a string, int, or float' ); |
||
| 161 | } |
||
| 162 | |||
| 163 | return $number; |
||
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @see Serializable::serialize |
||
| 168 | * |
||
| 169 | * @since 0.1 |
||
| 170 | * |
||
| 171 | * @return string |
||
| 172 | */ |
||
| 173 | public function serialize() { |
||
| 174 | return serialize( array( |
||
| 175 | $this->amount, |
||
| 176 | $this->unit, |
||
| 177 | $this->upperBound, |
||
| 178 | $this->lowerBound, |
||
| 179 | ) ); |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * @see Serializable::unserialize |
||
| 184 | * |
||
| 185 | * @since 0.1 |
||
| 186 | * |
||
| 187 | * @param string $data |
||
| 188 | */ |
||
| 189 | public function unserialize( $data ) { |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @see DataValue::getType |
||
| 196 | * |
||
| 197 | * @since 0.1 |
||
| 198 | * |
||
| 199 | * @return string |
||
| 200 | */ |
||
| 201 | public static function getType() { |
||
| 204 | |||
| 205 | /** |
||
| 206 | * @see DataValue::getSortKey |
||
| 207 | * |
||
| 208 | * @since 0.1 |
||
| 209 | * |
||
| 210 | * @return float |
||
| 211 | */ |
||
| 212 | public function getSortKey() { |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Returns the quantity object. |
||
| 218 | * @see DataValue::getValue |
||
| 219 | * |
||
| 220 | * @since 0.1 |
||
| 221 | * |
||
| 222 | * @return self |
||
| 223 | */ |
||
| 224 | public function getValue() { |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Returns the amount represented by this quantity. |
||
| 230 | * |
||
| 231 | * @since 0.1 |
||
| 232 | * |
||
| 233 | * @return DecimalValue |
||
| 234 | */ |
||
| 235 | public function getAmount() { |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Returns this quantity's upper bound. |
||
| 241 | * |
||
| 242 | * @since 0.1 |
||
| 243 | * |
||
| 244 | * @return DecimalValue |
||
| 245 | */ |
||
| 246 | public function getUpperBound() { |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Returns this quantity's lower bound. |
||
| 252 | * |
||
| 253 | * @since 0.1 |
||
| 254 | * |
||
| 255 | * @return DecimalValue |
||
| 256 | */ |
||
| 257 | public function getLowerBound() { |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Returns the size of the uncertainty interval. |
||
| 263 | * This can roughly be interpreted as "amount +/- uncertainty/2". |
||
| 264 | * |
||
| 265 | * The exact interpretation of the uncertainty interval is left to the concrete application or |
||
| 266 | * data point. For example, the uncertainty interval may be defined to be that part of a |
||
| 267 | * normal distribution that is required to cover the 95th percentile. |
||
| 268 | * |
||
| 269 | * @since 0.1 |
||
| 270 | * |
||
| 271 | * @return float |
||
| 272 | */ |
||
| 273 | public function getUncertainty() { |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Returns a DecimalValue representing the symmetrical offset to be applied |
||
| 279 | * to the raw amount for a rough representation of the uncertainty interval, |
||
| 280 | * as in "amount +/- offset". |
||
| 281 | * |
||
| 282 | * The offset is calculated as max( amount - lowerBound, upperBound - amount ). |
||
| 283 | * |
||
| 284 | * @since 0.1 |
||
| 285 | * |
||
| 286 | * @return DecimalValue |
||
| 287 | */ |
||
| 288 | public function getUncertaintyMargin() { |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Returns the order of magnitude of the uncertainty as the exponent of |
||
| 300 | * last significant digit in the amount-string. The value returned by this |
||
| 301 | * is suitable for use with @see DecimalMath::roundToExponent(). |
||
| 302 | * |
||
| 303 | * @example: if two digits after the decimal point are significant, this |
||
| 304 | * returns -2. |
||
| 305 | * |
||
| 306 | * @example: if the last two digits before the decimal point are insignificant, |
||
| 307 | * this returns 2. |
||
| 308 | * |
||
| 309 | * Note that this calculation assumes a symmetric uncertainty interval, |
||
| 310 | * and can be misleading. |
||
| 311 | * |
||
| 312 | * @since 0.1 |
||
| 313 | * |
||
| 314 | * @return int |
||
| 315 | */ |
||
| 316 | public function getOrderOfUncertainty() { |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Returns the unit held by this quantity. |
||
| 341 | * Unit-less quantities should use "1" as their unit. |
||
| 342 | * |
||
| 343 | * @since 0.1 |
||
| 344 | * |
||
| 345 | * @return string |
||
| 346 | */ |
||
| 347 | public function getUnit() { |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Returns a transformed value derived from this QuantityValue by applying |
||
| 353 | * the given transformation to the amount and the upper and lower bounds. |
||
| 354 | * The resulting amount and bounds are rounded to the significant number of |
||
| 355 | * digits. Note that for exact quantities (with at least one bound equal to |
||
| 356 | * the amount), no rounding is applied (since they are considered to have |
||
| 357 | * infinite precision). |
||
| 358 | * |
||
| 359 | * The transformation is provided as a callback, which must implement a |
||
| 360 | * monotonously increasing, fully differentiable function mapping a DecimalValue |
||
| 361 | * to a DecimalValue. Typically, it will be a linear transformation applying a |
||
| 362 | * factor and an offset. |
||
| 363 | * |
||
| 364 | * @param string $newUnit The unit of the transformed quantity. |
||
| 365 | * |
||
| 366 | * @param callable $transformation A callback that implements the desired transformation. |
||
| 367 | * The transformation will be called three times, once for the amount, once |
||
| 368 | * for the lower bound, and once for the upper bound. It must return a DecimalValue. |
||
| 369 | * The first parameter passed to $transformation is the DecimalValue to transform |
||
| 370 | * In addition, any extra parameters passed to transform() will be passed through |
||
| 371 | * to the transformation callback. |
||
| 372 | * |
||
| 373 | * @param mixed ... Any extra parameters will be passed to the $transformation function. |
||
| 374 | * |
||
| 375 | * @throws InvalidArgumentException |
||
| 376 | * @return self |
||
| 377 | */ |
||
| 378 | public function transform( $newUnit, $transformation ) { |
||
| 425 | |||
| 426 | public function __toString() { |
||
| 433 | |||
| 434 | /** |
||
| 435 | * @see DataValue::getArrayValue |
||
| 436 | * |
||
| 437 | * @since 0.1 |
||
| 438 | * |
||
| 439 | * @return array |
||
| 440 | */ |
||
| 441 | public function getArrayValue() { |
||
| 449 | |||
| 450 | /** |
||
| 451 | * Constructs a new instance of the DataValue from the provided data. |
||
| 452 | * This can round-trip with @see getArrayValue |
||
| 453 | * |
||
| 454 | * @since 0.1 |
||
| 455 | * |
||
| 456 | * @param mixed $data |
||
| 457 | * |
||
| 458 | * @return self |
||
| 459 | * @throws IllegalValueException |
||
| 460 | */ |
||
| 461 | public static function newFromArray( $data ) { |
||
| 471 | |||
| 472 | /** |
||
| 473 | * @see Comparable::equals |
||
| 474 | * |
||
| 475 | * @since 0.1 |
||
| 476 | * |
||
| 477 | * @param mixed $target |
||
| 478 | * |
||
| 479 | * @return bool |
||
| 480 | */ |
||
| 481 | public function equals( $target ) { |
||
| 489 | |||
| 490 | } |
||
| 491 |
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: