| Conditions | 21 |
| Paths | 64 |
| Total Lines | 93 |
| Code Lines | 55 |
| 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 |
||
| 71 | public function handle(ServerRequestInterface $request): ResponseInterface |
||
| 72 | { |
||
| 73 | $tree = $request->getAttribute('tree'); |
||
| 74 | assert($tree instanceof Tree); |
||
| 75 | |||
| 76 | $xref = $request->getAttribute('xref'); |
||
| 77 | assert(is_string($xref)); |
||
| 78 | |||
| 79 | $params = (array) $request->getParsedBody(); |
||
| 80 | |||
| 81 | $fact_id = $params['fact_id'] ?? ''; |
||
| 82 | |||
| 83 | $record = Factory::gedcomRecord()->make($xref, $tree); |
||
| 84 | $record = Auth::checkRecordAccess($record, true); |
||
| 85 | |||
| 86 | $keep_chan = (bool) ($params['keep_chan'] ?? false); |
||
| 87 | |||
| 88 | $this->gedcom_edit_service->glevels = $params['glevels']; |
||
| 89 | $this->gedcom_edit_service->tag = $params['tag']; |
||
| 90 | $this->gedcom_edit_service->text = $params['text']; |
||
| 91 | $this->gedcom_edit_service->islink = $params['islink']; |
||
| 92 | |||
| 93 | // If the fact has a DATE or PLAC, then delete any value of Y |
||
| 94 | if ($this->gedcom_edit_service->text[0] === 'Y') { |
||
| 95 | foreach ($this->gedcom_edit_service->tag as $n => $value) { |
||
| 96 | if ($this->gedcom_edit_service->glevels[$n] == 2 && ($value === 'DATE' || $value === 'PLAC') && $this->gedcom_edit_service->text[$n] !== '') { |
||
| 97 | $this->gedcom_edit_service->text[0] = ''; |
||
| 98 | break; |
||
| 99 | } |
||
| 100 | } |
||
| 101 | } |
||
| 102 | |||
| 103 | $newged = ''; |
||
| 104 | |||
| 105 | $NAME = $params['NAME'] ?? ''; |
||
| 106 | |||
| 107 | if ($NAME !== '') { |
||
| 108 | $newged .= "\n1 NAME " . $NAME; |
||
| 109 | $name_facts = [ |
||
| 110 | 'TYPE', |
||
| 111 | 'NPFX', |
||
| 112 | 'GIVN', |
||
| 113 | 'NICK', |
||
| 114 | 'SPFX', |
||
| 115 | 'SURN', |
||
| 116 | 'NSFX', |
||
| 117 | ]; |
||
| 118 | foreach ($name_facts as $name_fact) { |
||
| 119 | $NAME_FACT = $params[$name_fact] ?? ''; |
||
| 120 | if ($NAME_FACT !== '') { |
||
| 121 | $newged .= "\n2 " . $name_fact . ' ' . $NAME_FACT; |
||
| 122 | } |
||
| 123 | } |
||
| 124 | } |
||
| 125 | |||
| 126 | $newged = $this->gedcom_edit_service->handleUpdates($newged); |
||
| 127 | |||
| 128 | // Add new names after existing names |
||
| 129 | if ($NAME !== '') { |
||
| 130 | preg_match_all('/[_0-9A-Z]+/', $tree->getPreference('ADVANCED_NAME_FACTS'), $match); |
||
| 131 | $name_facts = array_unique(array_merge(['_MARNM'], $match[0])); |
||
| 132 | foreach ($name_facts as $name_fact) { |
||
| 133 | $NAME_FACT = $params[$name_fact] ?? ''; |
||
| 134 | // Ignore advanced facts that duplicate standard facts. |
||
| 135 | if ($NAME_FACT !== '' && !in_array($name_fact, ['TYPE', 'NPFX', 'GIVN', 'NICK', 'SPFX', 'SURN', 'NSFX'], true)) { |
||
| 136 | $newged .= "\n2 " . $name_fact . ' ' . $NAME_FACT; |
||
| 137 | } |
||
| 138 | } |
||
| 139 | } |
||
| 140 | |||
| 141 | $newged = trim($newged); // Remove leading newline |
||
| 142 | |||
| 143 | $census_assistant = $this->module_service->findByInterface(CensusAssistantModule::class)->first(); |
||
| 144 | if ($census_assistant instanceof CensusAssistantModule && $record instanceof Individual) { |
||
| 145 | $newged = $census_assistant->updateCensusAssistant($request, $record, $fact_id, $newged, $keep_chan); |
||
| 146 | } |
||
| 147 | |||
| 148 | $record->updateFact($fact_id, $newged, !$keep_chan); |
||
| 149 | |||
| 150 | // For the GEDFact_assistant module |
||
| 151 | $pid_array = $params['pid_array'] ?? ''; |
||
| 152 | if ($pid_array !== '') { |
||
| 153 | foreach (explode(',', $pid_array) as $pid) { |
||
| 154 | if ($pid !== $xref) { |
||
| 155 | $indi = Factory::individual()->make($pid, $tree); |
||
| 156 | if ($indi && $indi->canEdit()) { |
||
| 157 | $indi->updateFact($fact_id, $newged, !$keep_chan); |
||
| 158 | } |
||
| 159 | } |
||
| 160 | } |
||
| 161 | } |
||
| 162 | |||
| 163 | return redirect($record->url()); |
||
| 164 | } |
||
| 166 |