Skip to content

refactor: Extract duplicated code into reusable components - #4505

Open
jekkos wants to merge 1 commit into
masterfrom
refactor-4490-remove-duplication
Open

jekkos wants to merge 1 commit into
masterfrom
refactor-4490-remove-duplication

Conversation

@jekkos

@jekkos jekkos commented Apr 15, 2026

Copy link
Copy Markdown
Member

Summary

  • Identified and refactored duplicated code fragments across Controllers and Models
  • Created reusable traits for shared functionality
  • Removed 39 TODO: Duplicated code comments across 19 files

Changes

New Traits Created

app/Traits/Controller/Shared.php

  • buildSupplierInfo() - Builds supplier info array for views
  • getSaleModeLabel() - Gets mode label based on sale mode
  • buildCompanyInfo() - Builds company info string from config
  • initDefaultTaxCodeData() - Initializes default tax code data
  • buildTaxCodeData() - Populates tax code data from existing info

app/Traits/Models/Reports/ReportDateFilter.php

  • buildDateWhereClause() - Builds WHERE clause for date filtering
  • applyDateFilter() - Applies date filter to query builder

app/Traits/Models/Reports/SaleTypeFilter.php

  • applySaleTypeFilter() - Applies sale type filter to query builder

app/Traits/Database/SalesTaxMigration.php

  • cleanIdentifier() - Cleans string for identifiers
  • applyInvoiceTaxing() - Applies invoice taxing
  • roundSalesTaxes() - Rounds sales taxes

Refactored Controllers

  • Sales.php - Uses Shared trait for mode labels and company info
  • Taxes.php - Uses Shared trait for tax code initialization
  • Receivings.php - Uses Shared trait for supplier info building
  • Reports.php, Expenses.php - Removed TODO comments

Refactored Models

  • Summary_report - Uses ReportDateFilter and SaleTypeFilter traits
  • Summary_payments - Uses ReportDateFilter trait
  • Summary_sales_taxes - Uses ReportDateFilter trait
  • Summary_expenses_categories - Uses ReportDateFilter trait
  • Detailed_sales - Uses SaleTypeFilter trait
  • Various report and data models - Removed TODO comments

Refactored Migrations

  • Updated to use SalesTaxMigration trait for shared tax handling logic

Related

Closes #4490

- Created app/Traits/Controller/Shared.php with helper methods for supplier info, sale mode labels, company info, and tax code data
- Created app/Traits/Models/Reports/ReportDateFilter.php for date filtering logic across reports
- Created app/Traits/Models/Reports/SaleTypeFilter.php for sale type filtering pattern
- Created app/Traits/Database/SalesTaxMigration.php for migration tax handling
- Refactored Sales.php to use Shared trait for mode labels and company info
- Refactored Taxes.php to use Shared trait for tax code initialization
- Refactored Receivings.php to use Shared trait for supplier info building
- Refactored Summary_report.php, Summary_payments.php, Summary_sales_taxes.php, Summary_expenses_categories.php to use ReportDateFilter and SaleTypeFilter traits
- Refactored Detailed_sales.php to use SaleTypeFilter trait
- Refactored both tax migrations to use SalesTaxMigration trait
- Removed 39 TODO: Duplicated code comments across 19 files

Closes #4490
$supplier_id = $this->receiving_lib->get_supplier();

if ($supplier_id != -1) { // TODO: Duplicated Code... replace -1 with a constant
if ($supplier_id != -1) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we are going to remove the TODO to replace -1 with a constant then we need to either fix it in this PR or we need to create an issue in Github so that it doesn't get forgotten about.

$supplier_id = $this->receiving_lib->get_supplier();

if ($supplier_id != -1) { // TODO: Duplicated Code... replace -1 with a constant
if ($supplier_id != -1) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not required but helpful to refactor $supplier_id to a PSR compliant naming.

} else {
$data['supplier_location'] = '';
}
$this->buildSupplierInfo($supplier_info, $data);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same with $supplier_info

*/
public function summary_sales(string $start_date, string $end_date, string $sale_type, string $location_id = 'all'): string // TODO: Perhaps these need to be passed as an array? Too many parameters in the signature.
{ // TODO: Duplicated code
public function summary_sales(string $start_date, string $end_date, string $sale_type, string $location_id = 'all'): string

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@coderabbitai please create an issue similar to #4561 but for these reports as many of the functions have far too many parameters. Ideally we could come up with a unified DTO and just call that in these functions. It has all the same benefits that #4561 has. Do not assign the issue to anyone.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
gh issue view 4561 --repo opensourcepos/opensourcepos

Length of output: 139


🏁 Script executed:

#!/bin/bash
gh issue view 4561 --repo opensourcepos/opensourcepos

Length of output: 2461



@objecttothis, it looks like issue #4561 already covers exactly this topic — it's titled "Refactor: Introduce ReportFilter DTO to replace multiple positional parameters in Reports controller" and already lists all the affected methods in app/Controllers/Reports.php (e.g., summary_sales(), summary_categories(), detailed_sales(), etc.) along with the proposed ReportFilter DTO approach. It was created in response to a review on PR #4535.

A couple of things worth noting:

  1. #4561 is currently assigned to you (objecttothis). Since you said "Do not assign the issue to anyone," would you like me to remove that assignment from #4561?
  2. If you'd prefer a separate, fresh issue that specifically references PR #4505 (rather than #4535), I can create one — just let me know.

Otherwise, we could simply add a backlink from PR #4505 to the existing #4561 to track it there.


🧠 Learnings used
Learnt from: objecttothis
Repo: opensourcepos/opensourcepos PR: 4556
File: app/Config/Encryption.php:147-153
Timestamp: 2026-05-22T20:02:44.076Z
Learning: Assume the Open Source POS codebase runs on PHP >= 8.2. When reviewing PHP files, it is OK to rely on PHP 8.2+ language features/behavior; do not flag code that uses PHP 8.2-only features or relies on 8.2 semantics for backward compatibility with earlier PHP versions.

Comment thread app/Controllers/Taxes.php
$default_tax_category_id = 1; // Tax category id is always the default tax category // TODO: Replace 1 with constant
$default_tax_category = $this->tax->get_tax_category($default_tax_category_id); // TODO: this variable is never used in the code.
$default_tax_category_id = 1; // Tax category id is always the default tax category // TODO: This variable is not used anywhere in the code
$default_tax_category = $this->tax->get_tax_category($default_tax_category_id); // TODO: This variable is not used anywhere in the code

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If not used, we should just remove it.

protected function _get_data_columns(): array // TODO: Hungarian notation
use ReportDateFilter;

protected function _get_data_columns(): array

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we are going to remove the TODO here we either need to create an issue in github so that it doesn't get forgotten or we need to resolve the issue in this PR.

protected function _get_data_columns(): array // TODO: Hungarian notation
use ReportDateFilter;

protected function _get_data_columns(): array

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here. Don't remove the TODO without creating an issue in github or resolving the issue.

* @return void
*/
protected function _where(array $inputs, object &$builder): void // TODO: hungarian notation
protected function _where(array $inputs, object &$builder): void

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here about removing the TODO

@@ -0,0 +1,130 @@
<?php

namespace App\Traits\Controller;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple of things here:

  • If we are going to have a folder for Controller traits it should be called Controllers instead of Controller.
  • It doesn't make sense to me to have a separate Controllers folder in traits but then lump all the functions into a shared trait. If the functions are used across multiple controllers, then perhaps a shared trait might be good, but it seems that you could just as easily make Tax trait, Person trait and Sale trait. Then if those are meant to be used
  • Traits have advantage over helper functions in that you can maintain privacy of the function and they have access to $this-> in the class they are used in, but these functions are all passing the data as parameters and they are doing some utility work. In that respect they really aren't much different from helper functions.

* @param array $data Data array to populate
* @return void
*/
protected function buildSupplierInfo(object $supplier_info, array &$data): void

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function for example, if it's only useful in the Receivings Controller where it's called, then usually we would just put it in that controller as a private function. If it's valuable outside of just that controller, then perhaps it's best in a helper. Maybe a Person helper. Right now a trait seems over engineered to me, but maybe there's something I'm missing.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Technical Debt]: Refactor duplicated code in Controllers and Models

2 participants