Magento2: Creating a preference for category product indexer breaks setup:di:compile

Created on 7 May 2019  路  13Comments  路  Source: magento/magento2

Summary

When you create a preference for the category product indexer, code compilation breaks. This is because of the definition of the category product indexer execute method: https://github.com/magento/magento2/blob/2.3.1/app/code/Magento/Catalog/Model/Indexer/Category/Product/Action/Full.php#L155:

public function execute(): self
{
    $this->createTables();
    $this->clearReplicaTables();
    $this->reindex();
    $this->switchTables();
    return $this;
}

Due to the preference, the generated interceptor (Magento already has a plugin defined for the execute method), returns the preferenced class instead of the the Magento indexer, which then doesn't match : self.

Preconditions (*)

  1. Magento version: 2.3.1
  2. PHP 7.2.12

Steps to reproduce (*)

  1. Create a preference for \Magento\Catalog\Model\Indexer\Category\Product\Action\Full in a custom module
  2. Run bin/magento setup:di:compile

Expected result (*)

  1. Code is compiled

Actual result (*)

  1. Fatal error: Declaration of Vendor\Module\Model\Indexer\Category\Product\Action\Full\Interceptor::execute(): Vendor\Module\Model\Indexer\Category\Product\Action\Full must be compatible with Magento\Catalog\Model\Indexer\Category\Product\Action\Full::execute(): Magento\Catalog\Model\Indexer\Category\Product\Action\Full in /var/www/html/generated/code/Vendor/Module/Model/Indexer/Category/Product/Action/Full/Interceptor.php on line 7
/**
 * {@inheritdoc}
 */
public function execute() : \Vendor\Module\Model\Indexer\Category\Product\Action\Full
{
    $pluginInfo = $this->pluginList->getNext($this->subjectType, 'execute');
    if (!$pluginInfo) {
        return parent::execute();
    } else {
        return $this->___callPlugins('execute', func_get_args(), $pluginInfo);
    }
}
Catalog Fixed in 2.3.x Clear Description Confirmed Format is valid Ready for Work Youwe Reproduced on 2.3.x partners-contribution

Most helpful comment

Hi @orlangur, that issue only mentions Magento's code generation not being able to process self in general, which got fixed as the generated code resolves self to a class. However, the following scenario is not covered:

  1. Class A contains a public method that returns self and this method has a plugin attached: public function execute(): self
  2. Create a preference for class A, class B. This class doesn't extend the execute method.
  3. The interceptor that Magento generates now results in public function execute(): B, which is not compatible with the self definition of class A.

All 13 comments

Hi @mbijnsdorp. Thank you for your report.
To help us process this issue please make sure that you provided the following information:

  • [x] Summary of the issue
  • [x] Information on your environment
  • [x] Steps to reproduce
  • [x] Expected and actual results

Please make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, please, add a comment to the issue:

@magento-engcom-team give me 2.3-develop instance - upcoming 2.3.x release

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

@mbijnsdorp do you confirm that you was able to reproduce the issue on vanilla Magento instance following steps to reproduce?

  • [ ] yes
  • [ ] no

Hi @mbijnsdorp, how is that different from https://github.com/magento/magento2/issues/11905?

Hi @orlangur, that issue only mentions Magento's code generation not being able to process self in general, which got fixed as the generated code resolves self to a class. However, the following scenario is not covered:

  1. Class A contains a public method that returns self and this method has a plugin attached: public function execute(): self
  2. Create a preference for class A, class B. This class doesn't extend the execute method.
  3. The interceptor that Magento generates now results in public function execute(): B, which is not compatible with the self definition of class A.

Hi @davidverholen. Thank you for working on this issue.
In order to make sure that issue has enough information and ready for development, please read and check the following instruction: :point_down:

  • [x] 1. Verify that issue has all the required information. (Preconditions, Steps to reproduce, Expected result, Actual result).
    DetailsIf the issue has a valid description, the label Issue: Format is valid will be added to the issue automatically. Please, edit issue description if needed, until label Issue: Format is valid appears.
  • [x] 2. Verify that issue has a meaningful description and provides enough information to reproduce the issue. If the report is valid, add Issue: Clear Description label to the issue by yourself.

  • [x] 3. Add Component: XXXXX label(s) to the ticket, indicating the components it may be related to.

  • [x] 4. Verify that the issue is reproducible on 2.3-develop branch

    Details- Add the comment @magento give me 2.3-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.3-develop branch, please, add the label Reproduced on 2.3.x.
    - If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and _stop verification process here_!

  • [ ] 5. Verify that the issue is reproducible on 2.2-develop branch.

    Details- Add the comment @magento give me 2.2-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.2-develop branch, please add the label Reproduced on 2.2.x

  • [x] 6. Add label Issue: Confirmed once verification is complete.

  • [x] 7. Make sure that automatic system confirms that report has been added to the backlog.

:white_check_mark: Confirmed by @davidverholen
Thank you for verifying the issue. Based on the provided information internal tickets MC-17251 were created

Issue Available: @davidverholen, _You will be automatically unassigned. Contributors/Maintainers can claim this issue to continue. To reclaim and continue work, reassign the ticket to yourself._

@davidverholen as mentioned in #22826 you can test something like this to fix this issue:

diff --git a/lib/internal/Magento/Framework/Interception/Code/Generator/Interceptor.php b/lib/internal/Magento/Framework/Interception/Code/Generator/Interceptor.php
index 9297ca2..acee7c5 100644
--- a/lib/internal/Magento/Framework/Interception/Code/Generator/Interceptor.php
+++ b/lib/internal/Magento/Framework/Interception/Code/Generator/Interceptor.php
@@ -109,7 +109,7 @@ class Interceptor extends \Magento\Framework\Code\Generator\EntityAbstract
             $parameters[] = $this->_getMethodParameterInfo($parameter);
         }

-        $returnTypeValue = $this->getReturnTypeValue($method->getReturnType());
+        $returnTypeValue = $this->getReturnTypeValue($method);
         $methodInfo = [
             'name' => ($method->returnsReference() ? '& ' : '') . $method->getName(),
             'parameters' => $parameters,
@@ -217,16 +217,17 @@ METHOD_BODY
     /**
      * Returns return type
      *
-     * @param mixed $returnType
+     * @param \ReflectionMethod $method
      * @return null|string
      */
-    private function getReturnTypeValue($returnType): ?string
+    private function getReturnTypeValue(\ReflectionMethod $method): ?string
     {
         $returnTypeValue = null;
+        $returnType = $method->getReturnType();
         if ($returnType) {
             $returnTypeValue = ($returnType->allowsNull() ? '?' : '');
             $returnTypeValue .= ($returnType->getName() === 'self')
-                ? $this->getSourceClassName()
+                ? $method->getDeclaringClass()
                 : $returnType->getName();
         }
         return $returnTypeValue;

Hi @dcabrejas. Thank you for working on this issue.
Looks like this issue is already verified and confirmed. But if you want to validate it one more time, please, go though the following instruction:

  • [ ] 1. Add/Edit Component: XXXXX label(s) to the ticket, indicating the components it may be related to.
  • [ ] 2. Verify that the issue is reproducible on 2.3-develop branch

    Details- Add the comment @magento give me 2.3-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.3-develop branch, please, add the label Reproduced on 2.3.x.
    - If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and _stop verification process here_!

  • [ ] 3. Verify that the issue is reproducible on 2.2-develop branch.

    Details- Add the comment @magento give me 2.2-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.2-develop branch, please add the label Reproduced on 2.2.x

  • [ ] 4. If the issue is not relevant or is not reproducible any more, feel free to close it.


Hi @mbijnsdorp. Thank you for your report.
The issue has been fixed in magento/magento2#23300 by @dcabrejas in 2.3-develop branch
Related commit(s):

The fix will be available with the upcoming 2.3.4 release.

Could this really be considered as a fix to the reported issue as it just removed the appearance of the issue where it's still possible to create the same issue whilst using totally valid PHP feature? :/

This way, it's pretty easy to get back to a point where one has to revisit the same issue again in the future unless you ban the usage of 'self' within the code by throwing a meaningful exception (actual change to generator to respect 'self' would be better of course).

Kind of a "door聽hinge was squeaking, so we removed the door" situation :(

I'd humbly suggest this to be revisited with solution that would be along the lines of what @fsw suggested in his comment.

@allanpaiste Yes, as I said on my PR, this is more of a patch, it fixes the problem. The real issue is still there but the solution is more complicated. That I leave to Magento Team o another contributor who has the time.

@dcabrejas I totally dig the approach of getting the local appearance of the issue quickly dealt with! I was just surprised that the ticket was closed and issue declared as solved.

Created new report that is written in a way that does not allow a "solved" declaration due to not being based on anything that appears in the code but rather on the idea that one can create such a situation by using valid PHP features.

https://github.com/magento/magento2/issues/24443

It was suggested to me by a member of the Magento community engineering team at Meet Magento UK 2019 that's why it got merged. I would argue that merging a patch now solves this issue for a lot of people, which it does. Fixing the issue properly and applying a patch are not contradictory things. They can coexist since both add value. But I have got no horse on this race so I digress. I hope you guys fix the issue though :)

@dcabrejas I'm 100% on board with what you are saying :)

Maybe the new report gets tagged as "wontfix", just wanted to make sure that it's still considered as something that is contained as a problem in the distributed code (as: php feature not used), but that it聽remains as something that is still perceived as work in progress and as know/open issue :)

Was this page helpful?
0 / 5 - 0 ratings