Conditions | 22 |
Paths | 18 |
Total Lines | 80 |
Code Lines | 43 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | 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 |
||
118 | public function saveInto(DataObjectInterface $record) |
||
119 | { |
||
120 | $name = $this->name; |
||
121 | $idName = $name . "ID"; |
||
122 | |||
123 | $widgetarea = $record->getComponent($name); |
||
124 | $widgetarea->write(); |
||
125 | |||
126 | $record->$idName = $widgetarea->ID; |
||
127 | |||
128 | $widgets = $widgetarea->Items(); |
||
129 | |||
130 | // store the field IDs and delete the missing fields |
||
131 | // alternatively, we could delete all the fields and re add them |
||
132 | $missingWidgets = array(); |
||
133 | |||
134 | if ($widgets) { |
||
135 | foreach ($widgets as $existingWidget) { |
||
136 | $missingWidgets[$existingWidget->ID] = $existingWidget; |
||
137 | } |
||
138 | } |
||
139 | |||
140 | if (!$this->getForm()) { |
||
141 | throw new Exception("no form"); |
||
142 | } |
||
143 | |||
144 | $widgetData = $this->getForm()->getController()->getRequest()->requestVar('Widget'); |
||
145 | if ($widgetData && isset($widgetData[$this->getName()])) { |
||
146 | $widgetAreaData = $widgetData[$this->getName()]; |
||
147 | |||
148 | foreach ($widgetAreaData as $newWidgetID => $newWidgetData) { |
||
149 | // Sometimes the id is "new-1" or similar, ensure this doesn't get into the query |
||
150 | if (!is_numeric($newWidgetID)) { |
||
151 | $newWidgetID = 0; |
||
152 | } |
||
153 | |||
154 | $widget = null; |
||
155 | if ($newWidgetID) { |
||
156 | // \"ParentID\" = '0' is for the new page |
||
157 | $widget = Widget::get() |
||
158 | ->filter('ParentID', array(0, $record->$name()->ID)) |
||
159 | ->byID($newWidgetID); |
||
160 | |||
161 | // check if we are updating an existing widget |
||
162 | if ($widget && isset($missingWidgets[$widget->ID])) { |
||
163 | unset($missingWidgets[$widget->ID]); |
||
164 | } |
||
165 | } |
||
166 | |||
167 | // unsantise the class name |
||
168 | if (empty($newWidgetData['Type'])) { |
||
169 | $newWidgetData['Type'] = ''; |
||
170 | } |
||
171 | $newWidgetData['Type'] = str_replace('_', '\\', $newWidgetData['Type']); |
||
172 | |||
173 | // create a new object |
||
174 | if (!$widget |
||
175 | && !empty($newWidgetData['Type']) |
||
176 | && class_exists($newWidgetData['Type']) |
||
177 | && is_subclass_of($newWidgetData['Type'], Widget::class) |
||
178 | ) { |
||
179 | $widget = Injector::inst()->create($newWidgetData['Type']); |
||
180 | $widget->ID = 0; |
||
181 | $widget->ParentID = $record->$name()->ID; |
||
182 | } |
||
183 | |||
184 | if ($widget) { |
||
185 | if ($widget->ParentID == 0) { |
||
186 | $widget->ParentID = $record->$name()->ID; |
||
187 | } |
||
188 | $widget->populateFromPostData($newWidgetData); |
||
189 | } |
||
190 | } |
||
191 | } |
||
192 | |||
193 | // remove the fields not saved |
||
194 | if ($missingWidgets) { |
||
195 | foreach ($missingWidgets as $removedWidget) { |
||
196 | if (isset($removedWidget) && is_numeric($removedWidget->ID)) { |
||
197 | $removedWidget->delete(); |
||
198 | } |
||
203 |