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

phpstan: lvl 4 -> 5 #1864

Merged
merged 1 commit into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
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: 4
level: 5
paths:
- src
- tests
Expand Down
3 changes: 1 addition & 2 deletions src/Command/Proxy/DoctrineCommandHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ public static function setApplicationEntityManager(Application $application, $em
$em = $application->getKernel()->getContainer()->get('doctrine')->getManager($emName);
assert($em instanceof EntityManagerInterface);
$helperSet = $application->getHelperSet();
/** @psalm-suppress InvalidArgument ORM < 3 specific */
/* @phpstan-ignore class.notFound */
/* @phpstan-ignore class.notFound, argument.type (ORM < 3 specific) */
$helperSet->set(new EntityManagerHelper($em), 'em');

trigger_deprecation(
Expand Down
1 change: 1 addition & 0 deletions src/Command/Proxy/OrmProxyCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public function __construct(
protected function execute(InputInterface $input, OutputInterface $output): int
{
if (! $this->entityManagerProvider) {
/* @phpstan-ignore argument.type (ORM < 3 specific) */
DoctrineCommandHelper::setApplicationEntityManager($this->getApplication(), $input->getOption('em'));
}

Expand Down
4 changes: 4 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ public function getConfigTreeBuilder(): TreeBuilder
$treeBuilder = new TreeBuilder('doctrine');
$rootNode = $treeBuilder->getRootNode();

/* @phpstan-ignore argument.type (symfony plugin needed) */
$this->addDbalSection($rootNode);
/* @phpstan-ignore argument.type (symfony plugin needed) */
$this->addOrmSection($rootNode);

return $treeBuilder;
Expand Down Expand Up @@ -262,6 +264,7 @@ private function getDbalConnectionsNode(): ArrayNodeDefinition
)
->useAttributeAsKey('name')
->prototype('array');
/* @phpstan-ignore argument.type (symfony plugin needed) */
$this->configureDbalDriverNode($slaveNode);

// dbal >= 2.11
Expand All @@ -270,6 +273,7 @@ private function getDbalConnectionsNode(): ArrayNodeDefinition
->arrayNode('replicas')
->useAttributeAsKey('name')
->prototype('array');
/* @phpstan-ignore argument.type (symfony plugin needed) */
$this->configureDbalDriverNode($replicaNode);

assert($node instanceof ArrayNodeDefinition);
Expand Down
3 changes: 2 additions & 1 deletion src/Repository/LazyServiceEntityRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Doctrine\Bundle\DoctrineBundle\Repository;

use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use Doctrine\Persistence\ManagerRegistry;
use LogicException;
Expand Down Expand Up @@ -62,7 +63,7 @@ private function initialize(): void
{
$manager = $this->registry->getManagerForClass($this->entityClass);

if ($manager === null) {
if (! $manager instanceof EntityManagerInterface) {
throw new LogicException(sprintf(
'Could not find the entity manager for class "%s". Check your Doctrine configuration to make sure it is configured to load this entity’s metadata.',
$this->entityClass,
Expand Down
2 changes: 1 addition & 1 deletion src/Repository/ServiceEntityRepositoryProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ private function resolveRepository(): EntityRepository
{
$manager = $this->registry->getManagerForClass($this->entityClass);

if ($manager === null) {
if (! $manager instanceof EntityManagerInterface) {
throw new LogicException(sprintf(
'Could not find the entity manager for class "%s". Check your Doctrine configuration to make sure it is configured to load this entity’s metadata.',
$this->entityClass,
Expand Down
5 changes: 2 additions & 3 deletions src/Twig/DoctrineExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,13 @@ public static function escapeFunction(mixed $parameter)
/**
* Return a query with the parameters replaced
*
* @param string $query
* @param mixed[]|Data $parameters
* @param string $query
* @param array<array-key, mixed>|Data $parameters
*
* @return string
*/
public function replaceQueryParameters($query, $parameters)
{
/** @phpstan-ignore instanceof.alwaysTrue */
if ($parameters instanceof Data) {
$parameters = $parameters->getValue(true);
}
Expand Down
1 change: 1 addition & 0 deletions tests/Middleware/IdleConnectionMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class IdleConnectionMiddlewareTest extends TestCase
/** @requires function Symfony\Bridge\Doctrine\Middleware\IdleConnection\Driver::__construct */
public function testWrap()
{
/** @var ArrayObject<string, int> $connectionExpiries */
$connectionExpiries = new ArrayObject(['connectionone' => time() - 30, 'connectiontwo' => time() + 40]);
$ttlByConnection = ['connectionone' => 25, 'connectiontwo' => 60];

Expand Down
1 change: 1 addition & 0 deletions tests/ProfilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public function setUp(): void
if (class_exists(CodeExtension::class)) {
$this->twig->addExtension(new CodeExtension('', '', ''));
} elseif (class_exists(CodeExtensionLegacy::class)) {
/* @phpstan-ignore argument.type (available in symfony < 7) */
$this->twig->addExtension(new CodeExtensionLegacy('', '', ''));
}

Expand Down
28 changes: 23 additions & 5 deletions tests/Repository/ContainerRepositoryFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,37 +31,46 @@ public static function setUpBeforeClass(): void

public function testGetRepositoryReturnsService(): void
{
$em = $this->createEntityManager(['Foo\CoolEntity' => 'my_repo']);
/* @phpstan-ignore argument.type */
$em = $this->createEntityManager(['Foo\CoolEntity' => 'my_repo']);
/* @phpstan-ignore argument.type */
$repo = new StubRepository($em, new ClassMetadata('Foo\CoolEntity'));
$container = $this->createContainer(['my_repo' => $repo]);

$factory = new ContainerRepositoryFactory($container);
/* @phpstan-ignore argument.type */
$this->assertSame($repo, $factory->getRepository($em, 'Foo\CoolEntity'));
}

public function testGetRepositoryReturnsEntityRepository(): void
{
$container = $this->createContainer([]);
$em = $this->createEntityManager(['Foo\BoringEntity' => null]);
/* @phpstan-ignore argument.type */
$em = $this->createEntityManager(['Foo\BoringEntity' => null]);

$factory = new ContainerRepositoryFactory($container);
$factory = new ContainerRepositoryFactory($container);
/* @phpstan-ignore argument.type */
$actualRepo = $factory->getRepository($em, 'Foo\BoringEntity');
$this->assertInstanceOf(EntityRepository::class, $actualRepo);
// test the same instance is returned
/* @phpstan-ignore argument.type */
$this->assertSame($actualRepo, $factory->getRepository($em, 'Foo\BoringEntity'));
}

public function testCustomRepositoryIsReturned(): void
{
$container = $this->createContainer([]);
$em = $this->createEntityManager([
/* @phpstan-ignore argument.type */
$em = $this->createEntityManager([
'Foo\CustomNormalRepoEntity' => StubRepository::class,
]);

$factory = new ContainerRepositoryFactory($container);
$factory = new ContainerRepositoryFactory($container);
/* @phpstan-ignore argument.type */
$actualRepo = $factory->getRepository($em, 'Foo\CustomNormalRepoEntity');
$this->assertInstanceOf(StubRepository::class, $actualRepo);
// test the same instance is returned
/* @phpstan-ignore argument.type */
$this->assertSame($actualRepo, $factory->getRepository($em, 'Foo\CustomNormalRepoEntity'));
}

Expand All @@ -71,6 +80,7 @@ public function testServiceRepositoriesMustExtendObjectRepository(): void

$container = $this->createContainer(['my_repo' => $repo]);

/* @phpstan-ignore argument.type */
$em = $this->createEntityManager(['Foo\CoolEntity' => 'my_repo']);

$factory = new ContainerRepositoryFactory($container);
Expand All @@ -81,6 +91,7 @@ public function testServiceRepositoriesMustExtendObjectRepository(): void
$this->expectExceptionMessage('The service "my_repo" must implement ObjectRepository (or extend a base class, like ServiceEntityRepository), "stdClass" given.');
}

/* @phpstan-ignore argument.type */
$factory->getRepository($em, 'Foo\CoolEntity');
}

Expand All @@ -90,10 +101,13 @@ public function testServiceRepositoriesCanNotExtendsEntityRepository(): void

$container = $this->createContainer(['my_repo' => $repo]);

/* @phpstan-ignore argument.type */
$em = $this->createEntityManager(['Foo\CoolEntity' => 'my_repo']);

$factory = new ContainerRepositoryFactory($container);
/* @phpstan-ignore argument.type */
$factory->getRepository($em, 'Foo\CoolEntity');
/* @phpstan-ignore argument.type */
$actualRepo = $factory->getRepository($em, 'Foo\CoolEntity');
$this->assertSame($repo, $actualRepo);
}
Expand All @@ -102,6 +116,7 @@ public function testRepositoryMatchesServiceInterfaceButServiceNotFound(): void
{
$container = $this->createContainer([]);

/* @phpstan-ignore argument.type */
$em = $this->createEntityManager([
'Foo\CoolEntity' => StubServiceRepository::class,
]);
Expand All @@ -111,20 +126,23 @@ public function testRepositoryMatchesServiceInterfaceButServiceNotFound(): void
$this->expectExceptionMessage(<<<'EXCEPTION'
The "Doctrine\Bundle\DoctrineBundle\Tests\Repository\Fixtures\StubServiceRepository" entity repository implements "Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepositoryInterface", but its service could not be found. Make sure the service exists and is tagged with "doctrine.repository_service".
EXCEPTION);
/* @phpstan-ignore argument.type */
$factory->getRepository($em, 'Foo\CoolEntity');
}

public function testCustomRepositoryIsNotAValidClass(): void
{
$container = $this->createContainer([]);

/* @phpstan-ignore argument.type */
$em = $this->createEntityManager(['Foo\CoolEntity' => 'not_a_real_class']);

$factory = new ContainerRepositoryFactory($container);
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage(<<<'EXCEPTION'
The "Foo\CoolEntity" entity has a repositoryClass set to "not_a_real_class", but this is not a valid class. Check your class naming. If this is meant to be a service id, make sure this service exists and is tagged with "doctrine.repository_service".
EXCEPTION);
/* @phpstan-ignore argument.type */
$factory->getRepository($em, 'Foo\CoolEntity');
}

Expand Down