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

Internal Error : The class ... was not found in the chain configured namespaces #445

Open
amalricBzh opened this issue Apr 1, 2023 · 8 comments

Comments

@amalricBzh
Copy link

amalricBzh commented Apr 1, 2023

Bug report

With lastest Phpstan version, on an up-to-date Symfony 6.2/PHP 8.1 project, I run: php -d memory_limit=-1 vendor/bin/phpstan analyse src --level=5 -v

It says me : Post the following stack trace to https://github.com/phpstan/phpstan/issues/new?template=Bug_report.md:

So this is the stacktrace:

Internal error: Internal error: The class 'App\Entity\Spip\AuteurLien' was not found in the chain configured namespaces App\Entity\Tud in file /var/www/src/Repository/Spip/AuteurLienRepository.php

     Post the following stack trace to https://github.com/phpstan/phpstan/issues/new?template=Bug_report.md:
     #0 /var/www/vendor/doctrine/persistence/src/Persistence/Mapping/Driver/MappingDriverChain.php(89): Doctrine\Persistence\Mapping\MappingException::classNotFoundInNamespaces('App\\Entity\\Spip...', Array)
     phpstan/phpstan#1 /var/www/vendor/doctrine/doctrine-bundle/Mapping/MappingDriver.php(42): Doctrine\Persistence\Mapping\Driver\MappingDriverChain->loadMetadataForClass('App\\Entity\\Spip...',
     Object(Doctrine\ORM\Mapping\ClassMetadata))
     phpstan/phpstan#2 /var/www/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php(132): Doctrine\Bundle\DoctrineBundle\Mapping\MappingDriver->loadMetadataForClass('App\\Entity\\Spip...',
     Object(Doctrine\ORM\Mapping\ClassMetadata))
     phpstan/phpstan#3 /var/www/vendor/doctrine/doctrine-bundle/Mapping/ClassMetadataFactory.php(18): Doctrine\ORM\Mapping\ClassMetadataFactory->doLoadMetadata(Object(Doctrine\ORM\Mapping\ClassMetadata), NULL, false, Array)
     phpstan/phpstan#4 /var/www/vendor/doctrine/persistence/src/Persistence/Mapping/AbstractClassMetadataFactory.php(343):
     Doctrine\Bundle\DoctrineBundle\Mapping\ClassMetadataFactory->doLoadMetadata(Object(Doctrine\ORM\Mapping\ClassMetadata), NULL, false, Array)
     phpstan/phpstan#5 /var/www/vendor/doctrine/persistence/src/Persistence/Mapping/AbstractClassMetadataFactory.php(207): Doctrine\Persistence\Mapping\AbstractClassMetadataFactory->loadMetadata('App\\Entity\\Spip...')
     phpstan/phpstan#6 /var/www/vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php(318): Doctrine\Persistence\Mapping\AbstractClassMetadataFactory->getMetadataFor('App\\Entity\\Spip...')
     phpstan/phpstan#7 /var/www/vendor/doctrine/orm/lib/Doctrine/ORM/Query/Parser.php(1782): Doctrine\ORM\EntityManager->getClassMetadata('App\\Entity\\Spip...')
     phpstan/phpstan#8 /var/www/vendor/doctrine/orm/lib/Doctrine/ORM/Query/Parser.php(1620): Doctrine\ORM\Query\Parser->RangeVariableDeclaration()
     phpstan/phpstan#9 /var/www/vendor/doctrine/orm/lib/Doctrine/ORM/Query/Parser.php(1350): Doctrine\ORM\Query\Parser->IdentificationVariableDeclaration()
     phpstan/phpstan#10 /var/www/vendor/doctrine/orm/lib/Doctrine/ORM/Query/Parser.php(880): Doctrine\ORM\Query\Parser->FromClause()
     phpstan/phpstan#11 /var/www/vendor/doctrine/orm/lib/Doctrine/ORM/Query/Parser.php(849): Doctrine\ORM\Query\Parser->SelectStatement()
     phpstan/phpstan#12 /var/www/vendor/doctrine/orm/lib/Doctrine/ORM/Query/Parser.php(256): Doctrine\ORM\Query\Parser->QueryLanguage()
     phpstan/phpstan#13 /var/www/vendor/doctrine/orm/lib/Doctrine/ORM/Query/Parser.php(356): Doctrine\ORM\Query\Parser->getAST()
     phpstan/phpstan#14 /var/www/vendor/phpstan/phpstan-doctrine/src/Type/Doctrine/Query/QueryResultTypeWalker.php(119): Doctrine\ORM\Query\Parser->parse()
     phpstan/phpstan#15 /var/www/vendor/phpstan/phpstan-doctrine/src/Type/Doctrine/QueryBuilder/QueryBuilderGetQueryDynamicReturnTypeExtension.php(164):
     PHPStan\Type\Doctrine\Query\QueryResultTypeWalker::walk(Object(Doctrine\ORM\Query), Object(PHPStan\Type\Doctrine\Query\QueryResultTypeBuilder), Object(PHPStan\Type\Doctrine\DescriptorRegistry))
     phpstan/phpstan#16 /var/www/vendor/phpstan/phpstan-doctrine/src/Type/Doctrine/QueryBuilder/QueryBuilderGetQueryDynamicReturnTypeExtension.php(147):
     PHPStan\Type\Doctrine\QueryBuilder\QueryBuilderGetQueryDynamicReturnTypeExtension->getQueryType('SELECT a FROM A...')
     phpstan/phpstan#17 phar:///var/www/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/MutatingScope.php(3344):
     PHPStan\Type\Doctrine\QueryBuilder\QueryBuilderGetQueryDynamicReturnTypeExtension->getTypeFromMethodCall(Object(PHPStan\Reflection\ResolvedMethodReflection), Object(PhpParser\Node\Expr\MethodCall),
     Object(PHPStan\Analyser\MutatingScope))
     phpstan/phpstan#18 phar:///var/www/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/MutatingScope.php(1333):
     PHPStan\Analyser\MutatingScope->methodCallReturnType(Object(PHPStan\Type\Doctrine\QueryBuilder\BranchingQueryBuilderType), 'getQuery', Object(PhpParser\Node\Expr\MethodCall))
     phpstan/phpstan#19 phar:///var/www/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/MutatingScope.php(1339): PHPStan\Analyser\MutatingScope->PHPStan\Analyser\{closure}()
     phpstan/phpstan#20 phar:///var/www/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/MutatingScope.php(556): PHPStan\Analyser\MutatingScope->resolveType('$this->createQu...', Object(PhpParser\Node\Expr\MethodCall))
     phpstan/phpstan#21 phar:///var/www/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/MutatingScope.php(1333): PHPStan\Analyser\MutatingScope->getType(Object(PhpParser\Node\Expr\MethodCall))
     phpstan/phpstan#22 phar:///var/www/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/MutatingScope.php(1339): PHPStan\Analyser\MutatingScope->PHPStan\Analyser\{closure}()
     phpstan/phpstan#23 phar:///var/www/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/MutatingScope.php(556): PHPStan\Analyser\MutatingScope->resolveType('$this->createQu...', Object(PhpParser\Node\Expr\MethodCall))
     phpstan/phpstan#24 phar:///var/www/vendor/phpstan/phpstan/phpstan.phar/src/Rules/FunctionReturnTypeCheck.php(53): PHPStan\Analyser\MutatingScope->getType(Object(PhpParser\Node\Expr\MethodCall))
     phpstan/phpstan#25 phar:///var/www/vendor/phpstan/phpstan/phpstan.phar/src/Rules/Methods/ReturnTypeRule.php(43): PHPStan\Rules\FunctionReturnTypeCheck->checkReturnType(Object(PHPStan\Analyser\MutatingScope),
     Object(PHPStan\Type\ArrayType), Object(PhpParser\Node\Expr\MethodCall), Object(PhpParser\Node\Stmt\Return_), 'Method App\\Repo...', 'Method App\\Repo...', 'Method App\\Repo...', 'Method App\\Repo...', false)
     phpstan/phpstan#26 phar:///var/www/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/FileAnalyser.php(106): PHPStan\Rules\Methods\ReturnTypeRule->processNode(Object(PhpParser\Node\Stmt\Return_),
     Object(PHPStan\Analyser\MutatingScope))
     phpstan/phpstan#27 phar:///var/www/vendor/phpstan/phpstan/phpstan.phar/src/Node/ClassStatementsGatherer.php(98): PHPStan\Analyser\FileAnalyser->PHPStan\Analyser\{closure}(Object(PhpParser\Node\Stmt\Return_),
     Object(PHPStan\Analyser\MutatingScope))
     phpstan/phpstan#28 phar:///var/www/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/NodeScopeResolver.php(508): PHPStan\Node\ClassStatementsGatherer->__invoke(Object(PhpParser\Node\Stmt\Return_),
     Object(PHPStan\Analyser\MutatingScope))
     phpstan/phpstan#29 phar:///var/www/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/NodeScopeResolver.php(417): PHPStan\Analyser\NodeScopeResolver::PHPStan\Analyser\{closure}(Object(PhpParser\Node\Stmt\Return_),
     Object(PHPStan\Analyser\MutatingScope))
     phpstan/phpstan#30 phar:///var/www/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/NodeScopeResolver.php(362): PHPStan\Analyser\NodeScopeResolver->processStmtNode(Object(PhpParser\Node\Stmt\Return_),
     Object(PHPStan\Analyser\MutatingScope), Object(Closure), Object(PHPStan\Analyser\StatementContext))
     phpstan/phpstan#31 phar:///var/www/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/NodeScopeResolver.php(523): PHPStan\Analyser\NodeScopeResolver->processStmtNodes(Object(PhpParser\Node\Stmt\ClassMethod), Array,
     Object(PHPStan\Analyser\MutatingScope), Object(Closure), Object(PHPStan\Analyser\StatementContext))
     phpstan/phpstan#32 phar:///var/www/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/NodeScopeResolver.php(362): PHPStan\Analyser\NodeScopeResolver->processStmtNode(Object(PhpParser\Node\Stmt\ClassMethod),
     Object(PHPStan\Analyser\MutatingScope), Object(PHPStan\Node\ClassStatementsGatherer), Object(PHPStan\Analyser\StatementContext))
     phpstan/phpstan#33 phar:///var/www/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/NodeScopeResolver.php(601): PHPStan\Analyser\NodeScopeResolver->processStmtNodes(Object(PhpParser\Node\Stmt\Class_), Array,
     Object(PHPStan\Analyser\MutatingScope), Object(PHPStan\Node\ClassStatementsGatherer), Object(PHPStan\Analyser\StatementContext))
     phpstan/phpstan#34 phar:///var/www/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/NodeScopeResolver.php(362): PHPStan\Analyser\NodeScopeResolver->processStmtNode(Object(PhpParser\Node\Stmt\Class_),
     Object(PHPStan\Analyser\MutatingScope), Object(Closure), Object(PHPStan\Analyser\StatementContext))
     phpstan/phpstan#35 phar:///var/www/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/NodeScopeResolver.php(573): PHPStan\Analyser\NodeScopeResolver->processStmtNodes(Object(PhpParser\Node\Stmt\Namespace_), Array,
     Object(PHPStan\Analyser\MutatingScope), Object(Closure), Object(PHPStan\Analyser\StatementContext))
     phpstan/phpstan#36 phar:///var/www/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/NodeScopeResolver.php(331): PHPStan\Analyser\NodeScopeResolver->processStmtNode(Object(PhpParser\Node\Stmt\Namespace_),
     Object(PHPStan\Analyser\MutatingScope), Object(Closure), Object(PHPStan\Analyser\StatementContext))
     phpstan/phpstan#37 phar:///var/www/vendor/phpstan/phpstan/phpstan.phar/src/Analyser/FileAnalyser.php(175): PHPStan\Analyser\NodeScopeResolver->processNodes(Array, Object(PHPStan\Analyser\MutatingScope), Object(Closure))
     phpstan/phpstan#38 phar:///var/www/vendor/phpstan/phpstan/phpstan.phar/src/Command/WorkerCommand.php(130): PHPStan\Analyser\FileAnalyser->analyseFile('/var/www/src/Re...', Array, Object(PHPStan\Rules\LazyRegistry),
     Object(PHPStan\Collectors\Registry), NULL)
     phpstan/phpstan#39 phar:///var/www/vendor/phpstan/phpstan/phpstan.phar/vendor/evenement/evenement/src/Evenement/EventEmitterTrait.php(97): PHPStan\Command\WorkerCommand->PHPStan\Command\{closure}(Array)
     phpstan/phpstan#40 phar:///var/www/vendor/phpstan/phpstan/phpstan.phar/vendor/clue/ndjson-react/src/Decoder.php(110): _PHPStan_53d0d2174\Evenement\EventEmitter->emit('data', Array)
     phpstan/phpstan#41 phar:///var/www/vendor/phpstan/phpstan/phpstan.phar/vendor/evenement/evenement/src/Evenement/EventEmitterTrait.php(97): _PHPStan_53d0d2174\Clue\React\NDJson\Decoder->handleData(Array)
     phpstan/phpstan#42 phar:///var/www/vendor/phpstan/phpstan/phpstan.phar/vendor/react/stream/src/Util.php(62): _PHPStan_53d0d2174\Evenement\EventEmitter->emit('data', Array)
     phpstan/phpstan#43 phar:///var/www/vendor/phpstan/phpstan/phpstan.phar/vendor/evenement/evenement/src/Evenement/EventEmitterTrait.php(97):
     _PHPStan_53d0d2174\React\Stream\Util::_PHPStan_53d0d2174\React\Stream\{closure}('{"action":"anal...')
     phpstan/phpstan#44 phar:///var/www/vendor/phpstan/phpstan/phpstan.phar/vendor/react/stream/src/DuplexResourceStream.php(154): _PHPStan_53d0d2174\Evenement\EventEmitter->emit('data', Array)
     phpstan/phpstan#45 phar:///var/www/vendor/phpstan/phpstan/phpstan.phar/vendor/react/event-loop/src/StreamSelectLoop.php(201): _PHPStan_53d0d2174\React\Stream\DuplexResourceStream->handleData(Resource id phpstan/phpstan#3647)
     phpstan/phpstan#46 phar:///var/www/vendor/phpstan/phpstan/phpstan.phar/vendor/react/event-loop/src/StreamSelectLoop.php(173): _PHPStan_53d0d2174\React\EventLoop\StreamSelectLoop->waitForStreamActivity(NULL)
     phpstan/phpstan#47 phar:///var/www/vendor/phpstan/phpstan/phpstan.phar/src/Command/WorkerCommand.php(96): _PHPStan_53d0d2174\React\EventLoop\StreamSelectLoop->run()
     phpstan/phpstan#48 phar:///var/www/vendor/phpstan/phpstan/phpstan.phar/vendor/symfony/console/Command/Command.php(259):
     PHPStan\Command\WorkerCommand->execute(Object(_PHPStan_53d0d2174\Symfony\Component\Console\Input\ArgvInput), Object(_PHPStan_53d0d2174\Symfony\Component\Console\Output\ConsoleOutput))
     phpstan/phpstan#49 phar:///var/www/vendor/phpstan/phpstan/phpstan.phar/vendor/symfony/console/Application.php(870):
     _PHPStan_53d0d2174\Symfony\Component\Console\Command\Command->run(Object(_PHPStan_53d0d2174\Symfony\Component\Console\Input\ArgvInput), Object(_PHPStan_53d0d2174\Symfony\Component\Console\Output\ConsoleOutput))
     phpstan/phpstan#50 phar:///var/www/vendor/phpstan/phpstan/phpstan.phar/vendor/symfony/console/Application.php(261): _PHPStan_53d0d2174\Symfony\Component\Console\Application->doRunCommand(Object(PHPStan\Command\WorkerCommand),
     Object(_PHPStan_53d0d2174\Symfony\Component\Console\Input\ArgvInput), Object(_PHPStan_53d0d2174\Symfony\Component\Console\Output\ConsoleOutput))
     phpstan/phpstan#51 phar:///var/www/vendor/phpstan/phpstan/phpstan.phar/vendor/symfony/console/Application.php(157):
     _PHPStan_53d0d2174\Symfony\Component\Console\Application->doRun(Object(_PHPStan_53d0d2174\Symfony\Component\Console\Input\ArgvInput), Object(_PHPStan_53d0d2174\Symfony\Component\Console\Output\ConsoleOutput))
     phpstan/phpstan#52 phar:///var/www/vendor/phpstan/phpstan/phpstan.phar/bin/phpstan(124): _PHPStan_53d0d2174\Symfony\Component\Console\Application->run()
     phpstan/phpstan#53 phar:///var/www/vendor/phpstan/phpstan/phpstan.phar/bin/phpstan(125): _PHPStan_53d0d2174\{closure}()
     phpstan/phpstan#54 /var/www/vendor/phpstan/phpstan/phpstan(8): require('phar:///var/www...')
     phpstan/phpstan#55 /var/www/vendor/bin/phpstan(120): include('/var/www/vendor...')
     phpstan/phpstan#56 {main}

My doctrine conf:

    orm:
        auto_generate_proxy_classes: true
        default_entity_manager: default
        entity_managers:
            default:
                connection: tud
                mappings:
                    Tud:
                        is_bundle: false
                        dir: '%kernel.project_dir%/src/Entity/Tud'
                        prefix: 'App\Entity\Tud'
                        alias: Tud
                naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
            spip:
                connection: spip
                mappings:
                    Spip:
                        is_bundle: false
                        dir: '%kernel.project_dir%/src/Entity/Spip'
                        prefix: 'App\Entity\Spip'
                        alias: Spip
                naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware

Code snippet that reproduces the problem

The clas AuteurLien is very simple and never reference to namespace App\Entity\Tud:

<?php

namespace App\Entity\Spip;

use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity(repositoryClass: \App\Repository\Spip\AuteurLienRepository::class, readOnly: true)]
#[ORM\Table(name: 'spip_auteurs_liens')]
class AuteurLien
{
    #[ORM\Id]
    #[ORM\Column(name: 'id_auteur', type: 'integer')]
    private readonly int $idAuteur;
    #[ORM\Column(name: 'id_objet', type: 'integer')]
    private readonly int $idObjet;
    #[ORM\Column(type: 'string')]
    private readonly string $objet;

    public function __construct()
    {
        $this->idAuteur = 0;
        $this->idObjet = 0;
        $this->objet = '';
    }

    public function getIdAuteur(): int
    {
        return $this->idAuteur;
    }

    public function getIdObjet(): int
    {
        return $this->idObjet;
    }

    public function getObjet(): string
    {
        return $this->objet;
    }
}

@mergeable
Copy link

mergeable bot commented Apr 1, 2023

This bug report is missing a link to reproduction at phpstan.org/try.

It will most likely be closed after manual review.

@amalricBzh amalricBzh changed the title Internal Error : Ten class ... was not found in the chain configured namespaces Internal Error : The class ... was not found in the chain configured namespaces Apr 1, 2023
@amalricBzh
Copy link
Author

I tried to use phpstan.org/try. but it cannot handle several files...

@ondrejmirtes
Copy link
Member

  1. Have you set up objectManagerLoader?
  2. Looks like your Doctrine is misconfigured. You're referencing App\Entity\Spip\AuteurLien in your DQL but your MappingDriverChain is set up only for App\Entity\Tud namespace.

@amalricBzh
Copy link
Author

  1. Yes, this is how I did it:
<?php
// For phpstan. See https://github.com/phpstan/phpstan-doctrine#configuration
use App\Kernel;
use Symfony\Component\Dotenv\Dotenv;

require __DIR__ . '/../vendor/autoload.php';

(new Dotenv())->bootEnv(__DIR__ . '/../.env');

$kernel = new Kernel($_SERVER['APP_ENV'], (bool)$_SERVER['APP_DEBUG']);
$kernel->boot();
return $kernel->getContainer()->get('doctrine')->getManager();

It is referenced in phpstan.neon:

parameters:
	inferPrivatePropertyTypeFromConstructor: true
	doctrine:
		objectManagerLoader: phpstan/object-manager.php
	paths:
		- src

I tried with this following file => same result:

<?php declare(strict_types = 1);

use App\Kernel;

require dirname(__DIR__) . '/config/bootstrap.php';
$kernel = new Kernel($_SERVER['APP_ENV'], (bool) $_SERVER['APP_DEBUG']);
$kernel->boot();
return $kernel->getContainer()->get('doctrine')->getManager();
  1. I followed https://symfony.com/doc/current/doctrine/multiple_entity_managers.html because I have two databases, The application works fine on production since several years (it is updated constantly). I don't see MappingDriverChaininformation on the Symfony docs. Where can I find information on how to configure it ?

@ondrejmirtes ondrejmirtes transferred this issue from phpstan/phpstan Apr 5, 2023
@fsevestre
Copy link

Hello, I have exactly the same issue. I guess this is caused by the fact that there are multiple Doctrine entity manager (I'm working on a DDD app where I separate each aggregate in his own entity manager), and the $kernel->getContainer()->get('doctrine')->getManager() call in the object-manager.php will return the default entity manager. I didn't find any solution at the moment :(

@sasa-b
Copy link

sasa-b commented Oct 18, 2023

Yep, I've got the same issue, two databases and two entity managers setup, one MySQL and one Postgres

@amalricBzh
Copy link
Author

Today, Symfony 6.4/7.0 ; phpstan 1.10.50 ; php 8.2 => the issue is still here 😢.
Any hope this can be fixed ?

@fsevestre
Copy link

I used the code from this comment: #53 (comment)
and it's working fine for me so far. The project were I needed that is still small, so I don't know if it's working for all the cases.

I updated it a little, just so phpstan, psalm and rector don't complain 😅 (mostly 8.1+ stuff and some missing types) but all the credits goes to @adrienbrault :

<?php

use App\Kernel;
use Doctrine\Bundle\DoctrineBundle\Registry;
use Doctrine\Persistence\Mapping\ClassMetadata;
use Doctrine\Persistence\Mapping\ClassMetadataFactory;
use Doctrine\Persistence\ObjectManager;
use Doctrine\Persistence\ObjectRepository;
use Symfony\Component\Dotenv\Dotenv;

require __DIR__.'/../vendor/autoload.php';

(new Dotenv())->bootEnv(__DIR__.'/../.env');

$kernel = new Kernel($_SERVER['APP_ENV'], (bool) $_SERVER['APP_DEBUG']);
$kernel->boot();

$doctrine = $kernel->getContainer()->get('doctrine');

$metadataFactory = new class($doctrine) implements ClassMetadataFactory {
    private readonly Registry $doctrine;

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

    public function getAllMetadata(): array
    {
        $all = [];

        foreach ($this->doctrine->getManagers() as $manager) {
            $all = [...$all, ...$manager->getMetadataFactory()->getAllMetadata()];
        }

        return $all;
    }

    public function getMetadataFor(string $className): ClassMetadata
    {
        $manager = $this->doctrine->getManagerForClass($className);
        if (!$manager instanceof ObjectManager) {
            throw new \Exception(__FILE__);
        }

        return $manager->getClassMetadata($className);
    }

    public function isTransient(string $className): bool
    {
        $isTransient = true;

        foreach ($this->doctrine->getManagers() as $manager) {
            $isTransient = $isTransient && $manager->getMetadataFactory()->isTransient($className);
        }

        return $isTransient;
    }

    public function hasMetadataFor(string $className): bool
    {
        $hasMetadata = false;

        foreach ($this->doctrine->getManagers() as $manager) {
            $hasMetadata = $hasMetadata || $manager->getMetadataFactory()->hasMetadataFor($className);
        }

        return $hasMetadata;
    }

    public function setMetadataFor(string $className, ClassMetadata $class): void
    {
        throw new \Exception(__FILE__);
    }
};

return new class($doctrine, $metadataFactory) implements ObjectManager {
    public function __construct(
        private readonly Registry $doctrine,
        private readonly ClassMetadataFactory $metadataFactory
    ) {
    }

    public function getRepository(string $className): ObjectRepository
    {
        return $this->doctrine->getRepository($className);
    }

    public function getClassMetadata(string $className): ClassMetadata
    {
        $manager = $this->doctrine->getManagerForClass($className);
        if (!$manager instanceof ObjectManager) {
            throw new \Exception(__FILE__);
        }

        return $manager->getClassMetadata($className);
    }

    public function getMetadataFactory(): ClassMetadataFactory
    {
        return $this->metadataFactory;
    }

    public function find(string $className, mixed $id): ?object
    {
        throw new \Exception(__FILE__);
    }

    public function persist(object $object): void
    {
        throw new \Exception(__FILE__);
    }

    public function remove(object $object): void
    {
        throw new \Exception(__FILE__);
    }

    public function clear(): void
    {
        throw new \Exception(__FILE__);
    }

    public function detach(object $object): void
    {
        throw new \Exception(__FILE__);
    }

    public function refresh(object $object): void
    {
        throw new \Exception(__FILE__);
    }

    public function flush(): void
    {
        throw new \Exception(__FILE__);
    }

    public function initializeObject(object $obj): void
    {
        throw new \Exception(__FILE__);
    }

    public function contains(object $object): bool
    {
        throw new \Exception(__FILE__);
    }
};

If someone as a large project to test it, fix it (or give feedback), we can probably add this to the README for others?

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

No branches or pull requests

4 participants