Skip to content

Commit

Permalink
Make PurgerFactory generic
Browse files Browse the repository at this point in the history
This allows us to remove assertions and natively provide the right type
information.
  • Loading branch information
greg0ire committed Nov 29, 2024
1 parent 42a0cbe commit cccda85
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 13 deletions.
2 changes: 1 addition & 1 deletion phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
parameters:
ignoreErrors:
-
message: "#^Method class@anonymous/src/Command/LoadDataFixturesDoctrineCommand\\.php\\:121\\:\\:log\\(\\) has parameter \\$message with no type specified\\.$#"
message: "#^Method class@anonymous/src/Command/LoadDataFixturesDoctrineCommand\\.php\\:120\\:\\:log\\(\\) has parameter \\$message with no type specified\\.$#"
count: 1
path: src/Command/LoadDataFixturesDoctrineCommand.php

Expand Down
5 changes: 2 additions & 3 deletions src/Command/LoadDataFixturesDoctrineCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Doctrine\Bundle\FixturesBundle\Purger\ORMPurgerFactory;
use Doctrine\Bundle\FixturesBundle\Purger\PurgerFactory;
use Doctrine\Common\DataFixtures\Executor\ORMExecutor;
use Doctrine\Common\DataFixtures\Purger\ORMPurgerInterface;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\ManagerRegistry;
use Psr\Log\AbstractLogger;
Expand All @@ -32,6 +31,7 @@ class LoadDataFixturesDoctrineCommand extends DoctrineCommand
public function __construct(
private SymfonyFixturesLoader $fixturesLoader,
ManagerRegistry $doctrine,
/** @var array<string, ORMPurgerFactory> $purgerFactories */
private array $purgerFactories = [],
) {
parent::__construct($doctrine);
Expand Down Expand Up @@ -110,13 +110,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$factory = $this->purgerFactories[$input->getOption('purger')];
}

$purger = $factory->createForEntityManager(
$purger = $factory->createForEntityManager(
$input->getOption('em'),
$em,
$input->getOption('purge-exclusions'),
$input->getOption('purge-with-truncate'),
);
assert($purger instanceof ORMPurgerInterface);
$executor = new ORMExecutor($em, $purger);
$executor->setLogger(new class ($ui) extends AbstractLogger {
public function __construct(private SymfonyStyle $ui)
Expand Down
1 change: 1 addition & 0 deletions src/Purger/ORMPurgerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Doctrine\Common\DataFixtures\Purger\ORMPurger;
use Doctrine\ORM\EntityManagerInterface;

/** @template-implements PurgerFactory<ORMPurger> */
final class ORMPurgerFactory implements PurgerFactory
{
/**
Expand Down
7 changes: 6 additions & 1 deletion src/Purger/PurgerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@
use Doctrine\Common\DataFixtures\Purger\PurgerInterface;
use Doctrine\ORM\EntityManagerInterface;

/** @template T of PurgerInterface */
interface PurgerFactory
{
/** @phpstan-param list<string> $excluded */
/**
* @phpstan-param list<string> $excluded
*
* @return T
*/
public function createForEntityManager(
string|null $emName,
EntityManagerInterface $em,
Expand Down
8 changes: 0 additions & 8 deletions tests/Purger/ORMPurgerFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

use function assert;

class ORMPurgerFactoryTest extends TestCase
{
private ORMPurgerFactory $factory;
Expand All @@ -28,9 +26,7 @@ protected function setUp(): void
public function testCreateDefault(): void
{
$purger = $this->factory->createForEntityManager(null, $this->em);
assert($purger instanceof ORMPurger);

self::assertInstanceOf(ORMPurger::class, $purger);
self::assertSame(ORMPurger::PURGE_MODE_DELETE, $purger->getPurgeMode());
self::assertSame([], (function () {
return $this->excluded;
Expand All @@ -40,9 +36,7 @@ public function testCreateDefault(): void
public function testCreateWithExclusions(): void
{
$purger = $this->factory->createForEntityManager(null, $this->em, ['tableName']);
assert($purger instanceof ORMPurger);

self::assertInstanceOf(ORMPurger::class, $purger);
self::assertSame(ORMPurger::PURGE_MODE_DELETE, $purger->getPurgeMode());
self::assertSame(['tableName'], (function () {
return $this->excluded;
Expand All @@ -52,9 +46,7 @@ public function testCreateWithExclusions(): void
public function testCreateWithTruncate(): void
{
$purger = $this->factory->createForEntityManager(null, $this->em, [], true);
assert($purger instanceof ORMPurger);

self::assertInstanceOf(ORMPurger::class, $purger);
self::assertSame(ORMPurger::PURGE_MODE_TRUNCATE, $purger->getPurgeMode());
self::assertSame([], (function () {
return $this->excluded;
Expand Down

0 comments on commit cccda85

Please sign in to comment.