Skip to content

Commit 2799534

Browse files
Merge branch '4.1'
* 4.1: fix cs fix cs fix cs SCA: consolidate non empty array checks across codebase [cs] correct invalid @param types [Bridge/PhpUnit] Use composer to download phpunit [DI] fix taking lazy services into account when dumping the container [Form] Fixed empty data for compound date interval [Cache] fix optimizing Psr6Cache for AdapterInterface pools deal with explicitly enabled workflow nodes
2 parents 887d504 + 9508a26 commit 2799534

21 files changed

+95
-48
lines changed

Compiler/AnalyzeServiceReferencesPass.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ class AnalyzeServiceReferencesPass extends AbstractRecursivePass implements Repe
3434
private $onlyConstructorArguments;
3535
private $hasProxyDumper;
3636
private $lazy;
37+
private $expressionLanguage;
38+
private $byConstructor;
3739
private $definitions;
3840
private $aliases;
3941

@@ -64,6 +66,7 @@ public function process(ContainerBuilder $container)
6466
$this->graph = $container->getCompiler()->getServiceReferenceGraph();
6567
$this->graph->clear();
6668
$this->lazy = false;
69+
$this->byConstructor = false;
6770
$this->definitions = $container->getDefinitions();
6871
$this->aliases = $container->getAliases();
6972

@@ -102,7 +105,8 @@ protected function processValue($value, $isRoot = false)
102105
$targetDefinition,
103106
$value,
104107
$this->lazy || ($this->hasProxyDumper && $targetDefinition && $targetDefinition->isLazy()),
105-
ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE === $value->getInvalidBehavior()
108+
ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE === $value->getInvalidBehavior(),
109+
$this->byConstructor
106110
);
107111

108112
if ($inExpression) {
@@ -132,8 +136,11 @@ protected function processValue($value, $isRoot = false)
132136
}
133137
$this->lazy = false;
134138

139+
$byConstructor = $this->byConstructor;
140+
$this->byConstructor = true;
135141
$this->processValue($value->getFactory());
136142
$this->processValue($value->getArguments());
143+
$this->byConstructor = $byConstructor;
137144

138145
if (!$this->onlyConstructorArguments) {
139146
$this->processValue($value->getProperties());

Compiler/ServiceReferenceGraph.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,15 @@ public function clear()
7373
/**
7474
* Connects 2 nodes together in the Graph.
7575
*/
76-
public function connect(?string $sourceId, $sourceValue, ?string $destId, $destValue = null, $reference = null, bool $lazy = false, bool $weak = false)
76+
public function connect(?string $sourceId, $sourceValue, ?string $destId, $destValue = null, $reference = null, bool $lazy = false, bool $weak = false, bool $byConstructor = false)
7777
{
7878
if (null === $sourceId || null === $destId) {
7979
return;
8080
}
8181

8282
$sourceNode = $this->createNode($sourceId, $sourceValue);
8383
$destNode = $this->createNode($destId, $destValue);
84-
$edge = new ServiceReferenceGraphEdge($sourceNode, $destNode, $reference, $lazy, $weak);
84+
$edge = new ServiceReferenceGraphEdge($sourceNode, $destNode, $reference, $lazy, $weak, $byConstructor);
8585

8686
$sourceNode->addOutEdge($edge);
8787
$destNode->addInEdge($edge);

Compiler/ServiceReferenceGraphEdge.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,16 @@ class ServiceReferenceGraphEdge
2525
private $value;
2626
private $lazy;
2727
private $weak;
28+
private $byConstructor;
2829

29-
public function __construct(ServiceReferenceGraphNode $sourceNode, ServiceReferenceGraphNode $destNode, $value = null, bool $lazy = false, bool $weak = false)
30+
public function __construct(ServiceReferenceGraphNode $sourceNode, ServiceReferenceGraphNode $destNode, $value = null, bool $lazy = false, bool $weak = false, bool $byConstructor = false)
3031
{
3132
$this->sourceNode = $sourceNode;
3233
$this->destNode = $destNode;
3334
$this->value = $value;
3435
$this->lazy = $lazy;
3536
$this->weak = $weak;
37+
$this->byConstructor = $byConstructor;
3638
}
3739

3840
/**
@@ -84,4 +86,14 @@ public function isWeak()
8486
{
8587
return $this->weak;
8688
}
89+
90+
/**
91+
* Returns true if the edge links with a constructor argument.
92+
*
93+
* @return bool
94+
*/
95+
public function isReferencedByConstructor()
96+
{
97+
return $this->byConstructor;
98+
}
8799
}

Definition.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ public function getMethodCalls()
406406
/**
407407
* Sets the definition templates to conditionally apply on the current definition, keyed by parent interface/class.
408408
*
409-
* @param $instanceof ChildDefinition[]
409+
* @param ChildDefinition[] $instanceof
410410
*
411411
* @return $this
412412
*/

Dumper/PhpDumper.php

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -170,15 +170,19 @@ public function dump(array $options = array())
170170
}
171171
}
172172

173-
(new AnalyzeServiceReferencesPass(false))->process($this->container);
173+
(new AnalyzeServiceReferencesPass(false, !$this->getProxyDumper() instanceof NullDumper))->process($this->container);
174174
$this->circularReferences = array();
175175
$this->singleUsePrivateIds = array();
176-
$checkedNodes = array();
177-
foreach ($this->container->getCompiler()->getServiceReferenceGraph()->getNodes() as $id => $node) {
178-
$currentPath = array($id => $id);
179-
$this->analyzeCircularReferences($node->getOutEdges(), $checkedNodes, $currentPath);
180-
if ($this->isSingleUsePrivateNode($node)) {
181-
$this->singleUsePrivateIds[$id] = $id;
176+
foreach (array(true, false) as $byConstructor) {
177+
foreach ($this->container->getCompiler()->getServiceReferenceGraph()->getNodes() as $id => $node) {
178+
if (!$node->getValue() instanceof Definition) {
179+
continue;
180+
}
181+
$currentPath = array($id => $id);
182+
$this->analyzeCircularReferences($node->getOutEdges(), $currentPath, $id, $byConstructor);
183+
if ($this->isSingleUsePrivateNode($node)) {
184+
$this->singleUsePrivateIds[$id] = $id;
185+
}
182186
}
183187
}
184188
$this->container->getCompiler()->getServiceReferenceGraph()->clear();
@@ -333,27 +337,31 @@ private function getProxyDumper(): ProxyDumper
333337
return $this->proxyDumper;
334338
}
335339

336-
private function analyzeCircularReferences(array $edges, &$checkedNodes, &$currentPath)
340+
private function analyzeCircularReferences(array $edges, &$currentPath, $sourceId, $byConstructor)
337341
{
338342
foreach ($edges as $edge) {
343+
if ($byConstructor && !$edge->isReferencedByConstructor()) {
344+
continue;
345+
}
339346
$node = $edge->getDestNode();
340347
$id = $node->getId();
341348

342-
if ($node->getValue() && ($edge->isLazy() || $edge->isWeak())) {
349+
if (!$node->getValue() instanceof Definition || $sourceId === $id || $edge->isLazy() || $edge->isWeak()) {
343350
// no-op
344351
} elseif (isset($currentPath[$id])) {
345352
$currentId = $id;
346353
foreach (array_reverse($currentPath) as $parentId) {
347-
$this->circularReferences[$parentId][$currentId] = $currentId;
354+
if (!isset($this->circularReferences[$parentId][$currentId])) {
355+
$this->circularReferences[$parentId][$currentId] = $byConstructor;
356+
}
348357
if ($parentId === $id) {
349358
break;
350359
}
351360
$currentId = $parentId;
352361
}
353-
} elseif (!isset($checkedNodes[$id])) {
354-
$checkedNodes[$id] = true;
362+
} else {
355363
$currentPath[$id] = $id;
356-
$this->analyzeCircularReferences($node->getOutEdges(), $checkedNodes, $currentPath);
364+
$this->analyzeCircularReferences($node->getOutEdges(), $currentPath, $id, $byConstructor);
357365
unset($currentPath[$id]);
358366
}
359367
}
@@ -695,8 +703,14 @@ private function addInlineVariables(string $id, Definition $definition, array $a
695703

696704
private function addInlineReference(string $id, Definition $definition, string $targetId, bool $forConstructor): string
697705
{
706+
list($callCount, $behavior) = $this->serviceCalls[$targetId];
707+
708+
while ($this->container->hasAlias($targetId)) {
709+
$targetId = (string) $this->container->getAlias($targetId);
710+
}
711+
698712
if ($id === $targetId) {
699-
return $this->addInlineService($id, $definition, $definition, $forConstructor);
713+
return $this->addInlineService($id, $definition, $definition);
700714
}
701715

702716
if ('service_container' === $targetId || isset($this->referenceVariables[$targetId])) {
@@ -705,9 +719,7 @@ private function addInlineReference(string $id, Definition $definition, string $
705719

706720
$hasSelfRef = isset($this->circularReferences[$id][$targetId]);
707721
$forConstructor = $forConstructor && !isset($this->definitionVariables[$definition]);
708-
list($callCount, $behavior) = $this->serviceCalls[$targetId];
709-
710-
$code = $hasSelfRef && !$forConstructor ? $this->addInlineService($id, $definition, $definition, $forConstructor) : '';
722+
$code = $hasSelfRef && $this->circularReferences[$id][$targetId] && !$forConstructor ? $this->addInlineService($id, $definition, $definition) : '';
711723

712724
if (isset($this->referenceVariables[$targetId]) || (2 > $callCount && (!$hasSelfRef || !$forConstructor))) {
713725
return $code;
@@ -1250,7 +1262,7 @@ public function getParameterBag()
12501262
/*{$this->docStar}
12511263
* Computes a dynamic parameter.
12521264
*
1253-
* @param string The name of the dynamic parameter to load
1265+
* @param string \$name The name of the dynamic parameter to load
12541266
*
12551267
* @return mixed The value of the dynamic parameter
12561268
*
@@ -1736,7 +1748,7 @@ private function isHotPath(Definition $definition)
17361748

17371749
private function isSingleUsePrivateNode(ServiceReferenceGraphNode $node): bool
17381750
{
1739-
if (!$node->getValue() || $node->getValue()->isPublic()) {
1751+
if ($node->getValue()->isPublic()) {
17401752
return false;
17411753
}
17421754
$ids = array();

Tests/Fixtures/php/services10.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public function getParameterBag()
104104
/**
105105
* Computes a dynamic parameter.
106106
*
107-
* @param string The name of the dynamic parameter to load
107+
* @param string $name The name of the dynamic parameter to load
108108
*
109109
* @return mixed The value of the dynamic parameter
110110
*

Tests/Fixtures/php/services12.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public function getParameterBag()
111111
/**
112112
* Computes a dynamic parameter.
113113
*
114-
* @param string The name of the dynamic parameter to load
114+
* @param string $name The name of the dynamic parameter to load
115115
*
116116
* @return mixed The value of the dynamic parameter
117117
*

Tests/Fixtures/php/services19.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public function getParameterBag()
121121
/**
122122
* Computes a dynamic parameter.
123123
*
124-
* @param string The name of the dynamic parameter to load
124+
* @param string $name The name of the dynamic parameter to load
125125
*
126126
* @return mixed The value of the dynamic parameter
127127
*

Tests/Fixtures/php/services26.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public function getParameterBag()
125125
/**
126126
* Computes a dynamic parameter.
127127
*
128-
* @param string The name of the dynamic parameter to load
128+
* @param string $name The name of the dynamic parameter to load
129129
*
130130
* @return mixed The value of the dynamic parameter
131131
*

Tests/Fixtures/php/services8.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public function getParameterBag()
9191
/**
9292
* Computes a dynamic parameter.
9393
*
94-
* @param string The name of the dynamic parameter to load
94+
* @param string $name The name of the dynamic parameter to load
9595
*
9696
* @return mixed The value of the dynamic parameter
9797
*

0 commit comments

Comments
 (0)