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

Adapt to new type hierarchy #776

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions src/Service/DriverFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,19 @@
namespace DoctrineModule\Service;

use Doctrine\Common\Annotations;
use Doctrine\ORM\Mapping\Driver\AttributeDriver;
use Doctrine\Persistence\Mapping\Driver\AnnotationDriver;
use Doctrine\Persistence\Mapping\Driver\DefaultFileLocator;
use Doctrine\Persistence\Mapping\Driver\FileDriver;
use Doctrine\Persistence\Mapping\Driver\MappingDriver;
use Doctrine\Persistence\Mapping\Driver\MappingDriverChain;
use DoctrineModule\Options\Driver;
use Interop\Container\ContainerInterface;
use InvalidArgumentException;
use ReflectionClass;
use RuntimeException;

use function class_exists;
use function get_class;
use function interface_exists;
use function is_subclass_of;
use function sprintf;

/**
Expand Down Expand Up @@ -70,10 +68,10 @@ protected function createDriver(ContainerInterface $container, Driver $options):
// Not all drivers (DriverChain) require paths.
$paths = $options->getPaths();

$constructor = (new ReflectionClass($class))->getConstructor();
// Special options for AnnotationDrivers.
if (
$class !== AttributeDriver::class &&
($class === AnnotationDriver::class || is_subclass_of($class, AnnotationDriver::class))
$constructor !== null && $constructor->getNumberOfParameters() === 2
) {
$reader = new Annotations\AnnotationReader();
$reader = new Annotations\CachedReader(
Expand Down
28 changes: 28 additions & 0 deletions tests/Service/DriverFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace DoctrineModuleTest\Service;

use Doctrine\Common\Annotations\Reader;
use Doctrine\Common\Cache\ArrayCache;
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
use Doctrine\ORM\Mapping\Driver\AttributeDriver;
use Doctrine\Persistence\Mapping\Driver\MappingDriverChain;
use DoctrineModule\Service\DriverFactory;
Expand Down Expand Up @@ -90,4 +93,29 @@ public function testCreateAttributeDriver(): void
$driver = $factory->__invoke($serviceManager, AttributeDriver::class);
$this->assertInstanceOf(AttributeDriver::class, $driver);
}

public function testCreateAnnotationDriver(): void
{
$serviceManager = new ServiceManager();
$serviceManager->setService(
'config',
[
'doctrine' => [
'driver' => [
'testDriver' => ['class' => AnnotationDriver::class],
],
],
]
);
$serviceManager->setService(
'doctrine.cache.array',
new ArrayCache()
);

$factory = new DriverFactory('testDriver');
$driver = $factory->__invoke($serviceManager, AnnotationDriver::class);
$this->assertInstanceOf(AnnotationDriver::class, $driver);
assert($driver instanceof AnnotationDriver);
Copy link
Member Author

Choose a reason for hiding this comment

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

I don't understand why this is necessary, but it is. PHPstan and phpstan-phpunit are installed and up to date, yet $driver is not narrowed down to AnnotationDriver.

Copy link

Choose a reason for hiding this comment

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

I could have solved that with a simple comment:

/** @var AnnotationDriverORM|AnnotationDriverODM|AnnotationDriverPersistence $driver */
$driver  = $factory->__invoke($serviceManager, AnnotationDriver::class);

but PHPCS forced me to do the assert(...). This seemed to be the only way to make both tools happy.

Copy link
Member Author

Choose a reason for hiding this comment

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

I would expect phpstan not to need this thanks to line 117. I think that works in my other projects, so there must be a configuration issue somewhere. It's mentioned in this README: https://github.com/phpstan/phpstan-phpunit

Copy link
Member Author

Choose a reason for hiding this comment

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

Found the reason: we were only using that extension to add extra rules, not to add functionality.

$this->assertInstanceOf(Reader::class, $driver->getReader());
}
}