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 | public 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 | * @deprecated Kept for compatibility with older DataValues versions. |
||
212 | * Do not use. |
||
213 | * |
||
214 | * @return float |
||
215 | */ |
||
216 | public function getSortKey() { |
||
219 | |||
220 | /** |
||
221 | * Returns the value as a decimal string, using the format described in the class level |
||
222 | * documentation of @see DecimalValue and matching @see DecimalValue::QUANTITY_VALUE_PATTERN. |
||
223 | * In particular, the string always starts with a sign (either '+' or '-') |
||
224 | * and has no leading zeros (except immediately before the decimal point). The decimal point is |
||
225 | * optional, but must not be the last character. Trailing zeros are significant. |
||
226 | * |
||
227 | * @see DataValue::getValue |
||
228 | * |
||
229 | * @return string |
||
230 | 76 | */ |
|
231 | 76 | public function getValue() { |
|
234 | |||
235 | /** |
||
236 | * Returns the sign of the amount (+ or -). |
||
237 | * |
||
238 | * @return string "+" or "-". |
||
239 | 17 | */ |
|
240 | 17 | public function getSign() { |
|
243 | |||
244 | /** |
||
245 | * Determines whether this DecimalValue is zero. |
||
246 | * |
||
247 | * @return bool |
||
248 | 128 | */ |
|
249 | 128 | public function isZero() { |
|
252 | |||
253 | /** |
||
254 | * Returns a new DecimalValue that represents the complement of this DecimalValue. |
||
255 | * That is, it constructs a new DecimalValue with the same digits as this, |
||
256 | * but with the sign inverted. |
||
257 | * |
||
258 | * Note that if isZero() returns true, this method returns this |
||
259 | * DecimalValue itself (because zero is it's own complement). |
||
260 | * |
||
261 | * @return self |
||
262 | 8 | */ |
|
263 | 8 | public function computeComplement() { |
|
274 | |||
275 | /** |
||
276 | * Returns a new DecimalValue that represents the absolute (positive) value |
||
277 | * of this DecimalValue. That is, it constructs a new DecimalValue with the |
||
278 | * same digits as this, but with the positive sign. |
||
279 | * |
||
280 | * Note that if getSign() returns "+", this method returns this |
||
281 | * DecimalValue itself (because a positive value is its own absolute value). |
||
282 | * |
||
283 | * @return self |
||
284 | 7 | */ |
|
285 | 7 | public function computeAbsolute() { |
|
292 | |||
293 | /** |
||
294 | * Returns the integer part of the value, that is, the part before the decimal point, |
||
295 | * without the sign. |
||
296 | * |
||
297 | * @return string |
||
298 | 28 | */ |
|
299 | 28 | public function getIntegerPart() { |
|
308 | |||
309 | /** |
||
310 | * Returns the fractional part of the value, that is, the part after the decimal point, |
||
311 | * if any. |
||
312 | * |
||
313 | * @return string |
||
314 | 8 | */ |
|
315 | 8 | public function getFractionalPart() { |
|
324 | |||
325 | /** |
||
326 | * Returns a DecimalValue with the same digits as this one, but with any trailing zeros |
||
327 | * after the decimal point removed. If there are no trailing zeros after the decimal |
||
328 | * point, this method will return $this. |
||
329 | * |
||
330 | * @return self |
||
331 | 10 | */ |
|
332 | 10 | public function getTrimmed() { |
|
342 | |||
343 | /** |
||
344 | * Returns the value held by this object, as a float. |
||
345 | * Equivalent to floatval( $this->getvalue() ). |
||
346 | * |
||
347 | * @return float |
||
348 | 13 | */ |
|
349 | 13 | public function getValueFloat() { |
|
352 | |||
353 | /** |
||
354 | * @see DataValue::getArrayValue |
||
355 | * |
||
356 | * @return string |
||
357 | 39 | */ |
|
358 | 39 | public function getArrayValue() { |
|
361 | |||
362 | /** |
||
363 | * Constructs a new instance from the provided data. Required for @see DataValueDeserializer. |
||
364 | * This is expected to round-trip with @see getArrayValue. |
||
365 | * |
||
366 | * @deprecated since 0.8.3. Static DataValue::newFromArray constructors like this are |
||
367 | * underspecified (not in the DataValue interface), and misleadingly named (should be named |
||
368 | * newFromArrayValue). Instead, use DataValue builder callbacks in @see DataValueDeserializer. |
||
369 | * |
||
370 | * @param mixed $data Warning! Even if this is expected to be a value as returned by |
||
371 | * @see getArrayValue, callers of this specific newFromArray implementation can not guarantee |
||
372 | * this. This is not guaranteed to be a string! |
||
373 | * |
||
374 | * @throws InvalidArgumentException if $data is not in the expected format. Subclasses of |
||
375 | * InvalidArgumentException are expected and properly handled by @see DataValueDeserializer. |
||
376 | * @return self |
||
377 | */ |
||
378 | public static function newFromArray( $data ) { |
||
381 | |||
382 | /** |
||
383 | * @return string |
||
384 | 1 | */ |
|
385 | 1 | public function __toString() { |
|
388 | |||
389 | } |
||
390 |