Skip to content

Commit

Permalink
Merge branch '1.3.x' into 1.4.x
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Nov 17, 2023
2 parents abf994a + cc63f87 commit aad14a4
Show file tree
Hide file tree
Showing 15 changed files with 45 additions and 36 deletions.
1 change: 1 addition & 0 deletions extension.neon
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ services:
class: PHPStan\Type\Doctrine\ObjectMetadataResolver
arguments:
objectManagerLoader: %doctrine.objectManagerLoader%
tmpDir: %tmpDir%
-
class: PHPStan\Type\Doctrine\QueryBuilder\QueryBuilderGetDqlDynamicReturnTypeExtension
arguments:
Expand Down
47 changes: 25 additions & 22 deletions src/Doctrine/Mapping/ClassMetadataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,30 @@

use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\DocParser;
use Doctrine\Common\EventManager;
use Doctrine\DBAL\Platforms\MySqlPlatform;
use Doctrine\DBAL\DriverManager;
use Doctrine\ORM\Configuration;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
use Doctrine\ORM\Mapping\Driver\AttributeDriver;
use ReflectionClass;
use Doctrine\ORM\Proxy\ProxyFactory;
use function class_exists;
use function count;
use const PHP_VERSION_ID;

class ClassMetadataFactory extends \Doctrine\ORM\Mapping\ClassMetadataFactory
{

protected function initialize(): void
/** @var string */
private $tmpDir;

public function __construct(string $tmpDir)
{
$parentReflection = new ReflectionClass(parent::class);
$driverProperty = $parentReflection->getProperty('driver');
$driverProperty->setAccessible(true);
$this->tmpDir = $tmpDir;
}

protected function initialize(): void
{
$drivers = [];
if (class_exists(AnnotationReader::class)) {
$docParser = new DocParser();
Expand All @@ -33,23 +38,21 @@ protected function initialize(): void
$drivers[] = new AttributeDriver([]);
}

$driverProperty->setValue($this, count($drivers) === 1 ? $drivers[0] : new MappingDriverChain($drivers));
$config = new Configuration();
$config->setMetadataDriverImpl(count($drivers) === 1 ? $drivers[0] : new MappingDriverChain($drivers));
$config->setAutoGenerateProxyClasses(ProxyFactory::AUTOGENERATE_FILE_NOT_EXISTS_OR_CHANGED);
$config->setProxyDir($this->tmpDir);
$config->setProxyNamespace('__PHPStanDoctrine__\\Proxy');
$connection = DriverManager::getConnection([
'driver' => 'pdo_sqlite',
'memory' => true,
], $config);

$evmProperty = $parentReflection->getProperty('evm');
$evmProperty->setAccessible(true);
$evmProperty->setValue($this, new EventManager());
$this->initialized = true;

$targetPlatformProperty = $parentReflection->getProperty('targetPlatform');
$targetPlatformProperty->setAccessible(true);
$em = new EntityManager($connection, $config);
$this->setEntityManager($em);
parent::initialize();

if (class_exists(MySqlPlatform::class)) {
$platform = new MySqlPlatform();
} else {
$platform = new \Doctrine\DBAL\Platforms\MySQLPlatform();
}

$targetPlatformProperty->setValue($this, $platform);
$this->initialized = true;
}

/**
Expand Down
9 changes: 7 additions & 2 deletions src/Type/Doctrine/ObjectMetadataResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,16 @@ final class ObjectMetadataResolver
/** @var ClassMetadataFactory|null */
private $metadataFactory;

/** @var string */
private $tmpDir;

public function __construct(
?string $objectManagerLoader
?string $objectManagerLoader,
string $tmpDir
)
{
$this->objectManagerLoader = $objectManagerLoader;
$this->tmpDir = $tmpDir;
}

public function hasObjectManagerLoader(): bool
Expand Down Expand Up @@ -97,7 +102,7 @@ private function getMetadataFactory(): ?ClassMetadataFactory
return null;
}

return $this->metadataFactory = new ClassMetadataFactory();
return $this->metadataFactory = new ClassMetadataFactory($this->tmpDir);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/Rules/Doctrine/ORM/DqlRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class DqlRuleTest extends RuleTestCase

protected function getRule(): Rule
{
return new DqlRule(new ObjectMetadataResolver(__DIR__ . '/entity-manager.php'));
return new DqlRule(new ObjectMetadataResolver(__DIR__ . '/entity-manager.php', __DIR__ . '/../../../../tmp'));
}

public function testRule(): void
Expand Down
2 changes: 1 addition & 1 deletion tests/Rules/Doctrine/ORM/EntityColumnRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ protected function getRule(): Rule
}

return new EntityColumnRule(
new ObjectMetadataResolver($this->objectManagerLoader),
new ObjectMetadataResolver($this->objectManagerLoader, __DIR__ . '/../../../../tmp'),
new DescriptorRegistry([
new ArrayType(),
new BigIntType(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class EntityConstructorNotFinalRuleTest extends RuleTestCase
protected function getRule(): Rule
{
return new EntityConstructorNotFinalRule(
new ObjectMetadataResolver($this->objectManagerLoader)
new ObjectMetadataResolver($this->objectManagerLoader, __DIR__ . '/../../../../tmp')
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class EntityMappingExceptionRuleTest extends RuleTestCase
protected function getRule(): Rule
{
return new EntityMappingExceptionRule(
new ObjectMetadataResolver(__DIR__ . '/entity-manager.php')
new ObjectMetadataResolver(__DIR__ . '/entity-manager.php', __DIR__ . '/../../../../tmp')
);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Rules/Doctrine/ORM/EntityNotFinalRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class EntityNotFinalRuleTest extends RuleTestCase
protected function getRule(): Rule
{
return new EntityNotFinalRule(
new ObjectMetadataResolver($this->objectManagerLoader)
new ObjectMetadataResolver($this->objectManagerLoader, __DIR__ . '/../../../../tmp')
);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Rules/Doctrine/ORM/EntityRelationRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class EntityRelationRuleTest extends RuleTestCase
protected function getRule(): Rule
{
return new EntityRelationRule(
new ObjectMetadataResolver($this->objectManagerLoader),
new ObjectMetadataResolver($this->objectManagerLoader, __DIR__ . '/../../../../tmp'),
$this->allowNullablePropertyForRequiredField,
true
);
Expand Down
2 changes: 1 addition & 1 deletion tests/Rules/Doctrine/ORM/QueryBuilderDqlRuleSlowTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class QueryBuilderDqlRuleSlowTest extends RuleTestCase
protected function getRule(): Rule
{
return new QueryBuilderDqlRule(
new ObjectMetadataResolver(__DIR__ . '/entity-manager.php'),
new ObjectMetadataResolver(__DIR__ . '/entity-manager.php', __DIR__ . '/../../../../tmp'),
self::getContainer()->getByType(OtherMethodQueryBuilderParser::class),
true,
true
Expand Down
2 changes: 1 addition & 1 deletion tests/Rules/Doctrine/ORM/QueryBuilderDqlRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class QueryBuilderDqlRuleTest extends RuleTestCase
protected function getRule(): Rule
{
return new QueryBuilderDqlRule(
new ObjectMetadataResolver(__DIR__ . '/entity-manager.php'),
new ObjectMetadataResolver(__DIR__ . '/entity-manager.php', __DIR__ . '/../../../../tmp'),
self::getContainer()->getByType(OtherMethodQueryBuilderParser::class),
true,
true
Expand Down
2 changes: 1 addition & 1 deletion tests/Rules/Doctrine/ORM/RepositoryMethodCallRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class RepositoryMethodCallRuleTest extends RuleTestCase

protected function getRule(): Rule
{
return new RepositoryMethodCallRule(new ObjectMetadataResolver(__DIR__ . '/entity-manager.php'));
return new RepositoryMethodCallRule(new ObjectMetadataResolver(__DIR__ . '/entity-manager.php', __DIR__ . '/../../../../tmp'));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class RepositoryMethodCallRuleWithoutObjectManagerLoaderTest extends RuleTestCas

protected function getRule(): Rule
{
return new RepositoryMethodCallRule(new ObjectMetadataResolver(null));
return new RepositoryMethodCallRule(new ObjectMetadataResolver(null, __DIR__ . '/../../../../tmp'));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ protected function getRule(): Rule
protected function getReadWritePropertiesExtensions(): array
{
return [
new PropertiesExtension(new ObjectMetadataResolver(__DIR__ . '/entity-manager.php')),
new PropertiesExtension(new ObjectMetadataResolver(__DIR__ . '/entity-manager.php', __DIR__ . '/../../../../tmp')),
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ protected function getRule(): Rule
protected function getReadWritePropertiesExtensions(): array
{
return [
new PropertiesExtension(new ObjectMetadataResolver(__DIR__ . '/entity-manager.php')),
new PropertiesExtension(new ObjectMetadataResolver(__DIR__ . '/entity-manager.php', __DIR__ . '/../../../../tmp')),
];
}

Expand Down

0 comments on commit aad14a4

Please sign in to comment.