Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5f7f83c

Browse files
minor #47672 Leverage First-class callable syntax (tigitz)
This PR was merged into the 6.2 branch. Discussion ---------- Leverage First-class callable syntax | Q | A | ------------- | --- | Branch? | 6.2 | Bug fix? | no | New feature? | no | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | - | License | MIT | Doc PR | - ### Rationale https://wiki.php.net/rfc/first_class_callable_syntax Mainly: > The advantage is that the new syntax is accessible to static analysis, and respects the scope at the point where the callable is created. I'd argue that it also improves readability and IDE color syntax also helps: ![image](https://user-images.githubusercontent.com/1524501/191912084-7ee933c5-dda1-4176-86f1-cd6511c58aa4.png) I've manually reviewed each changes and discarded some of them where `[Foo::class, 'method']` was intended to be tested with this specific syntax Commits ------- 26d9ce9 Leverage First-class callable syntax
2 parents a0b82ee + 26d9ce9 commit 5f7f83c

File tree

19 files changed

+64
-48
lines changed

19 files changed

+64
-48
lines changed

src/Symfony/Bridge/Doctrine/Middleware/Debug/DebugDataHolder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function addQuery(string $connectionName, Query $query): void
2424
'sql' => $query->getSql(),
2525
'params' => $query->getParams(),
2626
'types' => $query->getTypes(),
27-
'executionMS' => [$query, 'getDuration'], // stop() may not be called at this point
27+
'executionMS' => $query->getDuration(...), // stop() may not be called at this point
2828
];
2929
}
3030

src/Symfony/Bundle/FrameworkBundle/Tests/Kernel/MicroKernelTraitTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ protected function configureContainer(ContainerConfigurator $c): void
129129

130130
protected function configureRoutes(RoutingConfigurator $routes): void
131131
{
132-
$routes->add('hello', '/')->controller([$this, 'helloAction']);
132+
$routes->add('hello', '/')->controller($this->helloAction(...));
133133
}
134134
};
135135

src/Symfony/Component/Config/Definition/Dumper/YamlReferenceDumper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ private function writeNode(NodeInterface $node, NodeInterface $parentNode = null
170170

171171
$this->writeLine('# '.$message.':', $depth * 4 + 4);
172172

173-
$this->writeArray(array_map([Inline::class, 'dump'], $example), $depth + 1);
173+
$this->writeArray(array_map(Inline::dump(...), $example), $depth + 1);
174174
}
175175

176176
if ($children) {

src/Symfony/Component/Console/Helper/Table.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,7 @@ private function buildTableRows(array $rows): TableRows
620620
if (!str_contains($cell ?? '', "\n")) {
621621
continue;
622622
}
623-
$escaped = implode("\n", array_map([OutputFormatter::class, 'escapeTrailingBackslash'], explode("\n", $cell)));
623+
$escaped = implode("\n", array_map(OutputFormatter::escapeTrailingBackslash(...), explode("\n", $cell)));
624624
$cell = $cell instanceof TableCell ? new TableCell($escaped, ['colspan' => $cell->getColspan()]) : $escaped;
625625
$lines = explode("\n", str_replace("\n", "<fg=default;bg=default></>\n", $cell));
626626
foreach ($lines as $lineKey => $line) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ private static function createClosure()
405405
public function testSetCodeWithNonClosureCallable()
406406
{
407407
$command = new \TestCommand();
408-
$ret = $command->setCode([$this, 'callableMethodCommand']);
408+
$ret = $command->setCode($this->callableMethodCommand(...));
409409
$this->assertEquals($command, $ret, '->setCode() implements a fluent interface');
410410
$tester = new CommandTester($command);
411411
$tester->execute([]);

src/Symfony/Component/DependencyInjection/Loader/IniFileLoader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function load(mixed $resource, string $type = null): mixed
3939
if (isset($result['parameters']) && \is_array($result['parameters'])) {
4040
foreach ($result['parameters'] as $key => $value) {
4141
if (\is_array($value)) {
42-
$this->container->setParameter($key, array_map([$this, 'phpize'], $value));
42+
$this->container->setParameter($key, array_map($this->phpize(...), $value));
4343
} else {
4444
$this->container->setParameter($key, $this->phpize($value));
4545
}

src/Symfony/Component/EventDispatcher/Tests/EventDispatcherTest.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function testInitialState()
5757
public function testAddListener()
5858
{
5959
$this->dispatcher->addListener('pre.foo', [$this->listener, 'preFoo']);
60-
$this->dispatcher->addListener('post.foo', [$this->listener, 'postFoo']);
60+
$this->dispatcher->addListener('post.foo', $this->listener->postFoo(...));
6161
$this->assertTrue($this->dispatcher->hasListeners());
6262
$this->assertTrue($this->dispatcher->hasListeners(self::preFoo));
6363
$this->assertTrue($this->dispatcher->hasListeners(self::postFoo));
@@ -71,21 +71,25 @@ public function testGetListenersSortsByPriority()
7171
$listener1 = new TestEventListener();
7272
$listener2 = new TestEventListener();
7373
$listener3 = new TestEventListener();
74+
$listener4 = new TestEventListener();
7475
$listener1->name = '1';
7576
$listener2->name = '2';
7677
$listener3->name = '3';
78+
$listener4->name = '4';
7779

7880
$this->dispatcher->addListener('pre.foo', [$listener1, 'preFoo'], -10);
7981
$this->dispatcher->addListener('pre.foo', [$listener2, 'preFoo'], 10);
8082
$this->dispatcher->addListener('pre.foo', [$listener3, 'preFoo']);
83+
$this->dispatcher->addListener('pre.foo', $listener4->preFoo(...), 20);
8184

8285
$expected = [
86+
$listener4->preFoo(...),
8387
[$listener2, 'preFoo'],
8488
[$listener3, 'preFoo'],
8589
[$listener1, 'preFoo'],
8690
];
8791

88-
$this->assertSame($expected, $this->dispatcher->getListeners('pre.foo'));
92+
$this->assertEquals($expected, $this->dispatcher->getListeners('pre.foo'));
8993
}
9094

9195
public function testGetAllListenersSortsByPriority()
@@ -129,7 +133,7 @@ public function testGetListenerPriority()
129133
public function testDispatch()
130134
{
131135
$this->dispatcher->addListener('pre.foo', [$this->listener, 'preFoo']);
132-
$this->dispatcher->addListener('post.foo', [$this->listener, 'postFoo']);
136+
$this->dispatcher->addListener('post.foo', $this->listener->postFoo(...));
133137
$this->dispatcher->dispatch(new Event(), self::preFoo);
134138
$this->assertTrue($this->listener->preFooInvoked);
135139
$this->assertFalse($this->listener->postFooInvoked);
@@ -160,7 +164,7 @@ public function testStopEventPropagation()
160164
// be executed
161165
// Manually set priority to enforce $this->listener to be called first
162166
$this->dispatcher->addListener('post.foo', [$this->listener, 'postFoo'], 10);
163-
$this->dispatcher->addListener('post.foo', [$otherListener, 'postFoo']);
167+
$this->dispatcher->addListener('post.foo', $otherListener->postFoo(...));
164168
$this->dispatcher->dispatch(new Event(), self::postFoo);
165169
$this->assertTrue($this->listener->postFooInvoked);
166170
$this->assertFalse($otherListener->postFooInvoked);
@@ -386,7 +390,7 @@ public function testMutatingWhilePropagationIsStopped()
386390
{
387391
$testLoaded = false;
388392
$test = new TestEventListener();
389-
$this->dispatcher->addListener('foo', [$test, 'postFoo']);
393+
$this->dispatcher->addListener('foo', $test->postFoo(...));
390394
$this->dispatcher->addListener('foo', [function () use ($test, &$testLoaded) {
391395
$testLoaded = true;
392396

@@ -398,7 +402,7 @@ public function testMutatingWhilePropagationIsStopped()
398402
$this->assertTrue($test->postFooInvoked);
399403
$this->assertFalse($test->preFooInvoked);
400404

401-
$this->assertsame(0, $this->dispatcher->getListenerPriority('foo', [$test, 'preFoo']));
405+
$this->assertEquals(0, $this->dispatcher->getListenerPriority('foo', $test->postFoo(...)));
402406

403407
$test->preFoo(new Event());
404408
$this->dispatcher->dispatch(new Event(), 'foo');
@@ -417,8 +421,8 @@ public function testNamedClosures()
417421
$this->assertNotSame($callback1, $callback2);
418422
$this->assertNotSame($callback1, $callback3);
419423
$this->assertNotSame($callback2, $callback3);
420-
$this->assertTrue($callback1 == $callback2);
421-
$this->assertFalse($callback1 == $callback3);
424+
$this->assertEquals($callback1, $callback2);
425+
$this->assertEquals($callback1, $callback3);
422426

423427
$this->dispatcher->addListener('foo', $callback1, 3);
424428
$this->dispatcher->addListener('foo', $callback2, 2);

src/Symfony/Component/Form/Extension/DataCollector/FormDataCollector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ protected function getCasters(): array
235235
Caster::PREFIX_VIRTUAL.'type_class' => new ClassStub(\get_class($f->getConfig()->getType()->getInnerType())),
236236
];
237237
},
238-
FormView::class => [StubCaster::class, 'cutInternals'],
238+
FormView::class => StubCaster::cutInternals(...),
239239
ConstraintViolationInterface::class => function (ConstraintViolationInterface $v, array $a) {
240240
return [
241241
Caster::PREFIX_VIRTUAL.'root' => $v->getRoot(),

src/Symfony/Component/Form/Tests/ChoiceList/Factory/DefaultChoiceListFactoryTest.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public function testCreateFromChoicesFlatValuesAsCallable()
141141
{
142142
$list = $this->factory->createListFromChoices(
143143
['A' => $this->obj1, 'B' => $this->obj2, 'C' => $this->obj3, 'D' => $this->obj4],
144-
[$this, 'getValue']
144+
$this->getValue(...)
145145
);
146146

147147
$this->assertObjectListWithCustomValues($list);
@@ -186,7 +186,7 @@ public function testCreateFromChoicesGroupedValuesAsCallable()
186186
'Group 1' => ['A' => $this->obj1, 'B' => $this->obj2],
187187
'Group 2' => ['C' => $this->obj3, 'D' => $this->obj4],
188188
],
189-
[$this, 'getValue']
189+
$this->getValue(...)
190190
);
191191

192192
$this->assertObjectListWithCustomValues($list);
@@ -335,7 +335,7 @@ public function testCreateViewFlatPreferredChoiceGroupsSameOrder()
335335
[$this->obj4, $this->obj2, $this->obj1, $this->obj3],
336336
null, // label
337337
null, // index
338-
[$this, 'getGroup']
338+
$this->getGroup(...)
339339
);
340340

341341
$preferredLabels = array_map(static function (ChoiceGroupView $groupView): array {
@@ -380,7 +380,7 @@ public function testCreateViewFlatPreferredChoicesAsCallable()
380380
{
381381
$view = $this->factory->createView(
382382
$this->list,
383-
[$this, 'isPreferred']
383+
$this->isPreferred(...)
384384
);
385385

386386
$this->assertFlatView($view);
@@ -430,7 +430,7 @@ public function testCreateViewFlatLabelAsCallable()
430430
$view = $this->factory->createView(
431431
$this->list,
432432
[$this->obj2, $this->obj3],
433-
[$this, 'getLabel']
433+
$this->getLabel(...)
434434
);
435435

436436
$this->assertFlatView($view);
@@ -486,7 +486,7 @@ public function testCreateViewFlatIndexAsCallable()
486486
$this->list,
487487
[$this->obj2, $this->obj3],
488488
null, // label
489-
[$this, 'getFormIndex']
489+
$this->getFormIndex(...)
490490
);
491491

492492
$this->assertFlatViewWithCustomIndices($view);
@@ -580,7 +580,7 @@ public function testCreateViewFlatGroupByAsCallable()
580580
[$this->obj2, $this->obj3],
581581
null, // label
582582
null, // index
583-
[$this, 'getGroup']
583+
$this->getGroup(...)
584584
);
585585

586586
$this->assertGroupedView($view);
@@ -593,7 +593,7 @@ public function testCreateViewFlatGroupByAsCallableReturnsArray()
593593
[],
594594
null, // label
595595
null, // index
596-
[$this, 'getGroupArray']
596+
$this->getGroupArray(...)
597597
);
598598

599599
$this->assertGroupedViewWithChoiceDuplication($view);
@@ -606,7 +606,7 @@ public function testCreateViewFlatGroupByObjectThatCanBeCastToString()
606606
[$this->obj2, $this->obj3],
607607
null, // label
608608
null, // index
609-
[$this, 'getGroupAsObject']
609+
$this->getGroupAsObject(...)
610610
);
611611

612612
$this->assertGroupedView($view);
@@ -699,7 +699,7 @@ public function testCreateViewFlatAttrAsCallable()
699699
null, // label
700700
null, // index
701701
null, // group
702-
[$this, 'getAttr']
702+
$this->getAttr(...)
703703
);
704704

705705
$this->assertFlatViewWithAttr($view);
@@ -837,7 +837,7 @@ public function testCreateViewFlatlabelTranslationParametersAsCallable()
837837
null, // index
838838
null, // group
839839
null, // attr
840-
[$this, 'getlabelTranslationParameters']
840+
$this->getlabelTranslationParameters(...)
841841
);
842842

843843
$this->assertFlatViewWithlabelTranslationParameters($view);

src/Symfony/Component/Form/Tests/Extension/Validator/Constraints/FormValidatorTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ public function testHandleGroupSequenceValidationGroups()
423423
public function testHandleCallbackValidationGroups()
424424
{
425425
$object = new \stdClass();
426-
$options = ['validation_groups' => [$this, 'getValidationGroups']];
426+
$options = ['validation_groups' => $this->getValidationGroups(...)];
427427
$form = $this->getCompoundForm($object, $options);
428428
$form->submit([]);
429429

@@ -543,7 +543,7 @@ public function testUseInheritedCallbackValidationGroup()
543543
{
544544
$object = new \stdClass();
545545

546-
$parentOptions = ['validation_groups' => [$this, 'getValidationGroups']];
546+
$parentOptions = ['validation_groups' => $this->getValidationGroups(...)];
547547
$parent = $this->getBuilder('parent', null, $parentOptions)
548548
->setCompound(true)
549549
->setDataMapper(new DataMapper())

src/Symfony/Component/Form/Tests/Extension/Validator/Type/BaseValidatorExtensionTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function testValidationGroupsCanBeSetToFalse()
5757
public function testValidationGroupsCanBeSetToCallback()
5858
{
5959
$form = $this->createForm([
60-
'validation_groups' => [$this, 'testValidationGroupsCanBeSetToCallback'],
60+
'validation_groups' => $this->testValidationGroupsCanBeSetToCallback(...),
6161
]);
6262

6363
$this->assertIsCallable($form->getConfig()->getOption('validation_groups'));

src/Symfony/Component/HttpClient/Response/AmpResponse.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ private static function select(ClientState $multi, float $timeout): int
207207
$timeout += microtime(true);
208208
self::$delay = Loop::defer(static function () use ($timeout) {
209209
if (0 < $timeout -= microtime(true)) {
210-
self::$delay = Loop::delay(ceil(1000 * $timeout), [Loop::class, 'stop']);
210+
self::$delay = Loop::delay(ceil(1000 * $timeout), Loop::stop(...));
211211
} else {
212212
Loop::stop();
213213
}
@@ -447,6 +447,6 @@ private static function stopLoop(): void
447447
self::$delay = null;
448448
}
449449

450-
Loop::defer([Loop::class, 'stop']);
450+
Loop::defer(Loop::stop(...));
451451
}
452452
}

src/Symfony/Component/HttpKernel/Tests/ControllerMetadata/ArgumentMetadataFactoryTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ protected function setUp(): void
3535

3636
public function testSignature1()
3737
{
38-
$arguments = $this->factory->createArgumentMetadata([$this, 'signature1']);
38+
$arguments = $this->factory->createArgumentMetadata($this->signature1(...));
3939

4040
$this->assertEquals([
4141
new ArgumentMetadata('foo', self::class, false, false, null),
@@ -46,7 +46,7 @@ public function testSignature1()
4646

4747
public function testSignature2()
4848
{
49-
$arguments = $this->factory->createArgumentMetadata([$this, 'signature2']);
49+
$arguments = $this->factory->createArgumentMetadata($this->signature2(...));
5050

5151
$this->assertEquals([
5252
new ArgumentMetadata('foo', self::class, false, true, null, true),
@@ -57,7 +57,7 @@ public function testSignature2()
5757

5858
public function testSignature3()
5959
{
60-
$arguments = $this->factory->createArgumentMetadata([$this, 'signature3']);
60+
$arguments = $this->factory->createArgumentMetadata($this->signature3(...));
6161

6262
$this->assertEquals([
6363
new ArgumentMetadata('bar', FakeClassThatDoesNotExist::class, false, false, null),
@@ -67,7 +67,7 @@ public function testSignature3()
6767

6868
public function testSignature4()
6969
{
70-
$arguments = $this->factory->createArgumentMetadata([$this, 'signature4']);
70+
$arguments = $this->factory->createArgumentMetadata($this->signature4(...));
7171

7272
$this->assertEquals([
7373
new ArgumentMetadata('foo', null, false, true, 'default'),
@@ -78,7 +78,7 @@ public function testSignature4()
7878

7979
public function testSignature5()
8080
{
81-
$arguments = $this->factory->createArgumentMetadata([$this, 'signature5']);
81+
$arguments = $this->factory->createArgumentMetadata($this->signature5(...));
8282

8383
$this->assertEquals([
8484
new ArgumentMetadata('foo', 'array', false, true, null, true),

src/Symfony/Component/HttpKernel/Tests/DataCollector/RequestDataCollectorTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,17 @@ function () { return 'foo'; },
122122
],
123123
],
124124

125+
[
126+
'First-class callable closure',
127+
$this->testControllerInspection(...),
128+
[
129+
'class' => self::class,
130+
'method' => 'testControllerInspection',
131+
'file' => __FILE__,
132+
'line' => $r1->getStartLine(),
133+
],
134+
],
135+
125136
[
126137
'Static callback as string',
127138
__NAMESPACE__.'\RequestDataCollectorTest::staticControllerMethod',

0 commit comments

Comments
 (0)