Conditions | 9 |
Paths | 2 |
Total Lines | 168 |
Code Lines | 102 |
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 |
||
168 | public function getCMSFields() |
||
169 | { |
||
170 | Requirements::css( |
||
171 | ModuleLoader::getModule('silverstripe/userforms') |
||
172 | ->getRelativeResourcePath('client/dist/styles/userforms-cms.css') |
||
173 | ); |
||
174 | |||
175 | $this->beforeUpdateCMSFields(function ($fields) { |
||
|
|||
176 | |||
177 | // remove |
||
178 | $fields->removeByName('OnCompleteMessageLabel'); |
||
179 | $fields->removeByName('OnCompleteMessage'); |
||
180 | $fields->removeByName('Fields'); |
||
181 | $fields->removeByName('EmailRecipients'); |
||
182 | |||
183 | // define tabs |
||
184 | $fields->findOrMakeTab('Root.FormOptions', _t(__CLASS__.'.CONFIGURATION', 'Configuration')); |
||
185 | $fields->findOrMakeTab('Root.Recipients', _t(__CLASS__.'.RECIPIENTS', 'Recipients')); |
||
186 | $fields->findOrMakeTab('Root.Submissions', _t(__CLASS__.'.SUBMISSIONS', 'Submissions')); |
||
187 | |||
188 | |||
189 | // text to show on complete |
||
190 | $onCompleteFieldSet = CompositeField::create( |
||
191 | $label = LabelField::create( |
||
192 | 'OnCompleteMessageLabel', |
||
193 | _t(__CLASS__.'.ONCOMPLETELABEL', 'Show on completion') |
||
194 | ), |
||
195 | $editor = HTMLEditorField::create( |
||
196 | 'OnCompleteMessage', |
||
197 | '', |
||
198 | _t(__CLASS__.'.ONCOMPLETEMESSAGE', $this->OnCompleteMessage) |
||
199 | ) |
||
200 | ); |
||
201 | |||
202 | $onCompleteFieldSet->addExtraClass('field'); |
||
203 | |||
204 | $editor->setRows(3); |
||
205 | $label->addExtraClass('left'); |
||
206 | |||
207 | // Define config for email recipients |
||
208 | $emailRecipientsConfig = GridFieldConfig_RecordEditor::create(10); |
||
209 | $emailRecipientsConfig->getComponentByType(GridFieldAddNewButton::class) |
||
210 | ->setButtonName( |
||
211 | _t(__CLASS__.'.ADDEMAILRECIPIENT', 'Add Email Recipient') |
||
212 | ); |
||
213 | |||
214 | // who do we email on submission |
||
215 | $emailRecipients = GridField::create( |
||
216 | 'EmailRecipients', |
||
217 | '', |
||
218 | $this->EmailRecipients(), |
||
219 | $emailRecipientsConfig |
||
220 | ); |
||
221 | $emailRecipients |
||
222 | ->getConfig() |
||
223 | ->getComponentByType(GridFieldDetailForm::class) |
||
224 | ->setItemRequestClass(UserFormRecipientItemRequest::class); |
||
225 | |||
226 | $fields->addFieldsToTab('Root.FormOptions', $onCompleteFieldSet); |
||
227 | $fields->addFieldToTab('Root.Recipients', $emailRecipients); |
||
228 | $fields->addFieldsToTab('Root.FormOptions', $this->getFormOptions()); |
||
229 | |||
230 | |||
231 | // view the submissions |
||
232 | // make sure a numeric not a empty string is checked against this int column for SQL server |
||
233 | $parentID = (!empty($this->ID)) ? (int) $this->ID : 0; |
||
234 | |||
235 | // get a list of all field names and values used for print and export CSV views of the GridField below. |
||
236 | $columnSQL = <<<SQL |
||
237 | SELECT "SubmittedFormField"."Name" as "Name", COALESCE("EditableFormField"."Title", "SubmittedFormField"."Title") as "Title", COALESCE("EditableFormField"."Sort", 999) AS "Sort" |
||
238 | FROM "SubmittedFormField" |
||
239 | LEFT JOIN "SubmittedForm" ON "SubmittedForm"."ID" = "SubmittedFormField"."ParentID" |
||
240 | LEFT JOIN "EditableFormField" ON "EditableFormField"."Name" = "SubmittedFormField"."Name" AND "EditableFormField"."ParentID" = '$parentID' |
||
241 | WHERE "SubmittedForm"."ParentID" = '$parentID' |
||
242 | ORDER BY "Sort", "Title" |
||
243 | SQL; |
||
244 | // Sanitise periods in title |
||
245 | $columns = array(); |
||
246 | |||
247 | foreach (DB::query($columnSQL)->map() as $name => $title) { |
||
248 | $columns[$name] = trim(strtr($title, '.', ' ')); |
||
249 | } |
||
250 | |||
251 | $config = GridFieldConfig::create(); |
||
252 | $config->addComponent(new GridFieldToolbarHeader()); |
||
253 | $config->addComponent($sort = new GridFieldSortableHeader()); |
||
254 | $config->addComponent($filter = new UserFormsGridFieldFilterHeader()); |
||
255 | $config->addComponent(new GridFieldDataColumns()); |
||
256 | $config->addComponent(new GridFieldEditButton()); |
||
257 | $config->addComponent(new GridFieldDeleteAction()); |
||
258 | $config->addComponent(new GridFieldPageCount('toolbar-header-right')); |
||
259 | $config->addComponent($pagination = new GridFieldPaginator(25)); |
||
260 | $config->addComponent(new GridFieldDetailForm()); |
||
261 | $config->addComponent(new GridFieldButtonRow('after')); |
||
262 | $config->addComponent($export = new GridFieldExportButton('buttons-after-left')); |
||
263 | $config->addComponent($print = new GridFieldPrintButton('buttons-after-left')); |
||
264 | |||
265 | // show user form items in the summary tab |
||
266 | $summaryarray = array( |
||
267 | 'ID' => 'ID', |
||
268 | 'Created' => 'Created', |
||
269 | 'LastEdited' => 'Last Edited' |
||
270 | ); |
||
271 | |||
272 | foreach (EditableFormField::get()->filter(array('ParentID' => $parentID)) as $eff) { |
||
273 | if ($eff->ShowInSummary) { |
||
274 | $summaryarray[$eff->Name] = $eff->Title ?: $eff->Name; |
||
275 | } |
||
276 | } |
||
277 | |||
278 | $config->getComponentByType(GridFieldDataColumns::class)->setDisplayFields($summaryarray); |
||
279 | |||
280 | /** |
||
281 | * Support for {@link https://github.com/colymba/GridFieldBulkEditingTools} |
||
282 | */ |
||
283 | if (class_exists(BulkManager::class)) { |
||
284 | $config->addComponent(new BulkManager); |
||
285 | } |
||
286 | |||
287 | $sort->setThrowExceptionOnBadDataType(false); |
||
288 | $filter->setThrowExceptionOnBadDataType(false); |
||
289 | $pagination->setThrowExceptionOnBadDataType(false); |
||
290 | |||
291 | // attach every column to the print view form |
||
292 | $columns['Created'] = 'Created'; |
||
293 | $columns['SubmittedBy.Email'] = 'Submitter'; |
||
294 | $filter->setColumns($columns); |
||
295 | |||
296 | // print configuration |
||
297 | |||
298 | $print->setPrintHasHeader(true); |
||
299 | $print->setPrintColumns($columns); |
||
300 | |||
301 | // export configuration |
||
302 | $export->setCsvHasHeader(true); |
||
303 | $export->setExportColumns($columns); |
||
304 | |||
305 | $submissions = GridField::create( |
||
306 | 'Submissions', |
||
307 | '', |
||
308 | $this->Submissions()->sort('Created', 'DESC'), |
||
309 | $config |
||
310 | ); |
||
311 | $fields->addFieldToTab('Root.Submissions', $submissions); |
||
312 | $fields->addFieldToTab( |
||
313 | 'Root.FormOptions', |
||
314 | CheckboxField::create( |
||
315 | 'DisableSaveSubmissions', |
||
316 | _t(__CLASS__.'.SAVESUBMISSIONS', 'Disable Saving Submissions to Server') |
||
317 | ) |
||
318 | ); |
||
319 | }); |
||
320 | |||
321 | $fields = parent::getCMSFields(); |
||
322 | |||
323 | if ($this->EmailRecipients()->Count() == 0 && static::config()->recipients_warning_enabled) { |
||
324 | $fields->addFieldToTab('Root.Main', LiteralField::create( |
||
325 | 'EmailRecipientsWarning', |
||
326 | '<p class="message warning">' . _t( |
||
327 | __CLASS__.'.NORECIPIENTS', |
||
328 | 'Warning: You have not configured any recipients. Form submissions may be missed.' |
||
329 | ) |
||
330 | . '</p>' |
||
331 | ), 'Title'); |
||
332 | } |
||
333 | |||
334 | return $fields; |
||
335 | } |
||
336 | |||
404 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.