From d8b47383c0415542d15c868979637a493500cf19 Mon Sep 17 00:00:00 2001 From: Antonio Pedicini Date: Fri, 6 Oct 2023 21:22:11 +0100 Subject: [PATCH] Fixed return type Happy path Code strict improvements --- Block/Category/Listpost.php | 72 ++++++++++++++++++++++--------------- 1 file changed, 43 insertions(+), 29 deletions(-) diff --git a/Block/Category/Listpost.php b/Block/Category/Listpost.php index b6a80eb2..747b9fe8 100755 --- a/Block/Category/Listpost.php +++ b/Block/Category/Listpost.php @@ -19,11 +19,16 @@ * @license https://www.mageplaza.com/LICENSE.txt */ +declare(strict_types=1); + namespace Mageplaza\Blog\Block\Category; +use Magento\Framework\Exception\LocalizedException; use Magento\Framework\Exception\NoSuchEntityException; use Magento\Framework\Phrase; +use Magento\Framework\View\Element\BlockInterface; use Mageplaza\Blog\Helper\Data; +use Mageplaza\Blog\Model\Category; use Mageplaza\Blog\Model\ResourceModel\Post\Collection; /** @@ -32,10 +37,7 @@ */ class Listpost extends \Mageplaza\Blog\Block\Listpost { - /** - * @var string - */ - protected $_category; + protected ?Category $category = null; /** * Override this function to apply collection for each type @@ -52,41 +54,52 @@ protected function getCollection() return null; } - /** - * @return mixed - */ - protected function getBlogObject() + protected function getBlogObject(): Category|null { - if (!$this->_category) { - $id = $this->getRequest()->getParam('id'); - if ($id) { - $category = $this->helperData->getObjectByParam($id, null, Data::TYPE_CATEGORY); - if ($category && $category->getId()) { - $this->_category = $category; - } - } + if ($this->category instanceof Category === true) { + return $this->category; + } + + $id = $this->getRequest()->getParam('id'); + if (empty($id) === true) { + return null; + } + + $category = $this->helperData->getObjectByParam($id, null, Data::TYPE_CATEGORY); + if (isset($category) === true + && $category instanceof Category === true + && $category->getId() !== null + ) { + $this->category = $category; } - return $this->_category; + return $this->category ?? null; } /** * @inheritdoc + * @throws LocalizedException */ - protected function _prepareLayout() + protected function _prepareLayout(): void { parent::_prepareLayout(); + $breadcrumbs = $this->getLayout()->getBlock('breadcrumbs'); + if ($breadcrumbs instanceof BlockInterface === false && is_bool($breadcrumbs) === false) { + return; + } - if ($breadcrumbs = $this->getLayout()->getBlock('breadcrumbs')) { - $category = $this->getBlogObject(); - $categoryName = preg_replace('/[^A-Za-z0-9\-]/', ' ', $category->getName()); - if ($category) { - $breadcrumbs->addCrumb($category->getUrlKey(), [ - 'label' => __($categoryName), - 'title' => __($categoryName) - ]); - } + $category = $this->getBlogObject(); + if ($category instanceof Category === false) { + return; } + + $categoryName = preg_replace('/[^A-Za-z0-9\-]/', ' ', $category->getName()); + + $breadcrumbs->addCrumb( + $category->getUrlKey(), [ + 'label' => __($categoryName), + 'title' => __($categoryName), + ]); } /** @@ -94,10 +107,11 @@ protected function _prepareLayout() * * @return array|Phrase|string */ - public function getBlogTitle($meta = false) + public function getBlogTitle( + $meta = false) { $blogTitle = parent::getBlogTitle($meta); - $category = $this->getBlogObject(); + $category = $this->getBlogObject(); if (!$category) { return $blogTitle; }