Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
15 | class EE_Money_Field extends EE_Float_Field |
||
16 | { |
||
17 | |||
18 | /** |
||
19 | * @var $money_factory MoneyFactory |
||
20 | */ |
||
21 | protected $money_factory; |
||
22 | |||
23 | |||
24 | |||
25 | /** |
||
26 | * @param string $table_column |
||
27 | * @param string $nicename |
||
28 | * @param bool $nullable |
||
29 | * @param null $default_value |
||
30 | * @param MoneyFactory $factory |
||
31 | * @throws \InvalidArgumentException |
||
32 | * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
||
33 | * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
||
34 | */ |
||
35 | public function __construct( |
||
49 | |||
50 | |||
51 | |||
52 | /** |
||
53 | * Schemas: |
||
54 | * 'localized_float': "3,023.00" |
||
55 | * 'no_currency_code': "$3,023.00" |
||
56 | * null: "$3,023.00<span>USD</span>" |
||
57 | * |
||
58 | * @param string|Money $value_on_field_to_be_outputted |
||
59 | * @param string $schema |
||
60 | * @return string |
||
61 | * @throws \EE_Error |
||
62 | */ |
||
63 | public function prepare_for_pretty_echoing($value_on_field_to_be_outputted, $schema = null) |
||
112 | |||
113 | |||
114 | |||
115 | /** |
||
116 | * Make sure this value is a money object |
||
117 | * |
||
118 | * @param string|float|int|Money $value |
||
119 | * @return Money |
||
120 | * @throws \InvalidArgumentException |
||
121 | * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
||
122 | * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
||
123 | * @throws \EE_Error |
||
124 | */ |
||
125 | private function ensureMoney($value) |
||
132 | |||
133 | |||
134 | |||
135 | /** |
||
136 | * Ensures we're dealing with something that isn't Money |
||
137 | * (for passing off to legacy systems or the parent field) |
||
138 | * @param string|float|int|Money $value |
||
139 | * @return string|float|int |
||
140 | */ |
||
141 | private function ensureNotMoney($value) |
||
148 | |||
149 | /** |
||
150 | * If provided with a string, strips out money-related formatting to turn it into a proper float. |
||
151 | * Rounds the float to the correct number of decimal places for this country's currency. |
||
152 | * Also, interprets periods and commas according to the country's currency settings. |
||
153 | * So if you want to pass in a string that NEEDS to interpret periods as decimal marks, call floatval() on it first. |
||
154 | * |
||
155 | * @param string|Money $value_inputted_for_field_on_model_object |
||
156 | * @return Money |
||
157 | */ |
||
158 | public function prepare_for_set($value_inputted_for_field_on_model_object) |
||
168 | |||
169 | |||
170 | |||
171 | /** |
||
172 | * @param string|float|int|Money $value_of_field_on_model_object |
||
173 | * @return float |
||
174 | * @throws \InvalidArgumentException |
||
175 | * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
||
176 | * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
||
177 | */ |
||
178 | public function prepare_for_get($value_of_field_on_model_object) |
||
184 | |||
185 | |||
186 | |||
187 | /** |
||
188 | * Takes the incoming float and create a money entity for the model object |
||
189 | * |
||
190 | * @param mixed $value_found_in_db_for_model_object |
||
191 | * @return Money |
||
192 | * @throws \InvalidArgumentException |
||
193 | * @throws \EventEspresso\core\exceptions\InvalidInterfaceException |
||
194 | * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
||
195 | * @throws \EE_Error |
||
196 | */ |
||
197 | public function prepare_for_set_from_db($value_found_in_db_for_model_object) |
||
201 | |||
202 | |||
203 | |||
204 | /** |
||
205 | * Prepares a value for use in the DB |
||
206 | * @param string|float|int|Money $value_of_field_on_model_object |
||
207 | * @return float |
||
208 | */ |
||
209 | public function prepare_for_use_in_db($value_of_field_on_model_object) |
||
214 | |||
215 | |||
216 | |||
217 | View Code Duplication | public function getSchemaProperties() |
|
236 | } |