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