Conditions | 21 |
Paths | 18 |
Total Lines | 78 |
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 |
||
119 | public function saveInto(DataObjectInterface $record) |
||
120 | { |
||
121 | $name = $this->name; |
||
122 | $idName = $name . "ID"; |
||
123 | |||
124 | $widgetarea = $record->getComponent($name); |
||
125 | $widgetarea->write(); |
||
126 | |||
127 | $record->$idName = $widgetarea->ID; |
||
128 | |||
129 | $widgets = $widgetarea->Items(); |
||
130 | |||
131 | // store the field IDs and delete the missing fields |
||
132 | // alternatively, we could delete all the fields and re add them |
||
133 | $missingWidgets = array(); |
||
134 | |||
135 | if ($widgets) { |
||
136 | foreach ($widgets as $existingWidget) { |
||
137 | $missingWidgets[$existingWidget->ID] = $existingWidget; |
||
138 | } |
||
139 | } |
||
140 | |||
141 | if (!$this->getForm()) { |
||
142 | throw new Exception("no form"); |
||
143 | } |
||
144 | |||
145 | $widgetData = $this->getForm()->getRequest()->requestVar('Widget'); |
||
146 | if ($widgetData && isset($widgetData[$this->getName()])) { |
||
147 | $widgetAreaData = $widgetData[$this->getName()]; |
||
148 | |||
149 | foreach ($widgetAreaData as $newWidgetID => $newWidgetData) { |
||
150 | // Sometimes the id is "new-1" or similar, ensure this doesn't get into the query |
||
151 | if (!is_numeric($newWidgetID)) { |
||
152 | $newWidgetID = 0; |
||
153 | } |
||
154 | |||
155 | $widget = null; |
||
156 | if ($newWidgetID) { |
||
157 | // \"ParentID\" = '0' is for the new page |
||
158 | $widget = Widget::get() |
||
159 | ->filter('ParentID', array(0, $record->$name()->ID)) |
||
160 | ->byID($newWidgetID); |
||
161 | |||
162 | // check if we are updating an existing widget |
||
163 | if ($widget && isset($missingWidgets[$widget->ID])) { |
||
164 | unset($missingWidgets[$widget->ID]); |
||
165 | } |
||
166 | } |
||
167 | |||
168 | // create a new object |
||
169 | if (!$widget |
||
170 | && !empty($newWidgetData['Type']) |
||
171 | && class_exists($newWidgetData['Type']) |
||
172 | && is_subclass_of($newWidgetData['Type'], Widget::class) |
||
173 | ) { |
||
174 | $widget = Injector::inst()->create($newWidgetData['Type']); |
||
175 | $widget->ID = 0; |
||
176 | $widget->ParentID = $record->$name()->ID; |
||
177 | } |
||
178 | |||
179 | if ($widget) { |
||
180 | if ($widget->ParentID == 0) { |
||
181 | $widget->ParentID = $record->$name()->ID; |
||
182 | } |
||
183 | $widget->populateFromPostData($newWidgetData); |
||
184 | } |
||
185 | } |
||
186 | } |
||
187 | |||
188 | // remove the fields not saved |
||
189 | if ($missingWidgets) { |
||
190 | foreach ($missingWidgets as $removedWidget) { |
||
191 | if (isset($removedWidget) && is_numeric($removedWidget->ID)) { |
||
192 | $removedWidget->delete(); |
||
193 | } |
||
194 | } |
||
195 | } |
||
196 | } |
||
197 | } |
||
198 |
Since your code implements the magic setter
_set
, this function will be called for any write access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.