8000 Leverage First-class callable syntax by tigitz · Pull Request #47672 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

Leverage First-class callable syntax #47672

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function addQuery(string $connectionName, Query $query): void
'sql' => $query->getSql(),
'params' => $query->getParams(),
'types' => $query->getTypes(),
'executionMS' => [$query, 'getDuration'], // stop() may not be called at this point
'executionMS' => $query->getDuration(...), // stop() may not be called at this point
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ protected function configureContainer(ContainerConfigurator $c): void

protected function configureRoutes(RoutingConfigurator $routes): void
{
$routes->add('hello', '/')->controller([$this, 'helloAction']);
$routes->add('hello', '/')->controller($this->helloAction(...));
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ private function writeNode(NodeInterface $node, NodeInterface $parentNode = null

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

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

if ($children) {
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Console/Helper/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,7 @@ private function buildTableRows(array $rows): TableRows
if (!str_contains($cell ?? '', "\n")) {
continue;
}
$escaped = implode("\n", array_map([OutputFormatter::class, 'escapeTrailingBackslash'], explode("\n", $cell)));
$escaped = implode("\n", array_map(OutputFormatter::escapeTrailingBackslash(...), explode("\n", $cell)));
$cell = $cell instanceof TableCell ? new TableCell($escaped, ['colspan' => $cell->getColspan()]) : $escaped;
$lines = explode("\n", str_replace("\n", "<fg=default;bg=default></>\n", $cell));
foreach ($lines as $lineKey => $line) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ private static function createClosure()
public function testSetCodeWithNonClosureCallable()
{
$command = new \TestCommand();
$ret = $command->setCode([$this, 'callableMethodCommand']);
$ret = $command->setCode($this->callableMethodCommand(...));
$this->assertEquals($command, $ret, '->setCode() implements a fluent interface');
$tester = new CommandTester($command);
$tester->execute([]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function load(mixed $resource, string $type = null): mixed
if (isset($result['parameters']) && \is_array($result['parameters'])) {
foreach ($result['parameters'] as $key => $value) {
if (\is_array($value)) {
$this->container->setParameter($key, array_map([$this, 'phpize'], $value));
$this->container->setParameter($key, array_map($this->phpize(...), $value));
} else {
$this->container->setParameter($key, $this->phpize($value));
}
Expand Down
20 change A92E s: 12 additions & 8 deletions src/Symfony/Component/EventDispatcher/Tests/EventDispatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function testInitialState()
public function testAddListener()
{
$this->dispatcher->addListener('pre.foo', [$this->listener, 'preFoo']);
$this->dispatcher->addListener('post.foo', [$this->listener, 'postFoo']);
$this->dispatcher->addListener('post.foo', $this->listener->postFoo(...));
$this->assertTrue($this->dispatcher->hasListeners());
$this->assertTrue($this->dispatcher->hasListeners(self::preFoo));
$this->assertTrue($this->dispatcher->hasListeners(self::postFoo));
Expand All @@ -71,21 +71,25 @@ public function testGetListenersSortsByPriority()
$listener1 = new TestEventListener();
$listener2 = new TestEventListener();
$listener3 = new TestEventListener();
$listener4 = new TestEventListener();
$listener1->name = '1';
$listener2->name = '2';
$listener3->name = '3';
$listener4->name = '4';

$this->dispatcher->addListener('pre.foo', [$listener1, 'preFoo'], -10);
$this->dispatcher->addListener('pre.foo', [$listener2, 'preFoo'], 10);
$this->dispatcher->addListener('pre.foo', [$listener3, 'preFoo']);
$this->dispatcher->addListener('pre.foo', $listener4->preFoo(...), 20);

$expected = [
$listener4->preFoo(...),
[$listener2, 'preFoo'],
[$listener3, 'preFoo'],
[$listener1, 'preFoo'],
];

$this->assertSame($expected, $this->dispatcher->getListeners('pre.foo'));
$this->assertEquals($expected, $this->dispatcher->getListeners('pre.foo'));
}

public function testGetAllListenersSortsByPriority()
Expand Down Expand Up @@ -129,7 +133,7 @@ public function testGetListenerPriority()
public function testDispatch()
{
$this->dispatcher->addListener('pre.foo', [$this->listener, 'preFoo']);
$this->dispatcher->addListener('post.foo', [$this->listener, 'postFoo']);
$this->dispatcher->addListener('post.foo', $this->listener->postFoo(...));
$this->dispatcher->dispatch(new Event(), self::preFoo);
$this->assertTrue($this->listener->preFooInvoked);
$this->assertFalse($this->listener->postFooInvoked);
Expand Down Expand Up @@ -160,7 +164,7 @@ public function testStopEventPropagation()
// be executed
// Manually set priority to enforce $this->listener to be called first
$this->dispatcher->addListener('post.foo', [$this->listener, 'postFoo'], 10);
$this->dispatcher->addListener('post.foo', [$otherListener, 'postFoo']);
$this->dispatcher->addListener('post.foo', $otherListener->postFoo(...));
$this->dispatcher->dispatch(new Event(), self::postFoo);
$this->assertTrue($this->listener->postFooInvoked);
$this->assertFalse($otherListener->postFooInvoked);
Expand Down Expand Up @@ -386,7 +390,7 @@ public function testMutatingWhilePropagationIsStopped()
{
$testLoaded = false;
$test = new TestEventListener();
$this->dispatcher->addListener('foo', [$test, 'postFoo']);
$this->dispatcher->addListener('foo', $test->postFoo(...));
$this->dispatcher->addListener('foo', [function () use ($test, &$testLoaded) {
$testLoaded = true;

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

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

$test->preFoo(new Event());
$this->dispatcher->dispatch(new Event(), 'foo');
Expand All @@ -417,8 +421,8 @@ public function testNamedClosures()
$this->assertNotSame($callback1, $callback2);
$this->assertNotSame($callback1, $callback3);
$this->assertNotSame($callback2, $callback3);
$this->assertTrue($callback1 == $callback2);
$this->assertFalse($callback1 == $callback3);
$this->assertEquals($callback1, $callback2);
$this->assertEquals($callback1, $callback3);

$this->dispatcher->addListener('foo', $callback1, 3);
$this->dispatcher->addListener('foo', $callback2, 2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ protected function getCasters(): array
Caster::PREFIX_VIRTUAL.'type_class' => new ClassStub(\get_class($f->getConfig()->getType()->getInnerType())),
];
},
FormView::class => [StubCaster::class, 'cutInternals'],
FormView::class => StubCaster::cutInternals(...),
ConstraintViolationInterface::class => function (ConstraintViolationInterface $v, array $a) {
return [
Caster::PREFIX_VIRTUAL.'root' => $v->getRoot(),
Expand Down
110E
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public function testCreateFromChoicesFlatValuesAsCallable()
{
$list = $this->factory->createListFromChoices(
['A' => $this->obj1, 'B' => $this->obj2, 'C' => $this->obj3, 'D' => $this->obj4],
[$this, 'getValue']
$this->getValue(...)
);

$this->assertObjectListWithCustomValues($list);
Expand Down Expand Up @@ -186,7 +186,7 @@ public function testCreateFromChoicesGroupedValuesAsCallable()
'Group 1' => ['A' => $this->obj1, 'B' => $this->obj2],
'Group 2' => ['C' => $this->obj3, 'D' => $this->obj4],
],
[$this, 'getValue']
$this->getValue(...)
);

$this->assertObjectListWithCustomValues($list);
Expand Down Expand Up @@ -335,7 +335,7 @@ public function testCreateViewFlatPreferredChoiceGroupsSameOrder()
[$this->obj4, $this->obj2, $this->obj1, $this->obj3],
null, // label
null, // index
[$this, 'getGroup']
$this->getGroup(...)
);

$preferredLabels = array_map(static function (ChoiceGroupView $groupView): array {
Expand Down Expand Up @@ -380,7 +380,7 @@ public function testCreateViewFlatPreferredChoicesAsCallable()
{
$view = $this->factory->createView(
$this->list,
[$this, 'isPreferred']
$this->isPreferred(...)
);

$this->assertFlatView($view);
Expand Down Expand Up @@ -430,7 +430,7 @@ public function testCreateViewFlatLabelAsCallable()
$view = $this->factory->createView(
$this->list,
[$this->obj2, $this->obj3],
[$this, 'getLabel']
$this->getLabel(...)
);

$this->assertFlatView($view);
Expand Down Expand Up @@ -486,7 +486,7 @@ public function testCreateViewFlatIndexAsCallable()
$this->list,
[$this->obj2, $this->obj3],
null, // label
[$this, 'getFormIndex']
$this->getFormIndex(...)
);

$this->assertFlatViewWithCustomIndices($view);
Expand Down Expand Up @@ -580,7 +580,7 @@ public function testCreateViewFlatGroupByAsCallable()
[$this->obj2, $this->obj3],
null, // label
null, // index
[$this, 'getGroup']
$this->getGroup(...)
);

$this->assertGroupedView($view);
Expand All @@ -593,7 +593,7 @@ public function testCreateViewFlatGroupByAsCallableReturnsArray()
[],
null, // label
null, // index
[$this, 'getGroupArray']
$this->getGroupArray(...)
);

$this->assertGroupedViewWithChoiceDuplication($view);
Expand All @@ -606,7 +606,7 @@ public function testCreateViewFlatGroupByObjectThatCanBeCastToString()
[$this->obj2, $this->obj3],
null, // label
null, // index
[$this, 'getGroupAsObject']
$this->getGroupAsObject(...)
);

$this->assertGroupedView($view);
Expand Down Expand Up @@ -699,7 +699,7 @@ public function testCreateViewFlatAttrAsCallable()
null, // label
null, // index
null, // group
[$this, 'getAttr']
$this->getAttr(...)
);

$this->assertFlatViewWithAttr($view);
Expand Down Expand Up @@ -837,7 +837,7 @@ public function testCreateViewFlatlabelTranslationParametersAsCallable()
null, // index
null, // group
null, // attr
[$this, 'getlabelTranslationParameters']
$this->getlabelTranslationParameters(...)
);

$this->assertFlatViewWithlabelTranslationParameters($view);
Expand Down
10000
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ public function testHandleGroupSequenceValidationGroups()
public function testHandleCallbackValidationGroups()
{
$object = new \stdClass();
$options = ['validation_groups' => [$this, 'getValidationGroups']];
$options = ['validation_groups' => $this->getValidationGroups(...)];
$form = $this->getCompoundForm($object, $options);
$form->submit([]);

Expand Down Expand Up @@ -543,7 +543,7 @@ public function testUseInheritedCallbackValidationGroup()
{
$object = new \stdClass();

$parentOptions = ['validation_groups' => [$this, 'getValidationGroups']];
$parentOptions = ['validation_groups' => $this->getValidationGroups(...)];
$parent = $this->getBuilder('parent', null, $parentOptions)
->setCompound(true)
->setDataMapper(new DataMapper())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function testValidationGroupsCanBeSetToFalse()
public function testValidationGroupsCanBeSetToCallback()
{
$form = $this->createForm([
'validation_groups' => [$this, 'testValidationGroupsCanBeSetToCallback'],
'validation_groups' => $this->testValidationGroupsCanBeSetToCallback(...),
]);

$this->assertIsCallable($form->getConfig()->getOption('validation_groups'));
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/HttpClient/Response/AmpResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ private static function select(ClientState $multi, float $timeout): int
$timeout += microtime(true);
self::$delay = Loop::defer(static function () use ($timeout) {
if (0 < $timeout -= microtime(true)) {
self::$delay = Loop::delay(ceil(1000 * $timeout), [Loop::class, 'stop']);
self::$delay = Loop::delay(ceil(1000 * $timeout), Loop::stop(...));
} else {
Loop::stop();
}
Expand Down Expand Up @@ -447,6 +447,6 @@ private static function stopLoop(): void
self::$delay = null;
}

Loop::defer([Loop::class, 'stop']);
Loop::defer(Loop::stop(...));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ protected function setUp(): void

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

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

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

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

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

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

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

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

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

$this->assertEquals([
new ArgumentMetadata('foo', 'array', false, true, null, true),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,17 @@ function () { return 'foo'; },
],
],

[
'First-class callable closure',
$this->testControllerInspection(...),
[
'class' => self::class,
'method' => 'testControllerInspection',
'file' => __FILE__,
'line' => $r1->getStartLine(),
],
],

[
'Static callback as string',
__NAMESPACE__.'\RequestDataCollectorTest::staticControllerMethod',
Expand Down
Loading
0