Skip to content

Commit

Permalink
bump to phpstan level 4
Browse files Browse the repository at this point in the history
  • Loading branch information
dmaicher committed Jan 27, 2025
1 parent 4af905c commit acda9f3
Show file tree
Hide file tree
Showing 17 changed files with 24 additions and 106 deletions.
2 changes: 1 addition & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
parameters:
level: 3
level: 4
paths:
- src
- tests
Expand Down
3 changes: 1 addition & 2 deletions src/CacheWarmer/DoctrineMetadataCacheWarmer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Doctrine\Bundle\DoctrineBundle\CacheWarmer;

use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\Mapping\AbstractClassMetadataFactory;
use LogicException;
use Symfony\Bundle\FrameworkBundle\CacheWarmer\AbstractPhpFileCacheWarmer;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
Expand Down Expand Up @@ -36,7 +35,7 @@ protected function doWarmUp(string $cacheDir, ArrayAdapter $arrayAdapter, string
}

$metadataFactory = $this->entityManager->getMetadataFactory();
if ($metadataFactory instanceof AbstractClassMetadataFactory && $metadataFactory->getLoadedMetadata()) {
if ($metadataFactory->getLoadedMetadata()) {
throw new LogicException('DoctrineMetadataCacheWarmer must load metadata first, check priority of your warmers.');
}

Expand Down
2 changes: 1 addition & 1 deletion src/Command/CreateDatabaseDoctrineCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
unset($params['dbname'], $params['path'], $params['url']);

if ($connection->getDatabasePlatform() instanceof PostgreSQLPlatform) {
/** @psalm-suppress InvalidArrayOffset It's still available in DBAL 3.x that we need to support */
/** @phpstan-ignore nullCoalesce.offset (needed for DBAL < 4) */
$params['dbname'] = $params['default_dbname'] ?? 'postgres';
}

Expand Down
5 changes: 2 additions & 3 deletions src/Command/DropDatabaseDoctrineCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
throw new InvalidArgumentException("Connection does not contain a 'path' or 'dbname' parameter and cannot be dropped.");
}

/** @psalm-suppress InvalidArrayOffset Need to be compatible with DBAL < 4, which still has `$params['url']` */
/* @phpstan-ignore unset.offset */
/* @phpstan-ignore unset.offset (Need to be compatible with DBAL < 4, which still has `$params['url']`) */
unset($params['dbname'], $params['url']);

if ($connection->getDatabasePlatform() instanceof PostgreSQLPlatform) {
/** @psalm-suppress InvalidArrayOffset It's still available in DBAL 3.x that we need to support */
/** @phpstan-ignore nullCoalesce.offset (for DBAL < 4) */
$params['dbname'] = $params['default_dbname'] ?? 'postgres';
}

Expand Down
10 changes: 7 additions & 3 deletions src/ConnectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class ConnectionFactory
'sqlite3' => 'pdo_sqlite',
];

/** @phpstan-ignore property.onlyWritten */
private readonly DsnParser $dsnParser;

private bool $initialized = false;
Expand Down Expand Up @@ -76,7 +77,7 @@ public function createConnection(array $params, Configuration|null $config = nul
}

$overriddenOptions = [];
/** @psalm-suppress InvalidArrayOffset We should adjust when https://github.com/vimeo/psalm/issues/8984 is fixed */
/** @phpstan-ignore isset.offset (We should adjust when https://github.com/phpstan/phpstan/issues/12414 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 @@ -96,7 +97,7 @@ public function createConnection(array $params, Configuration|null $config = nul
}
}

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

Expand Down Expand Up @@ -251,14 +252,17 @@ private function addDatabaseSuffix(array $params): array
* @phpstan-return Params
*
* @throws DBALException
*
* @phpstan-ignore throws.unusedType
*/
private function parseDatabaseUrl(array $params): array
{
/** @psalm-suppress InvalidArrayOffset Need to be compatible with DBAL < 4, which still has `$params['url']` */
/** @phpstan-ignore isset.offset (for DBAL < 4) */
if (! isset($params['url'])) {
return $params;
}

/** @phpstan-ignore deadCode.unreachable */
try {
$parsedParams = $this->dsnParser->parse($params['url']);
} catch (MalformedDsnException $e) {
Expand Down
6 changes: 1 addition & 5 deletions src/DataCollector/DoctrineDataCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@
use Doctrine\ORM\Cache\Logging\CacheLoggerChain;
use Doctrine\ORM\Cache\Logging\StatisticsCacheLogger;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Tools\SchemaValidator;
use Doctrine\Persistence\ManagerRegistry;
use Doctrine\Persistence\Mapping\AbstractClassMetadataFactory;
use Symfony\Bridge\Doctrine\DataCollector\DoctrineDataCollector as BaseCollector;
use Symfony\Bridge\Doctrine\Middleware\Debug\DebugDataHolder;
use Symfony\Component\HttpFoundation\Request;
Expand Down Expand Up @@ -54,6 +52,7 @@ class DoctrineDataCollector extends BaseCollector
/**
* @var mixed[][]|null
* @phpstan-var ?array<string, list<QueryType&array{count: int, index: int, executionPercent?: float}>>
* @phpstan-ignore property.unusedType
*/
private array|null $groupedQueries = null;

Expand Down Expand Up @@ -94,10 +93,7 @@ public function collect(Request $request, Response $response, Throwable|null $ex
$factory = $em->getMetadataFactory();
$validator = new SchemaValidator($em);

assert($factory instanceof AbstractClassMetadataFactory);

foreach ($factory->getLoadedMetadata() as $class) {
assert($class instanceof ClassMetadata);
if (isset($entities[$name][$class->getName()])) {
continue;
}
Expand Down
55 changes: 0 additions & 55 deletions src/DependencyInjection/Compiler/WellKnownSchemaFilterPass.php

This file was deleted.

5 changes: 0 additions & 5 deletions src/DependencyInjection/DoctrineExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
use Doctrine\DBAL\Connections\PrimaryReadReplicaConnection;
use Doctrine\DBAL\Driver\Middleware as MiddlewareInterface;
use Doctrine\DBAL\Schema\LegacySchemaManagerFactory;
use Doctrine\ORM\Configuration as OrmConfiguration;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Events;
use Doctrine\ORM\Id\AbstractIdGenerator;
Expand Down Expand Up @@ -697,10 +696,6 @@ protected function loadOrmEntityManager(array $entityManager, ContainerBuilder $
$methods['setEagerFetchBatchSize'] = $entityManager['fetch_mode_subselect_batch_size'];
}

if (! method_exists(OrmConfiguration::class, 'setLazyGhostObjectEnabled')) {
unset($methods['setLazyGhostObjectEnabled']);
}

$listenerId = sprintf('doctrine.orm.%s_listeners.attach_entity_listeners', $entityManager['name']);
$listenerDef = $container->setDefinition($listenerId, new Definition('%doctrine.orm.listeners.attach_entity_listeners.class%'));
$listenerTagParams = ['event' => 'loadClassMetadata'];
Expand Down
2 changes: 0 additions & 2 deletions src/DoctrineBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\RemoveLoggingMiddlewarePass;
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\RemoveProfilerControllerPass;
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\ServiceRepositoryCompilerPass;
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\WellKnownSchemaFilterPass;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Proxy\Autoloader;
use Doctrine\ORM\Proxy\DefaultProxyClassNameResolver;
Expand Down Expand Up @@ -68,7 +67,6 @@ public function process(ContainerBuilder $container): void
$container->addCompilerPass(new EntityListenerPass());
$container->addCompilerPass(new ServiceRepositoryCompilerPass());
$container->addCompilerPass(new IdGeneratorPass());
$container->addCompilerPass(new WellKnownSchemaFilterPass());
$container->addCompilerPass(new DbalSchemaFilterPass());
$container->addCompilerPass(new CacheSchemaSubscriberPass(), PassConfig::TYPE_BEFORE_REMOVING, -10);
$container->addCompilerPass(new RemoveProfilerControllerPass());
Expand Down
1 change: 1 addition & 0 deletions src/Mapping/ClassMetadataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ protected function doLoadMetadata($class, $parent, $rootEntityFound, array $nonS
return;
}

/** @phpstan-ignore function.impossibleType, instanceof.alwaysFalse */
assert($customGeneratorDefinition['instance'] instanceof AbstractIdGenerator);

$class->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_CUSTOM);
Expand Down
3 changes: 2 additions & 1 deletion src/Twig/DoctrineExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ public static function escapeFunction(mixed $parameter)
*/
public function replaceQueryParameters($query, $parameters)
{
/** @phpstan-ignore instanceof.alwaysTrue */
if ($parameters instanceof Data) {
$parameters = $parameters->getValue(true);
}
Expand All @@ -130,7 +131,7 @@ public function replaceQueryParameters($query, $parameters)
static function ($matches) use ($parameters, &$i) {
$key = substr($matches[0], 1);

if (! array_key_exists($i, $parameters) && ($key === false || ! array_key_exists($key, $parameters))) {
if (! array_key_exists($i, $parameters) && ! array_key_exists($key, $parameters)) {
return $matches[0];
}

Expand Down
27 changes: 3 additions & 24 deletions tests/DependencyInjection/AbstractDoctrineExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\CacheCompatibilityPass;
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\DbalSchemaFilterPass;
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;
Expand Down Expand Up @@ -39,7 +38,6 @@
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\ServiceLocator;
use Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler;
use Symfony\Component\Security\Core\User\UserInterface;

use function array_filter;
Expand Down Expand Up @@ -1142,7 +1140,6 @@ public function testDbalSchemaFilterNewConfig(): void
$container = $this->getContainer([]);
$loader = new DoctrineExtension();
$container->registerExtension($loader);
$container->addCompilerPass(new WellKnownSchemaFilterPass());
$container->addCompilerPass(new DbalSchemaFilterPass());

// ignore table1 table on "default" connection
Expand Down Expand Up @@ -1184,7 +1181,6 @@ public function testWellKnownSchemaFilterDefaultTables(): void
$container = $this->getContainer([]);
$loader = new DoctrineExtension();
$container->registerExtension($loader);
$container->addCompilerPass(new WellKnownSchemaFilterPass());
$container->addCompilerPass(new DbalSchemaFilterPass());

$this->loadFromFile($container, 'well_known_schema_filter_default_tables_session');
Expand All @@ -1197,20 +1193,8 @@ public function testWellKnownSchemaFilterDefaultTables(): void

$this->assertInstanceOf(BlacklistSchemaAssetFilter::class, $filter);

if (method_exists(PdoSessionHandler::class, 'configureSchema')) {
$this->assertNotSame([['sessions']], $definition->getArguments());
$this->assertTrue($filter->__invoke('sessions'));
} else {
$this->assertSame([['sessions']], $definition->getArguments());

$this->assertSame([['connection' => 'connection1'], ['connection' => 'connection2'], ['connection' => 'connection3']], $definition->getTag('doctrine.dbal.schema_filter'));

$definition = $container->getDefinition('doctrine.dbal.connection1_schema_asset_filter_manager');

$this->assertEquals([new Reference('doctrine.dbal.well_known_schema_asset_filter'), new Reference('doctrine.dbal.connection1_regex_schema_filter')], $definition->getArgument(0));
$this->assertFalse($filter->__invoke('sessions'));
}

$this->assertNotSame([['sessions']], $definition->getArguments());
$this->assertTrue($filter->__invoke('sessions'));
$this->assertTrue($filter->__invoke('anything_else'));
}

Expand All @@ -1220,7 +1204,6 @@ public function testWellKnownSchemaFilterOverriddenTables(): void
$container = $this->getContainer([]);
$loader = new DoctrineExtension();
$container->registerExtension($loader);
$container->addCompilerPass(new WellKnownSchemaFilterPass());
$container->addCompilerPass(new DbalSchemaFilterPass());

$this->loadFromFile($container, 'well_known_schema_filter_overridden_tables_session');
Expand All @@ -1231,11 +1214,7 @@ public function testWellKnownSchemaFilterOverriddenTables(): void

$this->assertInstanceOf(BlacklistSchemaAssetFilter::class, $filter);

if (method_exists(PdoSessionHandler::class, 'configureSchema')) {
$this->assertTrue($filter->__invoke('app_session'));
} else {
$this->assertFalse($filter->__invoke('app_session'));
}
$this->assertTrue($filter->__invoke('app_session'));
}

public function testEntityListenerResolver(): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ class TestCustomClassRepoEntity
#[ORM\Id]
#[ORM\GeneratedValue(strategy: 'AUTO')]
#[ORM\Column(type: Types::INTEGER)]
private int|null $id = null;
private int|null $id = null; /** @phpstan-ignore property.unusedType, property.onlyWritten */
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ class TestCustomServiceRepoEntity
#[ORM\Id]
#[ORM\GeneratedValue(strategy: 'AUTO')]
#[ORM\Column(type: Types::INTEGER)]
private int|null $id = null;
private int|null $id = null; /** @phpstan-ignore property.unusedType, property.onlyWritten */
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ class TestDefaultRepoEntity
#[ORM\Id]
#[ORM\GeneratedValue(strategy: 'AUTO')]
#[ORM\Column(type: Types::INTEGER)]
private int|null $id = null;
private int|null $id = null; /** @phpstan-ignore property.unusedType, property.onlyWritten */
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@

class Test
{
/** @phpstan-ignore property.unused */
private mixed $id;
}
2 changes: 1 addition & 1 deletion tests/Repository/ServiceEntityRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function testConstructInitializesWhenImplementingLazyObjectInterface(): v
$registry = $this->getMockBuilder(ManagerRegistry::class)->getMock();
$this->expectException(LogicException::class);

/* @phpstan-ignore class.notFound */
/* @phpstan-ignore class.notFound, expr.resultUnused */
new class ($registry, TestEntity::class) extends ServiceEntityRepository implements LazyObjectInterface {
use LazyGhostTrait;
};
Expand Down

0 comments on commit acda9f3

Please sign in to comment.