refactor: extract duplicate sendJson() and browseSnapshot logic from controllers #173

Open
opened 2026-06-29 14:21:12 +00:00 by jmiller · 2 comments
Owner

Summary

Two pieces of code are duplicated across AjaxController and SnapshotsController:

  1. sendJson() method — identical private method in both controllers (AjaxController:1388 and SnapshotsController:242)
  2. Browse snapshot logicAjaxController::browseSnapshot() and SnapshotsController::browse() perform nearly identical AJAX snapshot browsing with the same query, data formatting, and JSON response pattern

Locations

  • src/Controller/AjaxController.php:1388private function sendJson()
  • src/Controller/SnapshotsController.php:242private function sendJson()
  • src/Controller/AjaxController.phpbrowseSnapshot() method
  • src/Controller/SnapshotsController.phpbrowse() method

What to do

  • Extract sendJson() into a shared trait (e.g., JsonResponseTrait) used by both controllers
  • Consolidate browse snapshot logic — either delegate from one controller to the other, or extract to a shared model method
  • Remove the duplicate implementations

Why

Duplicate code means bug fixes must be applied in two places. The sendJson() duplication is pure copy-paste; the browse logic duplication means snapshot browsing could diverge between the two entry points.

## Summary Two pieces of code are duplicated across `AjaxController` and `SnapshotsController`: 1. **`sendJson()` method** — identical private method in both controllers (AjaxController:1388 and SnapshotsController:242) 2. **Browse snapshot logic** — `AjaxController::browseSnapshot()` and `SnapshotsController::browse()` perform nearly identical AJAX snapshot browsing with the same query, data formatting, and JSON response pattern ## Locations - `src/Controller/AjaxController.php:1388` — `private function sendJson()` - `src/Controller/SnapshotsController.php:242` — `private function sendJson()` - `src/Controller/AjaxController.php` — `browseSnapshot()` method - `src/Controller/SnapshotsController.php` — `browse()` method ## What to do - [ ] Extract `sendJson()` into a shared trait (e.g., `JsonResponseTrait`) used by both controllers - [ ] Consolidate browse snapshot logic — either delegate from one controller to the other, or extract to a shared model method - [ ] Remove the duplicate implementations ## Why Duplicate code means bug fixes must be applied in two places. The `sendJson()` duplication is pure copy-paste; the browse logic duplication means snapshot browsing could diverge between the two entry points.
jmiller added the component: admin label 2026-06-29 14:21:12 +00:00
Author
Owner

Branch created: feature/173-refactor-extract-duplicate-sendjson-and-

git fetch origin
git checkout feature/173-refactor-extract-duplicate-sendjson-and-
Branch created: [`feature/173-refactor-extract-duplicate-sendjson-and-`](https://git.mokoconsulting.tech/MokoConsulting/MokoSuiteBackup/src/branch/feature/173-refactor-extract-duplicate-sendjson-and-) ```bash git fetch origin git checkout feature/173-refactor-extract-duplicate-sendjson-and- ```
Author
Owner

Confirmed @ 02.56.05 — keep open.

sendJson() is still duplicated verbatim in AjaxController.php:1271 and SnapshotsController.php:242 (no shared trait). Browse logic also still in both: AjaxController::browseSnapshot() (:655) and SnapshotsController::browse() (:113). Line ref updated: sendJson() is now at AjaxController.php:1271 (was 1388 in the title — line drift only).

Confirmed @ 02.56.05 — keep open. `sendJson()` is still duplicated verbatim in `AjaxController.php:1271` and `SnapshotsController.php:242` (no shared trait). Browse logic also still in both: `AjaxController::browseSnapshot()` (`:655`) and `SnapshotsController::browse()` (`:113`). Line ref updated: `sendJson()` is now at `AjaxController.php:1271` (was 1388 in the title — line drift only).
Sign in to join this conversation.