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 |
||
7 | class Text_Field extends Field { |
||
8 | // Input field type attribute value. Defaults to text. Available types: text, email, url, number, password |
||
9 | public $field_type = 'text'; |
||
10 | public $placeholder; |
||
11 | |||
12 | /** |
||
13 | * Underscore template of this field. |
||
14 | */ |
||
15 | public function template() { |
||
20 | |||
21 | /** |
||
22 | * Change the type of the input |
||
23 | * |
||
24 | * @param string $type |
||
25 | */ |
||
26 | public function set_type( $type ) { |
||
30 | |||
31 | /** |
||
32 | * Set field placeholder text |
||
33 | * |
||
34 | * @param string $placeholder |
||
35 | * @return object $this |
||
36 | **/ |
||
37 | public function set_placeholder( $placeholder ) { |
||
41 | |||
42 | /** |
||
43 | * Returns an array that holds the field data, suitable for JSON representation. |
||
44 | * This data will be available in the Underscore template and the Backbone Model. |
||
45 | * |
||
46 | * @param bool $load Should the value be loaded from the database or use the value from the current instance. |
||
47 | * @return array |
||
48 | */ |
||
49 | View Code Duplication | public function to_json( $load ) { |
|
58 | |||
59 | } |
||
60 | ?> |
||
61 |
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.