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

Skip to content

Commit 03af7b9

Browse files
committed
minor #23674 [DI] Inline trivial private servives (nicolas-grekas)
This PR was merged into the 4.0-dev branch. Discussion ---------- [DI] Inline trivial private servives | Q | A | ------------- | --- | Branch? | 4.0 | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - There is no need to generate a method when the instantiation is just as verbose as calling that method. Commits ------- 0caed93 [DI] Inline trivial private servives
2 parents 01f05e3 + 0caed93 commit 03af7b9

File tree

9 files changed

+81
-52
lines changed

9 files changed

+81
-52
lines changed

src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,50 @@ private function isSimpleInstance($id, Definition $definition)
446446
return true;
447447
}
448448

449+
/**
450+
* Checks if the definition is a trivial instance.
451+
*
452+
* @param Definition $definition
453+
*
454+
* @return bool
455+
*/
456+
private function isTrivialInstance(Definition $definition)
457+
{
458+
if ($definition->isPublic() || $definition->getMethodCalls() || $definition->getProperties() || $definition->getConfigurator()) {
459+
return false;
460+
}
461+
if ($definition->isDeprecated() || $definition->isLazy() || $definition->getFactory() || 3 < count($definition->getArguments())) {
462+
return false;
463+
}
464+
465+
foreach ($definition->getArguments() as $arg) {
466+
if (!$arg || ($arg instanceof Reference && 'service_container' !== (string) $arg)) {
467+
continue;
468+
}
469+
if (is_array($arg) && 3 >= count($arg)) {
470+
foreach ($arg as $k => $v) {
471+
if ($this->dumpValue($k) !== $this->dumpValue($k, false)) {
472+
return false;
473+
}
474+
if (!$v || ($v instanceof Reference && 'service_container' !== (string) $v)) {
475+
continue;
476+
}
477+
if (!is_scalar($v) || $this->dumpValue($v) !== $this->dumpValue($v, false)) {
478+
return false;
479+
}
480+
}
481+
} elseif (!is_scalar($arg) || $this->dumpValue($arg) !== $this->dumpValue($arg, false)) {
482+
return false;
483+
}
484+
}
485+
486+
if (false !== strpos($this->dumpLiteralClass($this->dumpValue($definition->getClass())), '$')) {
487+
return false;
488+
}
489+
490+
return true;
491+
}
492+
449493
/**
450494
* Adds method calls to a service definition.
451495
*
@@ -675,7 +719,7 @@ private function addServices()
675719
foreach ($definitions as $id => $definition) {
676720
if ($definition->isPublic()) {
677721
$publicServices .= $this->addService($id, $definition);
678-
} else {
722+
} elseif (!$this->isTrivialInstance($definition)) {
679723
$privateServices .= $this->addService($id, $definition);
680724
}
681725
}
@@ -1504,10 +1548,18 @@ private function getServiceCall($id, Reference $reference = null)
15041548
}
15051549

15061550
if ($this->container->hasDefinition($id)) {
1507-
$code = sprintf('$this->%s()', $this->generateMethodName($id));
1551+
$definition = $this->container->getDefinition($id);
15081552

1509-
if ($this->container->getDefinition($id)->isShared()) {
1510-
$code = sprintf('($this->%s[\'%s\'] ?? %s)', $this->container->getDefinition($id)->isPublic() ? 'services' : 'privates', $id, $code);
1553+
if ($definition->isPublic() || !$this->isTrivialInstance($definition)) {
1554+
$code = sprintf('$this->%s()', $this->generateMethodName($id));
1555+
} else {
1556+
$code = substr($this->addNewInstance($definition, '', '', $id), 8, -2);
1557+
if ($definition->isShared()) {
1558+
$code = sprintf('($this->privates[\'%s\'] = %s)', $id, $code);
1559+
}
1560+
}
1561+
if ($definition->isShared()) {
1562+
$code = sprintf('($this->%s[\'%s\'] ?? %s)', $definition->isPublic() ? 'services' : 'privates', $id, $code);
15111563
}
15121564
} elseif (null !== $reference && ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE !== $reference->getInvalidBehavior()) {
15131565
$code = sprintf('$this->get(\'%s\', ContainerInterface::NULL_ON_INVALID_REFERENCE)', $id);

src/Symfony/Component/DependencyInjection/Tests/Fixtures/containers/container9.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@
125125
$container
126126
->register('factory_simple', 'SimpleFactoryClass')
127127
->addArgument('foo')
128+
->setDeprecated(true)
128129
->setPublic(false)
129130
;
130131
$container

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ protected function getFactoryServiceService()
231231
*/
232232
protected function getFactoryServiceSimpleService()
233233
{
234-
return $this->services['factory_service_simple'] = (new \SimpleFactoryClass('foo'))->getInstance();
234+
return $this->services['factory_service_simple'] = ($this->privates['factory_simple'] ?? $this->getFactorySimpleService())->getInstance();
235235
}
236236

237237
/**
@@ -372,6 +372,20 @@ protected function getServiceFromStaticMethodService()
372372
return $this->services['service_from_static_method'] = \Bar\FooClass::getInstance();
373373
}
374374

375+
/**
376+
* Gets the private 'factory_simple' shared service.
377+
*
378+
* @return \SimpleFactoryClass
379+
*
380+
* @deprecated The "factory_simple" service is deprecated. You should stop using it, as it will soon be removed.
381+
*/
382+
private function getFactorySimpleService()
383+
{
384+
@trigger_error('The "factory_simple" service is deprecated. You should stop using it, as it will soon be removed.', E_USER_DEPRECATED);
385+
386+
return $this->privates['factory_simple'] = new \SimpleFactoryClass('foo');
387+
}
388+
375389
/**
376390
* {@inheritdoc}
377391
*/

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_locator.php

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function isCompiled()
7474
*/
7575
protected function getBarServiceService()
7676
{
77-
return $this->services['bar_service'] = new \stdClass(($this->privates['baz_service'] ?? $this->getBazServiceService()));
77+
return $this->services['bar_service'] = new \stdClass(($this->privates['baz_service'] ?? ($this->privates['baz_service'] = new \stdClass())));
7878
}
7979

8080
/**
@@ -87,7 +87,7 @@ protected function getFooServiceService()
8787
return $this->services['foo_service'] = new \Symfony\Component\DependencyInjection\ServiceLocator(array('bar' => function () {
8888
return ($this->services['bar_service'] ?? $this->getBarServiceService());
8989
}, 'baz' => function (): \stdClass {
90-
return ($this->privates['baz_service'] ?? $this->getBazServiceService());
90+
return ($this->privates['baz_service'] ?? ($this->privates['baz_service'] = new \stdClass()));
9191
}, 'nil' => function () {
9292
return NULL;
9393
}));
@@ -169,14 +169,4 @@ protected function getTranslator3Service()
169169

170170
return $instance;
171171
}
172-
173-
/**
174-
* Gets the private 'baz_service' shared service.
175-
*
176-
* @return \stdClass
177-
*/
178-
private function getBazServiceService()
179-
{
180-
return $this->privates['baz_service'] = new \stdClass();
181-
}
182172
}

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_private_frozen.php

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public function isCompiled()
6868
*/
6969
protected function getBarServiceService()
7070
{
71-
return $this->services['bar_service'] = new \stdClass(($this->privates['baz_service'] ?? $this->getBazServiceService()));
71+
return $this->services['bar_service'] = new \stdClass(($this->privates['baz_service'] ?? ($this->privates['baz_service'] = new \stdClass())));
7272
}
7373

7474
/**
@@ -78,16 +78,6 @@ protected function getBarServiceService()
7878
*/
7979
protected function getFooServiceService()
8080
{
81-
return $this->services['foo_service'] = new \stdClass(($this->privates['baz_service'] ?? $this->getBazServiceService()));
82-
}
83-
84-
/**
85-
* Gets the private 'baz_service' shared service.
86-
*
87-
* @return \stdClass
88-
*/
89-
private function getBazServiceService()
90-
{
91-
return $this->privates['baz_service'] = new \stdClass();
81+
return $this->services['foo_service'] = new \stdClass(($this->privates['baz_service'] ?? ($this->privates['baz_service'] = new \stdClass())));
9282
}
9383
}

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_private_in_expression.php

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,6 @@ public function isCompiled()
6767
*/
6868
protected function getPublicFooService()
6969
{
70-
return $this->services['public_foo'] = new \stdClass(($this->privates['private_foo'] ?? $this->getPrivateFooService()));
71-
}
72-
73-
/**
74-
* Gets the private 'private_foo' shared service.
75-
*
76-
* @return \stdClass
77-
*/
78-
private function getPrivateFooService()
79-
{
80-
return $this->privates['private_foo'] = new \stdClass();
70+
return $this->services['public_foo'] = new \stdClass(($this->privates['private_foo'] ?? ($this->privates['private_foo'] = new \stdClass())));
8171
}
8272
}

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_subscriber.php

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -79,23 +79,13 @@ protected function getSymfony_Component_DependencyInjection_Tests_Fixtures_TestS
7979
protected function getFooServiceService()
8080
{
8181
return $this->services['foo_service'] = new \Symfony\Component\DependencyInjection\Tests\Fixtures\TestServiceSubscriber(new \Symfony\Component\DependencyInjection\ServiceLocator(array('Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\CustomDefinition' => function (): \Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition {
82-
return ($this->privates['autowired.Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition'] ?? $this->getAutowired_Symfony_Component_DependencyInjection_Tests_Fixtures_CustomDefinitionService());
82+
return ($this->privates['autowired.Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition'] ?? ($this->privates['autowired.Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition'] = new \Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition()));
8383
}, 'Symfony\\Component\\DependencyInjection\\Tests\\Fixtures\\TestServiceSubscriber' => function (): \Symfony\Component\DependencyInjection\Tests\Fixtures\TestServiceSubscriber {
8484
return ($this->services['Symfony\Component\DependencyInjection\Tests\Fixtures\TestServiceSubscriber'] ?? $this->getSymfony_Component_DependencyInjection_Tests_Fixtures_TestServiceSubscriberService());
8585
}, 'bar' => function (): \Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition {
8686
return ($this->services['Symfony\Component\DependencyInjection\Tests\Fixtures\TestServiceSubscriber'] ?? $this->getSymfony_Component_DependencyInjection_Tests_Fixtures_TestServiceSubscriberService());
8787
}, 'baz' => function (): \Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition {
88-
return ($this->privates['autowired.Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition'] ?? $this->getAutowired_Symfony_Component_DependencyInjection_Tests_Fixtures_CustomDefinitionService());
88+
return ($this->privates['autowired.Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition'] ?? ($this->privates['autowired.Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition'] = new \Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition()));
8989
})));
9090
}
91-
92-
/**
93-
* Gets the private 'autowired.Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition' shared autowired service.
94-
*
95-
* @return \Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition
96-
*/
97-
private function getAutowired_Symfony_Component_DependencyInjection_Tests_Fixtures_CustomDefinitionService()
98-
{
99-
return $this->privates['autowired.Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition'] = new \Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition();
100-
}
10191
}

src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/services9.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@
112112
</service>
113113
<service id="factory_simple" class="SimpleFactoryClass" public="false">
114114
<argument>foo</argument>
115+
<deprecated>The "%service_id%" service is deprecated. You should stop using it, as it will soon be removed.</deprecated>
115116
</service>
116117
<service id="factory_service_simple" class="Bar">
117118
<factory service="factory_simple" method="getInstance"/>

src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services9.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ services:
105105
factory: [Bar\FooClass, getInstance]
106106
factory_simple:
107107
class: SimpleFactoryClass
108+
deprecated: The "%service_id%" service is deprecated. You should stop using it, as it will soon be removed.
108109
public: false
109110
arguments: ['foo']
110111
factory_service_simple:

0 commit comments

Comments
 (0)