| Total Complexity | 50 |
| Total Lines | 376 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like GedcomWriter often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use GedcomWriter, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class GedcomWriter |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * Array of persons ID |
||
| 23 | * key - old GEDCOM ID |
||
| 24 | * value - new autoincrement ID. |
||
| 25 | * |
||
| 26 | * @var string |
||
| 27 | */ |
||
| 28 | protected $persons_id = []; |
||
| 29 | |||
| 30 | public function parse(string $filename, string $slug, bool $progressBar = false) |
||
| 31 | { |
||
| 32 | $parser = new Parser(); |
||
| 33 | $gedcom = @$parser->parse($filename); |
||
| 34 | |||
| 35 | /** |
||
| 36 | * work. |
||
| 37 | */ |
||
| 38 | $subn = $gedcom->getSubn(); |
||
| 39 | $subm = $gedcom->getSubm(); |
||
| 40 | $sour = $gedcom->getSour(); |
||
| 41 | $note = $gedcom->getNote(); |
||
| 42 | $repo = $gedcom->getRepo(); |
||
| 43 | $obje = $gedcom->getObje(); |
||
| 44 | |||
| 45 | /** |
||
| 46 | * work end. |
||
| 47 | */ |
||
| 48 | $c_subn = 0; |
||
| 49 | $c_subm = count($subm); |
||
| 50 | $c_sour = count($sour); |
||
| 51 | $c_note = count($note); |
||
| 52 | $c_repo = count($repo); |
||
| 53 | $c_obje = count($obje); |
||
| 54 | if ($subn != null) { |
||
| 55 | // |
||
| 56 | $c_subn = 1; |
||
| 57 | } |
||
| 58 | |||
| 59 | $individuals = $gedcom->getIndi(); |
||
| 60 | $families = $gedcom->getFam(); |
||
| 61 | $total = count($individuals) + count($families) + $c_subn + $c_subm + $c_sour + $c_note + $c_repo + $c_obje; |
||
| 62 | $complete = 0; |
||
| 63 | if ($progressBar === true) { |
||
| 64 | $bar = $this->getProgressBar(count($individuals) + count($families)); |
||
| 65 | event(new GedComProgressSent($slug, $total, $complete)); |
||
| 66 | } |
||
| 67 | |||
| 68 | if ($subn != null) { |
||
| 69 | // store the submission information for the GEDCOM file. |
||
| 70 | $this->getSubn($subn); |
||
| 71 | if ($progressBar === true) { |
||
| 72 | $bar->advance(); |
||
| 73 | $complete++; |
||
| 74 | event(new GedComProgressSent($slug, $total, $complete)); |
||
| 75 | } |
||
| 76 | } |
||
| 77 | |||
| 78 | // store information about all the submitters to the GEDCOM file. |
||
| 79 | foreach ($subm as $item) { |
||
| 80 | $this->getSubm($item); |
||
| 81 | if ($progressBar === true) { |
||
| 82 | $bar->advance(); |
||
| 83 | $complete++; |
||
| 84 | event(new GedComProgressSent($slug, $total, $complete)); |
||
| 85 | } |
||
| 86 | } |
||
| 87 | |||
| 88 | // store sources cited throughout the GEDCOM file. |
||
| 89 | foreach ($sour as $item) { |
||
| 90 | $this->getSour($item); |
||
| 91 | if ($progressBar === true) { |
||
| 92 | $bar->advance(); |
||
| 93 | $complete++; |
||
| 94 | event(new GedComProgressSent($slug, $total, $complete)); |
||
| 95 | } |
||
| 96 | } |
||
| 97 | |||
| 98 | // store all the notes contained within the GEDCOM file that are not inline. |
||
| 99 | foreach ($note as $item) { |
||
| 100 | $this->getNote($item); |
||
| 101 | if ($progressBar === true) { |
||
| 102 | $bar->advance(); |
||
| 103 | $complete++; |
||
| 104 | event(new GedComProgressSent($slug, $total, $complete)); |
||
| 105 | } |
||
| 106 | } |
||
| 107 | |||
| 108 | // store all repositories that are contained within the GEDCOM file and referenced by sources. |
||
| 109 | foreach ($repo as $item) { |
||
| 110 | $this->getRepo($item); |
||
| 111 | if ($progressBar === true) { |
||
| 112 | $bar->advance(); |
||
| 113 | $complete++; |
||
| 114 | event(new GedComProgressSent($slug, $total, $complete)); |
||
| 115 | } |
||
| 116 | } |
||
| 117 | // store all the media objects that are contained within the GEDCOM file. |
||
| 118 | foreach ($obje as $item) { |
||
| 119 | $this->getObje($item); |
||
| 120 | if ($progressBar === true) { |
||
| 121 | $bar->advance(); |
||
| 122 | $complete++; |
||
| 123 | event(new GedComProgressSent($slug, $total, $complete)); |
||
| 124 | } |
||
| 125 | } |
||
| 126 | |||
| 127 | foreach ($individuals as $individual) { |
||
| 128 | $this->getPerson($individual); |
||
| 129 | if ($progressBar === true) { |
||
| 130 | $bar->advance(); |
||
| 131 | $complete++; |
||
| 132 | event(new GedComProgressSent($slug, $total, $complete)); |
||
| 133 | } |
||
| 134 | } |
||
| 135 | |||
| 136 | foreach ($families as $family) { |
||
| 137 | $this->getFamily($family); |
||
| 138 | if ($progressBar === true) { |
||
| 139 | $bar->advance(); |
||
| 140 | $complete++; |
||
| 141 | event(new GedComProgressSent($slug, $total, $complete)); |
||
| 142 | } |
||
| 143 | } |
||
| 144 | |||
| 145 | if ($progressBar === true) { |
||
| 146 | $bar->finish(); |
||
| 147 | } |
||
| 148 | } |
||
| 149 | |||
| 150 | private function getProgressBar(int $max) |
||
| 151 | { |
||
| 152 | return (new OutputStyle( |
||
| 153 | new StringInput(''), |
||
| 154 | new StreamOutput(fopen('php://stdout', 'w')) |
||
| 155 | ))->createProgressBar($max); |
||
| 156 | } |
||
| 157 | |||
| 158 | private function getDate($input_date) |
||
| 161 | } |
||
| 162 | |||
| 163 | private function getPlace($place) |
||
| 164 | { |
||
| 165 | if (is_object($place)) { |
||
| 166 | $place = $place->getPlac(); |
||
| 167 | } |
||
| 168 | |||
| 169 | return $place; |
||
| 170 | } |
||
| 171 | |||
| 172 | private function getPerson($individual) |
||
| 173 | { |
||
| 174 | $g_id = $individual->getId(); |
||
| 175 | $name = ''; |
||
| 176 | $givn = ''; |
||
| 177 | $surn = ''; |
||
| 178 | |||
| 179 | if (!empty($individual->getName())) { |
||
| 180 | $surn = current($individual->getName())->getSurn(); |
||
| 181 | $givn = current($individual->getName())->getGivn(); |
||
| 182 | $name = current($individual->getName())->getName(); |
||
| 183 | } |
||
| 184 | |||
| 185 | // string value |
||
| 186 | $uid = $individual->getUid(); |
||
| 187 | $chan = $individual->getChan(); |
||
| 188 | $rin = $individual->getRin(); |
||
| 189 | $resn = $individual->getResn(); |
||
| 190 | $rfn = $individual->getRfn(); |
||
| 191 | $afn = $individual->getAfn(); |
||
| 192 | |||
| 193 | $sex = preg_replace('/[^MF]/', '', $individual->getSex()); |
||
| 194 | $attr = $individual->getAllAttr(); |
||
| 195 | $events = $individual->getAllEven(); |
||
| 196 | |||
| 197 | if ($givn == '') { |
||
| 198 | $givn = $name; |
||
| 199 | } |
||
| 200 | $person = Person::query()->updateOrCreate(compact('name', 'givn', 'surn', 'sex'), compact('name', 'givn', 'surn', 'sex', 'uid', 'chan', 'rin', 'resn', 'rfn', 'afn')); |
||
| 201 | $this->persons_id[$g_id] = $person->id; |
||
| 202 | |||
| 203 | if ($events !== null) { |
||
| 204 | foreach ($events as $event) { |
||
| 205 | $date = $this->getDate($event->getDate()); |
||
| 206 | $place = $this->getPlace($event->getPlac()); |
||
| 207 | $person->addEvent($event->getType(), $date, $place); |
||
| 208 | } |
||
| 209 | } |
||
| 210 | |||
| 211 | if ($attr !== null) { |
||
| 212 | foreach ($attr as $event) { |
||
| 213 | $date = $this->getDate($event->getDate()); |
||
| 214 | $place = $this->getPlace($event->getPlac()); |
||
| 215 | if (count($event->getNote()) > 0) { |
||
| 216 | $note = current($event->getNote())->getNote(); |
||
| 217 | } else { |
||
| 218 | $note = ''; |
||
| 219 | } |
||
| 220 | $person->addEvent($event->getType(), $date, $place, $event->getAttr().' '.$note); |
||
| 221 | } |
||
| 222 | } |
||
| 223 | } |
||
| 224 | |||
| 225 | private function getFamily($family) |
||
| 226 | { |
||
| 227 | $husb = $family->getHusb(); |
||
| 228 | $wife = $family->getWife(); |
||
| 229 | |||
| 230 | // string |
||
| 231 | $chan = $family->getChan(); |
||
| 232 | $nchi = $family->getNchi(); |
||
| 233 | |||
| 234 | $description = null; |
||
| 235 | $type_id = 0; |
||
| 236 | $children = $family->getChil(); |
||
| 237 | $events = $family->getAllEven(); |
||
| 238 | |||
| 239 | $husband_id = (isset($this->persons_id[$husb])) ? $this->persons_id[$husb] : 0; |
||
| 240 | $wife_id = (isset($this->persons_id[$wife])) ? $this->persons_id[$wife] : 0; |
||
| 241 | |||
| 242 | $family = Family::query()->updateOrCreate(compact('husband_id', 'wife_id'), compact('husband_id', 'wife_id', 'description', 'type_id', 'chan', 'nchi')); |
||
| 243 | |||
| 244 | if ($children !== null) { |
||
| 245 | foreach ($children as $child) { |
||
| 246 | if (isset($this->persons_id[$child])) { |
||
| 247 | $person = Person::query()->find($this->persons_id[$child]); |
||
| 248 | $person->child_in_family_id = $family->id; |
||
| 249 | $person->save(); |
||
| 250 | } |
||
| 251 | } |
||
| 252 | } |
||
| 253 | |||
| 254 | if ($events !== null) { |
||
| 255 | foreach ($events as $event) { |
||
| 256 | $date = $this->getDate($event->getDate()); |
||
| 257 | $place = $this->getPlace($event->getPlac()); |
||
| 258 | $family->addEvent($event->getType(), $date, $place); |
||
| 259 | } |
||
| 260 | } |
||
| 261 | } |
||
| 262 | |||
| 263 | private function getSubn($subn) |
||
| 264 | { |
||
| 265 | $subm = $subn->getSubm(); |
||
| 266 | $famf = $subn->getFamf(); |
||
| 267 | $temp = $subn->getTemp(); |
||
| 268 | $ance = $subn->getAnce(); |
||
| 269 | $desc = $subn->getDesc(); |
||
| 270 | $ordi = $subn->getOrdi(); |
||
| 271 | $rin = $subn->getRin(); |
||
| 272 | Subn::query()->updateOrCreate(compact('subm', 'famf', 'temp', 'ance', 'desc', 'ordi', 'rin'), compact('subm', 'famf', 'temp', 'ance', 'desc', 'ordi', 'rin')); |
||
| 273 | } |
||
| 274 | |||
| 275 | // insert subm data to model |
||
| 276 | private function getSubm($_subm) |
||
| 334 | } |
||
| 335 | |||
| 336 | // insert sour data to database |
||
| 337 | private function getSour($_sour) |
||
| 338 | { |
||
| 339 | $sour = $_sour->getSour(); // string |
||
| 340 | $titl = $_sour->getTitl(); // string |
||
| 341 | $auth = $_sour->getAuth(); // string |
||
| 342 | $data = $_sour->getData(); // string |
||
| 343 | $text = $_sour->getText(); // string |
||
| 344 | $publ = $_sour->getPubl(); // string |
||
| 345 | $abbr = $_sour->getAbbr(); // string |
||
| 346 | Source::query()->updateOrCreate(compact('sour', 'titl', 'auth', 'data', 'text', 'publ', 'abbr'), compact('sour', 'titl', 'auth', 'data', 'text', 'publ', 'abbr')); |
||
| 347 | } |
||
| 348 | |||
| 349 | // insert note data to database |
||
| 350 | private function getNote($_note) |
||
| 356 | } |
||
| 357 | |||
| 358 | // insert repo data to database |
||
| 359 | private function getRepo($_repo) |
||
| 360 | { |
||
| 382 | } |
||
| 383 | |||
| 384 | // insert obje data to database |
||
| 385 | private function getObje($_obje) |
||
| 395 | } |
||
| 396 | } |
||
| 397 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths