From fb3b97720e378d26300791fe33cfcb5b9fd70601 Mon Sep 17 00:00:00 2001 From: Fabian Schmengler Date: Mon, 19 Jun 2017 23:51:00 +0200 Subject: [PATCH 1/3] Add specific type hints and docblocks for Quote\Address\Total --- app/code/Magento/Quote/Model/Quote.php | 2 +- app/code/Magento/Quote/Model/Quote/Address/Total.php | 3 +++ app/code/Magento/Quote/Model/Quote/TotalsReader.php | 10 +++++----- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/app/code/Magento/Quote/Model/Quote.php b/app/code/Magento/Quote/Model/Quote.php index 7481fc76a6478..7aa3cb13df74a 100644 --- a/app/code/Magento/Quote/Model/Quote.php +++ b/app/code/Magento/Quote/Model/Quote.php @@ -1944,7 +1944,7 @@ public function collectTotals() /** * Get all quote totals (sorted by priority) * - * @return array + * @return Address\Total[] */ public function getTotals() { diff --git a/app/code/Magento/Quote/Model/Quote/Address/Total.php b/app/code/Magento/Quote/Model/Quote/Address/Total.php index 76363fd707d0e..4babf52cc1ada 100644 --- a/app/code/Magento/Quote/Model/Quote/Address/Total.php +++ b/app/code/Magento/Quote/Model/Quote/Address/Total.php @@ -5,6 +5,9 @@ */ namespace Magento\Quote\Model\Quote\Address; +/** + * @method string getCode() + */ class Total extends \Magento\Framework\DataObject { /** diff --git a/app/code/Magento/Quote/Model/Quote/TotalsReader.php b/app/code/Magento/Quote/Model/Quote/TotalsReader.php index c6c7bd9aacf03..914405b5c87ff 100644 --- a/app/code/Magento/Quote/Model/Quote/TotalsReader.php +++ b/app/code/Magento/Quote/Model/Quote/TotalsReader.php @@ -35,7 +35,7 @@ public function __construct( /** * @param \Magento\Quote\Model\Quote $quote * @param array $total - * @return array + * @return Total[] */ public function fetch(\Magento\Quote\Model\Quote $quote, array $total) { @@ -62,7 +62,7 @@ public function fetch(\Magento\Quote\Model\Quote $quote, array $total) /** * @param array $total - * @return Total|array + * @return Total|Total[] */ protected function convert($total) { @@ -85,10 +85,10 @@ protected function convert($total) /** * @param Total $totalInstance - * @param array $output - * @return array + * @param Total[] $output + * @return Total[] */ - protected function merge($totalInstance, $output) + protected function merge(Total $totalInstance, $output) { if (array_key_exists($totalInstance->getCode(), $output)) { $output[$totalInstance->getCode()] = $output[$totalInstance->getCode()]->addData( From 160327bc077af34313b72eaec4763041c77a17bc Mon Sep 17 00:00:00 2001 From: Fabian Schmengler Date: Mon, 19 Jun 2017 23:53:04 +0200 Subject: [PATCH 2/3] Add default behavior to Quote\Address\TotalFactory The factory is used like a default generated factory in TotalsReader but needed to pass the class name to create(). This violates the POLA. Also, the return type hint is adjusted now (Total is no subtype of Total\AbstractTotal) --- .../Magento/Quote/Model/Quote/Address/TotalFactory.php | 4 ++-- app/code/Magento/Quote/Model/Quote/TotalsReader.php | 8 +++----- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/app/code/Magento/Quote/Model/Quote/Address/TotalFactory.php b/app/code/Magento/Quote/Model/Quote/Address/TotalFactory.php index a997eada2d4a2..bef6f48a97267 100644 --- a/app/code/Magento/Quote/Model/Quote/Address/TotalFactory.php +++ b/app/code/Magento/Quote/Model/Quote/Address/TotalFactory.php @@ -33,9 +33,9 @@ public function __construct(\Magento\Framework\ObjectManagerInterface $objManage * * @param string $instanceName * @param array $data - * @return \Magento\Quote\Model\Quote\Address\Total\AbstractTotal + * @return Total\AbstractTotal|Total */ - public function create($instanceName, array $data = []) + public function create($instanceName = Total::class, array $data = []) { return $this->_objectManager->create($instanceName, $data); } diff --git a/app/code/Magento/Quote/Model/Quote/TotalsReader.php b/app/code/Magento/Quote/Model/Quote/TotalsReader.php index 914405b5c87ff..d0417c3455f5e 100644 --- a/app/code/Magento/Quote/Model/Quote/TotalsReader.php +++ b/app/code/Magento/Quote/Model/Quote/TotalsReader.php @@ -40,7 +40,7 @@ public function __construct( public function fetch(\Magento\Quote\Model\Quote $quote, array $total) { $output = []; - $total = $this->totalFactory->create(\Magento\Quote\Model\Quote\Address\Total::class)->setData($total); + $total = $this->totalFactory->create()->setData($total); /** @var ReaderInterface $reader */ foreach ($this->collectorList->getCollectors($quote->getStoreId()) as $reader) { $data = $reader->fetch($quote, $total); @@ -73,14 +73,12 @@ protected function convert($total) if (count(array_column($total, 'code')) > 0) { $totals = []; foreach ($total as $item) { - $totals[] = $this->totalFactory->create( - \Magento\Quote\Model\Quote\Address\Total::class - )->setData($item); + $totals[] = $this->totalFactory->create()->setData($item); } return $totals; } - return $this->totalFactory->create(\Magento\Quote\Model\Quote\Address\Total::class)->setData($total); + return $this->totalFactory->create()->setData($total); } /** From f7aea8349536b8ff858df7472fc16acec4f025c4 Mon Sep 17 00:00:00 2001 From: Fabian Schmengler Date: Mon, 19 Jun 2017 23:56:39 +0200 Subject: [PATCH 3/3] Properly initialize $totalAmounts Otherwise getAllTotalAmounts() does not return an array if no totals have been added. Same goes for $baseTotalAmounts --- app/code/Magento/Quote/Model/Quote/Address/Total.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/code/Magento/Quote/Model/Quote/Address/Total.php b/app/code/Magento/Quote/Model/Quote/Address/Total.php index 4babf52cc1ada..3571bdc36bee2 100644 --- a/app/code/Magento/Quote/Model/Quote/Address/Total.php +++ b/app/code/Magento/Quote/Model/Quote/Address/Total.php @@ -13,12 +13,12 @@ class Total extends \Magento\Framework\DataObject /** * @var array */ - protected $totalAmounts; + protected $totalAmounts = []; /** * @var array */ - protected $baseTotalAmounts; + protected $baseTotalAmounts = []; /** * Serializer interface instance.