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 GridFieldAddClassesButton implements GridField_ActionProvider { |
||
|
|
|||
| 7 | |||
| 8 | /** |
||
| 9 | * default value for the dropdown |
||
| 10 | */ |
||
| 11 | protected $defaultClass; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * @param array $default Class to be selected by default. |
||
| 15 | * @param string $targetFragment The fragment to render the button into |
||
| 16 | */ |
||
| 17 | public function __construct($default = null, $targetFragment = 'buttons-before-left') { |
||
| 21 | |||
| 22 | /** |
||
| 23 | * {@inheritDoc} |
||
| 24 | */ |
||
| 25 | public function getHTMLFragments($grid) { |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Handles adding a new instance of a selected class. |
||
| 60 | * |
||
| 61 | * @param GridField $grid |
||
| 62 | * @param Array $data from request |
||
| 63 | * @return null |
||
| 64 | */ |
||
| 65 | public function handleAdd($grid, $data) { |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Sets the default class that is selected automatically. |
||
| 80 | * |
||
| 81 | * @param string $default the class name to use as default |
||
| 82 | * @return UserFormAddNewClassesList $this |
||
| 83 | */ |
||
| 84 | public function setDefaultClass($default) { |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Get the action suburl for this component |
||
| 91 | * |
||
| 92 | * @return string |
||
| 93 | */ |
||
| 94 | protected function getAction() { |
||
| 97 | |||
| 98 | /** |
||
| 99 | * {@inheritDoc} |
||
| 100 | */ |
||
| 101 | View Code Duplication | public function handleAction(GridField $gridField, $actionName, $arguments, $data) { |
|
| 109 | |||
| 110 | /** |
||
| 111 | * Get the list of classes that can be selected and created |
||
| 112 | * |
||
| 113 | * @return array |
||
| 114 | */ |
||
| 115 | public function getFieldClasses() { |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Gets the selected value from the request data array |
||
| 121 | * |
||
| 122 | * @param array $data from request |
||
| 123 | * @return string|null; |
||
| 124 | */ |
||
| 125 | public function getSelectedClass($data = null) { |
||
| 139 | } |
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.