Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor - cleanup unnecessary around plugins #28767

Conversation

lewisvoncken
Copy link
Contributor

@lewisvoncken lewisvoncken commented Jun 17, 2020

Description (*)

Changed the following around plugins to before and after plugins:

after:

        \Magento\GroupedProduct\Helper\Product\Configuration\Plugin\Grouped::afterGetOptions
        \Magento\ConfigurableProduct\Helper\Product\Configuration\Plugin::afterGetOptions
        \Magento\AdvancedSearch\Model\Indexer\Fulltext\Plugin\CustomerGroup::afterSave
        \Magento\Catalog\Model\Indexer\Product\Price\Plugin\CustomerGroup::afterSave
        \Magento\CatalogSearch\Model\Attribute\SearchWeight::afterSave
        \Magento\CatalogUrlRewrite\Model\Category\Plugin\Category\Remove::afterDelete
        \Magento\ConfigurableProduct\Model\Entity\Product\Attribute\Group\AttributeMapper\Plugin::afterMap
        \Magento\ConfigurableProduct\Plugin\Model\ResourceModel\Product::afterDelete
        \Magento\Customer\Model\Plugin\CustomerFlushFormKey::afterExecute
        \Magento\GroupedProduct\Model\ResourceModel\Product\Link\RelationPersister::afterDeleteProductLink
        \Magento\Theme\Model\Indexer\Design\Config\Plugin\Store::afterSave
        \Magento\Theme\Model\Indexer\Design\Config\Plugin\Website::afterSave

before:

        \Magento\CatalogInventory\Plugin\MassUpdateProductAttribute::beforeExecute

Starting from Magento 2.2 it is no longer necessary to use around plugins in cases which you now could just use an after which also contains the parameters.

This refactor is because of performance reasons:
image
source:
https://devdocs.magento.com/guides/v2.4/extension-dev-guide/plugins.html#around-methods

Fixed Issues (if relevant)

  1. Fixes Probably unnecessary use of around plugin #27912

Contribution checklist (*)

  • Pull request has a meaningful description of its purpose
  • All commits are accompanied by meaningful commit messages
  • All new or changed code is covered with unit/integration tests (if applicable)
  • All automated tests passed successfully (all builds are green)

after:

	\Magento\GroupedProduct\Helper\Product\Configuration\Plugin\Grouped::afterGetOptions
	\Magento\ConfigurableProduct\Helper\Product\Configuration\Plugin::afterGetOptions
	\Magento\AdvancedSearch\Model\Indexer\Fulltext\Plugin\CustomerGroup::afterSave
	\Magento\Catalog\Model\Indexer\Product\Price\Plugin\CustomerGroup::afterSave
	\Magento\CatalogSearch\Model\Attribute\SearchWeight::afterSave
	\Magento\CatalogUrlRewrite\Model\Category\Plugin\Category\Remove::afterDelete
	\Magento\ConfigurableProduct\Model\Entity\Product\Attribute\Group\AttributeMapper\Plugin::afterMap
	\Magento\ConfigurableProduct\Plugin\Model\ResourceModel\Product::afterDelete
	\Magento\Customer\Model\Plugin\CustomerFlushFormKey::afterExecute
	\Magento\GroupedProduct\Model\ResourceModel\Product\Link\RelationPersister::afterDeleteProductLink
	\Magento\Theme\Model\Indexer\Design\Config\Plugin\Store::afterSave
	\Magento\Theme\Model\Indexer\Design\Config\Plugin\Website::afterSave

before:

	\Magento\CatalogInventory\Plugin\MassUpdateProductAttribute::beforeExecute
@m2-assistant
Copy link

m2-assistant bot commented Jun 17, 2020

Hi @lewisvoncken. Thank you for your contribution
Here is some useful tips how you can test your changes using Magento test environment.
Add the comment under your pull request to deploy test or vanilla Magento instance:

  • @magento give me test instance - deploy test instance based on PR changes
  • @magento give me 2.4-develop instance - deploy vanilla Magento instance

❗ Automated tests can be triggered manually with an appropriate comment:

  • @magento run all tests - run or re-run all required tests against the PR changes
  • @magento run <test-build(s)> - run or re-run specific test build(s)
    For example: @magento run Unit Tests

<test-build(s)> is a comma-separated list of build names. Allowed build names are:

  1. Database Compare
  2. Functional Tests CE
  3. Functional Tests EE,
  4. Functional Tests B2B
  5. Integration Tests
  6. Magento Health Index
  7. Sample Data Tests CE
  8. Sample Data Tests EE
  9. Sample Data Tests B2B
  10. Static Tests
  11. Unit Tests
  12. WebAPI Tests

You can find more information about the builds here

ℹ️ Please run only needed test builds instead of all when developing. Please run all test builds before sending your PR for review.

For more details, please, review the Magento Contributor Guide documentation.

@lewisvoncken
Copy link
Contributor Author

@magento run all tests

@ihor-sviziev ihor-sviziev self-assigned this Jun 19, 2020
@ihor-sviziev
Copy link
Contributor

@magento run all tests

@@ -118,7 +118,7 @@ public function testAroundSave(
*
* @return array
*/
public function aroundSaveDataProvider(): array
public function afterSaveDataProvider(): array
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is data provider, please update place where it is used

*/
public function aroundExecute(Save $subject, callable $proceed)
public function beforeExecute(Save $subject)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't really need to change arguments for "save", so it's better to use void return type here and not use return statement at all

Suggested change
public function beforeExecute(Save $subject)
public function beforeExecute(Save $subject): void

@@ -101,16 +100,16 @@ public function aroundExecute(Save $subject, callable $proceed)
$this->updateInventoryInProducts($productIds, $websiteId, $inventoryData);
}

return $proceed();
return [];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return [];

} catch (\Magento\Framework\Exception\LocalizedException $e) {
$this->messageManager->addErrorMessage($e->getMessage());
return $proceed();
return [];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return [];

} catch (\Exception $e) {
$this->messageManager->addExceptionMessage(
$e,
__('Something went wrong while updating the product(s) attributes.')
);
return $proceed();
return [];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return [];

\Magento\Catalog\Model\ResourceModel\Attribute $subject,
\Closure $proceed,
\Magento\Catalog\Model\ResourceModel\Attribute $result,
\Magento\Framework\Model\AbstractModel $attribute
) {
$isNew = $attribute->isObjectNew();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think after save it will be always "false", probably that's why we had there "around" plugin. Could you double check it?

\Magento\Catalog\Model\ResourceModel\Product $subject,
\Closure $proceed,
\Magento\Catalog\Model\ResourceModel\Product $result,
\Magento\Catalog\Model\Product $product
) {
$configurableProductIds = $this->configurable->getParentIdsByChild($product->getId());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure this still will be available after deleting product. Please double check it

*/
public function aroundDeleteProductLink(Link $subject, \Closure $proceed, $linkId)
public function afterDeleteProductLink(Link $subject, Link $result, $linkId)
{
/** @var \Magento\Catalog\Model\ProductLink\Link $link */
$link = $this->linkFactory->create();
$subject->load($link, $linkId, $subject->getIdFieldName());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it will not work after deleting

* @return StoreStore
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function aroundSave(StoreStore $subject, \Closure $proceed)
public function afterSave(StoreStore $subject, StoreStore $result)
{
$isObjectNew = $subject->getId() == 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks liike this will be always false, as object already saved and have id

* @return StoreWebsite
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function aroundSave(StoreWebsite $subject, \Closure $proceed)
public function afterSave(StoreWebsite $subject, StoreWebsite $result)
{
$isObjectNew = $subject->getId() == 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks liike this will be always false, as object already saved and have id

@ihor-sviziev ihor-sviziev added the Severity: S3 Affects non-critical data or functionality and does not force users to employ a workaround. label Jun 22, 2020
@ihor-sviziev
Copy link
Contributor

Hi @lewisvoncken,
Will you be able to update your PR?

@ihor-sviziev
Copy link
Contributor

@lewisvoncken I am closing this PR now due to inactivity.
Please reopen and update if you wish to continue.
Thank you for the collaboration!

@m2-assistant
Copy link

m2-assistant bot commented Jul 17, 2020

Hi @lewisvoncken, thank you for your contribution!
Please, complete Contribution Survey, it will take less than a minute.
Your feedback will help us to improve contribution process.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Probably unnecessary use of around plugin
3 participants