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:
Complex classes like PodsField_Number 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 PodsField_Number, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 6 | class PodsField_Number extends PodsField { |
||
| 7 | |||
| 8 | /** |
||
| 9 | * {@inheritdoc} |
||
| 10 | */ |
||
| 11 | public static $group = 'Number'; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * {@inheritdoc} |
||
| 15 | */ |
||
| 16 | public static $type = 'number'; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * {@inheritdoc} |
||
| 20 | */ |
||
| 21 | public static $label = 'Plain Number'; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * {@inheritdoc} |
||
| 25 | */ |
||
| 26 | public static $prepare = '%d'; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * {@inheritdoc} |
||
| 30 | */ |
||
| 31 | public function __construct() { |
||
| 35 | |||
| 36 | /** |
||
| 37 | * {@inheritdoc} |
||
| 38 | */ |
||
| 39 | public function options() { |
||
| 124 | |||
| 125 | /** |
||
| 126 | * {@inheritdoc} |
||
| 127 | */ |
||
| 128 | public function schema( $options = null ) { |
||
| 143 | |||
| 144 | /** |
||
| 145 | * {@inheritdoc} |
||
| 146 | */ |
||
| 147 | public function prepare( $options = null ) { |
||
| 160 | |||
| 161 | /** |
||
| 162 | * {@inheritdoc} |
||
| 163 | */ |
||
| 164 | public function is_empty( $value = null ) { |
||
| 177 | |||
| 178 | /** |
||
| 179 | * {@inheritdoc} |
||
| 180 | */ |
||
| 181 | public function display( $value = null, $name = null, $options = null, $pod = null, $id = null ) { |
||
| 187 | |||
| 188 | /** |
||
| 189 | * {@inheritdoc} |
||
| 190 | */ |
||
| 191 | public function input( $name, $value = null, $options = null, $pod = null, $id = null ) { |
||
| 227 | |||
| 228 | /** |
||
| 229 | * {@inheritdoc} |
||
| 230 | */ |
||
| 231 | public function regex( $value = null, $name = null, $options = null, $pod = null, $id = null ) { |
||
| 239 | |||
| 240 | /** |
||
| 241 | * {@inheritdoc} |
||
| 242 | */ |
||
| 243 | public function validate( $value, $name = null, $options = null, $fields = null, $pod = null, $id = null, $params = null ) { |
||
| 268 | |||
| 269 | /** |
||
| 270 | * {@inheritdoc} |
||
| 271 | */ |
||
| 272 | public function pre_save( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) { |
||
| 288 | |||
| 289 | /** |
||
| 290 | * {@inheritdoc} |
||
| 291 | */ |
||
| 292 | public function ui( $id, $value, $name = null, $options = null, $fields = null, $pod = null ) { |
||
| 296 | |||
| 297 | /** |
||
| 298 | * {@inheritdoc} |
||
| 299 | */ |
||
| 300 | public function format( $value = null, $name = null, $options = null, $pod = null, $id = null ) { |
||
| 301 | |||
| 302 | if ( null === $value ) { |
||
| 303 | // Don't enforce a default value here. |
||
| 304 | return null; |
||
| 305 | } |
||
| 306 | |||
| 307 | $format_args = $this->get_number_format_args( $options ); |
||
| 308 | $thousands = $format_args['thousands']; |
||
| 309 | $dot = $format_args['dot']; |
||
| 310 | $decimals = $format_args['decimals']; |
||
| 311 | |||
| 312 | View Code Duplication | if ( 'i18n' === pods_v( static::$type . '_format', $options ) ) { |
|
| 313 | $value = number_format_i18n( (float) $value, $decimals ); |
||
| 314 | } else { |
||
| 315 | $value = number_format( (float) $value, $decimals, $dot, $thousands ); |
||
| 316 | } |
||
| 317 | |||
| 318 | // Optionally remove trailing decimal zero's. |
||
| 319 | if ( pods_v( static::$type . '_format_soft', $options, 0 ) ) { |
||
| 320 | $parts = explode( $dot, $value ); |
||
| 321 | if ( isset( $parts[1] ) ) { |
||
| 322 | $parts[1] = rtrim( $parts[1], '0' ); |
||
| 323 | $parts = array_filter( $parts ); |
||
| 324 | } |
||
| 325 | $value = implode( $dot, $parts ); |
||
| 326 | } |
||
| 327 | |||
| 328 | return $value; |
||
| 329 | } |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Get the formatting arguments for numbers. |
||
| 333 | * |
||
| 334 | * @since 2.7 |
||
| 335 | * |
||
| 336 | * @param array $options Field options. |
||
| 337 | * |
||
| 338 | * @return array { |
||
| 339 | * @type string $thousands |
||
| 340 | * @type string $dot |
||
| 341 | * @type int $decimals |
||
| 342 | * } |
||
| 343 | */ |
||
| 344 | public function get_number_format_args( $options ) { |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Get the max allowed decimals. |
||
| 382 | * |
||
| 383 | * @since 2.7 |
||
| 384 | * |
||
| 385 | * @param array $options |
||
| 386 | * |
||
| 387 | * @return int |
||
| 388 | */ |
||
| 389 | View Code Duplication | public function get_max_decimals( $options ) { |
|
| 411 | } |
||
| 412 |
Instead of relying on
globalstate, we recommend one of these alternatives:1. Pass all data via parameters
2. Create a class that maintains your state