| Conditions | 6 |
| Paths | 1 |
| Total Lines | 99 |
| Code Lines | 68 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 62 | public function getCMSFields() |
||
| 63 | { |
||
| 64 | // PHP 5.3 only |
||
| 65 | $self = $this; |
||
| 66 | $this->beforeUpdateCMSFields(function (FieldList $fields) use ($self) { |
||
| 67 | $fields->removeFieldsFromTab( |
||
| 68 | 'Root.Main', |
||
| 69 | array('KeyValuePairs', 'SortBy', 'SortByDirection') |
||
| 70 | ); |
||
| 71 | // Don't put the GridField for documents in until the set has been created |
||
| 72 | if (!$self->isInDB()) { |
||
| 73 | $fields->addFieldToTab( |
||
| 74 | 'Root.Main', |
||
| 75 | LiteralField::create( |
||
| 76 | 'GridFieldNotice', |
||
| 77 | '<p class="message warning">' . _t( |
||
| 78 | 'DMSDocumentSet.GRIDFIELD_NOTICE', |
||
| 79 | 'Managing documents will be available once you have created this document set.' |
||
| 80 | ) . '</p>' |
||
| 81 | ), |
||
| 82 | 'Title' |
||
| 83 | ); |
||
| 84 | } else { |
||
| 85 | // Document listing |
||
| 86 | $gridFieldConfig = GridFieldConfig::create() |
||
| 87 | ->addComponents( |
||
| 88 | new GridFieldToolbarHeader(), |
||
| 89 | new GridFieldFilterHeader(), |
||
| 90 | new GridFieldSortableHeader(), |
||
| 91 | new GridFieldDataColumns(), |
||
| 92 | new GridFieldEditButton(), |
||
| 93 | // Special delete dialog to handle custom behaviour of unlinking and deleting |
||
| 94 | new GridFieldDeleteAction(true), |
||
| 95 | new GridFieldDetailForm() |
||
| 96 | ); |
||
| 97 | |||
| 98 | if (class_exists('GridFieldPaginatorWithShowAll')) { |
||
| 99 | $paginatorComponent = new GridFieldPaginatorWithShowAll(15); |
||
| 100 | } else { |
||
| 101 | $paginatorComponent = new GridFieldPaginator(15); |
||
| 102 | } |
||
| 103 | $gridFieldConfig->addComponent($paginatorComponent); |
||
| 104 | |||
| 105 | if (class_exists('GridFieldSortableRows')) { |
||
| 106 | $sortableComponent = new GridFieldSortableRows('DocumentSort'); |
||
| 107 | // setUsePagination method removed from newer version of SortableGridField. |
||
| 108 | if (method_exists($sortableComponent, 'setUsePagination')) { |
||
| 109 | $sortableComponent->setUsePagination(false)->setForceRedraw(true); |
||
| 110 | } |
||
| 111 | $gridFieldConfig->addComponent($sortableComponent); |
||
| 112 | } |
||
| 113 | |||
| 114 | $gridFieldConfig->getComponentByType('GridFieldDataColumns') |
||
| 115 | ->setDisplayFields($self->getDocumentDisplayFields()) |
||
| 116 | ->setFieldCasting(array('LastEdited' => 'Datetime->Ago')) |
||
| 117 | ->setFieldFormatting( |
||
| 118 | array( |
||
| 119 | 'FilenameWithoutID' => '<a target=\'_blank\' class=\'file-url\' href=\'$Link\'>$FilenameWithoutID</a>', |
||
| 120 | 'ManuallyAdded' => function ($value) { |
||
| 121 | if ($value) { |
||
| 122 | return _t('DMSDocumentSet.MANUAL', 'Manually'); |
||
| 123 | } |
||
| 124 | return _t('DMSDocumentSet.QUERYBUILDER', 'Query Builder'); |
||
| 125 | } |
||
| 126 | ) |
||
| 127 | ); |
||
| 128 | |||
| 129 | // Override delete functionality with this class |
||
| 130 | $gridFieldConfig->getComponentByType('GridFieldDetailForm') |
||
| 131 | ->setItemRequestClass('DMSGridFieldDetailForm_ItemRequest'); |
||
| 132 | $gridField = GridField::create( |
||
| 133 | 'Documents', |
||
| 134 | false, |
||
| 135 | $self->Documents(), |
||
| 136 | $gridFieldConfig |
||
| 137 | ); |
||
| 138 | $gridField->setModelClass('DMSDocument'); |
||
| 139 | $gridField->addExtraClass('documents'); |
||
| 140 | |||
| 141 | $gridFieldConfig->addComponent( |
||
| 142 | $addNewButton = new DMSGridFieldAddNewButton, |
||
| 143 | 'GridFieldExportButton' |
||
| 144 | ); |
||
| 145 | $addNewButton->setDocumentSetId($self->ID); |
||
| 146 | |||
| 147 | $fields->removeByName('Documents'); |
||
| 148 | $fields->addFieldsToTab( |
||
| 149 | 'Root.Main', |
||
| 150 | array( |
||
| 151 | $gridField, |
||
| 152 | HiddenField::create('DMSShortcodeHandlerKey', false, DMS::inst()->getShortcodeHandlerKey()) |
||
| 153 | ) |
||
| 154 | ); |
||
| 155 | $self->addQueryFields($fields); |
||
| 156 | } |
||
| 157 | }); |
||
| 158 | $this->addRequirements(); |
||
| 159 | return parent::getCMSFields(); |
||
| 160 | } |
||
| 161 | |||
| 309 |
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.