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 setup() { |
||
| 32 | |||
| 33 | self::$label = __( 'Plain Number', 'pods' ); |
||
| 34 | } |
||
| 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 ) { |
||
| 192 | |||
| 193 | $options = (array) $options; |
||
| 194 | $form_field_type = PodsForm::$field_type; |
||
| 195 | |||
| 196 | if ( is_array( $value ) ) { |
||
| 197 | $value = implode( '', $value ); |
||
| 198 | } |
||
| 199 | |||
| 200 | if ( 'slider' === pods_v( static::$type . '_format_type', $options, 'number' ) ) { |
||
| 201 | $field_type = 'slider'; |
||
| 202 | } else { |
||
| 203 | $field_type = static::$type; |
||
| 204 | } |
||
| 205 | |||
| 206 | if ( isset( $options['name'] ) && false === PodsForm::permission( static::$type, $options['name'], $options, null, $pod, $id ) ) { |
||
| 207 | if ( pods_v( 'read_only', $options, false ) ) { |
||
| 208 | $options['readonly'] = true; |
||
| 209 | |||
| 210 | $field_type = 'text'; |
||
| 211 | |||
| 212 | $value = $this->format( $value, $name, $options, $pod, $id ); |
||
| 213 | } else { |
||
| 214 | return; |
||
| 215 | } |
||
| 216 | } elseif ( ! pods_has_permissions( $options ) && pods_v( 'read_only', $options, false ) ) { |
||
| 217 | $options['readonly'] = true; |
||
| 218 | |||
| 219 | $field_type = 'text'; |
||
| 220 | |||
| 221 | $value = $this->format( $value, $name, $options, $pod, $id ); |
||
| 222 | } |
||
| 223 | |||
| 224 | pods_view( PODS_DIR . 'ui/fields/' . $field_type . '.php', compact( array_keys( get_defined_vars() ) ) ); |
||
| 225 | |||
| 226 | } |
||
| 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 format( $value = null, $name = null, $options = null, $pod = null, $id = null ) { |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Get the formatting arguments for numbers. |
||
| 325 | * |
||
| 326 | * @since 2.7 |
||
| 327 | * |
||
| 328 | * @param array $options Field options. |
||
| 329 | * |
||
| 330 | * @return array { |
||
| 331 | * @type string $thousands |
||
| 332 | * @type string $dot |
||
| 333 | * @type int $decimals |
||
| 334 | * } |
||
| 335 | */ |
||
| 336 | public function get_number_format_args( $options ) { |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Get the max allowed decimals. |
||
| 374 | * |
||
| 375 | * @since 2.7 |
||
| 376 | * |
||
| 377 | * @param array $options Field options. |
||
| 378 | * |
||
| 379 | * @return int |
||
| 380 | */ |
||
| 381 | public function get_max_decimals( $options ) { |
||
| 403 | } |
||
| 404 |
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.