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 |
||
| 10 | class Editor extends Fluent |
||
| 11 | { |
||
| 12 | use HasEvents; |
||
| 13 | |||
| 14 | const DISPLAY_LIGHTBOX = 'lightbox'; |
||
| 15 | const DISPLAY_ENVELOPE = 'envelope'; |
||
| 16 | const DISPLAY_BOOTSTRAP = 'bootstrap'; |
||
| 17 | const DISPLAY_FOUNDATION = 'foundation'; |
||
| 18 | const DISPLAY_JQUERYUI = 'jqueryui'; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Editor constructor. |
||
| 22 | * |
||
| 23 | * @param string $instance |
||
| 24 | */ |
||
| 25 | public function __construct($instance = 'editor') |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Make new Editor instance. |
||
| 34 | * |
||
| 35 | * @param string $instance |
||
| 36 | * @return Editor |
||
| 37 | */ |
||
| 38 | public static function make($instance = 'editor') |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Append raw scripts. |
||
| 45 | * |
||
| 46 | * @param string $scripts |
||
| 47 | * @return Editor |
||
| 48 | */ |
||
| 49 | public function scripts($scripts) |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Set Editor's variable name / instance. |
||
| 58 | * |
||
| 59 | * @param $instance |
||
| 60 | * @return $this |
||
| 61 | */ |
||
| 62 | public function instance($instance) |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Set Editor's ajax parameter. |
||
| 71 | * |
||
| 72 | * @param string|array $ajax |
||
| 73 | * @return $this |
||
| 74 | * @see https://editor.datatables.net/reference/option/ajax |
||
| 75 | */ |
||
| 76 | public function ajax($ajax) |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Set Editor's table source. |
||
| 85 | * |
||
| 86 | * @param string $table |
||
| 87 | * @return $this |
||
| 88 | * @see https://editor.datatables.net/reference/option/table |
||
| 89 | */ |
||
| 90 | public function table($table) |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Set Editor's idSrc option. |
||
| 99 | * |
||
| 100 | * @param string $idSrc |
||
| 101 | * @return $this |
||
| 102 | * @see https://editor.datatables.net/reference/option/idSrc |
||
| 103 | */ |
||
| 104 | public function idSrc($idSrc = 'DT_RowId') |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Set Editor's display option. |
||
| 113 | * |
||
| 114 | * @param string $display |
||
| 115 | * @return $this |
||
| 116 | * @see https://editor.datatables.net/reference/option/display |
||
| 117 | */ |
||
| 118 | public function display($display) |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Set Editor's fields. |
||
| 127 | * |
||
| 128 | * @param array $fields |
||
| 129 | * @return $this |
||
| 130 | * @see https://editor.datatables.net/reference/option/fields |
||
| 131 | */ |
||
| 132 | public function fields(array $fields) |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Set Editor's language. |
||
| 141 | * |
||
| 142 | * @param array $language |
||
| 143 | * @return $this |
||
| 144 | * @see https://editor.datatables.net/reference/option/i18n |
||
| 145 | */ |
||
| 146 | public function language(array $language) |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Set Editor's template. |
||
| 155 | * |
||
| 156 | * @param string $template |
||
| 157 | * @return $this |
||
| 158 | * @see https://editor.datatables.net/reference/option/template |
||
| 159 | */ |
||
| 160 | public function template($template) |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Convert the fluent instance to an array. |
||
| 169 | * |
||
| 170 | * @return array |
||
| 171 | */ |
||
| 172 | public function toArray() |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Convert the fluent instance to JSON. |
||
| 187 | * |
||
| 188 | * @param int $options |
||
| 189 | * @return string |
||
| 190 | */ |
||
| 191 | public function toJson($options = 0) |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Check if given key & value is a valid callback js function. |
||
| 224 | * |
||
| 225 | * @param string $value |
||
| 226 | * @param string $key |
||
| 227 | * @return bool |
||
| 228 | */ |
||
| 229 | protected function isCallbackFunction($value, $key) |
||
| 239 | } |
||
| 240 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.