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 |
||
| 17 | class EE_Money_Field extends EE_Float_Field |
||
| 18 | { |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var $money_factory MoneyFactory |
||
| 22 | */ |
||
| 23 | protected $money_factory; |
||
| 24 | |||
| 25 | |||
| 26 | /** |
||
| 27 | * @param string $table_column |
||
| 28 | * @param string $nicename |
||
| 29 | * @param bool $nullable |
||
| 30 | * @param null $default_value |
||
| 31 | * @param MoneyFactory $factory |
||
| 32 | * @throws InvalidArgumentException |
||
| 33 | * @throws InvalidInterfaceException |
||
| 34 | * @throws InvalidDataTypeException |
||
| 35 | */ |
||
| 36 | View Code Duplication | public function __construct( |
|
| 50 | |||
| 51 | |||
| 52 | /** |
||
| 53 | * Formats the value for pretty output, according to $schema. |
||
| 54 | * If legacy filters are being used, uses EEH_Money::format_currency() to format it and currency data from the database |
||
| 55 | * (which admins can change), otherwise uses MoneyFormatter which takes currency information from a JSON file |
||
| 56 | * (which admins CANNOT change). |
||
| 57 | * Legacy Schemas (use the admin-editable currency data from the database): |
||
| 58 | * 'localized_float': "3,023.00" |
||
| 59 | * 'no_currency_code': "$3,023.00" |
||
| 60 | * null: "$3,023.00<span>USD</span>" |
||
| 61 | * New Schemas (use the currency data from a JSON file that we control): |
||
| 62 | * MoneyFormatter::RAW: "3023.0000" |
||
| 63 | * MoneyFormatter::DECIMAL_ONLY: "3023.00" |
||
| 64 | * MoneyFormatter::ADD THOUSANDS/: "3,023.00" |
||
| 65 | * MoneyFormatter::ADD_CURRENCY_SIGN: "$3,023.00" |
||
| 66 | * MoneyFormatter::ADD_CURRENCY_CODE: "$3,023.00<span>USD</span>" |
||
| 67 | * |
||
| 68 | * @param string|Money $value_on_field_to_be_outputted |
||
| 69 | * @param string $schema |
||
| 70 | * @return string |
||
| 71 | * @throws InvalidIdentifierException |
||
| 72 | * @throws InvalidArgumentException |
||
| 73 | * @throws InvalidInterfaceException |
||
| 74 | * @throws InvalidDataTypeException |
||
| 75 | * @throws EE_Error |
||
| 76 | * @throws ReflectionException |
||
| 77 | */ |
||
| 78 | public function prepare_for_pretty_echoing($value_on_field_to_be_outputted, $schema = null) |
||
| 96 | |||
| 97 | |||
| 98 | /** |
||
| 99 | * Make sure this value is a money object |
||
| 100 | * |
||
| 101 | * @param string|float|int|Money $value |
||
| 102 | * @return Money |
||
| 103 | * @throws InvalidIdentifierException |
||
| 104 | * @throws InvalidArgumentException |
||
| 105 | * @throws InvalidInterfaceException |
||
| 106 | * @throws InvalidDataTypeException |
||
| 107 | * @throws EE_Error |
||
| 108 | */ |
||
| 109 | private function ensureMoney($value) |
||
| 116 | |||
| 117 | |||
| 118 | |||
| 119 | /** |
||
| 120 | * Ensures we're dealing with something that isn't Money |
||
| 121 | * (for passing off to legacy systems or the parent field) |
||
| 122 | * @param string|float|int|Money $value |
||
| 123 | * @return string|float|int |
||
| 124 | */ |
||
| 125 | private function ensureNotMoney($value) |
||
| 132 | |||
| 133 | |||
| 134 | /** |
||
| 135 | * If provided with a string, strips out money-related formatting to turn it into a proper float. |
||
| 136 | * Rounds the float to the correct number of decimal places for this country's currency. |
||
| 137 | * Also, interprets periods and commas according to the country's currency settings. |
||
| 138 | * So if you want to pass in a string that NEEDS to interpret periods as decimal marks, |
||
| 139 | * type cast it to a float first. |
||
| 140 | * |
||
| 141 | * @param string|float|int|Money $value_inputted_for_field_on_model_object |
||
| 142 | * @return Money |
||
| 143 | * @throws InvalidInterfaceException |
||
| 144 | * @throws InvalidIdentifierException |
||
| 145 | * @throws InvalidDataTypeException |
||
| 146 | * @throws EE_Error |
||
| 147 | * @throws InvalidArgumentException |
||
| 148 | */ |
||
| 149 | public function prepare_for_set($value_inputted_for_field_on_model_object) |
||
| 159 | |||
| 160 | |||
| 161 | |||
| 162 | /** |
||
| 163 | * @param string|float|int|Money $value_of_field_on_model_object |
||
| 164 | * @return float |
||
| 165 | * @throws InvalidArgumentException |
||
| 166 | * @throws InvalidInterfaceException |
||
| 167 | * @throws InvalidDataTypeException |
||
| 168 | */ |
||
| 169 | public function prepare_for_get($value_of_field_on_model_object) |
||
| 175 | |||
| 176 | |||
| 177 | /** |
||
| 178 | * Takes the incoming float and create a money entity for the model object |
||
| 179 | * |
||
| 180 | * @param string|float|int $value_found_in_db_for_model_object |
||
| 181 | * @return Money |
||
| 182 | * @throws InvalidIdentifierException |
||
| 183 | * @throws InvalidArgumentException |
||
| 184 | * @throws InvalidInterfaceException |
||
| 185 | * @throws InvalidDataTypeException |
||
| 186 | * @throws EE_Error |
||
| 187 | */ |
||
| 188 | public function prepare_for_set_from_db($value_found_in_db_for_model_object) |
||
| 192 | |||
| 193 | |||
| 194 | |||
| 195 | /** |
||
| 196 | * Prepares a value for use in the DB |
||
| 197 | * @param string|float|int|Money $value_of_field_on_model_object |
||
| 198 | * @return float |
||
| 199 | */ |
||
| 200 | public function prepare_for_use_in_db($value_of_field_on_model_object) |
||
| 205 | |||
| 206 | |||
| 207 | |||
| 208 | View Code Duplication | public function getSchemaProperties() |
|
| 227 | } |
||
| 228 |