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 |
||
| 6 | class PodsField_Email extends PodsField { |
||
| 7 | |||
| 8 | /** |
||
| 9 | * {@inheritdoc} |
||
| 10 | */ |
||
| 11 | public static $group = 'Text'; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * {@inheritdoc} |
||
| 15 | */ |
||
| 16 | public static $type = 'email'; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * {@inheritdoc} |
||
| 20 | */ |
||
| 21 | public static $label = 'E-mail'; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * {@inheritdoc} |
||
| 25 | */ |
||
| 26 | public static $prepare = '%s'; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * {@inheritdoc} |
||
| 30 | */ |
||
| 31 | public function setup() { |
||
| 32 | |||
| 33 | self::$label = __( 'E-mail', 'pods' ); |
||
| 34 | } |
||
| 35 | |||
| 36 | /** |
||
| 37 | * {@inheritdoc} |
||
| 38 | */ |
||
| 39 | public function options() { |
||
| 75 | |||
| 76 | /** |
||
| 77 | * {@inheritdoc} |
||
| 78 | */ |
||
| 79 | View Code Duplication | public function schema( $options = null ) { |
|
|
|
|||
| 80 | |||
| 81 | $length = (int) pods_v( static::$type . '_max_length', $options, 255 ); |
||
| 82 | |||
| 83 | $schema = 'VARCHAR(' . $length . ')'; |
||
| 84 | |||
| 85 | if ( 255 < $length || $length < 1 ) { |
||
| 86 | $schema = 'LONGTEXT'; |
||
| 87 | } |
||
| 88 | |||
| 89 | return $schema; |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * {@inheritdoc} |
||
| 94 | */ |
||
| 95 | View Code Duplication | public function input( $name, $value = null, $options = null, $pod = null, $id = null ) { |
|
| 122 | |||
| 123 | /** |
||
| 124 | * {@inheritdoc} |
||
| 125 | */ |
||
| 126 | public function regex( $value = null, $name = null, $options = null, $pod = null, $id = null ) { |
||
| 130 | |||
| 131 | /** |
||
| 132 | * {@inheritdoc} |
||
| 133 | */ |
||
| 134 | View Code Duplication | public function validate( $value, $name = null, $options = null, $fields = null, $pod = null, $id = null, $params = null ) { |
|
| 160 | |||
| 161 | /** |
||
| 162 | * {@inheritdoc} |
||
| 163 | */ |
||
| 164 | public function pre_save( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) { |
||
| 180 | |||
| 181 | /** |
||
| 182 | * {@inheritdoc} |
||
| 183 | */ |
||
| 184 | public function ui( $id, $value, $name = null, $options = null, $fields = null, $pod = null ) { |
||
| 188 | } |
||
| 189 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.