Show::getGraphData()   B
last analyzed

Complexity

Conditions 11
Paths 11

Size

Total Lines 54
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 11
eloc 35
nc 11
nop 2
dl 0
loc 54
rs 7.3166
c 2
b 1
f 0

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
3
namespace App\Http\Controllers\Trees;
4
5
use App\Models\Family;
6
use App\Models\Person;
7
use Illuminate\Http\Request;
8
use Illuminate\Routing\Controller;
9
10
class Show extends Controller
11
{
12
    private ?array $persons = null;
13
    private ?array $unions = null;
14
    private ?array $links = null;
15
    private $nest;
16
17
    public function __invoke(Request $request)
18
    {
19
        $start_id = $request->get('start_id');
20
        $nest = $request->get('generation');
21
        $ret = [];
22
        $ret['start'] = (int) $start_id;
23
        $this->persons = [];
24
        $this->unions = [];
25
        $this->links = [];
26
        $this->nest = $nest;
27
        $this->getGraphData((int) $start_id);
28
        $ret['persons'] = $this->persons;
29
        $ret['unions'] = $this->unions;
30
        $ret['links'] = $this->links;
31
        // ExportGedCom::dispatch(2, $request);
32
        // $file = 'file.GED';
33
        // $destinationPath = public_path().'/upload/';
34
        // $ret['link'] = $destinationPath.$file;
35
36
        return $ret;
37
    }
38
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) {
0 ignored issues
show
Bug introduced by
It seems like $families can also be of type Illuminate\Database\Eloq...elations\HasManyThrough and Illuminate\Database\Eloq...Relations\HasOneThrough; however, parameter $value of count() does only seem to accept Countable|array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

52
        if ((is_countable($families) ? count(/** @scrutinizer ignore-type */ $families) : 0) === 0) {
Loading history...
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
            }
94
        }
95
    }
96
97
    // private function getGraphData($start_id, $nest = 1)
98
    // {
99
    //     if ($this->nest >= $nest) {
100
101
    //         // add family
102
    //         $families = Family::where('husband_id', $start_id)->orwhere('wife_id', $start_id)->get();
103
104
    //         if (!count($families)) {
105
    //             $person = Person::find($start_id);
106
107
    //             // do not process for null
108
    //             if ($person == null) {
109
    //                 return;
110
    //             }
111
112
    //             $person->setAttribute('own_unions', []);
113
    //             $person['generation'] = $nest;
114
    //             $this->persons[$start_id] = $person;
115
116
    //             return true;
117
    //         }
118
119
    //         // add children
120
    //         foreach ($families as $family) {
121
    //             $family_id = $family->id;
122
    //             $father = Person::find($family->husband_id);
123
    //             $mother = Person::find($family->wife_id);
124
    //             // add partner to person
125
    //             // add partner link
126
127
    //             if (isset($father->id)) {
128
    //                 $_families = Family::where('husband_id', $father->id)->orwhere('wife_id', $father->id)->select('id')->get();
129
    //                 $_union_ids = [];
130
    //                 foreach ($_families as $item) {
131
    //                     $_union_ids[] = 'u'.$item->id;
132
    //                 }
133
    //                 $father->setAttribute('own_unions', $_union_ids);
134
    //                 $this->persons[$father->id] = $father;
135
    //                 $this->links[] = [$father->id, 'u'.$family_id];
136
    //             }
137
    //             if (isset($mother->id)) {
138
    //                 $_families = Family::where('husband_id', $mother->id)->orwhere('wife_id', $mother->id)->select('id')->get();
139
    //                 $_union_ids = [];
140
    //                 foreach ($_families as $item) {
141
    //                     $_union_ids[] = 'u'.$item->id;
142
    //                 }
143
    //                 $mother->setAttribute('own_unions', $_union_ids);
144
    //                 $this->persons[$mother->id] = $mother;
145
    //                 $this->links[] = [$mother->id, 'u'.$family_id];
146
    //             }
147
148
    //             // find children
149
    //             $children = Person::where('child_in_family_id', $family_id)->get();
150
    //             $children_ids = [];
151
    //             $nest++;
152
    //             foreach ($children as $child) {
153
    //                 $child_id = $child->id;
154
    //                 // add child to person
155
    //                 // parent_union
156
    //                 $child_data = Person::find($child_id);
157
    //                 $_families = Family::where('husband_id', $child_id)->orwhere('wife_id', $child_id)->select('id')->get();
158
    //                 $_union_ids = [];
159
    //                 foreach ($_families as $item) {
160
    //                     $_union_ids[] = 'u'.$item->id;
161
    //                 }
162
    //                 $child_data->setAttribute('own_unions', $_union_ids);
163
    //                 $child_data['generation'] = $nest;
164
    //                 $this->persons[$child_id] = $child_data;
165
166
    //                 // add union-child link
167
    //                 $this->links[] = ['u'.$family_id, $child_id];
168
169
    //                 // make union child filds
170
    //                 $children_ids[] = $child_id;
171
    //                 $this->getGraphData($child_id, $nest);
172
    //             }
173
174
    //             // compose union item and add to unions
175
    //             $union = [];
176
    //             $union['id'] = 'u'.$family_id;
177
    //             $union['partner'] = [isset($father->id) ? $father->id : null, isset($mother->id) ? $mother->id : null];
178
    //             $union['children'] = $children_ids;
179
    //             $this->unions['u'.$family_id] = $union;
180
    //         }
181
    //     }
182
183
    //     return true;
184
    // }
185
}
186