Skip to content

Commit

Permalink
psalm 4.x -> 5.x
Browse files Browse the repository at this point in the history
  • Loading branch information
ostrolucky committed Nov 15, 2023
1 parent 8c42712 commit eb96246
Show file tree
Hide file tree
Showing 24 changed files with 86 additions and 107 deletions.
2 changes: 2 additions & 0 deletions ConnectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public function createConnection(array $params, ?Configuration $config = null, ?
}

$overriddenOptions = [];
/** @psalm-suppress InvalidArrayOffset We should adjust when https://github.com/vimeo/psalm/issues/8984 is fixed */
if (isset($params['connection_override_options'])) {
trigger_deprecation('doctrine/doctrine-bundle', '2.4', 'The "connection_override_options" connection parameter is deprecated');
$overriddenOptions = $params['connection_override_options'];
Expand All @@ -97,6 +98,7 @@ public function createConnection(array $params, ?Configuration $config = null, ?
}
}

/** @psalm-suppress InvalidArrayOffset We should adjust when https://github.com/vimeo/psalm/issues/8984 is fixed */
if (! isset($params['pdo']) && (! isset($params['charset']) || $overriddenOptions || isset($params['dbname_suffix']))) {
$wrapperClass = null;

Expand Down
8 changes: 4 additions & 4 deletions DataCollector/DoctrineDataCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
* regions: array<"puts"|"hits"|"misses", array<string, int>>,
* },
* connections: list<string>,
* entities: array<string, array<class-string, class-string>>,
* entities: array<string, array<class-string, array{class: class-string, file: false|string, line: false|int}>>,
* errors: array<string, array<class-string, list<string>>>,
* managers: list<string>,
* queries: array<string, list<QueryType>>,
Expand All @@ -55,7 +55,7 @@ class DoctrineDataCollector extends BaseCollector

/**
* @var mixed[][]|null
* @psalm-var ?array<string, list<QueryType&array{count: int, index: int, executionPercent: float}>>
* @psalm-var ?array<string, list<QueryType&array{count: int, index: int, executionPercent?: float}>>
*/
private ?array $groupedQueries = null;

Expand Down Expand Up @@ -184,7 +184,7 @@ public function collect(Request $request, Response $response, ?Throwable $except
$this->groupedQueries = null;
}

/** @return array<string, array<string, string>> */
/** @return array<string, array<class-string, array{class: class-string, file: false|string, line: false|int}>> */
public function getEntities()
{
return $this->data['entities'];
Expand Down Expand Up @@ -243,7 +243,7 @@ public function getInvalidEntityCount()

/**
* @return string[][]
* @psalm-return array<string, list<QueryType&array{count: int, index: int, executionPercent: float}>>
* @psalm-return array<string, list<QueryType&array{count: int, index: int, executionPercent?: float}>>
*/
public function getGroupedQueries()
{
Expand Down
2 changes: 1 addition & 1 deletion DependencyInjection/Compiler/EntityListenerPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public function process(ContainerBuilder $container)
}
}

/** @param array{entity: class-string, event?: ?string} $attributes */
/** @param array{entity: class-string, event?: ?string, method?: string} $attributes */
private function attachToListener(ContainerBuilder $container, string $name, string $class, array $attributes): void
{
$listenerId = sprintf('doctrine.orm.%s_listeners.attach_entity_listeners', $name);
Expand Down
4 changes: 4 additions & 0 deletions DependencyInjection/DoctrineExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ protected function dbalLoad(array $config, ContainerBuilder $container)
$connections = [];

foreach (array_keys($config['connections']) as $name) {
/** @psalm-suppress InvalidArrayOffset */
$connections[$name] = sprintf('doctrine.dbal.%s_connection', $name);
}

Expand Down Expand Up @@ -533,6 +534,7 @@ protected function ormLoad(array $config, ContainerBuilder $container)

$entityManagers = [];
foreach (array_keys($config['entity_managers']) as $name) {
/** @psalm-suppress InvalidArrayOffset */
$entityManagers[$name] = sprintf('doctrine.orm.%s_entity_manager', $name);
}

Expand Down Expand Up @@ -838,10 +840,12 @@ protected function loadOrmEntityManagerMappingInformation(array $entityManager,
$this->registerMappingDrivers($entityManager, $container);

$container->getDefinition($this->getObjectManagerElementName($entityManager['name'] . '_metadata_driver'));
/** @psalm-suppress NoValue $this->drivers is set by $this->loadMappingInformation() call */
foreach (array_keys($this->drivers) as $driverType) {
$mappingService = $this->getObjectManagerElementName($entityManager['name'] . '_' . $driverType . '_metadata_driver');
$mappingDriverDef = $container->getDefinition($mappingService);
$args = $mappingDriverDef->getArguments();
/** @psalm-suppress TypeDoesNotContainType $this->drivers is set by $this->loadMappingInformation() call */
if ($driverType === 'annotation') {
$args[2] = $entityManager['report_fields_where_declared'];
} elseif ($driverType === 'attribute') {
Expand Down
1 change: 1 addition & 0 deletions DoctrineBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ public function boot()

if ($this->container->getParameter('doctrine.orm.auto_generate_proxy_classes')) {
// See https://github.com/symfony/symfony/pull/3419 for usage of references
/** @psalm-suppress UnsupportedPropertyReferenceUsage */
$container = &$this->container;

$proxyGenerator = static function ($proxyDir, $proxyNamespace, $class) use (&$container): void {
Expand Down
2 changes: 2 additions & 0 deletions Mapping/DisconnectedMetadataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,12 @@ public function findNamespaceAndPathForMetadata(ClassMetadataCollection $metadat
$ns = $r->getNamespaceName();
} elseif ($path) {
// Get namespace by removing the last component of the FQCN
/** @psalm-suppress NoValue */
$nsParts = explode('\\', $all[0]->name);
array_pop($nsParts);
$ns = implode('\\', $nsParts);
} else {
/** @psalm-suppress NoValue */
throw new RuntimeException(sprintf('Unable to determine where to save the "%s" class (use the --path option).', $all[0]->name));
}

Expand Down
1 change: 1 addition & 0 deletions Repository/ServiceEntityRepositoryProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ public function matching(Criteria $criteria): AbstractLazyCollection&Selectable

private function resolveRepository(): EntityRepository
{
/** @psalm-suppress UndefinedThisPropertyFetch We should adjust when https://github.com/vimeo/psalm/issues/8984 is fixed */
$manager = $this->registry->getManagerForClass($this->entityClass);

if ($manager === null) {
Expand Down
2 changes: 1 addition & 1 deletion Tests/Command/CreateDatabaseDoctrineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ private function getMockContainer(string $connectionName, ?array $params = null)
->willReturn($mockConnection);

$mockContainer = $this->getMockBuilder(Container::class)
->setMethods(['get'])
->onlyMethods(['get'])
->getMock();

$mockContainer->expects($this->any())
Expand Down
2 changes: 1 addition & 1 deletion Tests/Command/DropDatabaseDoctrineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ private function getMockContainer(string $connectionName, array $params): MockOb
->willReturn($mockConnection);

$mockContainer = $this->getMockBuilder(Container::class)
->setMethods(['get'])
->onlyMethods(['get'])
->getMock();

$mockContainer->expects($this->any())
Expand Down
3 changes: 3 additions & 0 deletions Tests/ConnectionFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ public function testConnectionOverrideOptions(): void
'password' => 'wordpass',
];

/** @psalm-suppress InvalidArgument We should adjust when https://github.com/vimeo/psalm/issues/8984 is fixed */
$connection = (new ConnectionFactory([]))->createConnection(
[
'url' => 'mysql://root:password@database:3306/main?serverVersion=mariadb-10.5.8',
Expand All @@ -119,6 +120,7 @@ public function testConnectionCharsetFromUrl()

public function testDbnameSuffix(): void
{
/** @psalm-suppress InvalidArgument We should adjust when https://github.com/vimeo/psalm/issues/8984 is fixed */
$connection = (new ConnectionFactory([]))->createConnection(
[
'url' => 'mysql://root:password@database:3306/main?serverVersion=mariadb-10.5.8',
Expand All @@ -132,6 +134,7 @@ public function testDbnameSuffix(): void

public function testDbnameSuffixForReplicas(): void
{
/** @psalm-suppress InvalidArgument We should adjust when https://github.com/vimeo/psalm/issues/8984 is fixed */
$connection = (new ConnectionFactory([]))->createConnection(
[
'driver' => 'pdo_mysql',
Expand Down
76 changes: 17 additions & 59 deletions Tests/DependencyInjection/AbstractDoctrineExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\EntityListenerPass;
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\WellKnownSchemaFilterPass;
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\DoctrineExtension;
use Doctrine\Bundle\DoctrineBundle\Tests\DependencyInjection\Fixtures\InvokableEntityListener;
use Doctrine\Common\Cache\Psr6\DoctrineProvider;
use Doctrine\DBAL\Configuration;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Connections\PrimaryReadReplicaConnection;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Schema\LegacySchemaManagerFactory;
use Doctrine\ORM\Configuration as OrmConfiguration;
use Doctrine\ORM\EntityManager;
Expand Down Expand Up @@ -56,7 +56,6 @@

use const DIRECTORY_SEPARATOR;

/** @psalm-import-type Params from DriverManager */
abstract class AbstractDoctrineExtensionTest extends TestCase
{
abstract protected function loadFromFile(ContainerBuilder $container, string $file): void;
Expand Down Expand Up @@ -944,8 +943,8 @@ public function testAddFilter(): void

$definition = $container->getDefinition('doctrine.orm.default_configuration');
$args = [
['soft_delete', 'Doctrine\Bundle\DoctrineBundle\Tests\DependencyInjection\TestFilter'],
['myFilter', 'Doctrine\Bundle\DoctrineBundle\Tests\DependencyInjection\TestFilter'],
['soft_delete', TestFilter::class],
['myFilter', TestFilter::class],
];
$this->assertDICDefinitionMethodCallCount($definition, 'addFilter', $args, 2);

Expand Down Expand Up @@ -1212,16 +1211,16 @@ public function testEntityListenerResolver(): void
$container = $this->loadContainer('orm_entity_listener_resolver', ['YamlBundle'], new EntityListenerPass());

$definition = $container->getDefinition('doctrine.orm.em1_configuration');
$this->assertDICDefinitionMethodCallOnce($definition, 'setEntityListenerResolver', [new Reference('doctrine.orm.em1_entity_listener_resolver')]);
$this->assertDICDefinitionMethodCallOnce($definition, 'setEntityListenerResolver', ['doctrine.orm.em1_entity_listener_resolver']);

$definition = $container->getDefinition('doctrine.orm.em2_configuration');
$this->assertDICDefinitionMethodCallOnce($definition, 'setEntityListenerResolver', [new Reference('doctrine.orm.em2_entity_listener_resolver')]);
$this->assertDICDefinitionMethodCallOnce($definition, 'setEntityListenerResolver', ['doctrine.orm.em2_entity_listener_resolver']);

$listener = $container->getDefinition('doctrine.orm.em1_entity_listener_resolver');
$this->assertDICDefinitionMethodCallOnce($listener, 'registerService', ['EntityListener', 'entity_listener1']);

$listener = $container->getDefinition('entity_listener_resolver');
$this->assertDICDefinitionMethodCallOnce($listener, 'register', [new Reference('entity_listener2')]);
$this->assertDICDefinitionMethodCallOnce($listener, 'register', ['entity_listener2']);
}

public function testAttachEntityListenerTag(): void
Expand All @@ -1243,17 +1242,17 @@ public function testAttachEntityListenerTag(): void
$this->assertDICDefinitionMethodCallCount($listener, 'registerService', [
['ParentEntityListener', 'children_entity_listener'],
['EntityListener1', 'entity_listener1'],
['Doctrine\Bundle\DoctrineBundle\Tests\DependencyInjection\Fixtures\InvokableEntityListener', 'invokable_entity_listener'],
['Doctrine\Bundle\DoctrineBundle\Tests\DependencyInjection\Fixtures\InvokableEntityListener', 'invokable_entity_listener'],
[InvokableEntityListener::class, 'invokable_entity_listener'],
[InvokableEntityListener::class, 'invokable_entity_listener'],
], 4);

$listener = $container->getDefinition('doctrine.orm.em2_entity_listener_resolver');
$this->assertDICDefinitionMethodCallOnce($listener, 'registerService', ['EntityListener2', 'entity_listener2']);

$attachListener = $container->getDefinition('doctrine.orm.em1_listeners.attach_entity_listeners');
$this->assertDICDefinitionMethodCallAt(1, $attachListener, 'addEntityListener', ['My/Entity1', 'EntityListener1', 'postLoad']);
$this->assertDICDefinitionMethodCallAt(2, $attachListener, 'addEntityListener', ['My/Entity1', 'Doctrine\Bundle\DoctrineBundle\Tests\DependencyInjection\Fixtures\InvokableEntityListener', 'loadClassMetadata', '__invoke']);
$this->assertDICDefinitionMethodCallAt(3, $attachListener, 'addEntityListener', ['My/Entity1', 'Doctrine\Bundle\DoctrineBundle\Tests\DependencyInjection\Fixtures\InvokableEntityListener', 'postPersist']);
$this->assertDICDefinitionMethodCallAt(2, $attachListener, 'addEntityListener', ['My/Entity1', InvokableEntityListener::class, 'loadClassMetadata', '__invoke']);
$this->assertDICDefinitionMethodCallAt(3, $attachListener, 'addEntityListener', ['My/Entity1', InvokableEntityListener::class, 'postPersist']);
$this->assertDICDefinitionMethodCallAt(0, $attachListener, 'addEntityListener', ['My/Entity3', 'ParentEntityListener', 'postLoad']);

$attachListener = $container->getDefinition('doctrine.orm.em2_listeners.attach_entity_listeners');
Expand All @@ -1276,24 +1275,15 @@ public function testAttachEntityListenersTwoConnections(): void
$this->compileContainer($container);

$defaultEventManager = $container->getDefinition('doctrine.dbal.default_connection.event_manager');
$this->assertDICDefinitionNoMethodCall($defaultEventManager, 'addEventListener', [['loadClassMetadata'], new Reference('doctrine.orm.em2_listeners.attach_entity_listeners')]);
$this->assertEmpty($defaultEventManager->getMethodCalls());
$defaultEventManagerArguments = $defaultEventManager->getArguments();

if (isset($defaultEventManagerArguments[1][1])) {
$this->assertSame([['loadClassMetadata'], 'doctrine.orm.em1_listeners.attach_entity_listeners'], end($defaultEventManagerArguments[1]));
} else {
$this->assertDICDefinitionMethodCallOnce($defaultEventManager, 'addEventListener', [['loadClassMetadata'], new Reference('doctrine.orm.em1_listeners.attach_entity_listeners')]);
}
$this->assertSame([['loadClassMetadata'], 'doctrine.orm.em1_listeners.attach_entity_listeners'], end($defaultEventManagerArguments[1]));

$foobarEventManager = $container->getDefinition('doctrine.dbal.foobar_connection.event_manager');
$this->assertDICDefinitionNoMethodCall($foobarEventManager, 'addEventListener', [['loadClassMetadata'], new Reference('doctrine.orm.em1_listeners.attach_entity_listeners')]);
$this->assertEmpty($foobarEventManager->getMethodCalls());
$foobarEventManagerArguments = $foobarEventManager->getArguments();

if (isset($foobarEventManagerArguments[1][1])) {
$this->assertSame([['loadClassMetadata'], 'doctrine.orm.em2_listeners.attach_entity_listeners'], end($foobarEventManagerArguments[1]));
} else {
$this->assertDICDefinitionMethodCallOnce($foobarEventManager, 'addEventListener', [['loadClassMetadata'], new Reference('doctrine.orm.em2_listeners.attach_entity_listeners')]);
}
$this->assertSame([['loadClassMetadata'], 'doctrine.orm.em2_listeners.attach_entity_listeners'], end($foobarEventManagerArguments[1]));
}

public function testAttachLazyEntityListener(): void
Expand All @@ -1313,7 +1303,7 @@ public function testAttachLazyEntityListener(): void

$resolver1 = $container->getDefinition('doctrine.orm.em1_entity_listener_resolver');
$this->assertDICDefinitionMethodCallAt(0, $resolver1, 'registerService', ['EntityListener1', 'entity_listener1']);
$this->assertDICDefinitionMethodCallAt(1, $resolver1, 'register', [new Reference('entity_listener3')]);
$this->assertDICDefinitionMethodCallAt(1, $resolver1, 'register', ['entity_listener3']);
$this->assertDICDefinitionMethodCallAt(2, $resolver1, 'registerService', ['EntityListener4', 'entity_listener4']);

$serviceLocatorReference = $resolver1->getArgument(0);
Expand Down Expand Up @@ -1506,10 +1496,7 @@ private function assertDICConstructorArguments(Definition $definition, array $ar
$this->assertEquals($args, $definition->getArguments(), "Expected and actual DIC Service constructor arguments of definition '" . $definition->getClass() . "' don't match.");
}

/**
* @param list<mixed> $params
* @psalm-param Params $params
*/
/** @param list<mixed> $params */
private function assertDICDefinitionMethodCallAt(
int $pos,
Definition $definition,
Expand All @@ -1534,7 +1521,6 @@ private function assertDICDefinitionMethodCallAt(
* Assertion for the DI Container, check if the given definition contains a method call with the given parameters.
*
* @param list<mixed> $params
* @psalm-param Params $params
*/
private function assertDICDefinitionMethodCallOnce(
Definition $definition,
Expand Down Expand Up @@ -1565,10 +1551,7 @@ private function assertDICDefinitionMethodCallOnce(
$this->fail("Method '" . $methodName . "' is expected to be called once, definition does not contain a call though.");
}

/**
* @param list<mixed> $params
* @psalm-param Params $params
*/
/** @param list<list<string>> $params */
private function assertDICDefinitionMethodCallCount(
Definition $definition,
string $methodName,
Expand Down Expand Up @@ -1596,31 +1579,6 @@ private function assertDICDefinitionMethodCallCount(
$this->assertEquals($nbCalls, $called, sprintf('The method "%s" should be called %d times', $methodName, $nbCalls));
}

/**
* Assertion for the DI Container, check if the given definition does not contain a method call with the given parameters.
*
* @param list<mixed> $params
* @psalm-param Params $params
*/
private function assertDICDefinitionNoMethodCall(
Definition $definition,
string $methodName,
?array $params = null
): void {
$calls = $definition->getMethodCalls();
foreach ($calls as $call) {
if ($call[0] !== $methodName) {
continue;
}

if ($params !== null) {
$this->assertNotEquals($params, $call[1], "Method '" . $methodName . "' is not expected to be called with the given parameters.");
} else {
$this->fail("Method '" . $methodName . "' is not expected to be called");
}
}
}

private function compileContainer(ContainerBuilder $container): void
{
$passConfig = $container->getCompilerPassConfig();
Expand Down
2 changes: 1 addition & 1 deletion Tests/DependencyInjection/Compiler/MiddlewarePassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public function testAddMiddlewareWithAttributeForAutoconfiguration(string $class
}

/** @dataProvider provideAddMiddleware */
public function testDontAddMiddlewareWhenDbalIsNotUsed(string $middlewareClass, bool $connectionNameAware): void
public function testDontAddMiddlewareWhenDbalIsNotUsed(string $middlewareClass): void
{
$container = $this->createContainer(static function (ContainerBuilder $container) use ($middlewareClass) {
$container
Expand Down
Loading

0 comments on commit eb96246

Please sign in to comment.