Skip to content

Commit 5d21bb1

Browse files
authored
Fix calls to Application::add() (doctrine#12006)
1 parent 7155010 commit 5d21bb1

13 files changed

+72
-15
lines changed

phpstan-persistence2.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ parameters:
1111

1212
# Fallback logic for DBAL 2
1313
-
14-
message: '/Application::add\(\) expects Symfony\\Component\\Console\\Command\\Command/'
14+
message: '/Parameter #2 \$command of static method Doctrine\\ORM\\Tools\\Console\\ConsoleRunner\:\:addCommandToApplication\(\) expects Symfony\\Component\\Console\\Command\\Command/'
1515
path: src/Tools/Console/ConsoleRunner.php
1616

1717
- '/^Class Doctrine\\DBAL\\Platforms\\SQLAnywherePlatform not found\.$/'

phpstan.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ parameters:
99

1010
# Fallback logic for DBAL 2
1111
-
12-
message: '/Application::add\(\) expects Symfony\\Component\\Console\\Command\\Command/'
12+
message: '/Parameter #2 \$command of static method Doctrine\\ORM\\Tools\\Console\\ConsoleRunner\:\:addCommandToApplication\(\) expects Symfony\\Component\\Console\\Command\\Command/'
1313
path: src/Tools/Console/ConsoleRunner.php
1414

1515
- '/^Class Doctrine\\DBAL\\Platforms\\SQLAnywherePlatform not found\.$/'
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\ORM\Tools\Console;
6+
7+
use Symfony\Component\Console\Application;
8+
use Symfony\Component\Console\Command\Command;
9+
10+
use function method_exists;
11+
12+
/**
13+
* Forward compatibility with Symfony Console 7.4
14+
*
15+
* @internal
16+
*/
17+
trait ApplicationCompatibility
18+
{
19+
private static function addCommandToApplication(Application $application, Command $command): ?Command
20+
{
21+
if (method_exists(Application::class, 'addCommand')) {
22+
// @phpstan-ignore method.notFound (This method will be added in Symfony 7.4)
23+
return $application->addCommand($command);
24+
}
25+
26+
return $application->add($command);
27+
}
28+
}

src/Tools/Console/ConsoleRunner.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
*/
2424
final class ConsoleRunner
2525
{
26+
use ApplicationCompatibility;
27+
2628
/**
2729
* Create a Symfony Console HelperSet
2830
*
@@ -91,7 +93,7 @@ public static function addCommands(Application $cli, ?EntityManagerProvider $ent
9193
$connectionProvider = new ConnectionFromManagerProvider($entityManagerProvider);
9294

9395
if (class_exists(DBALConsole\Command\ImportCommand::class)) {
94-
$cli->add(new DBALConsole\Command\ImportCommand());
96+
self::addCommandToApplication($cli, new DBALConsole\Command\ImportCommand());
9597
}
9698

9799
$cli->addCommands(

tests/Tests/ORM/Tools/Console/Command/ClearCacheCollectionRegionCommandTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Doctrine\Tests\ORM\Tools\Console\Command;
66

7+
use Doctrine\ORM\Tools\Console\ApplicationCompatibility;
78
use Doctrine\ORM\Tools\Console\Command\ClearCache\CollectionRegionCommand;
89
use Doctrine\ORM\Tools\Console\EntityManagerProvider\SingleManagerProvider;
910
use Doctrine\Tests\Models\Cache\State;
@@ -14,6 +15,8 @@
1415
/** @group DDC-2183 */
1516
class ClearCacheCollectionRegionCommandTest extends OrmFunctionalTestCase
1617
{
18+
use ApplicationCompatibility;
19+
1720
/** @var Application */
1821
private $application;
1922

@@ -29,7 +32,7 @@ protected function setUp(): void
2932
$this->command = new CollectionRegionCommand(new SingleManagerProvider($this->_em));
3033

3134
$this->application = new Application();
32-
$this->application->add($this->command);
35+
self::addCommandToApplication($this->application, $this->command);
3336
}
3437

3538
public function testClearAllRegion(): void

tests/Tests/ORM/Tools/Console/Command/ClearCacheEntityRegionCommandTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Doctrine\Tests\ORM\Tools\Console\Command;
66

7+
use Doctrine\ORM\Tools\Console\ApplicationCompatibility;
78
use Doctrine\ORM\Tools\Console\Command\ClearCache\EntityRegionCommand;
89
use Doctrine\ORM\Tools\Console\EntityManagerProvider\SingleManagerProvider;
910
use Doctrine\Tests\Models\Cache\Country;
@@ -17,6 +18,8 @@
1718
/** @group DDC-2183 */
1819
class ClearCacheEntityRegionCommandTest extends OrmFunctionalTestCase
1920
{
21+
use ApplicationCompatibility;
22+
2023
/** @var Application */
2124
private $application;
2225

@@ -32,7 +35,7 @@ protected function setUp(): void
3235
$this->command = new EntityRegionCommand(new SingleManagerProvider($this->_em));
3336

3437
$this->application = new Application();
35-
$this->application->add($this->command);
38+
self::addCommandToApplication($this->application, $this->command);
3639
}
3740

3841
public function testClearAllRegion(): void

tests/Tests/ORM/Tools/Console/Command/ClearCacheQueryRegionCommandTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Doctrine\Tests\ORM\Tools\Console\Command;
66

7+
use Doctrine\ORM\Tools\Console\ApplicationCompatibility;
78
use Doctrine\ORM\Tools\Console\Command\ClearCache\QueryRegionCommand;
89
use Doctrine\ORM\Tools\Console\EntityManagerProvider\SingleManagerProvider;
910
use Doctrine\Tests\OrmFunctionalTestCase;
@@ -13,6 +14,8 @@
1314
/** @group DDC-2183 */
1415
class ClearCacheQueryRegionCommandTest extends OrmFunctionalTestCase
1516
{
17+
use ApplicationCompatibility;
18+
1619
/** @var Application */
1720
private $application;
1821

@@ -28,7 +31,7 @@ protected function setUp(): void
2831
$this->command = new QueryRegionCommand(new SingleManagerProvider($this->_em));
2932

3033
$this->application = new Application();
31-
$this->application->add($this->command);
34+
self::addCommandToApplication($this->application, $this->command);
3235
}
3336

3437
public function testClearAllRegion(): void

tests/Tests/ORM/Tools/Console/Command/EnsureProductionSettingsCommandTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Doctrine\DBAL\Connection;
88
use Doctrine\ORM\Configuration;
99
use Doctrine\ORM\EntityManagerInterface;
10+
use Doctrine\ORM\Tools\Console\ApplicationCompatibility;
1011
use Doctrine\ORM\Tools\Console\Command\EnsureProductionSettingsCommand;
1112
use Doctrine\ORM\Tools\Console\EntityManagerProvider\SingleManagerProvider;
1213
use Doctrine\Tests\DoctrineTestCase;
@@ -18,6 +19,8 @@
1819

1920
class EnsureProductionSettingsCommandTest extends DoctrineTestCase
2021
{
22+
use ApplicationCompatibility;
23+
2124
public function testExecute(): void
2225
{
2326
$em = $this->createMock(EntityManagerInterface::class);
@@ -103,7 +106,7 @@ private function executeCommand(
103106
array $input = []
104107
): int {
105108
$application = new Application();
106-
$application->add(new EnsureProductionSettingsCommand(new SingleManagerProvider($em)));
109+
self::addCommandToApplication($application, new EnsureProductionSettingsCommand(new SingleManagerProvider($em)));
107110

108111
$command = $application->find('orm:ensure-production-settings');
109112
$tester = new CommandTester($command);

tests/Tests/ORM/Tools/Console/Command/GenerateRepositoriesCommandTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Doctrine\ORM\Configuration;
88
use Doctrine\ORM\EntityManagerInterface;
99
use Doctrine\ORM\EntityRepository;
10+
use Doctrine\ORM\Tools\Console\ApplicationCompatibility;
1011
use Doctrine\ORM\Tools\Console\Command\GenerateRepositoriesCommand;
1112
use Doctrine\ORM\Tools\Console\EntityManagerProvider\SingleManagerProvider;
1213
use Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper;
@@ -35,6 +36,8 @@
3536

3637
class GenerateRepositoriesCommandTest extends OrmFunctionalTestCase
3738
{
39+
use ApplicationCompatibility;
40+
3841
/** @var Application */
3942
private $application;
4043

@@ -53,7 +56,7 @@ protected function setUp(): void
5356
$metadataDriver->addPaths([__DIR__ . '/../../../../Models/DDC3231/']);
5457

5558
$this->application = new Application();
56-
$this->application->add(new GenerateRepositoriesCommand(new SingleManagerProvider($this->_em)));
59+
self::addCommandToApplication($this->application, new GenerateRepositoriesCommand(new SingleManagerProvider($this->_em)));
5760
}
5861

5962
public function tearDown(): void
@@ -163,7 +166,7 @@ public function testNoMetadataClassesToProcess(): void
163166

164167
$application = new Application();
165168
$application->setHelperSet(new HelperSet(['em' => new EntityManagerHelper($em)]));
166-
$application->add(new GenerateRepositoriesCommand());
169+
self::addCommandToApplication($application, new GenerateRepositoriesCommand());
167170

168171
$command = $application->find('orm:generate-repositories');
169172
$tester = new CommandTester($command);

tests/Tests/ORM/Tools/Console/Command/InfoCommandTest.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Doctrine\ORM\Configuration;
88
use Doctrine\ORM\EntityManagerInterface;
99
use Doctrine\ORM\Mapping\MappingException;
10+
use Doctrine\ORM\Tools\Console\ApplicationCompatibility;
1011
use Doctrine\ORM\Tools\Console\Command\InfoCommand;
1112
use Doctrine\ORM\Tools\Console\EntityManagerProvider\SingleManagerProvider;
1213
use Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper;
@@ -20,6 +21,8 @@
2021

2122
class InfoCommandTest extends OrmFunctionalTestCase
2223
{
24+
use ApplicationCompatibility;
25+
2326
/** @var Application */
2427
private $application;
2528

@@ -35,7 +38,7 @@ protected function setUp(): void
3538

3639
$this->application = new Application();
3740

38-
$this->application->add(new InfoCommand(new SingleManagerProvider($this->_em)));
41+
self::addCommandToApplication($this->application, new InfoCommand(new SingleManagerProvider($this->_em)));
3942

4043
$this->command = $this->application->find('orm:info');
4144
$this->tester = new CommandTester($this->command);
@@ -66,7 +69,7 @@ public function testEmptyEntityClassNames(): void
6669

6770
$application = new Application();
6871
$application->setHelperSet(new HelperSet(['em' => new EntityManagerHelper($em)]));
69-
$application->add(new InfoCommand());
72+
self::addCommandToApplication($application, new InfoCommand());
7073

7174
$command = $application->find('orm:info');
7275
$tester = new CommandTester($command);
@@ -105,7 +108,7 @@ public function testInvalidEntityClassMetadata(): void
105108

106109
$application = new Application();
107110
$application->setHelperSet(new HelperSet(['em' => new EntityManagerHelper($em)]));
108-
$application->add(new InfoCommand());
111+
self::addCommandToApplication($application, new InfoCommand());
109112

110113
$command = $application->find('orm:info');
111114
$tester = new CommandTester($command);

0 commit comments

Comments
 (0)