Conditions | 5 |
Paths | 5 |
Total Lines | 56 |
Code Lines | 41 |
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 |
||
72 | public function getHTMLFragments($gridField) |
||
73 | { |
||
74 | $state = $gridField->State->GridFieldDropdownAddNewButton; |
||
75 | $classesSource = $this->getClasses(); |
||
76 | |||
77 | if(empty($classesSource)) { |
||
78 | return []; |
||
79 | } else if(count($classesSource) > 1) { |
||
80 | $dropdown = DropdownField::create( |
||
81 | "Class", |
||
82 | "Class", |
||
83 | $classesSource |
||
84 | )->setFieldHolderTemplate("GridFieldDropdownAddNewButton_holder") |
||
85 | ->addExtraClass("gridfield-dropdown no-change-track"); |
||
86 | |||
87 | if (!$this->buttonName) { |
||
88 | $this->buttonName = 'Add new'; |
||
89 | } |
||
90 | } else { |
||
91 | $class = key($classesSource); |
||
92 | $dropdown = HiddenField::create( |
||
93 | "Class", |
||
94 | "Class", |
||
95 | $class |
||
96 | ); |
||
97 | |||
98 | if (!$this->buttonName) { |
||
99 | $this->buttonName = sprintf('Add new %s', $class); |
||
100 | } |
||
101 | } |
||
102 | |||
103 | $state->class = key($classesSource); |
||
104 | |||
105 | $action = GridField_FormAction::create( |
||
106 | $gridField, |
||
107 | 'add', |
||
108 | $this->buttonName, |
||
109 | 'add', |
||
110 | 'add' |
||
111 | )->setAttribute( |
||
112 | 'data-icon', |
||
113 | 'add' |
||
114 | )->addExtraClass("no-ajax ss-ui-action-constructive dropdown-action"); |
||
115 | |||
116 | Requirements::css(CONTENTBLOCKS_DIR . "/css/GridFieldDropdownAddNewButton.css"); |
||
117 | Requirements::javascript(CONTENTBLOCKS_DIR . "/javascript/GridFieldDropdownAddNewButton.js"); |
||
118 | |||
119 | return [ |
||
120 | $this->targetFragment => ArrayData::create([ |
||
121 | 'Fields' => ArrayList::create([ |
||
122 | $dropdown, |
||
123 | $action, |
||
124 | ]), |
||
125 | ])->renderWith("GridFieldDropdownAddNewButton"), |
||
126 | ]; |
||
127 | } |
||
128 | |||
196 |
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.