Skip to content

di:setup:compile fails with anonymous classes #26532

@YevSent

Description

@YevSent

Preconditions (*)

The current implementation of \Magento\Setup\Module\Di\Code\Scanner\PhpScanner does not support classes that contain anonymous classes. This leads to failures for di:setup:compile command. The problem that PhpScanner treats anonymous classes as classes that might require code generation for factories and extension attributes.

All Magento versions have this issue.

Steps to reproduce (*)

  1. Create a dummy class which contains an anonymous class, like Magento\DummyModule\Dummy (see a sample below)
  2. Run bin/magento setup:di:compile CLI command

Expected result (*)

  1. Compilation is successful

Actual result (*)

  1. Compilation fails with an error Class \Magento\DummyModule\$arg does not exist

Sample class:

namespace Magento\DummyModule;

class Dummy
{
    public function execute($arg)
    {
        return new class($arg) {
            private $arg;

            public function __construct($arg)
            {
                $this->arg = $arg;
            }

            public function serialize()
            {
                return (string) $this->arg;
            }
        };
    }
}

To achieve the same error as described in #21555, the anonymous class should not provide a possibility to specify arguments for a constructor. In simple words, return new class {.

Metadata

Metadata

Assignees

Labels

Component: Framework/SetupUSE ONLY for FRAMEWORK RELATED BUG! E.g If bug occurs during Catalog Setup use just CatalogFixed in 2.4.xThe issue has been fixed in 2.4-develop branchIssue: Format is validGate 1 Passed. Automatic verification of issue format passed

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions