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 |
||
| 3 | class CommentsGridFieldAction implements GridField_ColumnProvider, GridField_ActionProvider |
||
|
|
|||
| 4 | { |
||
| 5 | /** |
||
| 6 | * {@inheritdoc} |
||
| 7 | */ |
||
| 8 | public function augmentColumns($gridField, &$columns) |
||
| 9 | { |
||
| 10 | if (!in_array('Actions', $columns)) { |
||
| 11 | $columns[] = 'Actions'; |
||
| 12 | } |
||
| 13 | } |
||
| 14 | |||
| 15 | /** |
||
| 16 | * {@inheritdoc} |
||
| 17 | */ |
||
| 18 | public function getColumnAttributes($gridField, $record, $columnName) |
||
| 22 | |||
| 23 | /** |
||
| 24 | * {@inheritdoc} |
||
| 25 | */ |
||
| 26 | public function getColumnMetadata($gridField, $columnName) |
||
| 27 | { |
||
| 28 | if ($columnName == 'Actions') { |
||
| 29 | return array('title' => ''); |
||
| 30 | } |
||
| 31 | } |
||
| 32 | |||
| 33 | /** |
||
| 34 | * {@inheritdoc} |
||
| 35 | */ |
||
| 36 | public function getColumnsHandled($gridField) |
||
| 40 | |||
| 41 | /** |
||
| 42 | * {@inheritdoc} |
||
| 43 | */ |
||
| 44 | public function getColumnContent($gridField, $record, $columnName) |
||
| 45 | { |
||
| 46 | if (!$record->canEdit()) { |
||
| 47 | return; |
||
| 48 | } |
||
| 49 | |||
| 50 | $field = ""; |
||
| 51 | |||
| 52 | View Code Duplication | if (!$record->IsSpam || !$record->Moderated) { |
|
| 53 | $field .= GridField_FormAction::create( |
||
| 54 | $gridField, |
||
| 55 | 'CustomAction' . $record->ID . 'Spam', |
||
| 56 | 'Spam', |
||
| 57 | 'spam', |
||
| 58 | array('RecordID' => $record->ID) |
||
| 59 | )->Field(); |
||
| 60 | } |
||
| 61 | |||
| 62 | View Code Duplication | if ($record->IsSpam || !$record->Moderated) { |
|
| 63 | $field .= GridField_FormAction::create( |
||
| 64 | $gridField, |
||
| 65 | 'CustomAction' . $record->ID . 'Approve', |
||
| 66 | 'Approve', |
||
| 67 | 'approve', |
||
| 68 | array('RecordID' => $record->ID) |
||
| 69 | )->Field(); |
||
| 70 | } |
||
| 71 | |||
| 72 | return $field; |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * {@inheritdoc} |
||
| 77 | */ |
||
| 78 | public function getActions($gridField) |
||
| 82 | |||
| 83 | /** |
||
| 84 | * {@inheritdoc} |
||
| 85 | */ |
||
| 86 | public function handleAction(GridField $gridField, $actionName, $arguments, $data) |
||
| 110 | } |
||
| 111 |
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.