Complex classes like Kohana_Jam_Price 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 Kohana_Jam_Price, and based on these observations, apply Extract Interface, too.
| 1 | <?php defined('SYSPATH') OR die('No direct script access.'); |
||
| 11 | class Kohana_Jam_Price implements Serializable { |
||
| 12 | |||
| 13 | const EQUAL_TO = '='; |
||
| 14 | const GREATER_THAN = '>'; |
||
| 15 | const LESS_THAN = '<'; |
||
| 16 | const GREATER_THAN_OR_EQUAL_TO = '>='; |
||
| 17 | const LESS_THAN_OR_EQUAL_TO = '<='; |
||
| 18 | |||
| 19 | const EPSILON = 0.000001; |
||
| 20 | |||
| 21 | 1 | public static function min(array $prices) |
|
| 35 | |||
| 36 | 1 | public static function max(array $prices) |
|
| 50 | |||
| 51 | 5 | public static function ceil($amount, $precision) |
|
| 57 | |||
| 58 | 1 | public static function sum(array $prices, $currency, $monetary = NULL, $display_currency = NULL, $ceil_on_convert = FALSE) |
|
| 80 | |||
| 81 | protected $_amount = 0; |
||
| 82 | protected $_currency; |
||
| 83 | protected $_monetary; |
||
| 84 | protected $_ceil_on_convert = FALSE; |
||
| 85 | protected $_display_currency; |
||
| 86 | |||
| 87 | 3 | public function ceil_on_convert($ceil_on_convert = NULL) |
|
| 96 | |||
| 97 | 2 | public function display_currency($display_currency = NULL) |
|
| 106 | |||
| 107 | /** |
||
| 108 | * Getter / Setter of amount, casts to float |
||
| 109 | * @param float $amount |
||
| 110 | * @return float|$this |
||
| 111 | */ |
||
| 112 | 3 | public function amount($amount = NULL) |
|
| 121 | |||
| 122 | /** |
||
| 123 | * Getter / Setter |
||
| 124 | * @param string $currency |
||
| 125 | * @return string|$this |
||
| 126 | */ |
||
| 127 | 3 | public function currency($currency = NULL) |
|
| 136 | |||
| 137 | /** |
||
| 138 | * Getter / Setter, defaults to Monetary::instance() |
||
| 139 | * @param OpenBuildings\Monetary\Monetary $monetary |
||
| 140 | * @return OpenBuildings\Monetary\Monetary|$this |
||
| 141 | */ |
||
| 142 | 3 | public function monetary($monetary = NULL) |
|
| 157 | |||
| 158 | 3 | public function __construct($amount, $currency, $monetary = NULL, $display_currency = NULL, $ceil_on_convert = FALSE) |
|
| 166 | |||
| 167 | /** |
||
| 168 | * Use as_string method |
||
| 169 | * @return string |
||
| 170 | */ |
||
| 171 | 2 | public function __toString() |
|
| 175 | |||
| 176 | /** |
||
| 177 | * Use number_format with 2 digits after the decimal dot |
||
| 178 | * @return string |
||
| 179 | */ |
||
| 180 | 2 | public function as_string($currency = NULL) |
|
| 184 | |||
| 185 | /** |
||
| 186 | * Use Monetary's "format" method |
||
| 187 | * @param string $currency optionally convert to another currency |
||
| 188 | * @return string |
||
| 189 | */ |
||
| 190 | 1 | public function humanize($currency = NULL) |
|
| 196 | |||
| 197 | /** |
||
| 198 | * Replace HTML entities in humanize |
||
| 199 | * @param string $currency optionally convert to another currency |
||
| 200 | * @return string |
||
| 201 | */ |
||
| 202 | 1 | public function as_html($currency = NULL) |
|
| 206 | |||
| 207 | /** |
||
| 208 | * Perform price arithmetic - add / remove prices with correct currency convertions |
||
| 209 | * @return $this |
||
| 210 | */ |
||
| 211 | 1 | public function add($price) |
|
| 218 | |||
| 219 | /** |
||
| 220 | * Myltiply by a value |
||
| 221 | * @param mixed $value |
||
| 222 | * @return Jam_Price |
||
| 223 | */ |
||
| 224 | 1 | public function multiply_by($value) |
|
| 228 | |||
| 229 | /** |
||
| 230 | * Convert the price to a different currency |
||
| 231 | * @param string $currency |
||
| 232 | * @return Jam_Price |
||
| 233 | */ |
||
| 234 | 1 | public function convert_to($currency) |
|
| 238 | |||
| 239 | /** |
||
| 240 | * Perform price comparation, e.g. =, >, <, => or =<. Performs currency conversion if nesessary |
||
| 241 | * @return boolean |
||
| 242 | * @param string $operator |
||
| 243 | * @param mixed value |
||
| 244 | */ |
||
| 245 | 2 | public function is($operator, $value) |
|
| 274 | |||
| 275 | /** |
||
| 276 | * Display the amount in a currency |
||
| 277 | * @param string|null $currency |
||
| 278 | * @param OpenBuildings\Monetary\Monetary|null $monetary |
||
| 279 | * @return float |
||
| 280 | */ |
||
| 281 | 2 | public function in($currency = NULL, $monetary = NULL) |
|
| 301 | |||
| 302 | /** |
||
| 303 | * implement Serializable |
||
| 304 | * @return string |
||
| 305 | */ |
||
| 306 | 1 | public function serialize() |
|
| 310 | |||
| 311 | /** |
||
| 312 | * implement Serializable |
||
| 313 | * @param string $data |
||
| 314 | */ |
||
| 315 | 1 | public function unserialize($data) |
|
| 325 | } |
||
| 326 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.