如何重构/简化以下PHP功能?

发布于 2025-02-05 20:09:28 字数 716 浏览 1 评论 0原文

我的功能可以返回发票上有多少个产品,对我来说似乎很简单,但是也许可以使用一些重构?

    public function getTotalProductsNumber(): int
{
    $dataset = 'supplier_invoice_products inner join supplier_invoices as si using (supplier_invoice_id)';
    $dbmSupplier = new Dbm_Supplier($dataset);

    $whereAndOpt = $this->getConditionsAndOptions();
    $where = $whereAndOpt['where'];
    $opt = $whereAndOpt['opt'];
    $select = 'sum(product_quantity) as sumTotal';
    $invoiceTotalProductsNumber =  $dbmSupplier->findFirstSimple($where, $select, $opt);
    $invoiceTotalProductsNumber['sumTotal'] = (int)$invoiceTotalProductsNumber['sumTotal'];
    return $invoiceTotalProductsNumber['sumTotal'];
}

如何将其提取到至少两个功能中?

I have a function that will return how many products there are on an invoice and it seems pretty straightforward to me but maybe it can use some refactoring?

    public function getTotalProductsNumber(): int
{
    $dataset = 'supplier_invoice_products inner join supplier_invoices as si using (supplier_invoice_id)';
    $dbmSupplier = new Dbm_Supplier($dataset);

    $whereAndOpt = $this->getConditionsAndOptions();
    $where = $whereAndOpt['where'];
    $opt = $whereAndOpt['opt'];
    $select = 'sum(product_quantity) as sumTotal';
    $invoiceTotalProductsNumber =  $dbmSupplier->findFirstSimple($where, $select, $opt);
    $invoiceTotalProductsNumber['sumTotal'] = (int)$invoiceTotalProductsNumber['sumTotal'];
    return $invoiceTotalProductsNumber['sumTotal'];
}

How can I extract this into at least two functions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

森末i 2025-02-12 20:09:28

对于这种事情,理想情况下,您会使用ORM。由于我不确定您正在使用哪个,如果有的话,我可能会在这个方向上重新分配一些东西,以便更接近一个ORM的工作方式:

public function getTotalProductsNumber(): int
{
    $dataset = 'supplier_invoice_products inner join supplier_invoices as si using (supplier_invoice_id)';
    $extras = $this->getConditionsAndOptions();
    $queryBuilder = [
        'dbm' => new Dbm_Supplier($dataset),
        'where' =>  $extras['where'],
        'opt' => $extras['opt']
    ];
    return $this->sumOfProductQuantities($queryBuilder);
}

private function sumOfProductQuantities(array $queryBuilder): int
{
    $select = 'sum(product_quantity) as sumTotal';
    $row = $this->queryBuilderFirstRow($queryBuilder, $select);
    return (int)$row['sumTotal'];
}

private function queryBuilderFirstRow(array $qb, string $select): array
{
    return $qb['dbm']->findFirstSimple($qb['where'], $select, $qb['opt']);
}

For this kind of thing, ideally you'd use an ORM. As I'm not sure which you're using, if at all, I'd probably refactor it to something more in this direction so it feels closer to how an ORM would work:

public function getTotalProductsNumber(): int
{
    $dataset = 'supplier_invoice_products inner join supplier_invoices as si using (supplier_invoice_id)';
    $extras = $this->getConditionsAndOptions();
    $queryBuilder = [
        'dbm' => new Dbm_Supplier($dataset),
        'where' =>  $extras['where'],
        'opt' => $extras['opt']
    ];
    return $this->sumOfProductQuantities($queryBuilder);
}

private function sumOfProductQuantities(array $queryBuilder): int
{
    $select = 'sum(product_quantity) as sumTotal';
    $row = $this->queryBuilderFirstRow($queryBuilder, $select);
    return (int)$row['sumTotal'];
}

private function queryBuilderFirstRow(array $qb, string $select): array
{
    return $qb['dbm']->findFirstSimple($qb['where'], $select, $qb['opt']);
}

凉城凉梦凉人心 2025-02-12 20:09:28

我个人认为这非常简单明了,但是我建议的唯一优化是提取getConditionsandOptions部分,因为它似乎在代码的其他几个部分中使用。请看一下:

public function getTotalProductsNumber(): int
{
    $dataset = 'supplier_invoice_products inner join supplier_invoices as si using (supplier_invoice_id)';
    $dbmSupplier = new Dbm_Supplier($dataset);

    list($where, $opt) = $this->getWhereAndOpt();

    $select = 'sum(product_quantity) as sumTotal';
    $invoiceTotalProductsNumber = $dbmSupplier->findFirstSimple($where, $select, $opt);
    $invoiceTotalProductsNumber['sumTotal'] = (int)$invoiceTotalProductsNumber['sumTotal'];
    return $invoiceTotalProductsNumber['sumTotal'];
}

private function getWhereAndOpt(): array
{
    $whereAndOpt = $this->getConditionsAndOptions();
    return [
        $whereAndOpt['where'],
        $whereAndOpt['opt'],
    ];
}

另一点也许是命名约定。我可以看到您已经使用了Snail_case dbm_supplier的命名。最好是dbm_supplierdbmsupplier

I personally think this is pretty straightforward and nicely clean, but the only optimization I can suggest is to extract getConditionsAndOptions part as it seems to be used in some other several parts of your code. Look at this please:

public function getTotalProductsNumber(): int
{
    $dataset = 'supplier_invoice_products inner join supplier_invoices as si using (supplier_invoice_id)';
    $dbmSupplier = new Dbm_Supplier($dataset);

    list($where, $opt) = $this->getWhereAndOpt();

    $select = 'sum(product_quantity) as sumTotal';
    $invoiceTotalProductsNumber = $dbmSupplier->findFirstSimple($where, $select, $opt);
    $invoiceTotalProductsNumber['sumTotal'] = (int)$invoiceTotalProductsNumber['sumTotal'];
    return $invoiceTotalProductsNumber['sumTotal'];
}

private function getWhereAndOpt(): array
{
    $whereAndOpt = $this->getConditionsAndOptions();
    return [
        $whereAndOpt['where'],
        $whereAndOpt['opt'],
    ];
}

Another point maybe name conventions. I can see you have used snail_case naming for Dbm_Supplier. It's better to be either dbm_supplier or DbmSupplier.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文