Total Complexity | 46 |
Total Lines | 210 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like LineageBuilder 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 LineageBuilder, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
29 | class LineageBuilder |
||
30 | { |
||
31 | private string $surname; |
||
32 | private Tree $tree; |
||
33 | private PatronymicLineageModule $patrolineage_module; |
||
34 | |||
35 | /** |
||
36 | * @var Collection<string, bool> $used_indis Individuals already processed |
||
37 | */ |
||
38 | private Collection $used_indis; |
||
39 | |||
40 | /** |
||
41 | * Constructor for Lineage Builder |
||
42 | * |
||
43 | * @param string $surname Reference surname |
||
44 | * @param Tree $tree Gedcom tree |
||
45 | */ |
||
46 | public function __construct(string $surname, Tree $tree, PatronymicLineageModule $patrolineage_module) |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * Build all patronymic lineages for the reference surname. |
||
56 | * |
||
57 | * @return Collection<LineageRootNode>|NULL List of root patronymic lineages |
||
58 | */ |
||
59 | public function buildLineages(): ?Collection |
||
60 | { |
||
61 | $indis = $this->patrolineage_module->individuals( |
||
62 | $this->tree, |
||
63 | $this->surname, |
||
64 | '', |
||
65 | '', |
||
66 | false, |
||
67 | false, |
||
68 | I18N::locale() |
||
69 | ); |
||
70 | //Warning - the individuals method returns a clone of individuals objects. Cannot be used for object equality |
||
71 | if (count($indis) === 0) { |
||
72 | return null; |
||
73 | } |
||
74 | |||
75 | $root_lineages = new Collection(); |
||
76 | |||
77 | foreach ($indis as $indi) { |
||
78 | /** @var Individual $indi */ |
||
79 | if ($this->used_indis->get($indi->xref(), false) === false) { |
||
80 | $indi_first = $this->getLineageRootIndividual($indi); |
||
81 | if ($indi_first !== null) { |
||
82 | // The root lineage needs to be recreated from the Factory, to retrieve the proper object |
||
83 | $indi_first = Registry::individualFactory()->make($indi_first->xref(), $this->tree); |
||
84 | } |
||
85 | if ($indi_first === null) { |
||
86 | continue; |
||
87 | } |
||
88 | $this->used_indis->put($indi_first->xref(), true); |
||
89 | if ($indi_first->canShow()) { |
||
90 | //Check if the root individual has brothers and sisters, without parents |
||
91 | $indi_first_child_family = $indi_first->childFamilies()->first(); |
||
92 | if ($indi_first_child_family !== null) { |
||
93 | $root_node = new LineageRootNode(null); |
||
94 | $root_node->addFamily($indi_first_child_family); |
||
95 | } else { |
||
96 | $root_node = new LineageRootNode($indi_first); |
||
97 | } |
||
98 | $root_node = $this->buildLineage($root_node); |
||
99 | $root_lineages->add($root_node); |
||
100 | } |
||
101 | } |
||
102 | } |
||
103 | |||
104 | return $root_lineages->sort(function (LineageRootNode $a, LineageRootNode $b) { |
||
105 | if ($a->numberChildNodes() === $b->numberChildNodes()) { |
||
106 | return 0; |
||
107 | } |
||
108 | return ($a->numberChildNodes() > $b->numberChildNodes()) ? -1 : 1; |
||
109 | })->values(); |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * Retrieve the root individual, from any individual, by recursion. |
||
114 | * The Root individual is the individual without a father, or without a mother holding the same name. |
||
115 | * |
||
116 | * @param Individual $indi |
||
117 | * @return Individual|NULL Root individual |
||
118 | */ |
||
119 | private function getLineageRootIndividual(Individual $indi): ?Individual |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * Computes descendent Lineage from a node. |
||
154 | * Uses recursion to build the lineage tree |
||
155 | * |
||
156 | * @param LineageNode $node |
||
157 | * @return LineageNode Computed lineage |
||
158 | */ |
||
159 | private function buildLineage(LineageNode $node): LineageNode |
||
241 |