Skip to content

Using virtual type to configure plugin, interceptor method cannot be generated correctly in setup:di:compile command #33980

@GrassH

Description

@GrassH

Preconditions (*)

  1. Magento CE 2.4.2 and above (Only tested these versions)

Steps to reproduce (*)

  1. Create a Magento 2 Module - Vendor_Module (Sample code can be found here)
  2. Configure a plugin:
<?xml version="1.0"?> <!-- /** * @package Vendor_Module */ --> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <virtualType name="Vendor\Module\Plugin\Sub" type="Vendor\Module\Plugin\Overload"> <arguments> <argument name="type" xsi:type="string">-</argument> </arguments> </virtualType> <type name="Vendor\Module\Model\Calculator"> <plugin name="Vendor_Module::change_to_sub" type="Vendor\Module\Plugin\Sub" /> </type> <type name="Magento\Framework\Console\CommandList"> <arguments> <argument name="commands" xsi:type="array"> <item name="test_plugin" xsi:type="object">Vendor\Module\Console\Command\TestCommand</item> </argument> </arguments> </type> </config> 

Vendor\Module\Plugin\Overload:

<?php /** * * @package Vendor_Module */ namespace Vendor\Module\Plugin; use Vendor\Module\Model\Calculator; /* For testing */ class Overload { /** * @var string */ protected $type; /** * @param string $type */ public function __construct($type = '+') { $this->type = $type; } /** * Modified to multiplication * * @param Calculator $subject * @param callable $procced * @param int|float $left * @param int|float $right * @return int|float */ public function aroundCalculate( Calculator $subject, callable $proceed, $left, $right ) { $result = null; switch ($this->type) { case '+': $result = $proceed($left, $right); break; case '-': $result = $left - $right; break; case '*': $result = $left * $right; break; case '/': $result = $left / $right; break; case '%': $result = $left % $right; break; } return $result; } } 

Vendor\Module\Model\Calculator:

<?php /** * * @package Vendor_Module */ namespace Vendor\Module\Model; /* For testing */ class Calculator { /** * @param int|float $left * @param int|float $right * @return int|float */ public function calculate($left, $right) { return $left + $right; } } 

Vendor\Module\Console\Command\TestCommand:

<?php /** * * @package Vendor_Module */ namespace Vendor\Module\Console\Command; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Vendor\Module\Model\Calculator; class TestCommand extends Command { /** * @var Calculator */ protected $calculator; /** * {@inheritdoc} */ public function __construct(Calculator $calculator, string $name = null) { $this->calculator = $calculator; parent::__construct($name); } /** * {@inheritdoc} */ protected function configure() { $this->setName('test:plugin:calculate') ->setDescription('Test Plugin'); parent::configure(); } /** * {@inheritdoc} */ protected function execute(InputInterface $input, OutputInterface $output) { try { $output->writeln($this->calculator->calculate(1, 1)); return \Magento\Framework\Console\Cli::RETURN_SUCCESS; } catch (\Exception $e) { $output->writeln('<error>' . $e->getMessage() . '</error>'); if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) { $output->writeln($e->getTraceAsString()); } return \Magento\Framework\Console\Cli::RETURN_FAILURE; } } } 
  1. Remove files in the generated and execute command bin/magento setup:di:compile
  2. Execute test command bin/magento test:plugin:calculate
    Result is 2
  3. Remove files in the generated and execute command bin/magento test:plugin:calculate
    Result is 0

Expected result (*)

  1. Precompilation or runtime compilation, the result of execution should be 0.

Actual result (*)

  1. When precompiled, the result is 2
  2. When runtime compilation, the result is 0

Please provide Severity assessment for the Issue as Reporter. This information will help during Confirmation and Issue triage processes.

  • Severity: S0 - Affects critical data or functionality and leaves users without workaround.
  • Severity: S1 - Affects critical data or functionality and forces users to employ a workaround.
  • Severity: S2 - Affects non-critical data or functionality and forces users to employ a workaround.
  • Severity: S3 - Affects non-critical data or functionality and does not force users to employ a workaround.
  • Severity: S4 - Affects aesthetics, professional look and feel, “quality” or “usability”.

Metadata

Metadata

Assignees

Labels

Area: Other Developer ToolsComponent: OtherIssue: ConfirmedGate 3 Passed. Manual verification of the issue completed. Issue is confirmedPriority: P3May be fixed according to the position in the backlog.Progress: doneReproduced on 2.4.xThe issue has been reproduced on latest 2.4-develop branchSeverity: S1Affects critical data or functionality and forces users to employ a workaround.Triage: Dev.ExperienceIssue related to Developer Experience and needs help with Triage to Confirm or Reject it

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions