Conditions | 4 |
Paths | 1 |
Total Lines | 55 |
Code Lines | 36 |
Lines | 11 |
Ratio | 20 % |
Changes | 8 | ||
Bugs | 1 | Features | 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 |
||
33 | public function getCMSFields() |
||
34 | { |
||
35 | $elements = $this->Elements(); |
||
36 | $isInDb = $this->isInDB(); |
||
37 | $allowed = $this->config()->get('allowed_elements'); |
||
38 | |||
39 | $this->beforeUpdateCMSFields(function ($fields) use ($elements, $isInDb, $allowed) { |
||
40 | $desc = HTMLEditorField::create('ListDescription', 'List Description'); |
||
41 | $desc->setRightTitle('Optional'); |
||
42 | $fields->addFieldToTab('Root.Main', $desc); |
||
43 | |||
44 | if ($isInDb) { |
||
45 | $adder = new GridFieldAddNewMultiClass(); |
||
46 | |||
47 | View Code Duplication | if (is_array($allowed)) { |
|
48 | $list = $allowed; |
||
49 | } else { |
||
50 | $classes = ClassInfo::subclassesFor('BaseElement'); |
||
51 | $list = array(); |
||
52 | unset($classes['BaseElement']); |
||
53 | |||
54 | foreach ($classes as $class) { |
||
55 | $list[$class] = singleton($class)->i18n_singular_name(); |
||
56 | } |
||
57 | } |
||
58 | |||
59 | asort($list); |
||
60 | |||
61 | $adder->setClasses($list); |
||
62 | |||
63 | $config = GridFieldConfig_RecordEditor::create(100); |
||
64 | $config->addComponent(new GridFieldSortableRows('Sort')); |
||
65 | $config->removeComponentsByType('GridFieldAddNewButton'); |
||
66 | $config->addComponent($adder); |
||
67 | |||
68 | $config->removeComponentsByType('GridFieldDetailForm'); |
||
69 | $config->addComponent(new VersionedDataObjectDetailsForm()); |
||
70 | |||
71 | $widgetArea = new GridField( |
||
72 | 'Elements', |
||
73 | Config::inst()->get("ElementPageExtension", 'elements_title'), |
||
74 | $elements, |
||
75 | $config |
||
76 | ); |
||
77 | |||
78 | $fields->addFieldToTab('Root.Main', $widgetArea); |
||
79 | } else { |
||
80 | $fields->addFieldToTab('Root.Main', LiteralField::create('warn', '<p class="message notice">Once you save this object you will be able to add items</p>')); |
||
81 | } |
||
82 | |||
83 | $fields->removeByName('Root.Elements'); |
||
84 | }); |
||
85 | |||
86 | return parent::getCMSFields(); |
||
87 | } |
||
88 | |||
109 |
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.