Conditions | 67 |
Paths | > 20000 |
Total Lines | 184 |
Code Lines | 127 |
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 |
||
200 | function insert($obj, $force=true, $object = null) { |
||
201 | if ($obj->isNew()) { |
||
202 | $new = true; |
||
203 | $old = $this->create(); |
||
204 | $obj->setVar('created', time()); |
||
205 | } else { |
||
206 | $new = false; |
||
207 | $old = $this->get($obj->getVar('sid')); |
||
208 | $obj->setVar('updated', time()); |
||
209 | } |
||
210 | |||
211 | $albums_handler = xoops_getmodulehandler('albums', 'songlist'); |
||
212 | $artists_handler = xoops_getmodulehandler('artists', 'songlist'); |
||
213 | $genre_handler = xoops_getmodulehandler('genre', 'songlist'); |
||
214 | $voice_handler = xoops_getmodulehandler('voice', 'songlist'); |
||
215 | $category_handler = xoops_getmodulehandler('category', 'songlist'); |
||
216 | |||
217 | if ($obj->vars['gid']['changed']==true) { |
||
218 | if ($new==true||($obj->vars['gid']['value']!=0)) { |
||
219 | $genre = $genre_handler->get($obj->vars['gid']['value']); |
||
220 | if (is_object($genre)) { |
||
221 | $genre->setVar('songs', $genre->getVar('songs')+1); |
||
222 | $genre_handler->insert($genre, true, $obj); |
||
223 | } |
||
224 | } |
||
225 | if (!$old->isNew()&&$old->getVar('gid')>0) { |
||
226 | $genre = $genre_handler->get($old->vars['gid']['value']); |
||
227 | if (is_object($genre)) { |
||
228 | $genre->setVar('songs', $genre->getVar('songs')-1); |
||
229 | $genre_handler->insert($genre, true, null); |
||
230 | } |
||
231 | } |
||
232 | } |
||
233 | |||
234 | if ($obj->vars['vcid']['changed']==true) { |
||
235 | if ($new==true||($obj->vars['vcid']['value']!=0)) { |
||
236 | $voice = $voice_handler->get($obj->vars['vcid']['value']); |
||
237 | if (is_object($voice)) { |
||
238 | $voice->setVar('songs', $voice->getVar('songs')+1); |
||
239 | $voice_handler->insert($voice, true, $obj); |
||
240 | } |
||
241 | } |
||
242 | if (!$old->isNew()&&$old->getVar('vcid')>0) { |
||
243 | $voice = $voice_handler->get($old->vars['vcid']['value']); |
||
244 | if (is_object($voice)) { |
||
245 | $voice->setVar('songs', $voice->getVar('songs')-1); |
||
246 | $voice_handler->insert($voice, true, null); |
||
247 | } |
||
248 | } |
||
249 | } |
||
250 | |||
251 | if ($obj->vars['cid']['changed']==true) { |
||
252 | if ($new==true||($obj->vars['cid']['value']!=0)) { |
||
253 | $category = $category_handler->get($obj->vars['cid']['value']); |
||
254 | if (is_object($category)) { |
||
255 | $category->setVar('songs', $category->getVar('songs')+1); |
||
256 | $category_handler->insert($category, true, $obj); |
||
257 | foreach($obj->getVar('aids') as $aid) { |
||
258 | $artists = $artists_handler->get($aid); |
||
259 | $cids = $artists->getVar('cids'); |
||
260 | $cids[$obj->getVar('cid')] = $obj->getVar('cid'); |
||
261 | if (is_object($artists)) { |
||
262 | $artists->setVar('cids', $cids); |
||
263 | $artists_handler->insert($artists, true, null); |
||
264 | } |
||
265 | } |
||
266 | } |
||
267 | } |
||
268 | if (!$old->isNew()&&$old->getVar('cid')>0) { |
||
269 | $category = $category_handler->get($old->vars['cid']['value']); |
||
270 | if (is_object($category)) { |
||
271 | $category->setVar('songs', $category->getVar('songs')-1); |
||
272 | $category_handler->insert($category, true, null); |
||
273 | foreach($obj->getVar('aids') as $aid) { |
||
274 | $artists = $artists_handler->get($aid); |
||
275 | $cids=array(); |
||
276 | foreach($artists->getVar('cids') as $cid) { |
||
277 | if($cid!=$old->getVar('cid')||$cid==$obj->getVar('cid')) |
||
278 | $cids[$cid] = $cid; |
||
279 | } |
||
280 | if (is_object($artists)) { |
||
281 | $artists->setVar('cids', $cids); |
||
282 | $artists_handler->insert($artists, true, null); |
||
283 | } |
||
284 | } |
||
285 | } |
||
286 | } |
||
287 | } |
||
288 | |||
289 | if ($obj->vars['aids']['changed']==true&&count($obj->vars['aids']['value'])!=0) { |
||
290 | if ($new==true||count($obj->vars['aids']['value'])!=0) { |
||
291 | foreach($obj->vars['aids']['value'] as $aid) { |
||
292 | if (!in_array($aid, $old->vars['aids']['value'])) { |
||
293 | $artists = $artists_handler->get($aid); |
||
294 | if (is_object($artists)) { |
||
295 | $artists->setVar('songs', $artists->getVar('songs')+1); |
||
296 | $artists_handler->insert($artists, true, $obj); |
||
297 | if ($new==true||($obj->vars['vcid']['value']!=0)) { |
||
298 | $voice = $voice_handler->get($obj->vars['vcid']['value']); |
||
299 | if (is_object($voice)) { |
||
300 | $voice->setVar('artists', $voice->getVar('artists')+1); |
||
301 | $voice_handler->insert($voice, true, $obj); |
||
302 | } |
||
303 | } |
||
304 | } |
||
305 | } |
||
306 | } |
||
307 | } |
||
308 | if (!$old->isNew()&&count($old->getVar('aids'))==0) { |
||
309 | foreach($old->getVar('aids') as $aid) { |
||
310 | if (!in_array($aid, $obj->vars['aids']['value'])) { |
||
311 | $artists = $artists_handler->get($aid); |
||
312 | if (is_object($artists)) { |
||
313 | $artists->setVar('songs', $artists->getVar('songs')-1); |
||
314 | $artists_handler->insert($artists, true, null); |
||
315 | if (!$old->isNew()&&$old->getVar('vcid')>0) { |
||
316 | $voice = $voice_handler->get($old->vars['vcid']['value']); |
||
317 | if (is_object($voice)) { |
||
318 | $voice->setVar('artists', $voice->getVar('artists')-1); |
||
319 | $voice_handler->insert($voice, true, null); |
||
320 | } |
||
321 | } |
||
322 | } |
||
323 | } |
||
324 | } |
||
325 | } |
||
326 | } |
||
327 | |||
328 | if ($obj->vars['abid']['changed']==true) { |
||
329 | if ($new==true||($obj->vars['abid']['value']!=0)) { |
||
330 | $album = $albums_handler->get($obj->vars['abid']['value']); |
||
331 | if (is_object($album)) { |
||
332 | $album->setVar('songs', $album->getVar('songs')+1); |
||
333 | $albums_handler->insert($album, true, $obj); |
||
334 | if ($new==true||($obj->vars['vcid']['value']!=0)) { |
||
335 | $voice = $voice_handler->get($obj->vars['vcid']['value']); |
||
336 | if (is_object($voice)) { |
||
337 | $voice->setVar('albums', $voice->getVar('albums')+1); |
||
338 | $voice_handler->insert($voice, true, $obj); |
||
339 | } |
||
340 | } |
||
341 | } |
||
342 | } |
||
343 | if (!$old->isNew()&&$old->getVar('abid')>0) { |
||
344 | $album = $albums_handler->get($obj->vars['abid']['value']); |
||
345 | if (is_object($album)) { |
||
346 | $album->setVar('songs', $album->getVar('songs')-1); |
||
347 | $albums_handler->insert($album, true, null); |
||
348 | if (!$old->isNew()&&$old->getVar('vcid')>0) { |
||
349 | $voice = $voice_handler->get($old->vars['vcid']['value']); |
||
350 | if (is_object($voice)) { |
||
351 | $voice->setVar('albums', $voice->getVar('albums')-1); |
||
352 | $voice_handler->insert($voice, true, null); |
||
353 | } |
||
354 | } |
||
355 | } |
||
356 | } |
||
357 | } |
||
358 | |||
359 | if (strlen($obj->getVar('title'))==0) |
||
360 | return false; |
||
361 | |||
362 | $sid = parent::insert($obj, $force); |
||
363 | if ($obj->vars['abid']['value']>0) { |
||
364 | $album = $albums_handler->get($obj->vars['abid']['value']); |
||
365 | $arry = $album->getVar('sids'); |
||
366 | $arry[$sid] = $sid; |
||
367 | if (is_object($album)) { |
||
368 | $album->setVar('sids', $arry); |
||
369 | $albums_handler->insert($album); |
||
370 | } |
||
371 | } |
||
372 | if (count($obj->getVar('aids'))>0) { |
||
373 | foreach($obj->getVar('aids') as $aid) { |
||
374 | $artist = $artists_handler->get($aid); |
||
375 | $arry = $artist->getVar('sids'); |
||
376 | $arry[$sid] = $sid; |
||
377 | if (is_object($artists)) { |
||
378 | $artist->setVar('sids', $arry); |
||
379 | $artists_handler->insert($artist); |
||
380 | } |
||
381 | } |
||
382 | } |
||
383 | return $sid; |
||
384 | } |
||
466 | ?> |
||
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.