Conditions | 5 |
Paths | 2 |
Total Lines | 77 |
Code Lines | 43 |
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 |
||
16 | public function updateEditForm($form) |
||
17 | { |
||
18 | if (Permission::check('ADMIN')) { |
||
19 | $fields = $form->Fields(); |
||
20 | |||
21 | $loggerTab = $fields->findOrMakeTab('Root.IPLogs', 'IP Logs'); |
||
22 | |||
23 | // List ban entries |
||
24 | $banClass = get_class($this->banEntry); |
||
25 | |||
26 | $banEntries = $banClass::get()->sort('Created DESC'); |
||
27 | |||
28 | $banGrid = GridField::create('BanGrid', 'Bans', $banEntries); |
||
29 | |||
30 | $banGrid->setForm($form); |
||
31 | |||
32 | $banGridConfig = GridFieldConfig_RecordEditor::create(); |
||
33 | $banGridConfig->removeComponentsByType('GridFieldAddNewButton'); |
||
34 | |||
35 | // Add a new sudo column to show whether or not a ban is active |
||
36 | $dataColumns = $banGridConfig->getComponentByType('GridFieldDataColumns'); |
||
37 | |||
38 | $displayFields = $dataColumns->getDisplayFields($banGrid); |
||
39 | |||
40 | $displayFields = array('Active' => '') + $displayFields; |
||
41 | |||
42 | $dataColumns->setDisplayFields($displayFields); |
||
43 | |||
44 | $loggerService = $this->loggerService; |
||
45 | $dataColumns->setFieldFormatting( |
||
46 | array( |
||
47 | 'Active' => function ($value, $item) use ($loggerService) { |
||
48 | $banSecondsAgo = $item->obj('Created')->TimeDiffIn('seconds'); |
||
49 | $banSeconds = $loggerService->getRule($item->Event)['bantime']; |
||
50 | |||
51 | $active = $banSeconds === 0 || $banSecondsAgo < $banSeconds; |
||
52 | |||
53 | switch ($active) { |
||
54 | case true: |
||
55 | $colour = 'rgb(76, 153, 0)'; |
||
56 | $value = 'Active'; |
||
57 | break; |
||
58 | case false: |
||
59 | $colour = 'rgb(255, 0, 0)'; |
||
60 | $value = 'Expired'; |
||
61 | break; |
||
62 | } |
||
63 | |||
64 | $html = "<strong style='color: {$colour}; |
||
65 | font-size: 120%;'>{$value}</strong>"; |
||
66 | |||
67 | return $html; |
||
68 | } |
||
69 | ) |
||
70 | ); |
||
71 | |||
72 | $banGrid->setConfig($banGridConfig); |
||
73 | |||
74 | $loggerTab->push($banGrid); |
||
75 | |||
76 | // List log entries |
||
77 | $entryClass = get_class($this->loggerEntry); |
||
78 | |||
79 | $logEntries = $entryClass::get()->sort('Created DESC'); |
||
80 | |||
81 | $entryGrid = GridField::create('EntryGrid', 'Log Entries', $logEntries); |
||
82 | |||
83 | $entryGrid->setForm($form); |
||
84 | |||
85 | $entryGridConfig = GridFieldConfig_RecordEditor::create(); |
||
86 | $entryGridConfig->removeComponentsByType('GridFieldAddNewButton'); |
||
87 | |||
88 | $entryGrid->setConfig($entryGridConfig); |
||
89 | |||
90 | $loggerTab->push($entryGrid); |
||
91 | } |
||
92 | } |
||
93 | } |
||
94 |
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.