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. |
||
50 | * |
||
51 | * @throws IllegalValueException |
||
52 | */ |
||
53 | public function __construct( $value ) { |
||
76 | |||
77 | /** |
||
78 | * Converts the given number to decimal notation. The resulting string conforms to the |
||
79 | * rules described in the class level documentation of @see DecimalValue and matches |
||
80 | * @see DecimalValue::QUANTITY_VALUE_PATTERN. |
||
81 | * |
||
82 | * @param int|float $number |
||
83 | * |
||
84 | * @return string |
||
85 | * @throws InvalidArgumentException |
||
86 | */ |
||
87 | private function convertToDecimal( $number ) { |
||
109 | |||
110 | /** |
||
111 | * Compares this DecimalValue to another DecimalValue. |
||
112 | * |
||
113 | * @since 0.1 |
||
114 | * |
||
115 | * @param self $that |
||
116 | * |
||
117 | * @throws LogicException |
||
118 | * @return int +1 if $this > $that, 0 if $this == $that, -1 if $this < $that |
||
119 | */ |
||
120 | public function compare( self $that ) { |
||
173 | |||
174 | /** |
||
175 | * @see Serializable::serialize |
||
176 | * |
||
177 | * @return string |
||
178 | */ |
||
179 | public function serialize() { |
||
182 | |||
183 | /** |
||
184 | * @see Serializable::unserialize |
||
185 | * |
||
186 | * @param string $data |
||
187 | */ |
||
188 | public function unserialize( $data ) { |
||
191 | |||
192 | /** |
||
193 | * @see DataValue::getType |
||
194 | * |
||
195 | * @return string |
||
196 | */ |
||
197 | public static function getType() { |
||
200 | |||
201 | /** |
||
202 | * @see DataValue::getSortKey |
||
203 | * |
||
204 | * @return float |
||
205 | */ |
||
206 | public function getSortKey() { |
||
209 | |||
210 | /** |
||
211 | * Returns the value as a decimal string, using the format described in the class level |
||
212 | * documentation of @see DecimalValue and matching @see DecimalValue::QUANTITY_VALUE_PATTERN. |
||
213 | * In particular, the string always starts with a sign (either '+' or '-') |
||
214 | * and has no leading zeros (except immediately before the decimal point). The decimal point is |
||
215 | * optional, but must not be the last character. Trailing zeros are significant. |
||
216 | * |
||
217 | * @see DataValue::getValue |
||
218 | * |
||
219 | * @return string |
||
220 | */ |
||
221 | public function getValue() { |
||
224 | |||
225 | /** |
||
226 | * Returns the sign of the amount (+ or -). |
||
227 | * |
||
228 | * @since 0.1 |
||
229 | * |
||
230 | * @return string "+" or "-". |
||
231 | */ |
||
232 | public function getSign() { |
||
235 | |||
236 | /** |
||
237 | * Determines whether this DecimalValue is zero. |
||
238 | * |
||
239 | * @return bool |
||
240 | */ |
||
241 | public function isZero() { |
||
244 | |||
245 | /** |
||
246 | * Returns a new DecimalValue that represents the complement of this DecimalValue. |
||
247 | * That is, it constructs a new DecimalValue with the same digits as this, |
||
248 | * but with the sign inverted. |
||
249 | * |
||
250 | * Note that if isZero() returns true, this method returns this |
||
251 | * DecimalValue itself (because zero is it's own complement). |
||
252 | * |
||
253 | * @return self |
||
254 | */ |
||
255 | public function computeComplement() { |
||
266 | |||
267 | /** |
||
268 | * Returns a new DecimalValue that represents the absolute (positive) value |
||
269 | * of this DecimalValue. That is, it constructs a new DecimalValue with the |
||
270 | * same digits as this, but with the positive sign. |
||
271 | * |
||
272 | * Note that if getSign() returns "+", this method returns this |
||
273 | * DecimalValue itself (because a positive value is its own absolute value). |
||
274 | * |
||
275 | * @return self |
||
276 | */ |
||
277 | public function computeAbsolute() { |
||
284 | |||
285 | /** |
||
286 | * Returns the integer part of the value, that is, the part before the decimal point, |
||
287 | * without the sign. |
||
288 | * |
||
289 | * @since 0.1 |
||
290 | * |
||
291 | * @return string |
||
292 | */ |
||
293 | public function getIntegerPart() { |
||
302 | |||
303 | /** |
||
304 | * Returns the fractional part of the value, that is, the part after the decimal point, |
||
305 | * if any. |
||
306 | * |
||
307 | * @since 0.1 |
||
308 | * |
||
309 | * @return string |
||
310 | */ |
||
311 | public function getFractionalPart() { |
||
320 | |||
321 | /** |
||
322 | * Returns the value held by this object, as a float. |
||
323 | * Equivalent to floatval( $this->getvalue() ). |
||
324 | * |
||
325 | * @since 0.1 |
||
326 | * |
||
327 | * @return float |
||
328 | */ |
||
329 | public function getValueFloat() { |
||
332 | |||
333 | /** |
||
334 | * @see DataValue::getArrayValue |
||
335 | * |
||
336 | * @return string |
||
337 | */ |
||
338 | public function getArrayValue() { |
||
341 | |||
342 | /** |
||
343 | * Constructs a new instance of the DataValue from the provided data. |
||
344 | * This can round-trip with @see getArrayValue |
||
345 | * |
||
346 | * @param string|int|float $data |
||
347 | * |
||
348 | * @return self |
||
349 | * @throws IllegalValueException |
||
350 | */ |
||
351 | public static function newFromArray( $data ) { |
||
354 | |||
355 | /** |
||
356 | * Create DecimalValue from regular numeric string or value. |
||
357 | * @param int|float|string $number |
||
358 | * |
||
359 | * @return self |
||
360 | */ |
||
361 | public static function makeDecimalValue( $number ) { |
||
371 | |||
372 | |||
373 | /** |
||
374 | * @return string |
||
375 | */ |
||
376 | public function __toString() { |
||
379 | |||
380 | } |
||
381 |