如何重构/简化以下PHP功能?
我的功能可以返回发票上有多少个产品,对我来说似乎很简单,但是也许可以使用一些重构?
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于这种事情,理想情况下,您会使用ORM。由于我不确定您正在使用哪个,如果有的话,我可能会在这个方向上重新分配一些东西,以便更接近一个ORM的工作方式:
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:
我个人认为这非常简单明了,但是我建议的唯一优化是提取
getConditionsandOptions
部分,因为它似乎在代码的其他几个部分中使用。请看一下:另一点也许是命名约定。我可以看到您已经使用了
Snail_case
dbm_supplier的命名。最好是dbm_supplier
或dbmsupplier
。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:Another point maybe name conventions. I can see you have used
snail_case
naming for Dbm_Supplier. It's better to be eitherdbm_supplier
orDbmSupplier
.