Skip to content

Commit

Permalink
perf: Applied phpcs and phpmd rules
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Code is refactored based on phpcs and phpmd rules.
  • Loading branch information
jansentjeu committed Mar 29, 2024
1 parent f2eabde commit 9a499fd
Show file tree
Hide file tree
Showing 169 changed files with 912 additions and 534 deletions.
10 changes: 8 additions & 2 deletions Block/Autocomplete/FormMini.php
@@ -1,4 +1,5 @@
<?php

/**
* Tweakwise (https://www.tweakwise.com/) - All Rights Reserved
*
Expand Down Expand Up @@ -40,8 +41,13 @@ class FormMini extends Template
* @param Context $context
* @param array $data
*/
public function __construct(Config $config, LocaleFormat $localeFormat, Registry $registry, Context $context, array $data = [])
{
public function __construct(
Config $config,
LocaleFormat $localeFormat,
Registry $registry,
Context $context,
array $data = []
) {
parent::__construct($context, $data);

$this->config = $config;
Expand Down
27 changes: 18 additions & 9 deletions Block/Cart/Crosssell/Plugin.php
@@ -1,4 +1,5 @@
<?php

/**
* Tweakwise (https://www.tweakwise.com/) - All Rights Reserved
*
Expand All @@ -16,6 +17,7 @@
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Registry;
use Tweakwise\Magento2Tweakwise\Model\Catalog\Product\Recommendation\Collection;
use Tweakwise\Magento2Tweakwise\Model\Catalog\Product\Recommendation\Context as RecommendationsContext;
use Tweakwise\Magento2Tweakwise\Block\Catalog\Product\ProductList\AbstractRecommendationPlugin;
use Tweakwise\Magento2Tweakwise\Exception\ApiException;
Expand Down Expand Up @@ -66,8 +68,7 @@ public function __construct(
Session $checkoutSession,
RecommendationsContext $recommendationsContext,
?ProductRepositoryInterface $productRepository = null
)
{
) {
$this->productRepository = $productRepository;
$this->checkoutSession = $checkoutSession;
$this->recommendationsContext = $recommendationsContext;
Expand All @@ -94,6 +95,7 @@ protected function getType()
/**
* Get the last added product before running the default getItems because the last added products gets deleted from the session
*
* @param Crosssell $crosssell
* @param Closure $proceed
* @return array
*/
Expand Down Expand Up @@ -124,6 +126,7 @@ private function getLastAddedProduct(): ?ProductInterface
$product = null;
}
}

return $product;
}

Expand All @@ -139,7 +142,7 @@ protected function _getLastAddedProductId()

/**
* @param Crosssell $crosssell
* @param $result
* @param array $result
* @return array
*/
public function afterGetItems(Crosssell $crosssell, $result)
Expand All @@ -166,7 +169,11 @@ private function getShoppingcartCrosssellItems(array $cartProductIds, array $res

if ($cartProductIds) {
if ($this->lastAddedProduct) {
$items = $this->getShoppingcartCrosssellTweakwiseItems($this->lastAddedProduct, $result, $cartProductIds);
$items = $this->getShoppingcartCrosssellTweakwiseItems(
$this->lastAddedProduct,
$result,
$cartProductIds
);
}

if (empty($items)) {
Expand Down Expand Up @@ -197,7 +204,8 @@ private function getShoppingcartCrosssellItems(array $cartProductIds, array $res
* @param array $cartItems
* @return array
*/
private function getShoppingcartCrosssellTweakwiseItems (ProductInterface $product, array $result, array $cartItems) {
private function getShoppingcartCrosssellTweakwiseItems(ProductInterface $product, array $result, array $cartItems)
{
$items = [];

//show featured products
Expand Down Expand Up @@ -235,19 +243,20 @@ private function getShoppingcartCrosssellTweakwiseItems (ProductInterface $produ
}

/**
* @param $collection
* @param $filteredProducts
* @return void
* @param Collection $collection
* @param array $cartItems
* @return array
*/
protected function removeCartItems($collection, $cartItems)
{
$items = $collection->getItems();

if(!empty($cartItems)) {
if (!empty($cartItems)) {
foreach ($cartItems as $cartItem) {
unset($items[$cartItem]);
}
}

return $items;
}

Expand Down
Expand Up @@ -57,7 +57,7 @@ public function __construct(Config $config, Registry $registry, Context $context
/**
* @return string
*/
protected abstract function getType();
abstract protected function getType();

/**
* @param ProductRequest $request
Expand All @@ -75,6 +75,7 @@ protected function configureRequest(ProductRequest $request)

/**
* @return Collection
* @throws InvalidArgumentException
*/
protected function getCollection()
{
Expand Down
11 changes: 3 additions & 8 deletions Block/Catalog/Product/ProductList/Featured.php
@@ -1,4 +1,5 @@
<?php

/**
* Tweakwise (https://www.tweakwise.com/) - All Rights Reserved
*
Expand All @@ -23,13 +24,6 @@
use Magento\Framework\Data\Helper\PostHelper;
use Magento\Framework\Url\Helper\Data;

/**
* Class Featured
*
* @package Tweakwise\Magento2Tweakwise\Block\Catalog\Product\ProductList
*
* @method string getRenderLocation();
*/
class Featured extends ListProduct
{
/**
Expand Down Expand Up @@ -122,6 +116,7 @@ public function toHtml()
if (!$this->getData('view_model') && $this->preparePostDataFactory->getPreparePostData()) {
$this->setData('view_model', $this->preparePostDataFactory->getPreparePostData());
}

try {
$this->_getProductCollection();
} catch (ApiException $e) {
Expand Down Expand Up @@ -159,7 +154,7 @@ protected function checkRecommendationEnabled(): bool
}

/**
* @param $request
* @param FeaturedRequest $request
*/
protected function configureRequest(FeaturedRequest $request)
{
Expand Down
9 changes: 5 additions & 4 deletions Block/Catalog/Product/ProductList/Plugin.php
@@ -1,4 +1,5 @@
<?php

/**
* Tweakwise (https://www.tweakwise.com/) - All Rights Reserved
*
Expand All @@ -11,10 +12,10 @@
class Plugin
{
/**
* Retrieve additional blocks html
*
* @return string
*/
* Retrieve additional blocks html
*
* @return string
*/
public function afterGetAdditionalHtml($subject, $result)
{
$searchBanner = $subject->getBlockHtml('navigation.search.banner.products.top');
Expand Down
2 changes: 2 additions & 0 deletions Block/Catalog/Product/ProductList/Related/Plugin.php
@@ -1,4 +1,5 @@
<?php

/**
* Tweakwise (https://www.tweakwise.com/) - All Rights Reserved
*
Expand Down Expand Up @@ -34,6 +35,7 @@ public function aroundGetItems(Related $subject, Closure $proceed)
if (!$this->config->isRecommendationsEnabled(Config::RECOMMENDATION_TYPE_CROSSSELL)) {
return $proceed();
}

if (!$this->templateFinder->forProduct($subject->getProduct(), $this->getType())) {
return $proceed();
}
Expand Down
9 changes: 5 additions & 4 deletions Block/Catalog/Product/ProductList/Toolbar/Plugin.php
@@ -1,4 +1,5 @@
<?php

/**
* Tweakwise (https://www.tweakwise.com/) - All Rights Reserved
*
Expand All @@ -8,7 +9,6 @@

namespace Tweakwise\Magento2Tweakwise\Block\Catalog\Product\ProductList\Toolbar;


use Closure;
use Magento\CatalogInventory\Api\StockConfigurationInterface;
use Magento\Framework\Exception\LocalizedException;
Expand Down Expand Up @@ -90,6 +90,7 @@ public function aroundGetAvailableOrders(Toolbar $subject, Closure $proceed)
foreach ($sortFields as $field) {
$result[$field->getUrlValue()] = $field->getDisplayTitle();
}

return $result;
}

Expand All @@ -109,12 +110,13 @@ public function afterGetWidgetOptionsJson(Toolbar $subject, string $result)

return json_encode($options);
}

/**
* Update toolbar count if store is in single source mode
* Used in the commerce version
*
* @param Toolbar $subject
* @param int $result
* @param callable $proceed
* @return int
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
* @throws LocalizedException
Expand All @@ -134,8 +136,7 @@ public function aroundGetTotalNum(Toolbar $subject, callable $proceed): int
}
}

if (!isset($result))
{
if (!isset($result)) {
return $proceed();
}

Expand Down
2 changes: 2 additions & 0 deletions Block/Catalog/Product/ProductList/Upsell/Plugin.php
@@ -1,4 +1,5 @@
<?php

/**
* Tweakwise (https://www.tweakwise.com/) - All Rights Reserved
*
Expand Down Expand Up @@ -35,6 +36,7 @@ public function aroundGetItemCollection(Upsell $subject, Closure $proceed)
if (!$this->config->isRecommendationsEnabled(Config::RECOMMENDATION_TYPE_UPSELL)) {
return $proceed();
}

if (!$this->templateFinder->forProduct($subject->getProduct(), $this->getType())) {
return $proceed();
}
Expand Down
11 changes: 2 additions & 9 deletions Block/Catalog/Product/Widget/FeaturedProducts.php
@@ -1,4 +1,5 @@
<?php

/**
* Tweakwise (https://www.tweakwise.com/) - All Rights Reserved
*
Expand All @@ -13,14 +14,6 @@
use Tweakwise\Magento2Tweakwise\Model\Client\Request\Recommendations\FeaturedRequest;
use Magento\Widget\Block\BlockInterface;

/**
* Class FeaturedProducts
*
* @package Tweakwise\Magento2Tweakwise\Block\Catalog\Product\Widget
* @method int getRuleId();
* @method string getDisplayType();
* @method bool getCanItemsAddToCart();
*/
class FeaturedProducts extends Featured implements BlockInterface
{
/**
Expand All @@ -41,7 +34,7 @@ protected function checkRecommendationEnabled(): bool
}

/**
* @param $request
* @param FeaturedRequest $request
*/
protected function configureRequest(FeaturedRequest $request)
{
Expand Down
26 changes: 17 additions & 9 deletions Block/Checkout/Cart/Crosssell/Plugin.php
@@ -1,4 +1,5 @@
<?php

/**
* Tweakwise (https://www.tweakwise.com/) - All Rights Reserved
*
Expand All @@ -18,6 +19,7 @@
use Magento\Framework\Registry;
use Tweakwise\Magento2Tweakwise\Block\Catalog\Product\ProductList\AbstractRecommendationPlugin;
use Tweakwise\Magento2Tweakwise\Exception\ApiException;
use Tweakwise\Magento2Tweakwise\Model\Catalog\Product\Recommendation\Collection;
use Tweakwise\Magento2Tweakwise\Model\Catalog\Product\Recommendation\Context;
use Tweakwise\Magento2Tweakwise\Model\Client\Request\Recommendations\FeaturedRequest;
use Tweakwise\Magento2Tweakwise\Model\Client\Request\Recommendations\ProductRequest;
Expand Down Expand Up @@ -61,8 +63,7 @@ public function __construct(
TemplateFinder $templateFinder,
Session $checkoutSession,
?ProductRepositoryInterface $productRepository = null
)
{
) {
$this->productRepository = $productRepository;
$this->checkoutSession = $checkoutSession;
$this->productRepository = $productRepository
Expand Down Expand Up @@ -97,6 +98,7 @@ public function aroundGetLastAddedProduct(Crosssell $crossell, Closure $proceed)
$product = null;
}
}

$this->lastAddedProduct = $product;
}

Expand All @@ -115,7 +117,7 @@ protected function _getLastAddedProductId()

/**
* @param Crosssell $crosssell
* @param $result
* @param array $result
* @return array
*/
public function afterGetItemCollection(Crosssell $crosssell, $result)
Expand Down Expand Up @@ -146,7 +148,11 @@ private function getShoppingcartCrosssellItems(array $cartProductIds, array $res

if ($cartProductIds) {
if ($this->lastAddedProduct) {
$items = $this->getShoppingcartCrosssellTweakwiseItems($this->lastAddedProduct, $result, $cartProductIds);
$items = $this->getShoppingcartCrosssellTweakwiseItems(
$this->lastAddedProduct,
$result,
$cartProductIds
);
}

if (empty($items)) {
Expand Down Expand Up @@ -177,7 +183,8 @@ private function getShoppingcartCrosssellItems(array $cartProductIds, array $res
* @param array $cartItems
* @return array
*/
private function getShoppingcartCrosssellTweakwiseItems (ProductInterface $product, array $result, array $cartItems) {
private function getShoppingcartCrosssellTweakwiseItems(ProductInterface $product, array $result, array $cartItems)
{
$items = [];

//show featured products
Expand Down Expand Up @@ -215,19 +222,20 @@ private function getShoppingcartCrosssellTweakwiseItems (ProductInterface $produ
}

/**
* @param $collection
* @param $filteredProducts
* @return void
* @param Collection $collection
* @param array $cartItems
* @return array
*/
protected function removeCartItems($collection, $cartItems)
{
$items = $collection->getItems();

if(!empty($cartItems)) {
if (!empty($cartItems)) {
foreach ($cartItems as $cartItem) {
unset($items[$cartItem]);
}
}

return $items;
}

Expand Down
Expand Up @@ -23,6 +23,7 @@ public function renderAnchorHtmlTagAttributes(Item $item)
foreach ($anchorAttributes as $anchorAttribute => $anchorAttributeValue) {
$attributeHtml[] = sprintf('%s="%s"', $anchorAttribute, $anchorAttributeValue);
}

return implode(' ', $attributeHtml);
}

Expand Down

0 comments on commit 9a499fd

Please sign in to comment.