Conditions | 40 |
Paths | > 20000 |
Total Lines | 219 |
Code Lines | 131 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
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 |
||
67 | private function getGraphDataUpward($start_id, $nest = 0) |
||
68 | { |
||
69 | |||
70 | // $conn = $this->getConnection(); |
||
71 | // $db = $this->getDB(); |
||
72 | if ($this->nest >= $nest) { |
||
73 | $person = Person::find($start_id); |
||
74 | |||
75 | if ($person) { |
||
76 | $user = $person->user; |
||
77 | if ($user) { |
||
78 | $av = Avatar::where('user_id', '=', $user->id)->first(); |
||
79 | $file = UploadFile::where('attachable_id', '=', $av->id)->where('attachable_type', '=', 'avatar')->first(); |
||
80 | |||
81 | if ($file) { |
||
82 | $person->setAttribute('avatar', '/api/core/avatars/'.$av->id); |
||
83 | } else { |
||
84 | $person->setAttribute('avatar', ''); |
||
85 | } |
||
86 | } |
||
87 | } |
||
88 | |||
89 | // do not process for null |
||
90 | if ($person == null) { |
||
91 | return; |
||
92 | } |
||
93 | |||
94 | // do not process again |
||
95 | if (array_key_exists($start_id, $this->persons)) { |
||
96 | return; |
||
97 | } |
||
98 | // do self |
||
99 | if (! array_key_exists($start_id, $this->persons)) { |
||
100 | // this is not added |
||
101 | $_families = Family::where('husband_id', $start_id)->orwhere('wife_id', $start_id)->select('id')->get(); |
||
102 | $_union_ids = []; |
||
103 | foreach ($_families as $item) { |
||
104 | $_union_ids[] = 'u'.$item->id; |
||
105 | // add current family link |
||
106 | // $this->links[] = [$start_id, 'u'.$item->id]; |
||
107 | array_unshift($this->links, [$start_id, 'u'.$item->id]); |
||
108 | } |
||
109 | $person->setAttribute('own_unions', $_union_ids); |
||
110 | $person->setAttribute('parent_union', 'u'.$person->child_in_family_id); |
||
111 | // add to persons |
||
112 | $this->persons[$start_id] = $person; |
||
113 | |||
114 | // get self's parents data |
||
115 | if ($person) { |
||
116 | if ($person->id == 680) { |
||
117 | array_unshift($this->person_id, 'first'.$person->id); |
||
118 | } |
||
119 | } |
||
120 | $p_family_id = $person->child_in_family_id; |
||
121 | array_unshift($this->start_id, 'family_in_child_id'.$person->child_in_family_id); |
||
122 | if ($p_family_id) { |
||
123 | array_unshift($this->family_id, 'first'.$p_family_id); |
||
124 | } |
||
125 | if (! empty($p_family_id)) { |
||
126 | // add parent family link |
||
127 | // $this->links[] = ['u'.$p_family_id, $start_id] ; |
||
128 | array_unshift($this->links, ['u'.$p_family_id, $start_id]); |
||
129 | $p_family = Family::find($p_family_id); |
||
130 | if (isset($p_family->husband_id)) { |
||
131 | $p_fatherid = $p_family->husband_id; |
||
132 | $this->getGraphDataUpward($p_fatherid, $nest + 1); |
||
133 | } |
||
134 | if (isset($p_family->wife_id)) { |
||
135 | $p_motherid = $p_family->wife_id; |
||
136 | $this->getGraphDataUpward($p_motherid, $nest + 1); |
||
137 | } |
||
138 | } |
||
139 | } |
||
140 | // get partner |
||
141 | $cu_families = Family::where('husband_id', $start_id)->orwhere('wife_id', $start_id)->get(); |
||
142 | foreach ($cu_families as $family) { |
||
143 | $family_id = $family->id; |
||
144 | $father = Person::find($family->husband_id); |
||
145 | $mother = Person::find($family->wife_id); |
||
146 | |||
147 | $user = null; //$father->user; |
||
148 | |||
149 | if ($user) { |
||
150 | $av = Avatar::where('user_id', '=', $user->id)->first(); |
||
151 | $file = UploadFile::where('attachable_id', '=', $av->id)->where('attachable_type', '=', 'avatar')->first(); |
||
152 | |||
153 | if ($file) { |
||
154 | $father->setAttribute('avatar', '/api/core/avatars/'.$av->id); |
||
155 | } else { |
||
156 | $father->setAttribute('avatar', ''); |
||
157 | } |
||
158 | $father->setAttribute('relation', 'father'); |
||
159 | } |
||
160 | |||
161 | if ($mother) { |
||
162 | $user = $mother->user; |
||
163 | |||
164 | if ($user) { |
||
165 | $av = Avatar::where('user_id', '=', $user->id)->first(); |
||
166 | $file = UploadFile::where('attachable_id', '=', $av->id)->where('attachable_type', '=', 'avatar')->first(); |
||
167 | |||
168 | if ($file) { |
||
169 | $mother->setAttribute('avatar', '/api/core/avatars/'.$av->id); |
||
170 | } else { |
||
171 | $mother->setAttribute('avatar', ''); |
||
172 | } |
||
173 | $mother->setAttribute('relation', 'mother'); |
||
174 | } |
||
175 | } |
||
176 | |||
177 | if (isset($father->id)) { |
||
178 | if (! array_key_exists($father->id, $this->persons)) { |
||
179 | // this is not added |
||
180 | $_families = Family::where('husband_id', $father->id)->orwhere('wife_id', $father->id)->select('id')->get(); |
||
181 | $_union_ids = []; |
||
182 | foreach ($_families as $item) { |
||
183 | $_union_ids[] = 'u'.$item->id; |
||
184 | } |
||
185 | $father->setAttribute('own_unions', $_union_ids); |
||
186 | $father->setAttribute('parent_union', 'u'.$father->child_in_family_id); |
||
187 | // add to persons |
||
188 | $this->persons[$father->id] = $father; |
||
189 | |||
190 | // add current family link |
||
191 | // $this->links[] = [$father->id, 'u'.$family_id]; |
||
192 | array_unshift($this->links, [$father->id, 'u'.$family_id]); |
||
193 | // get husband's parents data |
||
194 | $p_family_id = $father->child_in_family_id; |
||
195 | if ($p_family_id) { |
||
196 | array_unshift($this->family_id, 'father'.$p_family_id); |
||
197 | } |
||
198 | if (! empty($p_family_id)) { |
||
199 | // add parent family link |
||
200 | // $this->links[] = ['u'.$p_family_id, $father->id] ; |
||
201 | array_unshift($this->links, ['u'.$p_family_id, $father->id]); |
||
202 | $p_family = Family::find($p_family_id); |
||
203 | if (isset($p_family->husband_id)) { |
||
204 | $p_fatherid = $p_family->husband_id; |
||
205 | $this->getGraphDataUpward($p_fatherid, $nest + 1); |
||
206 | } |
||
207 | if (isset($p_family->wife_id)) { |
||
208 | $p_motherid = $p_family->wife_id; |
||
209 | $this->getGraphDataUpward($p_motherid, $nest + 1); |
||
210 | } |
||
211 | } |
||
212 | } |
||
213 | } |
||
214 | if (isset($mother->id)) { |
||
215 | if (! array_key_exists($mother->id, $this->persons)) { |
||
216 | // this is not added |
||
217 | $_families = Family::where('husband_id', $mother->id)->orwhere('wife_id', $mother->id)->select('id')->get(); |
||
218 | $_union_ids = []; |
||
219 | foreach ($_families as $item) { |
||
220 | $_union_ids[] = $item->id; |
||
221 | } |
||
222 | $mother->setAttribute('own_unions', $_union_ids); |
||
223 | $mother->setAttribute('parent_union', 'u'.$mother->child_in_family_id); |
||
224 | // add to person |
||
225 | $this->persons[$mother->id] = $mother; |
||
226 | // add current family link |
||
227 | // $this->links[] = [$mother->id, 'u'.$family_id]; |
||
228 | array_unshift($this->links, [$mother->id, 'u'.$family_id]); |
||
229 | // get wifee's parents data |
||
230 | $p_family_id = $mother->child_in_family_id; |
||
231 | if ($p_family_id) { |
||
232 | } |
||
233 | if (! empty($p_family_id)) { |
||
234 | // add parent family link |
||
235 | // $this->links[] = ['u'.$p_family_id, $father->id] ; |
||
236 | array_unshift($this->links, ['u'.$p_family_id, $mother->id]); |
||
237 | |||
238 | $p_family = Family::find($p_family_id); |
||
239 | if (isset($p_family->husband_id)) { |
||
240 | array_unshift($this->family_id, 'mother'.$p_family_id); |
||
241 | $p_fatherid = $p_family->husband_id; |
||
242 | $this->getGraphDataUpward($p_fatherid, $nest + 1); |
||
243 | } |
||
244 | if (isset($p_family->wife_id)) { |
||
245 | array_unshift($this->family_id, 'mother'.$p_family_id); |
||
246 | $p_motherid = $p_family->wife_id; |
||
247 | $this->getGraphDataUpward($p_motherid, $nest + 1); |
||
248 | } |
||
249 | } |
||
250 | } |
||
251 | } |
||
252 | |||
253 | // find children |
||
254 | $children = Person::where('child_in_family_id', $family_id)->get(); |
||
255 | $children_ids = []; |
||
256 | foreach ($children as $child) { |
||
257 | $this->getGraphDataUpward($child->id, $nest + 1); |
||
258 | if ($child->id == 680) { |
||
259 | array_unshift($this->family_id, $child->id); |
||
260 | } |
||
261 | $child_id = $child->id; |
||
262 | $children_ids[] = $child_id; |
||
263 | } |
||
264 | |||
265 | // compose union item and add to unions |
||
266 | $union = []; |
||
267 | $union['id'] = 'u'.$family_id; |
||
268 | $union['partner'] = [isset($father->id) ? $father->id : null, isset($mother->id) ? $mother->id : null]; |
||
269 | $union['children'] = $children_ids; |
||
270 | $this->unions['u'.$family_id] = $union; |
||
271 | } |
||
272 | //get brother/sisters |
||
273 | $i = 0; |
||
274 | $brothers = Person::where('child_in_family_id', $person->child_in_family_id) |
||
275 | ->whereNotNull('child_in_family_id') |
||
276 | ->where('id', '<>', $start_id)->get(); |
||
277 | |||
278 | foreach ($brothers as $brother) { |
||
279 | // if($i < 2) { |
||
280 | $this->getGraphDataUpward($brother->id, $nest + 1); |
||
281 | // } |
||
282 | $i++; |
||
283 | } |
||
284 | } else { |
||
285 | return; |
||
286 | } |
||
289 |
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: