Conditions | 11 |
Paths | 11 |
Total Lines | 54 |
Code Lines | 35 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 1 | 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 |
||
39 | private function getGraphData($start_id, $nest = 1) |
||
40 | { |
||
41 | if ($this->nest < $nest) { |
||
42 | return; |
||
43 | } |
||
44 | |||
45 | $person = Person::find($start_id); |
||
46 | // do not process for null |
||
47 | if ($person === null) { |
||
48 | return; |
||
49 | } |
||
50 | |||
51 | $families = Family::where('husband_id', $start_id)->orwhere('wife_id', $start_id)->get(); |
||
52 | if ((is_countable($families) ? count($families) : 0) === 0) { |
||
|
|||
53 | $person = Person::find($start_id); |
||
54 | |||
55 | // do not process for null |
||
56 | if ($person === null) { |
||
57 | return; |
||
58 | } |
||
59 | |||
60 | $person->setAttribute('own_unions', []); |
||
61 | $person['generation'] = $nest; |
||
62 | $this->persons[$start_id] = $person; |
||
63 | |||
64 | return true; |
||
65 | } |
||
66 | $own_unions = $families->pluck('id')->map(fn ($id) => 'u'.$id)->toArray(); |
||
67 | $person->setAttribute('own_unions', $own_unions); |
||
68 | $person->setAttribute('generation', $nest); |
||
69 | |||
70 | $this->persons[$start_id] = $person; |
||
71 | |||
72 | // add children |
||
73 | foreach ($families as $family) { |
||
74 | $union_id = 'u'.$family->id; |
||
75 | $this->links[] = [$start_id, $union_id]; |
||
76 | $partners = [$family->husband_id ?? null, $family->wife_id ?? null]; |
||
77 | $this->unions[$union_id] = [ |
||
78 | 'id' => $union_id, |
||
79 | 'partner' => $partners, |
||
80 | 'children' => $family->children->pluck('id')->toArray(), |
||
81 | ]; |
||
82 | foreach ($partners as $partner) { |
||
83 | $p = Person::find($partner); |
||
84 | if (isset($p) && ! isset($this->persons[$partner])) { |
||
85 | $this->persons[$partner] = $p; |
||
86 | } |
||
87 | } |
||
88 | foreach ($family->children as $child) { |
||
89 | $this->links[] = ['u'.$family->id, $child->id]; |
||
90 | $child->setAttribute('generation', $nest + 1); |
||
91 | $this->persons[$child->id] = $child; |
||
92 | $this->getGraphData($child->id, $nest + 1); |
||
93 | } |
||
186 |