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