Complex classes like Money 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 Money, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | class Money extends DBField implements CompositeDBField { |
||
26 | |||
27 | /** |
||
28 | * @var string $getCurrency() |
||
29 | */ |
||
30 | protected $currency; |
||
31 | |||
32 | /** |
||
33 | * @var float $currencyAmount |
||
34 | */ |
||
35 | protected $amount; |
||
36 | |||
37 | /** |
||
38 | * @var boolean $isChanged |
||
39 | */ |
||
40 | protected $isChanged = false; |
||
41 | |||
42 | /** |
||
43 | * @var string $locale |
||
44 | */ |
||
45 | protected $locale = null; |
||
46 | |||
47 | /** |
||
48 | * @var Zend_Currency |
||
49 | */ |
||
50 | protected $currencyLib; |
||
51 | |||
52 | /** |
||
53 | * Limit the currencies |
||
54 | * @var array $allowedCurrencies |
||
55 | */ |
||
56 | protected $allowedCurrencies; |
||
57 | |||
58 | /** |
||
59 | * @param array |
||
60 | */ |
||
61 | private static $composite_db = array( |
||
62 | "Currency" => "Varchar(3)", |
||
63 | "Amount" => 'Decimal(19,4)' |
||
64 | ); |
||
65 | |||
66 | public function __construct($name = null) { |
||
71 | |||
72 | public function compositeDatabaseFields() { |
||
75 | |||
76 | public function requireField() { |
||
82 | |||
83 | public function writeToManipulation(&$manipulation) { |
||
98 | |||
99 | public function addToQuery(&$query) { |
||
104 | |||
105 | public function setValue($value, $record = null, $markChanged = true) { |
||
142 | |||
143 | /** |
||
144 | * @return string |
||
145 | */ |
||
146 | public function Nice($options = array()) { |
||
155 | |||
156 | /** |
||
157 | * @return string |
||
158 | */ |
||
159 | public function NiceWithShortname($options = array()){ |
||
163 | |||
164 | /** |
||
165 | * @return string |
||
166 | */ |
||
167 | public function NiceWithName($options = array()){ |
||
171 | |||
172 | /** |
||
173 | * @return string |
||
174 | */ |
||
175 | public function getCurrency() { |
||
178 | |||
179 | /** |
||
180 | * @param string |
||
181 | */ |
||
182 | public function setCurrency($currency, $markChanged = true) { |
||
186 | |||
187 | /** |
||
188 | * @todo Return casted Float DBField? |
||
189 | * |
||
190 | * @return float |
||
191 | */ |
||
192 | public function getAmount() { |
||
195 | |||
196 | /** |
||
197 | * @param float $amount |
||
198 | */ |
||
199 | public function setAmount($amount, $markChanged = true) { |
||
203 | |||
204 | /** |
||
205 | * @return boolean |
||
206 | */ |
||
207 | public function exists() { |
||
210 | |||
211 | /** |
||
212 | * @return boolean |
||
213 | */ |
||
214 | public function hasAmount() { |
||
218 | |||
219 | public function isChanged() { |
||
222 | |||
223 | /** |
||
224 | * @param string $locale |
||
225 | */ |
||
226 | public function setLocale($locale) { |
||
230 | |||
231 | /** |
||
232 | * @return string |
||
233 | */ |
||
234 | public function getLocale() { |
||
237 | |||
238 | /** |
||
239 | * @return string |
||
240 | */ |
||
241 | public function getSymbol($currency = null, $locale = null) { |
||
248 | |||
249 | /** |
||
250 | * @return string |
||
251 | */ |
||
252 | public function getShortName($currency = null, $locale = null) { |
||
258 | |||
259 | /** |
||
260 | * @return string |
||
261 | */ |
||
262 | public function getName($currency = null, $locale = null) { |
||
268 | |||
269 | /** |
||
270 | * @param array $arr |
||
271 | */ |
||
272 | public function setAllowedCurrencies($arr) { |
||
275 | |||
276 | /** |
||
277 | * @return array |
||
278 | */ |
||
279 | public function getAllowedCurrencies() { |
||
282 | |||
283 | /** |
||
284 | * Returns a CompositeField instance used as a default |
||
285 | * for form scaffolding. |
||
286 | * |
||
287 | * Used by {@link SearchContext}, {@link ModelAdmin}, {@link DataObject::scaffoldFormFields()} |
||
288 | * |
||
289 | * @param string $title Optional. Localized title of the generated instance |
||
290 | * @return FormField |
||
291 | */ |
||
292 | public function scaffoldFormField($title = null) { |
||
299 | |||
300 | /** |
||
301 | * For backwards compatibility reasons |
||
302 | * (mainly with ecommerce module), |
||
303 | * this returns the amount value of the field, |
||
304 | * rather than a {@link Nice()} formatting. |
||
305 | */ |
||
306 | public function __toString() { |
||
309 | |||
310 | public function scalarValueOnly() |
||
314 | |||
315 | } |
||
316 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.