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 UserFormAddNewClassesList extends Object implements GridField_HTMLProvider, GridField_ActionProvider { |
||
|
|
|||
| 7 | |||
| 8 | /** |
||
| 9 | * Name of fragment to insert into |
||
| 10 | * |
||
| 11 | * @var string |
||
| 12 | */ |
||
| 13 | protected $targetFragment; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * Button title |
||
| 17 | * |
||
| 18 | * @var string |
||
| 19 | */ |
||
| 20 | protected $buttonName; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Additonal CSS classes for the button |
||
| 24 | * |
||
| 25 | * @var string |
||
| 26 | */ |
||
| 27 | protected $buttonClass = null; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * default value for the dropdown |
||
| 31 | */ |
||
| 32 | protected $defaultClass; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @param array $default Class to be selected by default. |
||
| 36 | * @param string $targetFragment The fragment to render the button into |
||
| 37 | */ |
||
| 38 | public function __construct($default = null, $targetFragment = 'buttons-before-left') { |
||
| 42 | |||
| 43 | /** |
||
| 44 | * {@inheritDoc} |
||
| 45 | */ |
||
| 46 | public function getHTMLFragments($grid) { |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Get extra button class |
||
| 81 | * |
||
| 82 | * @return string |
||
| 83 | */ |
||
| 84 | public function getButtonClass() { |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Sets extra CSS classes for this button |
||
| 90 | * |
||
| 91 | * @param string $buttonClass |
||
| 92 | * @return $this |
||
| 93 | */ |
||
| 94 | public function setButtonClass($buttonClass) { |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Change the button name |
||
| 101 | * |
||
| 102 | * @param string $name |
||
| 103 | * @return $this |
||
| 104 | */ |
||
| 105 | public function setButtonName($name) { |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Get the button name |
||
| 112 | * |
||
| 113 | * @return string |
||
| 114 | */ |
||
| 115 | public function getButtonName() { |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Gets the fragment name this button is rendered into. |
||
| 121 | * |
||
| 122 | * @return string |
||
| 123 | */ |
||
| 124 | public function getFragment() { |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Sets the fragment name this button is rendered into. |
||
| 130 | * |
||
| 131 | * @param string $fragment |
||
| 132 | * @return GridFieldAddNewInlineButton $this |
||
| 133 | */ |
||
| 134 | public function setFragment($fragment) { |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Handles adding a new instance of a selected class. |
||
| 141 | * |
||
| 142 | * @param GridField $grid |
||
| 143 | * @param Array $data from request |
||
| 144 | * @return null |
||
| 145 | */ |
||
| 146 | View Code Duplication | public function handleAdd($grid, $data) { |
|
| 162 | |||
| 163 | /** |
||
| 164 | * Gets the default class that is selected automatically. |
||
| 165 | * |
||
| 166 | * @return string |
||
| 167 | */ |
||
| 168 | public function getDefaultClass() { |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Sets the default class that is selected automatically. |
||
| 174 | * |
||
| 175 | * @param string $default the class name to use as default |
||
| 176 | * @return UserFormAddNewClassesList $this |
||
| 177 | */ |
||
| 178 | public function setDefaultClass($default) { |
||
| 182 | |||
| 183 | /** |
||
| 184 | * {@inheritDoc} |
||
| 185 | */ |
||
| 186 | public function getActions($gridField) { |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Get the action suburl for this component |
||
| 194 | * |
||
| 195 | * @return string |
||
| 196 | */ |
||
| 197 | protected function getAction() { |
||
| 200 | |||
| 201 | /** |
||
| 202 | * {@inheritDoc} |
||
| 203 | */ |
||
| 204 | View Code Duplication | public function handleAction(GridField $gridField, $actionName, $arguments, $data) { |
|
| 212 | |||
| 213 | /** |
||
| 214 | * Get the list of classes that can be selected and created |
||
| 215 | * |
||
| 216 | * @return array |
||
| 217 | */ |
||
| 218 | public function getFieldClasses() { |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Gets the selected value from the request data array |
||
| 224 | * |
||
| 225 | * @param array $data from request |
||
| 226 | * @return string|null; |
||
| 227 | */ |
||
| 228 | public function getSelectedClass($data = null) { |
||
| 242 | } |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.