| Conditions | 11 |
| Paths | 2 |
| Total Lines | 72 |
| Code Lines | 44 |
| 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 |
||
| 37 | private function getGraphData($start_id, $nest = 1) |
||
| 38 | { |
||
| 39 | if ($this->nest >= $nest) { |
||
| 40 | $nest++; |
||
| 41 | |||
| 42 | // add family |
||
| 43 | $families = Family::where('husband_id', $start_id)->orwhere('wife_id', $start_id)->get(); |
||
| 44 | $own_unions = []; |
||
|
|
|||
| 45 | |||
| 46 | // add children |
||
| 47 | foreach ($families as $family) { |
||
| 48 | $family_id = $family->id; |
||
| 49 | $father = Person::find($family->husband_id); |
||
| 50 | $mother = Person::find($family->wife_id); |
||
| 51 | // add partner to person |
||
| 52 | // add partner link |
||
| 53 | |||
| 54 | if(isset($father->id)){ |
||
| 55 | $_families = Family::where('husband_id', $father->id)->orwhere('wife_id', $father->id)->select('id')->get(); |
||
| 56 | $_union_ids = []; |
||
| 57 | foreach($_families as $item){ |
||
| 58 | $_union_ids[] = 'u'.$item->id; |
||
| 59 | } |
||
| 60 | $father->setAttribute('own_unions', $_union_ids); |
||
| 61 | $this->persons[$father->id] = $father; |
||
| 62 | $this->links[] = [$father->id, 'u'.$family_id]; |
||
| 63 | } |
||
| 64 | if(isset($mother->id)){ |
||
| 65 | $_families = Family::where('husband_id', $mother->id)->orwhere('wife_id', $mother->id)->select('id')->get(); |
||
| 66 | $_union_ids = []; |
||
| 67 | foreach($_families as $item){ |
||
| 68 | $_union_ids[] = $item->id; |
||
| 69 | } |
||
| 70 | $mother->setAttribute('own_unions', $_union_ids); |
||
| 71 | $this->persons[$mother->id] = $mother; |
||
| 72 | $this->links[] = [$mother->id, 'u'.$family_id]; |
||
| 73 | } |
||
| 74 | |||
| 75 | // find children |
||
| 76 | $children = Person::where('child_in_family_id', $family_id)->get(); |
||
| 77 | $children_ids = []; |
||
| 78 | foreach ($children as $child) { |
||
| 79 | $child_id = $child->id; |
||
| 80 | // add child to person |
||
| 81 | // parent_union |
||
| 82 | $child_data = Person::find($child_id); |
||
| 83 | $_families = Family::where('husband_id', $child_id)->orwhere('wife_id', $child_id)->select('id')->get(); |
||
| 84 | $_union_ids = []; |
||
| 85 | foreach($_families as $item){ |
||
| 86 | $_union_ids[] = $item->id; |
||
| 87 | } |
||
| 88 | $child_data->setAttribute('own_unions', $_union_ids); |
||
| 89 | $this->persons[$child_id] = $child_data; |
||
| 90 | |||
| 91 | // add union-child link |
||
| 92 | $this->links[] = ['u'.$family_id, $child_id]; |
||
| 93 | |||
| 94 | // make union child filds |
||
| 95 | $children_ids[] = $child_id; |
||
| 96 | $this->getGraphData($child_id, $nest); |
||
| 97 | } |
||
| 98 | |||
| 99 | // compose union item and add to unions |
||
| 100 | $union = array(); |
||
| 101 | $union['id'] = 'u'.$family_id; |
||
| 102 | $union['partner'] = [isset($father->id) ? $father->id : null, isset($mother->id)? $mother->id: null]; |
||
| 103 | $union['children'] = $children_ids; |
||
| 104 | $this->unions['u'.$family_id] = $union; |
||
| 105 | } |
||
| 106 | } |
||
| 107 | |||
| 108 | return true; |
||
| 109 | } |
||
| 260 |