| Conditions | 26 | 
| Paths | 4719 | 
| Total Lines | 151 | 
| Code Lines | 91 | 
| 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 | ||
| 239 | private function getGraphDataUpward($start_id, $nest = 0) | ||
| 240 |     { | ||
| 241 | $conn = $this->getConnection(); | ||
| 242 | $db = $this->getDB(); | ||
| 243 | |||
| 244 | $threshold = (int) ($this->nest) * 1; | ||
| 245 | $has = (int) ($nest) * 1; | ||
| 246 |         if ($threshold >= $has) { | ||
| 247 | $person = Person::on($conn)->find($start_id); | ||
| 248 | // do not process for null | ||
| 249 |             if ($person == null) { | ||
| 250 | return; | ||
| 251 | } | ||
| 252 | |||
| 253 | // do not process again | ||
| 254 |             if (array_key_exists($start_id, $this->persons)) { | ||
| 255 | return; | ||
| 256 | } | ||
| 257 | // do self | ||
| 258 |             if (! array_key_exists($start_id, $this->persons)) { | ||
| 259 | // this is not added | ||
| 260 |                 $_families = Family::on($conn)->where('husband_id', $start_id)->orwhere('wife_id', $start_id)->select('id')->get(); | ||
| 261 | $_union_ids = []; | ||
| 262 |                 foreach ($_families as $item) { | ||
| 263 | $_union_ids[] = 'u'.$item->id; | ||
| 264 | // add current family link | ||
| 265 | // $this->links[] = [$start_id, 'u'.$item->id]; | ||
| 266 | array_unshift($this->links, [$start_id, 'u'.$item->id]); | ||
| 267 | } | ||
| 268 |                 $person->setAttribute('own_unions', $_union_ids); | ||
| 269 |                 $person->setAttribute('parent_union', 'u'.$person->child_in_family_id); | ||
| 270 | // add to persons | ||
| 271 | $this->persons[$start_id] = $person; | ||
| 272 | |||
| 273 | // get self's parents data | ||
| 274 | $p_family_id = $person->child_in_family_id; | ||
| 275 |                 if (! empty($p_family_id)) { | ||
| 276 | // add parent family link | ||
| 277 | // $this->links[] = ['u'.$p_family_id, $start_id] ; | ||
| 278 | array_unshift($this->links, ['u'.$p_family_id, $start_id]); | ||
| 279 | $p_family = Family::on($conn)->find($p_family_id); | ||
| 280 |                     if (isset($p_family->husband_id)) { | ||
| 281 | $p_fatherid = $p_family->husband_id; | ||
| 282 | $this->getGraphDataUpward($p_fatherid, $nest + 1); | ||
| 283 | } | ||
| 284 |                     if (isset($p_family->wife_id)) { | ||
| 285 | $p_motherid = $p_family->wife_id; | ||
| 286 | $this->getGraphDataUpward($p_motherid, $nest + 1); | ||
| 287 | } | ||
| 288 | } | ||
| 289 | } | ||
| 290 | // get partner | ||
| 291 |             $cu_families = Family::on($conn)->where('husband_id', $start_id)->orwhere('wife_id', $start_id)->get(); | ||
| 292 |             foreach ($cu_families as $family) { | ||
| 293 | $family_id = $family->id; | ||
| 294 | $father = Person::on($conn)->find($family->husband_id); | ||
| 295 | $mother = Person::on($conn)->find($family->wife_id); | ||
| 296 |                 if (isset($father->id)) { | ||
| 297 |                     if (! array_key_exists($father->id, $this->persons)) { | ||
| 298 | // this is not added | ||
| 299 |                         $_families = Family::on($conn)->where('husband_id', $father->id)->orwhere('wife_id', $father->id)->select('id')->get(); | ||
| 300 | $_union_ids = []; | ||
| 301 |                         foreach ($_families as $item) { | ||
| 302 | $_union_ids[] = 'u'.$item->id; | ||
| 303 | } | ||
| 304 |                         $father->setAttribute('own_unions', $_union_ids); | ||
| 305 |                         $father->setAttribute('parent_union', 'u'.$father->child_in_family_id); | ||
| 306 | // add to persons | ||
| 307 | $this->persons[$father->id] = $father; | ||
| 308 | |||
| 309 | // add current family link | ||
| 310 | // $this->links[] = [$father->id, 'u'.$family_id]; | ||
| 311 | array_unshift($this->links, [$father->id, 'u'.$family_id]); | ||
| 312 | // get husband's parents data | ||
| 313 | $p_family_id = $father->child_in_family_id; | ||
| 314 |                         if (! empty($p_family_id)) { | ||
| 315 | // add parent family link | ||
| 316 | // $this->links[] = ['u'.$p_family_id, $father->id] ; | ||
| 317 | array_unshift($this->links, ['u'.$p_family_id, $father->id]); | ||
| 318 | $p_family = Family::on($conn)->find($p_family_id); | ||
| 319 |                             if (isset($p_family->husband_id)) { | ||
| 320 | $p_fatherid = $p_family->husband_id; | ||
| 321 | $this->getGraphDataUpward($p_fatherid, $nest + 1); | ||
| 322 | } | ||
| 323 |                             if (isset($p_family->wife_id)) { | ||
| 324 | $p_motherid = $p_family->wife_id; | ||
| 325 | $this->getGraphDataUpward($p_motherid, $nest + 1); | ||
| 326 | } | ||
| 327 | } | ||
| 328 | } | ||
| 329 | } | ||
| 330 |                 if (isset($mother->id)) { | ||
| 331 |                     if (! array_key_exists($mother->id, $this->persons)) { | ||
| 332 | // this is not added | ||
| 333 |                         $_families = Family::on($conn)->where('husband_id', $mother->id)->orwhere('wife_id', $mother->id)->select('id')->get(); | ||
| 334 | $_union_ids = []; | ||
| 335 |                         foreach ($_families as $item) { | ||
| 336 | $_union_ids[] = $item->id; | ||
| 337 | } | ||
| 338 |                         $mother->setAttribute('own_unions', $_union_ids); | ||
| 339 |                         $mother->setAttribute('parent_union', 'u'.$mother->child_in_family_id); | ||
| 340 | // add to person | ||
| 341 | $this->persons[$mother->id] = $mother; | ||
| 342 | // add current family link | ||
| 343 | // $this->links[] = [$mother->id, 'u'.$family_id]; | ||
| 344 | array_unshift($this->links, [$mother->id, 'u'.$family_id]); | ||
| 345 | // get wifee's parents data | ||
| 346 | $p_family_id = $mother->child_in_family_id; | ||
| 347 |                         if (! empty($p_family_id)) { | ||
| 348 | // add parent family link | ||
| 349 | // $this->links[] = ['u'.$p_family_id, $father->id] ; | ||
| 350 | array_unshift($this->links, ['u'.$p_family_id, $mother->id]); | ||
| 351 | |||
| 352 | $p_family = Family::on($conn)->find($p_family_id); | ||
| 353 |                             if (isset($p_family->husband_id)) { | ||
| 354 | $p_fatherid = $p_family->husband_id; | ||
| 355 | $this->getGraphDataUpward($p_fatherid, $nest + 1); | ||
| 356 | } | ||
| 357 |                             if (isset($p_family->wife_id)) { | ||
| 358 | $p_motherid = $p_family->wife_id; | ||
| 359 | $this->getGraphDataUpward($p_motherid, $nest + 1); | ||
| 360 | } | ||
| 361 | } | ||
| 362 | } | ||
| 363 | } | ||
| 364 | |||
| 365 | // find children | ||
| 366 |                 $children = Person::on($conn)->where('child_in_family_id', $family_id)->get(); | ||
| 367 | $children_ids = []; | ||
| 368 |                 foreach ($children as $child) { | ||
| 369 | $child_id = $child->id; | ||
| 370 | $children_ids[] = $child_id; | ||
| 371 | } | ||
| 372 | |||
| 373 | // compose union item and add to unions | ||
| 374 | $union = []; | ||
| 375 | $union['id'] = 'u'.$family_id; | ||
| 376 | $union['partner'] = [isset($father->id) ? $father->id : null, isset($mother->id) ? $mother->id : null]; | ||
| 377 | $union['children'] = $children_ids; | ||
| 378 | $this->unions['u'.$family_id] = $union; | ||
| 379 | } | ||
| 380 | // get brother/sisters | ||
| 381 |             $brothers = Person::on($conn)->where('child_in_family_id', $person->child_in_family_id) | ||
| 382 |                 ->whereNotNull('child_in_family_id') | ||
| 383 |                 ->where('id', '<>', $start_id)->get(); | ||
| 384 | // $nest = $nest -1; | ||
| 385 |             foreach ($brothers as $brother) { | ||
| 386 | $this->getGraphDataUpward($brother->id, $nest); | ||
| 387 | } | ||
| 388 |         } else { | ||
| 389 | return; | ||
| 390 | } | ||
| 446 |