Conditions | 7 |
Paths | 1 |
Total Lines | 108 |
Code Lines | 74 |
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 |
||
67 | public function getCMSFields() |
||
68 | { |
||
69 | // PHP 5.3 only |
||
70 | $self = $this; |
||
71 | $this->beforeUpdateCMSFields(function (FieldList $fields) use ($self) { |
||
72 | $fields->removeFieldsFromTab( |
||
73 | 'Root.Main', |
||
74 | array('KeyValuePairs', 'SortBy', 'SortByDirection') |
||
75 | ); |
||
76 | // Don't put the GridField for documents in until the set has been created |
||
77 | if (!$self->isInDB()) { |
||
78 | $fields->addFieldToTab( |
||
79 | 'Root.Main', |
||
80 | LiteralField::create( |
||
81 | 'GridFieldNotice', |
||
82 | '<p class="message warning">' . _t( |
||
83 | 'DMSDocumentSet.GRIDFIELD_NOTICE', |
||
84 | 'Managing documents will be available once you have created this document set.' |
||
85 | ) . '</p>' |
||
86 | ), |
||
87 | 'Title' |
||
88 | ); |
||
89 | } else { |
||
90 | // Document listing |
||
91 | $gridFieldConfig = GridFieldConfig::create() |
||
92 | ->addComponents( |
||
93 | new GridFieldButtonRow('before'), |
||
94 | new GridFieldToolbarHeader(), |
||
95 | new GridFieldFilterHeader(), |
||
96 | new GridFieldSortableHeader(), |
||
97 | new GridFieldDataColumns(), |
||
98 | new GridFieldEditButton(), |
||
99 | // Special delete dialog to handle custom behaviour of unlinking and deleting |
||
100 | new GridFieldDeleteAction(true), |
||
101 | new GridFieldDetailForm() |
||
102 | ); |
||
103 | |||
104 | if (class_exists('GridFieldPaginatorWithShowAll')) { |
||
105 | $paginatorComponent = new GridFieldPaginatorWithShowAll(15); |
||
106 | } else { |
||
107 | $paginatorComponent = new GridFieldPaginator(15); |
||
108 | } |
||
109 | $gridFieldConfig->addComponent($paginatorComponent); |
||
110 | |||
111 | if (class_exists('GridFieldSortableRows')) { |
||
112 | $sortableComponent = new GridFieldSortableRows('DocumentSort'); |
||
113 | // setUsePagination method removed from newer version of SortableGridField. |
||
114 | if (method_exists($sortableComponent, 'setUsePagination')) { |
||
115 | $sortableComponent->setUsePagination(false)->setForceRedraw(true); |
||
116 | } |
||
117 | $gridFieldConfig->addComponent($sortableComponent); |
||
118 | } |
||
119 | |||
120 | // Don't show which page this is if we're already editing within a page context |
||
121 | if (Controller::curr() instanceof CMSPageEditController) { |
||
122 | $fields->removeByName('PageID'); |
||
123 | } else { |
||
124 | $fields->fieldByName('Root.Main.PageID')->setTitle(_t('DMSDocumentSet.SHOWONPAGE', 'Show on page')); |
||
125 | } |
||
126 | |||
127 | $gridFieldConfig->getComponentByType('GridFieldDataColumns') |
||
128 | ->setDisplayFields($self->getDocumentDisplayFields()) |
||
129 | ->setFieldCasting(array('LastEdited' => 'Datetime->Ago')) |
||
130 | ->setFieldFormatting( |
||
131 | array( |
||
132 | 'FilenameWithoutID' => '<a target=\'_blank\' class=\'file-url\'' |
||
133 | . ' href=\'$Link\'>$FilenameWithoutID</a>', |
||
134 | 'ManuallyAdded' => function ($value) { |
||
135 | if ($value) { |
||
136 | return _t('DMSDocumentSet.MANUAL', 'Manually'); |
||
137 | } |
||
138 | return _t('DMSDocumentSet.QUERYBUILDER', 'Query Builder'); |
||
139 | } |
||
140 | ) |
||
141 | ); |
||
142 | |||
143 | // Override delete functionality with this class |
||
144 | $gridFieldConfig->getComponentByType('GridFieldDetailForm') |
||
145 | ->setItemRequestClass('DMSGridFieldDetailForm_ItemRequest'); |
||
146 | $gridField = GridField::create( |
||
147 | 'Documents', |
||
148 | false, |
||
149 | $self->Documents(), |
||
150 | $gridFieldConfig |
||
151 | ); |
||
152 | $gridField->setModelClass('DMSDocument'); |
||
153 | $gridField->addExtraClass('documents'); |
||
154 | |||
155 | $gridFieldConfig->addComponent( |
||
156 | $addNewButton = new DMSGridFieldAddNewButton('buttons-before-left'), |
||
157 | 'GridFieldExportButton' |
||
158 | ); |
||
159 | $addNewButton->setDocumentSetId($self->ID); |
||
160 | |||
161 | $fields->removeByName('Documents'); |
||
162 | $fields->addFieldsToTab( |
||
163 | 'Root.Main', |
||
164 | array( |
||
165 | $gridField, |
||
166 | HiddenField::create('DMSShortcodeHandlerKey', false, DMS::inst()->getShortcodeHandlerKey()) |
||
167 | ) |
||
168 | ); |
||
169 | $self->addQueryFields($fields); |
||
170 | } |
||
171 | }); |
||
172 | $this->addRequirements(); |
||
173 | return parent::getCMSFields(); |
||
174 | } |
||
175 | |||
333 |
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.