Skip to content

Commit

Permalink
Merge branch '3.8.x' into 4.0.x
Browse files Browse the repository at this point in the history
* 3.8.x:
  Raise PHPStan level to 8 (doctrine#1409)
  Remove unused paramaters from DiffCommand and VersionCommand (doctrine#1386)
  Simplify InlineParameterFormatterTest (doctrine#1411)
  fix: Allow enum param types: ArrayParameterType and ParameterType (doctrine#1408)
  • Loading branch information
derrabus committed Mar 11, 2024
2 parents b84080d + 6458e87 commit c47e739
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 33 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
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: 7
level: 8
paths:
- src
- tests
Expand Down
10 changes: 5 additions & 5 deletions src/DependencyFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -170,8 +170,8 @@ public function getEntityManager(): EntityManagerInterface
throw MissingDependency::noEntityManager();
}

$this->em = $this->emLoader->getEntityManager($this->getConfiguration()->getEntityManagerName());
$this->freeze();
$this->em = $this->emLoader->getEntityManager($this->getConfiguration()->getEntityManagerName());
$this->frozen = true;
}

return $this->em;
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/InlineParameterFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
3 changes: 1 addition & 2 deletions src/Tools/Console/Command/DiffCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,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;
Expand Down Expand Up @@ -186,7 +186,6 @@ private function checkNewMigrationsOrExecutedUnavailable(
AvailableMigrationsList $newMigrations,
ExecutedMigrationsList $executedUnavailableMigrations,
InputInterface $input,
OutputInterface $output,
): bool {
if (count($newMigrations) === 0 && count($executedUnavailableMigrations) === 0) {
return true;
Expand Down
10 changes: 7 additions & 3 deletions src/Tools/Console/Command/DoctrineCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Symfony\Component\Console\Style\StyleInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

use function assert;
use function is_string;

/**
Expand Down Expand Up @@ -89,15 +90,17 @@ protected function initialize(InputInterface $input, OutputInterface $output): v
$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
Expand All @@ -116,6 +119,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) {
Expand Down
16 changes: 8 additions & 8 deletions src/Tools/Console/Command/VersionCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,19 +113,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');
Expand All @@ -149,15 +149,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) {
Expand All @@ -166,7 +166,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) {
Expand All @@ -183,7 +183,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);
Expand Down
2 changes: 1 addition & 1 deletion src/Version/SortedMigrationPlanCalculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
22 changes: 11 additions & 11 deletions tests/InlineParameterFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +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;

class InlineParameterFormatterTest extends TestCase
{
private Connection $connection;

private AbstractPlatform $platform;

private InlineParameterFormatter $parameterFormatter;

public function testFormatParameters(): void
Expand All @@ -35,6 +33,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',
];

Expand All @@ -54,25 +54,25 @@ public function testFormatParameters(): void
'unknown',
'unknown',
'unknown',
ParameterType::STRING,
ArrayParameterType::INTEGER,
];

$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);
}

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);
}
}

0 comments on commit c47e739

Please sign in to comment.