| Conditions | 26 |
| Paths | 4719 |
| Total Lines | 148 |
| Code Lines | 89 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 1 | Features | 1 |
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 |
||
| 119 | private function getGraphDataUpward($start_id, $nest = 0) |
||
| 120 | { |
||
| 121 | $threshold = (int) ($this->nest) * 1; |
||
| 122 | $has = (int) ($nest) * 1; |
||
| 123 | if ($threshold >= $has) { |
||
| 124 | $person = Person::find($start_id); |
||
| 125 | // do not process for null |
||
| 126 | if ($person == null) { |
||
| 127 | return; |
||
| 128 | } |
||
| 129 | |||
| 130 | // do not process again |
||
| 131 | if (array_key_exists($start_id, $this->persons)) { |
||
| 132 | return; |
||
| 133 | } |
||
| 134 | // do self |
||
| 135 | if (! array_key_exists($start_id, $this->persons)) { |
||
| 136 | // this is not added |
||
| 137 | $_families = Family::where('husband_id', $start_id)->orwhere('wife_id', $start_id)->select('id')->get(); |
||
| 138 | $_union_ids = []; |
||
| 139 | foreach ($_families as $item) { |
||
| 140 | $_union_ids[] = 'u'.$item->id; |
||
| 141 | // add current family link |
||
| 142 | // $this->links[] = [$start_id, 'u'.$item->id]; |
||
| 143 | array_unshift($this->links, [$start_id, 'u'.$item->id]); |
||
| 144 | } |
||
| 145 | $person->setAttribute('own_unions', $_union_ids); |
||
| 146 | $person->setAttribute('parent_union', 'u'.$person->child_in_family_id); |
||
| 147 | // add to persons |
||
| 148 | $this->persons[$start_id] = $person; |
||
| 149 | |||
| 150 | // get self's parents data |
||
| 151 | $p_family_id = $person->child_in_family_id; |
||
| 152 | if (! empty($p_family_id)) { |
||
| 153 | // add parent family link |
||
| 154 | // $this->links[] = ['u'.$p_family_id, $start_id] ; |
||
| 155 | array_unshift($this->links, ['u'.$p_family_id, $start_id]); |
||
| 156 | $p_family = Family::find($p_family_id); |
||
| 157 | if (isset($p_family->husband_id)) { |
||
| 158 | $p_fatherid = $p_family->husband_id; |
||
| 159 | $this->getGraphDataUpward($p_fatherid, $nest + 1); |
||
| 160 | } |
||
| 161 | if (isset($p_family->wife_id)) { |
||
| 162 | $p_motherid = $p_family->wife_id; |
||
| 163 | $this->getGraphDataUpward($p_motherid, $nest + 1); |
||
| 164 | } |
||
| 165 | } |
||
| 166 | } |
||
| 167 | // get partner |
||
| 168 | $cu_families = Family::where('husband_id', $start_id)->orwhere('wife_id', $start_id)->get(); |
||
| 169 | foreach ($cu_families as $family) { |
||
| 170 | $family_id = $family->id; |
||
| 171 | $father = Person::find($family->husband_id); |
||
| 172 | $mother = Person::find($family->wife_id); |
||
| 173 | if (isset($father->id)) { |
||
| 174 | if (! array_key_exists($father->id, $this->persons)) { |
||
| 175 | // this is not added |
||
| 176 | $_families = Family::where('husband_id', $father->id)->orwhere('wife_id', $father->id)->select('id')->get(); |
||
| 177 | $_union_ids = []; |
||
| 178 | foreach ($_families as $item) { |
||
| 179 | $_union_ids[] = 'u'.$item->id; |
||
| 180 | } |
||
| 181 | $father->setAttribute('own_unions', $_union_ids); |
||
| 182 | $father->setAttribute('parent_union', 'u'.$father->child_in_family_id); |
||
| 183 | // add to persons |
||
| 184 | $this->persons[$father->id] = $father; |
||
| 185 | |||
| 186 | // add current family link |
||
| 187 | // $this->links[] = [$father->id, 'u'.$family_id]; |
||
| 188 | array_unshift($this->links, [$father->id, 'u'.$family_id]); |
||
| 189 | // get husband's parents data |
||
| 190 | $p_family_id = $father->child_in_family_id; |
||
| 191 | if (! empty($p_family_id)) { |
||
| 192 | // add parent family link |
||
| 193 | // $this->links[] = ['u'.$p_family_id, $father->id] ; |
||
| 194 | array_unshift($this->links, ['u'.$p_family_id, $father->id]); |
||
| 195 | $p_family = Family::find($p_family_id); |
||
| 196 | if (isset($p_family->husband_id)) { |
||
| 197 | $p_fatherid = $p_family->husband_id; |
||
| 198 | $this->getGraphDataUpward($p_fatherid, $nest + 1); |
||
| 199 | } |
||
| 200 | if (isset($p_family->wife_id)) { |
||
| 201 | $p_motherid = $p_family->wife_id; |
||
| 202 | $this->getGraphDataUpward($p_motherid, $nest + 1); |
||
| 203 | } |
||
| 204 | } |
||
| 205 | } |
||
| 206 | } |
||
| 207 | if (isset($mother->id)) { |
||
| 208 | if (! array_key_exists($mother->id, $this->persons)) { |
||
| 209 | // this is not added |
||
| 210 | $_families = Family::where('husband_id', $mother->id)->orwhere('wife_id', $mother->id)->select('id')->get(); |
||
| 211 | $_union_ids = []; |
||
| 212 | foreach ($_families as $item) { |
||
| 213 | $_union_ids[] = $item->id; |
||
| 214 | } |
||
| 215 | $mother->setAttribute('own_unions', $_union_ids); |
||
| 216 | $mother->setAttribute('parent_union', 'u'.$mother->child_in_family_id); |
||
| 217 | // add to person |
||
| 218 | $this->persons[$mother->id] = $mother; |
||
| 219 | // add current family link |
||
| 220 | // $this->links[] = [$mother->id, 'u'.$family_id]; |
||
| 221 | array_unshift($this->links, [$mother->id, 'u'.$family_id]); |
||
| 222 | // get wifee's parents data |
||
| 223 | $p_family_id = $mother->child_in_family_id; |
||
| 224 | if (! empty($p_family_id)) { |
||
| 225 | // add parent family link |
||
| 226 | // $this->links[] = ['u'.$p_family_id, $father->id] ; |
||
| 227 | array_unshift($this->links, ['u'.$p_family_id, $mother->id]); |
||
| 228 | |||
| 229 | $p_family = Family::find($p_family_id); |
||
| 230 | if (isset($p_family->husband_id)) { |
||
| 231 | $p_fatherid = $p_family->husband_id; |
||
| 232 | $this->getGraphDataUpward($p_fatherid, $nest + 1); |
||
| 233 | } |
||
| 234 | if (isset($p_family->wife_id)) { |
||
| 235 | $p_motherid = $p_family->wife_id; |
||
| 236 | $this->getGraphDataUpward($p_motherid, $nest + 1); |
||
| 237 | } |
||
| 238 | } |
||
| 239 | } |
||
| 240 | } |
||
| 241 | |||
| 242 | // find children |
||
| 243 | $children = Person::where('child_in_family_id', $family_id)->get(); |
||
| 244 | $children_ids = []; |
||
| 245 | foreach ($children as $child) { |
||
| 246 | $child_id = $child->id; |
||
| 247 | $children_ids[] = $child_id; |
||
| 248 | } |
||
| 249 | |||
| 250 | // compose union item and add to unions |
||
| 251 | $union = []; |
||
| 252 | $union['id'] = 'u'.$family_id; |
||
| 253 | $union['partner'] = [isset($father->id) ? $father->id : null, isset($mother->id) ? $mother->id : null]; |
||
| 254 | $union['children'] = $children_ids; |
||
| 255 | $this->unions['u'.$family_id] = $union; |
||
| 256 | } |
||
| 257 | // get brother/sisters |
||
| 258 | $brothers = Person::where('child_in_family_id', $person->child_in_family_id) |
||
| 259 | ->whereNotNull('child_in_family_id') |
||
| 260 | ->where('id', '<>', $start_id)->get(); |
||
| 261 | // $nest = $nest -1; |
||
| 262 | foreach ($brothers as $brother) { |
||
| 263 | $this->getGraphDataUpward($brother->id, $nest); |
||
| 264 | } |
||
| 265 | } else { |
||
| 266 | return; |
||
| 267 | } |
||
| 270 |