Complex classes like DecimalValue 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 DecimalValue, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 29 | class DecimalValue extends DataValueObject { |
||
| 30 | |||
| 31 | /** |
||
| 32 | * The $value as a decimal string, in the format described in the class |
||
| 33 | * level documentation of @see DecimalValue, matching @see QUANTITY_VALUE_PATTERN. |
||
| 34 | * |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | private $value; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Regular expression for matching decimal strings that conform to the format |
||
| 41 | * described in the class level documentation of @see DecimalValue. |
||
| 42 | */ |
||
| 43 | const QUANTITY_VALUE_PATTERN = '/^[-+]([1-9]\d*|\d)(\.\d+)?\z/'; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Constructs a new DecimalValue object, representing the given value. |
||
| 47 | * |
||
| 48 | * @param string|int|float $value If given as a string, the value must match |
||
| 49 | * QUANTITY_VALUE_PATTERN. The leading plus sign is optional. |
||
| 50 | * |
||
| 51 | * @throws InvalidArgumentException |
||
| 52 | */ |
||
| 53 | 143 | public function __construct( $value ) { |
|
| 78 | |||
| 79 | /** |
||
| 80 | * Converts the given number to decimal notation. The resulting string conforms to the |
||
| 81 | * rules described in the class level documentation of @see DecimalValue and matches |
||
| 82 | * @see DecimalValue::QUANTITY_VALUE_PATTERN. |
||
| 83 | * |
||
| 84 | * @param int|float $number |
||
| 85 | * |
||
| 86 | * @return string |
||
| 87 | * @throws InvalidArgumentException |
||
| 88 | */ |
||
| 89 | 33 | private function convertToDecimal( $number ) { |
|
| 120 | |||
| 121 | /** |
||
| 122 | * Compares this DecimalValue to another DecimalValue. |
||
| 123 | * |
||
| 124 | * @param self $that |
||
| 125 | * |
||
| 126 | * @throws LogicException |
||
| 127 | * @return int +1 if $this > $that, 0 if $this == $that, -1 if $this < $that |
||
| 128 | */ |
||
| 129 | 19 | public function compare( self $that ) { |
|
| 182 | |||
| 183 | /** |
||
| 184 | * @see Serializable::serialize |
||
| 185 | * |
||
| 186 | * @return string |
||
| 187 | */ |
||
| 188 | 58 | public function serialize() { |
|
| 191 | |||
| 192 | /** |
||
| 193 | * @see Serializable::unserialize |
||
| 194 | * |
||
| 195 | * @param string $data |
||
| 196 | */ |
||
| 197 | 57 | public function unserialize( $data ) { |
|
| 200 | |||
| 201 | /** |
||
| 202 | * @see DataValue::getType |
||
| 203 | * |
||
| 204 | * @return string |
||
| 205 | */ |
||
| 206 | 38 | public static function getType() { |
|
| 209 | |||
| 210 | /** |
||
| 211 | * @see DataValue::getSortKey |
||
| 212 | * |
||
| 213 | * @return float |
||
| 214 | */ |
||
| 215 | public function getSortKey() { |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Returns the value as a decimal string, using the format described in the class level |
||
| 221 | * documentation of @see DecimalValue and matching @see DecimalValue::QUANTITY_VALUE_PATTERN. |
||
| 222 | * In particular, the string always starts with a sign (either '+' or '-') |
||
| 223 | * and has no leading zeros (except immediately before the decimal point). The decimal point is |
||
| 224 | * optional, but must not be the last character. Trailing zeros are significant. |
||
| 225 | * |
||
| 226 | * @see DataValue::getValue |
||
| 227 | * |
||
| 228 | * @return string |
||
| 229 | */ |
||
| 230 | 76 | public function getValue() { |
|
| 233 | |||
| 234 | /** |
||
| 235 | * Returns the sign of the amount (+ or -). |
||
| 236 | * |
||
| 237 | * @return string "+" or "-". |
||
| 238 | */ |
||
| 239 | 17 | public function getSign() { |
|
| 242 | |||
| 243 | /** |
||
| 244 | * Determines whether this DecimalValue is zero. |
||
| 245 | * |
||
| 246 | * @return bool |
||
| 247 | */ |
||
| 248 | 128 | public function isZero() { |
|
| 251 | |||
| 252 | /** |
||
| 253 | * Returns a new DecimalValue that represents the complement of this DecimalValue. |
||
| 254 | * That is, it constructs a new DecimalValue with the same digits as this, |
||
| 255 | * but with the sign inverted. |
||
| 256 | * |
||
| 257 | * Note that if isZero() returns true, this method returns this |
||
| 258 | * DecimalValue itself (because zero is it's own complement). |
||
| 259 | * |
||
| 260 | * @return self |
||
| 261 | */ |
||
| 262 | 8 | public function computeComplement() { |
|
| 273 | |||
| 274 | /** |
||
| 275 | * Returns a new DecimalValue that represents the absolute (positive) value |
||
| 276 | * of this DecimalValue. That is, it constructs a new DecimalValue with the |
||
| 277 | * same digits as this, but with the positive sign. |
||
| 278 | * |
||
| 279 | * Note that if getSign() returns "+", this method returns this |
||
| 280 | * DecimalValue itself (because a positive value is its own absolute value). |
||
| 281 | * |
||
| 282 | * @return self |
||
| 283 | */ |
||
| 284 | 7 | public function computeAbsolute() { |
|
| 291 | |||
| 292 | /** |
||
| 293 | * Returns the integer part of the value, that is, the part before the decimal point, |
||
| 294 | * without the sign. |
||
| 295 | * |
||
| 296 | * @return string |
||
| 297 | */ |
||
| 298 | 28 | public function getIntegerPart() { |
|
| 307 | |||
| 308 | /** |
||
| 309 | * Returns the fractional part of the value, that is, the part after the decimal point, |
||
| 310 | * if any. |
||
| 311 | * |
||
| 312 | * @return string |
||
| 313 | */ |
||
| 314 | 8 | public function getFractionalPart() { |
|
| 323 | |||
| 324 | /** |
||
| 325 | * Returns a DecimalValue with the same digits as this one, but with any trailing zeros |
||
| 326 | * after the decimal point removed. If there are no trailing zeros after the decimal |
||
| 327 | * point, this method will return $this. |
||
| 328 | * |
||
| 329 | * @return self |
||
| 330 | */ |
||
| 331 | 10 | public function getTrimmed() { |
|
| 341 | |||
| 342 | /** |
||
| 343 | * Returns the value held by this object, as a float. |
||
| 344 | * Equivalent to floatval( $this->getvalue() ). |
||
| 345 | * |
||
| 346 | * @return float |
||
| 347 | */ |
||
| 348 | 13 | public function getValueFloat() { |
|
| 351 | |||
| 352 | /** |
||
| 353 | * @see DataValue::getArrayValue |
||
| 354 | * |
||
| 355 | * @return string |
||
| 356 | */ |
||
| 357 | 39 | public function getArrayValue() { |
|
| 360 | |||
| 361 | /** |
||
| 362 | * Constructs a new instance from the provided data. Required for @see DataValueDeserializer. |
||
| 363 | * This is expected to round-trip with @see getArrayValue. |
||
| 364 | * |
||
| 365 | * @deprecated since 0.8.3. Static DataValue::newFromArray constructors like this are |
||
| 366 | * underspecified (not in the DataValue interface), and misleadingly named (should be named |
||
| 367 | * newFromArrayValue). Instead, use DataValue builder callbacks in @see DataValueDeserializer. |
||
| 368 | * |
||
| 369 | * @param mixed $data Warning! Even if this is expected to be a value as returned by |
||
| 370 | * @see getArrayValue, callers of this specific newFromArray implementation can not guarantee |
||
| 371 | * this. This is not guaranteed to be a string! |
||
| 372 | * |
||
| 373 | * @throws InvalidArgumentException if $data is not in the expected format. Subclasses of |
||
| 374 | * InvalidArgumentException are expected and properly handled by @see DataValueDeserializer. |
||
| 375 | * @return self |
||
| 376 | */ |
||
| 377 | public static function newFromArray( $data ) { |
||
| 380 | |||
| 381 | /** |
||
| 382 | * @return string |
||
| 383 | */ |
||
| 384 | 1 | public function __toString() { |
|
| 387 | |||
| 388 | } |
||
| 389 |