Skip to content

Commit ca28a84

Browse files
committed
Use short arrow closure for EmptyIterator
1 parent e2d5ff0 commit ca28a84

7 files changed

+34
-54
lines changed

Dumper/PhpDumper.php

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1835,36 +1835,36 @@ private function dumpValue(mixed $value, bool $interpolate = true): string
18351835
}
18361836

18371837
if ($value instanceof IteratorArgument) {
1838-
$operands = [0];
1838+
if (!$values = $value->getValues()) {
1839+
return 'new RewindableGenerator(fn () => new \EmptyIterator(), 0)';
1840+
}
1841+
1842+
$this->addContainerRef = true;
1843+
18391844
$code = [];
1845+
$code[] = 'new RewindableGenerator(function () use ($containerRef) {';
1846+
$code[] = ' $container = $containerRef->get();';
1847+
$code[] = '';
18401848

1841-
if (!$values = $value->getValues()) {
1842-
$code[] = 'new RewindableGenerator(function () {';
1843-
$code[] = ' return new \EmptyIterator();';
1844-
} else {
1845-
$this->addContainerRef = true;
1846-
$code[] = 'new RewindableGenerator(function () use ($containerRef) {';
1847-
$code[] = ' $container = $containerRef->get();';
1848-
$code[] = '';
1849-
$countCode = [];
1850-
$countCode[] = 'function () use ($containerRef) {';
1851-
1852-
foreach ($values as $k => $v) {
1853-
($c = $this->getServiceConditionals($v)) ? $operands[] = "(int) ($c)" : ++$operands[0];
1854-
$v = $this->wrapServiceConditionals($v, sprintf(" yield %s => %s;\n", $this->dumpValue($k, $interpolate), $this->dumpValue($v, $interpolate)));
1855-
foreach (explode("\n", $v) as $v) {
1856-
if ($v) {
1857-
$code[] = ' '.$v;
1858-
}
1849+
$countCode = [];
1850+
$countCode[] = 'function () use ($containerRef) {';
1851+
1852+
$operands = [0];
1853+
foreach ($values as $k => $v) {
1854+
($c = $this->getServiceConditionals($v)) ? $operands[] = "(int) ($c)" : ++$operands[0];
1855+
$v = $this->wrapServiceConditionals($v, sprintf(" yield %s => %s;\n", $this->dumpValue($k, $interpolate), $this->dumpValue($v, $interpolate)));
1856+
foreach (explode("\n", $v) as $v) {
1857+
if ($v) {
1858+
$code[] = ' '.$v;
18591859
}
18601860
}
1861-
1862-
$countCode[] = ' $container = $containerRef->get();';
1863-
$countCode[] = '';
1864-
$countCode[] = sprintf(' return %s;', implode(' + ', $operands));
1865-
$countCode[] = ' }';
18661861
}
18671862

1863+
$countCode[] = ' $container = $containerRef->get();';
1864+
$countCode[] = '';
1865+
$countCode[] = sprintf(' return %s;', implode(' + ', $operands));
1866+
$countCode[] = ' }';
1867+
18681868
$code[] = sprintf(' }, %s)', \count($operands) > 1 ? implode("\n", $countCode) : $operands[0]);
18691869

18701870
return implode("\n", $code);

Tests/Fixtures/php/services9_as_files.txt

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -368,9 +368,7 @@ class getLazyContextService extends ProjectServiceContainer
368368

369369
yield 'k1' => ($container->services['foo.baz'] ?? $container->load('getFoo_BazService'));
370370
yield 'k2' => $container;
371-
}, 2), new RewindableGenerator(function () {
372-
return new \EmptyIterator();
373-
}, 0));
371+
}, 2), new RewindableGenerator(fn () => new \EmptyIterator(), 0));
374372
}
375373
}
376374

@@ -391,9 +389,7 @@ class getLazyContextIgnoreInvalidRefService extends ProjectServiceContainer
391389
$container = $containerRef->get();
392390

393391
yield 0 => ($container->services['foo.baz'] ?? $container->load('getFoo_BazService'));
394-
}, 1), new RewindableGenerator(function () {
395-
return new \EmptyIterator();
396-
}, 0));
392+
}, 1), new RewindableGenerator(fn () => new \EmptyIterator(), 0));
397393
}
398394
}
399395

Tests/Fixtures/php/services9_compiled.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -337,9 +337,7 @@ protected static function getLazyContextService($container)
337337

338338
yield 'k1' => ($container->services['foo.baz'] ?? self::getFoo_BazService($container));
339339
yield 'k2' => $container;
340-
}, 2), new RewindableGenerator(function () {
341-
return new \EmptyIterator();
342-
}, 0));
340+
}, 2), new RewindableGenerator(fn () => new \EmptyIterator(), 0));
343341
}
344342

345343
/**
@@ -355,9 +353,7 @@ protected static function getLazyContextIgnoreInvalidRefService($container)
355353
$container = $containerRef->get();
356354

357355
yield 0 => ($container->services['foo.baz'] ?? self::getFoo_BazService($container));
358-
}, 1), new RewindableGenerator(function () {
359-
return new \EmptyIterator();
360-
}, 0));
356+
}, 1), new RewindableGenerator(fn () => new \EmptyIterator(), 0));
361357
}
362358

363359
/**

Tests/Fixtures/php/services9_inlined_factories.txt

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -360,9 +360,7 @@ class ProjectServiceContainer extends Container
360360

361361
yield 'k1' => ($container->services['foo.baz'] ?? self::getFoo_BazService($container));
362362
yield 'k2' => $container;
363-
}, 2), new RewindableGenerator(function () {
364-
return new \EmptyIterator();
365-
}, 0));
363+
}, 2), new RewindableGenerator(fn () => new \EmptyIterator(), 0));
366364
}
367365

368366
/**
@@ -380,9 +378,7 @@ class ProjectServiceContainer extends Container
380378
$container = $containerRef->get();
381379

382380
yield 0 => ($container->services['foo.baz'] ?? self::getFoo_BazService($container));
383-
}, 1), new RewindableGenerator(function () {
384-
return new \EmptyIterator();
385-
}, 0));
381+
}, 1), new RewindableGenerator(fn () => new \EmptyIterator(), 0));
386382
}
387383

388384
/**

Tests/Fixtures/php/services_almost_circular_private.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -559,9 +559,7 @@ protected static function getMailer_TransportFactory_AmazonService($container)
559559
*/
560560
protected static function getMailerInline_MailerService($container)
561561
{
562-
return $container->privates['mailer_inline.mailer'] = new \stdClass((new \FactoryCircular(new RewindableGenerator(function () {
563-
return new \EmptyIterator();
564-
}, 0)))->create());
562+
return $container->privates['mailer_inline.mailer'] = new \stdClass((new \FactoryCircular(new RewindableGenerator(fn () => new \EmptyIterator(), 0)))->create());
565563
}
566564

567565
/**

Tests/Fixtures/php/services_almost_circular_public.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -559,9 +559,7 @@ protected static function getMailer_TransportFactory_AmazonService($container)
559559
*/
560560
protected static function getMailerInline_TransportFactoryService($container)
561561
{
562-
return $container->services['mailer_inline.transport_factory'] = new \FactoryCircular(new RewindableGenerator(function () {
563-
return new \EmptyIterator();
564-
}, 0));
562+
return $container->services['mailer_inline.transport_factory'] = new \FactoryCircular(new RewindableGenerator(fn () => new \EmptyIterator(), 0));
565563
}
566564

567565
/**

Tests/Fixtures/php/services_errored_definition.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -337,9 +337,7 @@ protected static function getLazyContextService($container)
337337

338338
yield 'k1' => ($container->services['foo.baz'] ?? self::getFoo_BazService($container));
339339
yield 'k2' => $container;
340-
}, 2), new RewindableGenerator(function () {
341-
return new \EmptyIterator();
342-
}, 0));
340+
}, 2), new RewindableGenerator(fn () => new \EmptyIterator(), 0));
343341
}
344342

345343
/**
@@ -355,9 +353,7 @@ protected static function getLazyContextIgnoreInvalidRefService($container)
355353
$container = $containerRef->get();
356354

357355
yield 0 => ($container->services['foo.baz'] ?? self::getFoo_BazService($container));
358-
}, 1), new RewindableGenerator(function () {
359-
return new \EmptyIterator();
360-
}, 0));
356+
}, 1), new RewindableGenerator(fn () => new \EmptyIterator(), 0));
361357
}
362358

363359
/**

0 commit comments

Comments
 (0)