Skip to content

Commit 376e594

Browse files
committed
minor symfony#57901 Code style change in `@PER-CS2.0 affecting @Symfony` (parentheses for anonymous classes) (bonroyage)
This PR was squashed before being merged into the 7.2 branch. Discussion ---------- Code style change in ``@PER`-CS2.0` affecting ``@Symfony`` (parentheses for anonymous classes) | Q | A | ------------- | --- | Branch? | 7.2 | Bug fix? | no | New feature? | no | Deprecations? | no | Issues | - | License | MIT I have created a PR (PHP-CS-Fixer/PHP-CS-Fixer#8140) in the PHP-CS-Fixer repo to bring the ``@PER`-CS2.0` ruleset in line with the specifications on the `new_with_parentheses` rule for anonymous classes. Since the ``@Symfony`` ruleset builds upon the ``@PER`-CS2.0` ruleset, they would like confirmation that the Symfony community is OK with this change affecting the Symfony ruleset as well. Should it not be, I'll push another commit there ensuring that the change does not affect ``@Symfony``. Therefore, this PR is not meant to be merged, but function as an RFC to get your opinion and show the effect it would have when applied to the Symfony source. Commits ------- 506e0dd Code style change in ``@PER`-CS2.0` affecting ``@Symfony`` (parentheses for anonymous classes)
2 parents b7e0258 + 506e0dd commit 376e594

File tree

108 files changed

+202
-201
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

108 files changed

+202
-201
lines changed

.php-cs-fixer.dist.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
'nullable_type_declaration' => true,
3838
'nullable_type_declaration_for_default_null_value' => true,
3939
'trailing_comma_in_multiline' => ['elements' => ['arrays', 'match', 'parameters']],
40+
'new_with_parentheses' => ['anonymous_class' => false],
4041
])
4142
->setRiskyAllowed(true)
4243
->setFinder(

src/Symfony/Bridge/Doctrine/Tests/Messenger/DoctrineOpenTransactionLoggerMiddlewareTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class DoctrineOpenTransactionLoggerMiddlewareTest extends MiddlewareTestCase
2929

3030
protected function setUp(): void
3131
{
32-
$this->logger = new class() extends AbstractLogger {
32+
$this->logger = new class extends AbstractLogger {
3333
public array $logs = [];
3434

3535
public function log($level, $message, $context = []): void

src/Symfony/Bridge/Doctrine/Tests/Validator/Constraints/UniqueEntityValidatorTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -926,7 +926,7 @@ public static function resultWithEmptyIterator(): array
926926
$entity = new SingleIntIdEntity(1, 'foo');
927927

928928
return [
929-
[$entity, new class() implements \Iterator {
929+
[$entity, new class implements \Iterator {
930930
public function current(): mixed
931931
{
932932
return null;
@@ -950,7 +950,7 @@ public function rewind(): void
950950
{
951951
}
952952
}],
953-
[$entity, new class() implements \Iterator {
953+
[$entity, new class implements \Iterator {
954954
public function current(): mixed
955955
{
956956
return false;

src/Symfony/Bridge/Twig/Extension/TranslationExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function getTranslator(): TranslatorInterface
4747
throw new \LogicException(\sprintf('You cannot use the "%s" if the Translation Contracts are not available. Try running "composer require symfony/translation".', __CLASS__));
4848
}
4949

50-
$this->translator = new class() implements TranslatorInterface {
50+
$this->translator = new class implements TranslatorInterface {
5151
use TranslatorTrait;
5252
};
5353
}

src/Symfony/Bridge/Twig/Tests/Extension/AbstractLayoutTestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2652,7 +2652,7 @@ public function testHelpWithTranslatableMessage()
26522652

26532653
public function testHelpWithTranslatableInterface()
26542654
{
2655-
$message = new class() implements TranslatableInterface {
2655+
$message = new class implements TranslatableInterface {
26562656
public function trans(TranslatorInterface $translator, ?string $locale = null): string
26572657
{
26582658
return $translator->trans('foo');

src/Symfony/Bundle/FrameworkBundle/Tests/CacheWarmer/ConfigBuilderCacheWarmerTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ public function testExtensionAddedInKernel()
189189
$kernel = new class($this->varDir) extends TestKernel {
190190
protected function build(ContainerBuilder $container): void
191191
{
192-
$container->registerExtension(new class() extends Extension implements ConfigurationInterface {
192+
$container->registerExtension(new class extends Extension implements ConfigurationInterface {
193193
public function load(array $configs, ContainerBuilder $container): void
194194
{
195195
}
@@ -276,7 +276,7 @@ protected function build(ContainerBuilder $container): void
276276
{
277277
/** @var TestSecurityExtension $extension */
278278
$extension = $container->getExtension('test_security');
279-
$extension->addAuthenticatorFactory(new class() implements TestAuthenticatorFactoryInterface {
279+
$extension->addAuthenticatorFactory(new class implements TestAuthenticatorFactoryInterface {
280280
public function getKey(): string
281281
{
282282
return 'token';
@@ -292,19 +292,19 @@ public function registerBundles(): iterable
292292
{
293293
yield from parent::registerBundles();
294294

295-
yield new class() extends Bundle {
295+
yield new class extends Bundle {
296296
public function getContainerExtension(): ExtensionInterface
297297
{
298298
return new TestSecurityExtension();
299299
}
300300
};
301301

302-
yield new class() extends Bundle {
302+
yield new class extends Bundle {
303303
public function build(ContainerBuilder $container): void
304304
{
305305
/** @var TestSecurityExtension $extension */
306306
$extension = $container->getExtension('test_security');
307-
$extension->addAuthenticatorFactory(new class() implements TestAuthenticatorFactoryInterface {
307+
$extension->addAuthenticatorFactory(new class implements TestAuthenticatorFactoryInterface {
308308
public function getKey(): string
309309
{
310310
return 'form-login';

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/ProfilerPassTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function testValidCollector()
6666

6767
public static function provideValidCollectorWithTemplateUsingAutoconfigure(): \Generator
6868
{
69-
yield [new class() implements TemplateAwareDataCollectorInterface {
69+
yield [new class implements TemplateAwareDataCollectorInterface {
7070
public function collect(Request $request, Response $response, ?\Throwable $exception = null): void
7171
{
7272
}
@@ -86,7 +86,7 @@ public static function getTemplate(): string
8686
}
8787
}];
8888

89-
yield [new class() extends AbstractDataCollector {
89+
yield [new class extends AbstractDataCollector {
9090
public function collect(Request $request, Response $response, ?\Throwable $exception = null): void
9191
{
9292
}

src/Symfony/Bundle/FrameworkBundle/Tests/Test/WebTestCaseTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ private function getRequestTester(): WebTestCase
388388

389389
private function getTester(KernelBrowser $client): WebTestCase
390390
{
391-
$tester = new class() extends WebTestCase {
391+
$tester = new class extends WebTestCase {
392392
use WebTestAssertionsTrait {
393393
getClient as public;
394394
}

src/Symfony/Bundle/SecurityBundle/Tests/DataCollector/SecurityDataCollectorTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ public function testCollectCollectsDecisionLogWhenStrategyIsAffirmative()
226226
$voter1 = new DummyVoter();
227227
$voter2 = new DummyVoter();
228228

229-
$decoratedVoter1 = new TraceableVoter($voter1, new class() implements EventDispatcherInterface {
229+
$decoratedVoter1 = new TraceableVoter($voter1, new class implements EventDispatcherInterface {
230230
public function dispatch(object $event, ?string $eventName = null): object
231231
{
232232
return new \stdClass();
@@ -301,7 +301,7 @@ public function testCollectCollectsDecisionLogWhenStrategyIsUnanimous()
301301
$voter1 = new DummyVoter();
302302
$voter2 = new DummyVoter();
303303

304-
$decoratedVoter1 = new TraceableVoter($voter1, new class() implements EventDispatcherInterface {
304+
$decoratedVoter1 = new TraceableVoter($voter1, new class implements EventDispatcherInterface {
305305
public function dispatch(object $event, ?string $eventName = null): object
306306
{
307307
return new \stdClass();

src/Symfony/Component/AssetMapper/Tests/AssetMapperCompilerTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class AssetMapperCompilerTest extends TestCase
2121
{
2222
public function testCompile()
2323
{
24-
$compiler1 = new class() implements AssetCompilerInterface {
24+
$compiler1 = new class implements AssetCompilerInterface {
2525
public function supports(MappedAsset $asset): bool
2626
{
2727
return 'css' === $asset->publicExtension;
@@ -33,7 +33,7 @@ public function compile(string $content, MappedAsset $asset, AssetMapperInterfac
3333
}
3434
};
3535

36-
$compiler2 = new class() implements AssetCompilerInterface {
36+
$compiler2 = new class implements AssetCompilerInterface {
3737
public function supports(MappedAsset $asset): bool
3838
{
3939
return 'js' === $asset->publicExtension;
@@ -45,7 +45,7 @@ public function compile(string $content, MappedAsset $asset, AssetMapperInterfac
4545
}
4646
};
4747

48-
$compiler3 = new class() implements AssetCompilerInterface {
48+
$compiler3 = new class implements AssetCompilerInterface {
4949
public function supports(MappedAsset $asset): bool
5050
{
5151
return 'js' === $asset->publicExtension;

src/Symfony/Component/AssetMapper/Tests/Factory/MappedAssetFactoryTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function testCreateMappedAssetRespectsPreDigestedPaths()
5050

5151
public function testCreateMappedAssetWithContentThatChanged()
5252
{
53-
$file1Compiler = new class() implements AssetCompilerInterface {
53+
$file1Compiler = new class implements AssetCompilerInterface {
5454
public function supports(MappedAsset $asset): bool
5555
{
5656
return true;
@@ -92,7 +92,7 @@ public function testCreateMappedAssetWithContentErrorsOnCircularReferences()
9292

9393
public function testCreateMappedAssetWithDigest()
9494
{
95-
$file6Compiler = new class() implements AssetCompilerInterface {
95+
$file6Compiler = new class implements AssetCompilerInterface {
9696
public function supports(MappedAsset $asset): bool
9797
{
9898
return true;

src/Symfony/Component/Cache/Tests/Messenger/EarlyExpirationDispatcherTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function testFetch()
4141

4242
$item = $pool->getItem('foo');
4343

44-
$computationService = new class() {
44+
$computationService = new class {
4545
public function __invoke(CacheItem $item)
4646
{
4747
return 123;
@@ -90,7 +90,7 @@ public function testEarlyExpiration()
9090
$pool->save($item->set(789));
9191
$item = $pool->getItem('foo');
9292

93-
$computationService = new class() {
93+
$computationService = new class {
9494
public function __invoke(CacheItem $item)
9595
{
9696
return 123;

src/Symfony/Component/Cache/Tests/Messenger/EarlyExpirationHandlerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function testHandle()
3838
$item = $pool->getItem('foo');
3939
$item->set(234);
4040

41-
$computationService = new class() implements CallbackInterface {
41+
$computationService = new class implements CallbackInterface {
4242
public function __invoke(CacheItemInterface $item, bool &$save): mixed
4343
{
4444
usleep(30000);

src/Symfony/Component/Cache/Tests/Messenger/EarlyExpirationMessageTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function testCreate()
2727
$item = $pool->getItem('foo');
2828
$item->set(234);
2929

30-
$computationService = new class() {
30+
$computationService = new class {
3131
public function __invoke(CacheItem $item)
3232
{
3333
return 123;

src/Symfony/Component/Cache/Tests/Traits/RedisTraitTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function testCreateConnection(string $dsn, string $expectedClass)
3232
self::markTestSkipped('REDIS_CLUSTER_HOSTS env var is not defined.');
3333
}
3434

35-
$mock = new class() {
35+
$mock = new class {
3636
use RedisTrait;
3737
};
3838
$connection = $mock::createConnection($dsn);
@@ -46,7 +46,7 @@ public function testUrlDecodeParameters()
4646
self::markTestSkipped('REDIS_AUTHENTICATED_HOST env var is not defined.');
4747
}
4848

49-
$mock = new class() {
49+
$mock = new class {
5050
use RedisTrait;
5151
};
5252
$connection = $mock::createConnection('redis://:p%40ssword@'.getenv('REDIS_AUTHENTICATED_HOST'));
@@ -105,7 +105,7 @@ public function testPconnectSelectsCorrectDatabase()
105105
}
106106

107107
try {
108-
$mock = new class() {
108+
$mock = new class {
109109
use RedisTrait;
110110
};
111111

@@ -143,7 +143,7 @@ public function testDbIndexDsnParameter(string $dsn, int $expectedDb)
143143
self::markTestSkipped('REDIS_AUTHENTICATED_HOST env var is not defined.');
144144
}
145145

146-
$mock = new class () {
146+
$mock = new class {
147147
use RedisTrait;
148148
};
149149
$connection = $mock::createConnection($dsn);
@@ -190,7 +190,7 @@ public function testInvalidDbIndexDsnParameter(string $dsn)
190190
}
191191
$this->expectException(InvalidArgumentException::class);
192192

193-
$mock = new class () {
193+
$mock = new class {
194194
use RedisTrait;
195195
};
196196
$mock::createConnection($dsn);

src/Symfony/Component/Clock/Tests/ClockTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function testMockClockFreeze()
7474

7575
public function testPsrClock()
7676
{
77-
$psrClock = new class() implements ClockInterface {
77+
$psrClock = new class implements ClockInterface {
7878
public function now(): \DateTimeImmutable
7979
{
8080
return new \DateTimeImmutable('@1234567');

src/Symfony/Component/Console/Tests/ApplicationTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -965,7 +965,7 @@ public function testRenderAnonymousException()
965965
$application = new Application();
966966
$application->setAutoExit(false);
967967
$application->register('foo')->setCode(function () {
968-
throw new \InvalidArgumentException(\sprintf('Dummy type "%s" is invalid.', (new class() {})::class));
968+
throw new \InvalidArgumentException(\sprintf('Dummy type "%s" is invalid.', (new class {})::class));
969969
});
970970
$tester = new ApplicationTester($application);
971971

@@ -991,7 +991,7 @@ public function testRenderExceptionStackTraceContainsRootException()
991991
$application = new Application();
992992
$application->setAutoExit(false);
993993
$application->register('foo')->setCode(function () {
994-
throw new \InvalidArgumentException(\sprintf('Dummy type "%s" is invalid.', (new class() {})::class));
994+
throw new \InvalidArgumentException(\sprintf('Dummy type "%s" is invalid.', (new class {})::class));
995995
});
996996
$tester = new ApplicationTester($application);
997997

src/Symfony/Component/Console/Tests/Command/SingleCommandApplicationTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class SingleCommandApplicationTest extends TestCase
2121
{
2222
public function testRun()
2323
{
24-
$command = new class() extends SingleCommandApplication {
24+
$command = new class extends SingleCommandApplication {
2525
protected function execute(InputInterface $input, OutputInterface $output): int
2626
{
2727
return 0;

src/Symfony/Component/Console/Tests/Messenger/RunCommandMessageHandlerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ private function createApplicationWithCommand(): Application
8686
$application = new Application();
8787
$application->setAutoExit(false);
8888
$application->addCommands([
89-
new class() extends Command {
89+
new class extends Command {
9090
public function configure(): void
9191
{
9292
$this

src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class AutowirePass extends AbstractRecursivePass
5050
public function __construct(
5151
private bool $throwOnAutowiringException = true,
5252
) {
53-
$this->defaultArgument = new class() {
53+
$this->defaultArgument = new class {
5454
public $value;
5555
public $names;
5656
public $bag;

src/Symfony/Component/DependencyInjection/Compiler/CheckExceptionOnInvalidReferenceBehaviorPass.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ private function throwServiceNotFoundException(Reference $ref, string $sourceId,
9898
}
9999
}
100100

101-
$pass = new class() extends AbstractRecursivePass {
101+
$pass = new class extends AbstractRecursivePass {
102102
public Reference $ref;
103103
public string $sourceId;
104104
public array $alternatives;

src/Symfony/Component/DependencyInjection/Tests/Compiler/AbstractRecursivePassTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function testGetConstructorResolvesFactoryChildDefinitionsClass()
3535
->register('foo', \stdClass::class)
3636
->setFactory([new Reference('child'), 'createFactory']);
3737

38-
$pass = new class() extends AbstractRecursivePass {
38+
$pass = new class extends AbstractRecursivePass {
3939
public \ReflectionMethod $actual;
4040

4141
protected function processValue($value, $isRoot = false): mixed
@@ -61,7 +61,7 @@ public function testGetConstructorResolvesChildDefinitionsClass()
6161
->setAbstract(true);
6262
$container->setDefinition('foo', new ChildDefinition('parent'));
6363

64-
$pass = new class() extends AbstractRecursivePass {
64+
$pass = new class extends AbstractRecursivePass {
6565
public \ReflectionMethod $actual;
6666

6767
protected function processValue($value, $isRoot = false): mixed
@@ -87,7 +87,7 @@ public function testGetReflectionMethodResolvesChildDefinitionsClass()
8787
->setAbstract(true);
8888
$container->setDefinition('foo', new ChildDefinition('parent'));
8989

90-
$pass = new class() extends AbstractRecursivePass {
90+
$pass = new class extends AbstractRecursivePass {
9191
public \ReflectionMethod $actual;
9292

9393
protected function processValue($value, $isRoot = false): mixed
@@ -113,7 +113,7 @@ public function testGetConstructorDefinitionNoClass()
113113
$this->expectException(RuntimeException::class);
114114
$this->expectExceptionMessage('Invalid service "foo": the class is not set.');
115115

116-
(new class() extends AbstractRecursivePass {
116+
(new class extends AbstractRecursivePass {
117117
protected function processValue($value, $isRoot = false): mixed
118118
{
119119
if ($value instanceof Definition && 'foo' === $this->currentId) {

src/Symfony/Component/DependencyInjection/Tests/Compiler/CheckDefinitionValidityPassTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,17 +108,17 @@ public static function provideInvalidTags(): iterable
108108
$message = 'A "tags" attribute must be of a scalar-type for service "a", tag "%s", attribute "%s".';
109109
yield 'object attribute value' => [
110110
'foo',
111-
['bar' => new class() {}],
111+
['bar' => new class {}],
112112
\sprintf($message, 'foo', 'bar'),
113113
];
114114
yield 'nested object attribute value' => [
115115
'foo',
116-
['bar' => ['baz' => new class() {}]],
116+
['bar' => ['baz' => new class {}]],
117117
\sprintf($message, 'foo', 'bar.baz'),
118118
];
119119
yield 'deeply nested object attribute value' => [
120120
'foo',
121-
['bar' => ['baz' => ['qux' => new class() {}]]],
121+
['bar' => ['baz' => ['qux' => new class {}]]],
122122
\sprintf($message, 'foo', 'bar.baz.qux'),
123123
];
124124
}

src/Symfony/Component/DependencyInjection/Tests/Compiler/CustomExpressionLanguageFunctionTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function testDump()
2727
->setPublic(true)
2828
->setArguments([new Expression('custom_func("foobar")')]);
2929

30-
$container->addExpressionLanguageProvider(new class() implements ExpressionFunctionProviderInterface {
30+
$container->addExpressionLanguageProvider(new class implements ExpressionFunctionProviderInterface {
3131
public function getFunctions(): array
3232
{
3333
return [

0 commit comments

Comments
 (0)