From 954e0a314c2f0eb9fb418210445111747de254a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Bundyra?= Date: Wed, 6 Mar 2024 13:41:11 +0000 Subject: [PATCH 1/5] fix: Allow enum param types: ArrayParameterType and ParameterType (#1408) Fixes #1407 A bit lame fix, but it looks like we did not really use other int types before anyway With this change we can use DBAL v4 new enum types: `ArrayParameterType` and `ParameterType`. Co-authored-by: Alexander M. Turek --- lib/Doctrine/Migrations/InlineParameterFormatter.php | 2 +- .../Migrations/Tests/InlineParameterFormatterTest.php | 11 ++++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/Doctrine/Migrations/InlineParameterFormatter.php b/lib/Doctrine/Migrations/InlineParameterFormatter.php index 9aa6fc782..741a5ca3d 100644 --- a/lib/Doctrine/Migrations/InlineParameterFormatter.php +++ b/lib/Doctrine/Migrations/InlineParameterFormatter.php @@ -53,7 +53,7 @@ public function formatParameters(array $params, array $types): string return sprintf('with parameters (%s)', implode(', ', $formattedParameters)); } - private function formatParameter(mixed $value, string|int $type): string|int|bool|float|null + private function formatParameter(mixed $value, mixed $type): string|int|bool|float|null { if (is_string($type) && Type::hasType($type)) { return Type::getType($type)->convertToDatabaseValue( diff --git a/tests/Doctrine/Migrations/Tests/InlineParameterFormatterTest.php b/tests/Doctrine/Migrations/Tests/InlineParameterFormatterTest.php index 07a45d565..642c223fc 100644 --- a/tests/Doctrine/Migrations/Tests/InlineParameterFormatterTest.php +++ b/tests/Doctrine/Migrations/Tests/InlineParameterFormatterTest.php @@ -4,12 +4,16 @@ namespace Doctrine\Migrations\Tests; +use Doctrine\DBAL\ArrayParameterType; use Doctrine\DBAL\Connection; +use Doctrine\DBAL\ParameterType; use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Types\Types; use Doctrine\Migrations\InlineParameterFormatter; use PHPUnit\Framework\TestCase; +use function class_exists; + class InlineParameterFormatterTest extends TestCase { private Connection $connection; @@ -35,6 +39,8 @@ public function testFormatParameters(): void 11 => true, 12 => false, 13 => [1, true, false, 'string value'], + 14 => 'string value', + 15 => [1, 2, 3], 'named' => 'string value', ]; @@ -54,11 +60,14 @@ public function testFormatParameters(): void 'unknown', 'unknown', 'unknown', + ParameterType::STRING, + // @phpstan-ignore-next-line + class_exists(ArrayParameterType::class) ? ArrayParameterType::INTEGER : Connection::PARAM_INT_ARRAY, ]; $result = $this->parameterFormatter->formatParameters($params, $types); - $expected = 'with parameters ([string value], [1], [], [1], [1.5], [1,1,,string value], [], [], [string value], [1], [1.5], [true], [false], [1, true, false, string value], :named => [string value])'; + $expected = 'with parameters ([string value], [1], [], [1], [1.5], [1,1,,string value], [], [], [string value], [1], [1.5], [true], [false], [1, true, false, string value], [string value], [1, 2, 3], :named => [string value])'; self::assertSame($expected, $result); } From c8f23d84794545e43141184431b477b2d78e306f Mon Sep 17 00:00:00 2001 From: "Alexander M. Turek" Date: Wed, 6 Mar 2024 14:54:48 +0100 Subject: [PATCH 2/5] Simplify InlineParameterFormatterTest (#1411) --- composer.json | 2 +- tests/InlineParameterFormatterTest.php | 19 +++++-------------- 2 files changed, 6 insertions(+), 15 deletions(-) diff --git a/composer.json b/composer.json index 6a8dfef4b..da85fa3a4 100644 --- a/composer.json +++ b/composer.json @@ -26,7 +26,7 @@ "require": { "php": "^8.1", "composer-runtime-api": "^2", - "doctrine/dbal": "^3.5.1 || ^4", + "doctrine/dbal": "^3.6 || ^4", "doctrine/deprecations": "^0.5.3 || ^1", "doctrine/event-manager": "^1.2 || ^2.0", "psr/log": "^1.1.3 || ^2 || ^3", diff --git a/tests/InlineParameterFormatterTest.php b/tests/InlineParameterFormatterTest.php index 642c223fc..02a4ccc64 100644 --- a/tests/InlineParameterFormatterTest.php +++ b/tests/InlineParameterFormatterTest.php @@ -12,14 +12,8 @@ use Doctrine\Migrations\InlineParameterFormatter; use PHPUnit\Framework\TestCase; -use function class_exists; - class InlineParameterFormatterTest extends TestCase { - private Connection $connection; - - private AbstractPlatform $platform; - private InlineParameterFormatter $parameterFormatter; public function testFormatParameters(): void @@ -61,8 +55,7 @@ public function testFormatParameters(): void 'unknown', 'unknown', ParameterType::STRING, - // @phpstan-ignore-next-line - class_exists(ArrayParameterType::class) ? ArrayParameterType::INTEGER : Connection::PARAM_INT_ARRAY, + ArrayParameterType::INTEGER, ]; $result = $this->parameterFormatter->formatParameters($params, $types); @@ -74,14 +67,12 @@ class_exists(ArrayParameterType::class) ? ArrayParameterType::INTEGER : Connecti protected function setUp(): void { - $this->connection = $this->createMock(Connection::class); - - $this->platform = $this->createMock(AbstractPlatform::class); + $connection = $this->createMock(Connection::class); - $this->connection->expects(self::any()) + $connection->expects(self::any()) ->method('getDatabasePlatform') - ->willReturn($this->platform); + ->willReturn(self::createStub(AbstractPlatform::class)); - $this->parameterFormatter = new InlineParameterFormatter($this->connection); + $this->parameterFormatter = new InlineParameterFormatter($connection); } } From 7767cf44b4ff48e0a902735c8f7ed76d3d748be5 Mon Sep 17 00:00:00 2001 From: Nicolas Olive Date: Wed, 6 Mar 2024 14:58:59 +0100 Subject: [PATCH 3/5] Remove unused paramaters from DiffCommand and VersionCommand (#1386) --- src/Tools/Console/Command/DiffCommand.php | 3 +-- src/Tools/Console/Command/VersionCommand.php | 16 ++++++++-------- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/src/Tools/Console/Command/DiffCommand.php b/src/Tools/Console/Command/DiffCommand.php index 6cf6ebd94..6ec790128 100644 --- a/src/Tools/Console/Command/DiffCommand.php +++ b/src/Tools/Console/Command/DiffCommand.php @@ -138,7 +138,7 @@ protected function execute( $executedUnavailableMigrations = $statusCalculator->getExecutedUnavailableMigrations(); $newMigrations = $statusCalculator->getNewMigrations(); - if (! $this->checkNewMigrationsOrExecutedUnavailable($newMigrations, $executedUnavailableMigrations, $input, $output)) { + if (! $this->checkNewMigrationsOrExecutedUnavailable($newMigrations, $executedUnavailableMigrations, $input)) { $this->io->error('Migration cancelled!'); return 3; @@ -189,7 +189,6 @@ private function checkNewMigrationsOrExecutedUnavailable( AvailableMigrationsList $newMigrations, ExecutedMigrationsList $executedUnavailableMigrations, InputInterface $input, - OutputInterface $output, ): bool { if (count($newMigrations) === 0 && count($executedUnavailableMigrations) === 0) { return true; diff --git a/src/Tools/Console/Command/VersionCommand.php b/src/Tools/Console/Command/VersionCommand.php index 8397babc9..7e4053682 100644 --- a/src/Tools/Console/Command/VersionCommand.php +++ b/src/Tools/Console/Command/VersionCommand.php @@ -115,19 +115,19 @@ protected function execute(InputInterface $input, OutputInterface $output): int $confirmation = $this->io->confirm($question); if ($confirmation) { - $this->markVersions($input, $output); + $this->markVersions($input); } else { $this->io->error('Migration cancelled!'); } } else { - $this->markVersions($input, $output); + $this->markVersions($input); } return 0; } /** @throws InvalidOptionUsage */ - private function markVersions(InputInterface $input, OutputInterface $output): void + private function markVersions(InputInterface $input): void { $affectedVersion = $input->getArgument('version'); $allOption = $input->getOption('all'); @@ -151,15 +151,15 @@ private function markVersions(InputInterface $input, OutputInterface $output): v if ($allOption === true) { if ($input->getOption('delete') === true) { foreach ($executedMigrations->getItems() as $availableMigration) { - $this->mark($input, $output, $availableMigration->getVersion(), false, $executedMigrations); + $this->mark($input, $availableMigration->getVersion(), false, $executedMigrations); } } foreach ($availableVersions->getItems() as $availableMigration) { - $this->mark($input, $output, $availableMigration->getVersion(), true, $executedMigrations); + $this->mark($input, $availableMigration->getVersion(), true, $executedMigrations); } } elseif ($affectedVersion !== null) { - $this->mark($input, $output, new Version($affectedVersion), false, $executedMigrations); + $this->mark($input, new Version($affectedVersion), false, $executedMigrations); } elseif ($rangeFromOption !== null && $rangeToOption !== null) { $migrate = false; foreach ($availableVersions->getItems() as $availableMigration) { @@ -168,7 +168,7 @@ private function markVersions(InputInterface $input, OutputInterface $output): v } if ($migrate) { - $this->mark($input, $output, $availableMigration->getVersion(), true, $executedMigrations); + $this->mark($input, $availableMigration->getVersion(), true, $executedMigrations); } if ((string) $availableMigration->getVersion() === $rangeToOption) { @@ -185,7 +185,7 @@ private function markVersions(InputInterface $input, OutputInterface $output): v * @throws VersionDoesNotExist * @throws UnknownMigrationVersion */ - private function mark(InputInterface $input, OutputInterface $output, Version $version, bool $all, ExecutedMigrationsList $executedMigrations): void + private function mark(InputInterface $input, Version $version, bool $all, ExecutedMigrationsList $executedMigrations): void { try { $availableMigration = $this->getDependencyFactory()->getMigrationRepository()->getMigration($version); From 6458e87b5e576975b4d598681ca6f6867458192c Mon Sep 17 00:00:00 2001 From: "Alexander M. Turek" Date: Mon, 11 Mar 2024 23:30:28 +0100 Subject: [PATCH 4/5] Raise PHPStan level to 8 (#1409) --- phpstan.neon.dist | 2 +- src/DependencyFactory.php | 8 ++++---- src/Tools/Console/Command/DoctrineCommand.php | 8 +++++--- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 9da956f32..957f21f1c 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -1,5 +1,5 @@ parameters: - level: 7 + level: 8 paths: - src - tests diff --git a/src/DependencyFactory.php b/src/DependencyFactory.php index f415b5729..e30ebb891 100644 --- a/src/DependencyFactory.php +++ b/src/DependencyFactory.php @@ -145,7 +145,7 @@ public function getConfiguration(): Configuration { if ($this->configuration === null) { $this->configuration = $this->configurationLoader->getConfiguration(); - $this->freeze(); + $this->frozen = true; } return $this->configuration; @@ -157,7 +157,7 @@ public function getConnection(): Connection $this->connection = $this->hasEntityManager() ? $this->getEntityManager()->getConnection() : $this->connectionLoader->getConnection($this->getConfiguration()->getConnectionName()); - $this->freeze(); + $this->frozen = true; } return $this->connection; @@ -171,7 +171,7 @@ public function getEntityManager(): EntityManagerInterface } $this->em = $this->emLoader->getEntityManager($this->getConfiguration()->getEntityManagerName()); - $this->freeze(); + $this->frozen = true; } return $this->em; @@ -222,7 +222,7 @@ public function getSchemaDumper(): SchemaDumper private function getEmptySchemaProvider(): SchemaProvider { - return $this->getDependency(EmptySchemaProvider::class, fn (): SchemaProvider => new EmptySchemaProvider($this->connection->createSchemaManager())); + return $this->getDependency(EmptySchemaProvider::class, fn (): SchemaProvider => new EmptySchemaProvider($this->getConnection()->createSchemaManager())); } public function hasSchemaProvider(): bool diff --git a/src/Tools/Console/Command/DoctrineCommand.php b/src/Tools/Console/Command/DoctrineCommand.php index d7b54a5b4..01f90b140 100644 --- a/src/Tools/Console/Command/DoctrineCommand.php +++ b/src/Tools/Console/Command/DoctrineCommand.php @@ -88,16 +88,17 @@ protected function initialize(InputInterface $input, OutputInterface $output): v $configurationLoader = new ConfigurationFileWithFallback($configurationParameter); $this->dependencyFactory->setConfigurationLoader($configurationLoader); } + $dependencyFactory = $this->dependencyFactory; $this->setNamedEmOrConnection($input); - if ($this->dependencyFactory->isFrozen()) { + if ($dependencyFactory->isFrozen()) { return; } $logger = new ConsoleLogger($output); - $this->dependencyFactory->setService(LoggerInterface::class, $logger); - $this->dependencyFactory->freeze(); + $dependencyFactory->setService(LoggerInterface::class, $logger); + $dependencyFactory->freeze(); } protected function getDependencyFactory(): DependencyFactory @@ -116,6 +117,7 @@ protected function canExecute(string $question, InputInterface $input): bool private function setNamedEmOrConnection(InputInterface $input): void { + assert($this->dependencyFactory !== null); $emName = $input->getOption('em'); $connName = $input->getOption('conn'); if ($emName !== null && $connName !== null) { From 94bb945ff7416faf13eb4bb6001a757d87913774 Mon Sep 17 00:00:00 2001 From: "Alexander M. Turek" Date: Mon, 11 Mar 2024 23:57:48 +0100 Subject: [PATCH 5/5] Fix CS --- src/DependencyFactory.php | 6 +++--- src/Tools/Console/Command/DoctrineCommand.php | 2 ++ src/Version/SortedMigrationPlanCalculator.php | 2 +- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/DependencyFactory.php b/src/DependencyFactory.php index e30ebb891..fece1f5c7 100644 --- a/src/DependencyFactory.php +++ b/src/DependencyFactory.php @@ -145,7 +145,7 @@ public function getConfiguration(): Configuration { if ($this->configuration === null) { $this->configuration = $this->configurationLoader->getConfiguration(); - $this->frozen = true; + $this->frozen = true; } return $this->configuration; @@ -157,7 +157,7 @@ public function getConnection(): Connection $this->connection = $this->hasEntityManager() ? $this->getEntityManager()->getConnection() : $this->connectionLoader->getConnection($this->getConfiguration()->getConnectionName()); - $this->frozen = true; + $this->frozen = true; } return $this->connection; @@ -170,7 +170,7 @@ public function getEntityManager(): EntityManagerInterface throw MissingDependency::noEntityManager(); } - $this->em = $this->emLoader->getEntityManager($this->getConfiguration()->getEntityManagerName()); + $this->em = $this->emLoader->getEntityManager($this->getConfiguration()->getEntityManagerName()); $this->frozen = true; } diff --git a/src/Tools/Console/Command/DoctrineCommand.php b/src/Tools/Console/Command/DoctrineCommand.php index 01f90b140..cc908ec88 100644 --- a/src/Tools/Console/Command/DoctrineCommand.php +++ b/src/Tools/Console/Command/DoctrineCommand.php @@ -18,6 +18,7 @@ use Symfony\Component\Console\Style\StyleInterface; use Symfony\Component\Console\Style\SymfonyStyle; +use function assert; use function is_string; /** @@ -88,6 +89,7 @@ protected function initialize(InputInterface $input, OutputInterface $output): v $configurationLoader = new ConfigurationFileWithFallback($configurationParameter); $this->dependencyFactory->setConfigurationLoader($configurationLoader); } + $dependencyFactory = $this->dependencyFactory; $this->setNamedEmOrConnection($input); diff --git a/src/Version/SortedMigrationPlanCalculator.php b/src/Version/SortedMigrationPlanCalculator.php index f7f96a8c9..cf631b598 100644 --- a/src/Version/SortedMigrationPlanCalculator.php +++ b/src/Version/SortedMigrationPlanCalculator.php @@ -45,7 +45,7 @@ public function getPlanForVersions(array $versions, string $direction): Migratio $availableMigrations = array_filter( $migrationsToCheck, // in_array third parameter is intentionally false to force object to string casting - static fn (AvailableMigration $availableMigration): bool => in_array($availableMigration->getVersion(), $versions, false) + static fn (AvailableMigration $availableMigration): bool => in_array($availableMigration->getVersion(), $versions, false), ); $planItems = array_map(static fn (AvailableMigration $availableMigration): MigrationPlan => new MigrationPlan($availableMigration->getVersion(), $availableMigration->getMigration(), $direction), $availableMigrations);