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 | 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 ) { |
||
118 | |||
119 | /** |
||
120 | * Compares this DecimalValue to another DecimalValue. |
||
121 | * |
||
122 | * @param self $that |
||
123 | * |
||
124 | * @throws LogicException |
||
125 | * @return int +1 if $this > $that, 0 if $this == $that, -1 if $this < $that |
||
126 | */ |
||
127 | public function compare( self $that ) { |
||
180 | |||
181 | /** |
||
182 | * @see Serializable::serialize |
||
183 | * |
||
184 | * @return string |
||
185 | */ |
||
186 | public function serialize() { |
||
189 | |||
190 | /** |
||
191 | * @see Serializable::unserialize |
||
192 | * |
||
193 | * @param string $data |
||
194 | */ |
||
195 | public function unserialize( $data ) { |
||
198 | |||
199 | /** |
||
200 | * @see DataValue::getType |
||
201 | * |
||
202 | * @return string |
||
203 | */ |
||
204 | public static function getType() { |
||
207 | |||
208 | /** |
||
209 | * @see DataValue::getSortKey |
||
210 | * |
||
211 | * @return float |
||
212 | */ |
||
213 | public function getSortKey() { |
||
216 | |||
217 | /** |
||
218 | * Returns the value as a decimal string, using the format described in the class level |
||
219 | * documentation of @see DecimalValue and matching @see DecimalValue::QUANTITY_VALUE_PATTERN. |
||
220 | * In particular, the string always starts with a sign (either '+' or '-') |
||
221 | * and has no leading zeros (except immediately before the decimal point). The decimal point is |
||
222 | * optional, but must not be the last character. Trailing zeros are significant. |
||
223 | * |
||
224 | * @see DataValue::getValue |
||
225 | * |
||
226 | * @return string |
||
227 | */ |
||
228 | public function getValue() { |
||
231 | |||
232 | /** |
||
233 | * Returns the sign of the amount (+ or -). |
||
234 | * |
||
235 | * @return string "+" or "-". |
||
236 | */ |
||
237 | public function getSign() { |
||
240 | |||
241 | /** |
||
242 | * Determines whether this DecimalValue is zero. |
||
243 | * |
||
244 | * @return bool |
||
245 | */ |
||
246 | public function isZero() { |
||
249 | |||
250 | /** |
||
251 | * Returns a new DecimalValue that represents the complement of this DecimalValue. |
||
252 | * That is, it constructs a new DecimalValue with the same digits as this, |
||
253 | * but with the sign inverted. |
||
254 | * |
||
255 | * Note that if isZero() returns true, this method returns this |
||
256 | * DecimalValue itself (because zero is it's own complement). |
||
257 | * |
||
258 | * @return self |
||
259 | */ |
||
260 | public function computeComplement() { |
||
271 | |||
272 | /** |
||
273 | * Returns a new DecimalValue that represents the absolute (positive) value |
||
274 | * of this DecimalValue. That is, it constructs a new DecimalValue with the |
||
275 | * same digits as this, but with the positive sign. |
||
276 | * |
||
277 | * Note that if getSign() returns "+", this method returns this |
||
278 | * DecimalValue itself (because a positive value is its own absolute value). |
||
279 | * |
||
280 | * @return self |
||
281 | */ |
||
282 | public function computeAbsolute() { |
||
289 | |||
290 | /** |
||
291 | * Returns the integer part of the value, that is, the part before the decimal point, |
||
292 | * without the sign. |
||
293 | * |
||
294 | * @return string |
||
295 | */ |
||
296 | public function getIntegerPart() { |
||
305 | |||
306 | /** |
||
307 | * Returns the fractional part of the value, that is, the part after the decimal point, |
||
308 | * if any. |
||
309 | * |
||
310 | * @return string |
||
311 | */ |
||
312 | public function getFractionalPart() { |
||
321 | |||
322 | /** |
||
323 | * Returns a DecimalValue with the same digits as this one, but with any trailing zeros |
||
324 | * after the decimal point removed. If there are no trailing zeros after the decimal |
||
325 | * point, this method will return $this. |
||
326 | * |
||
327 | * @return self |
||
328 | */ |
||
329 | public function getTrimmed() { |
||
339 | |||
340 | /** |
||
341 | * Returns the value held by this object, as a float. |
||
342 | * Equivalent to floatval( $this->getvalue() ). |
||
343 | * |
||
344 | * @return float |
||
345 | */ |
||
346 | public function getValueFloat() { |
||
349 | |||
350 | /** |
||
351 | * @see DataValue::getArrayValue |
||
352 | * |
||
353 | * @return string |
||
354 | */ |
||
355 | public function getArrayValue() { |
||
358 | |||
359 | /** |
||
360 | * Constructs a new instance from the provided data. Required for @see DataValueDeserializer. |
||
361 | * This is expected to round-trip with @see getArrayValue. |
||
362 | * |
||
363 | * @deprecated since 0.8.3. Static DataValue::newFromArray constructors like this are |
||
364 | * underspecified (not in the DataValue interface), and misleadingly named (should be named |
||
365 | * newFromArrayValue). Instead, use DataValue builder callbacks in @see DataValueDeserializer. |
||
366 | * |
||
367 | * @param mixed $data Warning! Even if this is expected to be a value as returned by |
||
368 | * @see getArrayValue, callers of this specific newFromArray implementation can not guarantee |
||
369 | * this. This is not guaranteed to be a string! |
||
370 | * |
||
371 | * @throws InvalidArgumentException if $data is not in the expected format. Subclasses of |
||
372 | * InvalidArgumentException are expected and properly handled by @see DataValueDeserializer. |
||
373 | * @return self |
||
374 | */ |
||
375 | public static function newFromArray( $data ) { |
||
378 | |||
379 | /** |
||
380 | * @return string |
||
381 | */ |
||
382 | public function __toString() { |
||
385 | |||
386 | } |
||
387 |