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