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 IllegalValueException |
||
| 52 | */ |
||
| 53 | 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 | private function convertToDecimal( $number ) { |
||
| 90 | if ( $number === NAN || abs( $number ) === INF ) { |
||
| 91 | throw new InvalidArgumentException( '$number must not be NAN or INF.' ); |
||
| 92 | } |
||
| 93 | |||
| 94 | if ( is_int( $number ) || $number === (float)(int)$number ) { |
||
| 95 | $decimal = strval( (int)abs( $number ) ); |
||
| 96 | } else { |
||
| 97 | $decimal = trim( number_format( abs( $number ), 100, '.', '' ), '0' ); |
||
| 98 | |||
| 99 | if ( $decimal[0] === '.' ) { |
||
| 100 | $decimal = '0' . $decimal; |
||
| 101 | } |
||
| 102 | |||
| 103 | if ( substr( $decimal, -1 ) === '.' ) { |
||
| 104 | $decimal = substr( $decimal, 0, -1 ); |
||
| 105 | } |
||
| 106 | } |
||
| 107 | |||
| 108 | $decimal = ( ( $number >= 0.0 ) ? '+' : '-' ) . $decimal; |
||
| 109 | return $decimal; |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Compares this DecimalValue to another DecimalValue. |
||
| 114 | * |
||
| 115 | * @since 0.1 |
||
| 116 | * |
||
| 117 | * @param self $that |
||
| 118 | * |
||
| 119 | * @throws LogicException |
||
| 120 | * @return int +1 if $this > $that, 0 if $this == $that, -1 if $this < $that |
||
| 121 | */ |
||
| 122 | public function compare( self $that ) { |
||
| 175 | |||
| 176 | /** |
||
| 177 | * @see Serializable::serialize |
||
| 178 | * |
||
| 179 | * @return string |
||
| 180 | */ |
||
| 181 | public function serialize() { |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @see Serializable::unserialize |
||
| 187 | * |
||
| 188 | * @param string $data |
||
| 189 | */ |
||
| 190 | public function unserialize( $data ) { |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @see DataValue::getType |
||
| 196 | * |
||
| 197 | * @return string |
||
| 198 | */ |
||
| 199 | public static function getType() { |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @see DataValue::getSortKey |
||
| 205 | * |
||
| 206 | * @return float |
||
| 207 | */ |
||
| 208 | public function getSortKey() { |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Returns the value as a decimal string, using the format described in the class level |
||
| 214 | * documentation of @see DecimalValue and matching @see DecimalValue::QUANTITY_VALUE_PATTERN. |
||
| 215 | * In particular, the string always starts with a sign (either '+' or '-') |
||
| 216 | * and has no leading zeros (except immediately before the decimal point). The decimal point is |
||
| 217 | * optional, but must not be the last character. Trailing zeros are significant. |
||
| 218 | * |
||
| 219 | * @see DataValue::getValue |
||
| 220 | * |
||
| 221 | * @return string |
||
| 222 | */ |
||
| 223 | public function getValue() { |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Returns the sign of the amount (+ or -). |
||
| 229 | * |
||
| 230 | * @since 0.1 |
||
| 231 | * |
||
| 232 | * @return string "+" or "-". |
||
| 233 | */ |
||
| 234 | public function getSign() { |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Determines whether this DecimalValue is zero. |
||
| 240 | * |
||
| 241 | * @return bool |
||
| 242 | */ |
||
| 243 | public function isZero() { |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Returns a new DecimalValue that represents the complement of this DecimalValue. |
||
| 249 | * That is, it constructs a new DecimalValue with the same digits as this, |
||
| 250 | * but with the sign inverted. |
||
| 251 | * |
||
| 252 | * Note that if isZero() returns true, this method returns this |
||
| 253 | * DecimalValue itself (because zero is it's own complement). |
||
| 254 | * |
||
| 255 | * @return self |
||
| 256 | */ |
||
| 257 | public function computeComplement() { |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Returns a new DecimalValue that represents the absolute (positive) value |
||
| 271 | * of this DecimalValue. That is, it constructs a new DecimalValue with the |
||
| 272 | * same digits as this, but with the positive sign. |
||
| 273 | * |
||
| 274 | * Note that if getSign() returns "+", this method returns this |
||
| 275 | * DecimalValue itself (because a positive value is its own absolute value). |
||
| 276 | * |
||
| 277 | * @return self |
||
| 278 | */ |
||
| 279 | public function computeAbsolute() { |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Returns the integer part of the value, that is, the part before the decimal point, |
||
| 289 | * without the sign. |
||
| 290 | * |
||
| 291 | * @since 0.1 |
||
| 292 | * |
||
| 293 | * @return string |
||
| 294 | */ |
||
| 295 | public function getIntegerPart() { |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Returns the fractional part of the value, that is, the part after the decimal point, |
||
| 307 | * if any. |
||
| 308 | * |
||
| 309 | * @since 0.1 |
||
| 310 | * |
||
| 311 | * @return string |
||
| 312 | */ |
||
| 313 | public function getFractionalPart() { |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Returns a DecimalValue with the same digits as this one, but with any trailing zeros |
||
| 325 | * after the decimal point removed. If there are no trailing zeros after the decimal |
||
| 326 | * point, this method will return $this. |
||
| 327 | * |
||
| 328 | * @return DecimalValue |
||
| 329 | */ |
||
| 330 | public function getTrimmed() { |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Returns the value held by this object, as a float. |
||
| 343 | * Equivalent to floatval( $this->getvalue() ). |
||
| 344 | * |
||
| 345 | * @since 0.1 |
||
| 346 | * |
||
| 347 | * @return float |
||
| 348 | */ |
||
| 349 | public function getValueFloat() { |
||
| 352 | |||
| 353 | /** |
||
| 354 | * @see DataValue::getArrayValue |
||
| 355 | * |
||
| 356 | * @return string |
||
| 357 | */ |
||
| 358 | public function getArrayValue() { |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Constructs a new instance of the DataValue from the provided data. |
||
| 364 | * This can round-trip with @see getArrayValue |
||
| 365 | * |
||
| 366 | * @param string|int|float $data |
||
| 367 | * |
||
| 368 | * @return self |
||
| 369 | * @throws IllegalValueException |
||
| 370 | */ |
||
| 371 | public static function newFromArray( $data ) { |
||
| 374 | |||
| 375 | /** |
||
| 376 | * @return string |
||
| 377 | */ |
||
| 378 | public function __toString() { |
||
| 381 | |||
| 382 | } |
||
| 383 |