| Conditions | 6 |
| Paths | 1 |
| Total Lines | 52 |
| Code Lines | 35 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 82 | public function handle(ServerRequestInterface $request): ResponseInterface |
||
| 83 | { |
||
| 84 | $tree = $request->getAttribute('tree'); |
||
| 85 | assert($tree instanceof Tree, new InvalidArgumentException()); |
||
| 86 | |||
| 87 | $query = $this->pending_changes_service->changesQuery($request); |
||
| 88 | |||
| 89 | $callback = function (stdClass $row) use ($tree): array { |
||
| 90 | $old_lines = explode("\n", $row->old_gedcom); |
||
| 91 | $new_lines = explode("\n", $row->new_gedcom); |
||
| 92 | |||
| 93 | $differences = $this->myers_diff->calculate($old_lines, $new_lines); |
||
| 94 | $diff_lines = []; |
||
| 95 | |||
| 96 | foreach ($differences as $difference) { |
||
| 97 | switch ($difference[1]) { |
||
| 98 | case MyersDiff::DELETE: |
||
| 99 | $diff_lines[] = '<del>' . $difference[0] . '</del>'; |
||
| 100 | break; |
||
| 101 | case MyersDiff::INSERT: |
||
| 102 | $diff_lines[] = '<ins>' . $difference[0] . '</ins>'; |
||
| 103 | break; |
||
| 104 | default: |
||
| 105 | $diff_lines[] = $difference[0]; |
||
| 106 | } |
||
| 107 | } |
||
| 108 | |||
| 109 | // Only convert valid xrefs to links |
||
| 110 | $record = GedcomRecord::getInstance($row->xref, $tree); |
||
| 111 | |||
| 112 | return [ |
||
| 113 | $row->change_id, |
||
| 114 | Carbon::make($row->change_time)->local()->format('Y-m-d H:i:s'), |
||
| 115 | I18N::translate($row->status), |
||
| 116 | $record ? '<a href="' . e($record->url()) . '">' . $record->xref() . '</a>' : $row->xref, |
||
| 117 | '<div class="gedcom-data" dir="ltr">' . |
||
| 118 | preg_replace_callback( |
||
| 119 | '/@(' . Gedcom::REGEX_XREF . ')@/', |
||
| 120 | static function (array $match) use ($tree): string { |
||
| 121 | $record = GedcomRecord::getInstance($match[1], $tree); |
||
| 122 | |||
| 123 | return $record ? '<a href="' . e($record->url()) . '">' . $match[0] . '</a>' : $match[0]; |
||
| 124 | }, |
||
| 125 | implode("\n", $diff_lines) |
||
| 126 | ) . |
||
| 127 | '</div>', |
||
| 128 | $row->user_name, |
||
| 129 | $row->gedcom_name, |
||
| 130 | ]; |
||
| 131 | }; |
||
| 132 | |||
| 133 | return $this->datatables_service->handle($request, $query, [], [], $callback); |
||
| 134 | } |
||
| 136 |