Skip to content

Commit 9508a26

Browse files
Merge branch '3.4' into 4.1
* 3.4: 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 b845813 + e1a4f85 commit 9508a26

21 files changed

+91
-45
lines changed

Compiler/AnalyzeServiceReferencesPass.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class AnalyzeServiceReferencesPass extends AbstractRecursivePass implements Repe
3737
private $hasProxyDumper;
3838
private $lazy;
3939
private $expressionLanguage;
40+
private $byConstructor;
4041

4142
/**
4243
* @param bool $onlyConstructorArguments Sets this Service Reference pass to ignore method calls
@@ -64,6 +65,7 @@ public function process(ContainerBuilder $container)
6465
$this->graph = $container->getCompiler()->getServiceReferenceGraph();
6566
$this->graph->clear();
6667
$this->lazy = false;
68+
$this->byConstructor = false;
6769

6870
foreach ($container->getAliases() as $id => $alias) {
6971
$targetId = $this->getDefinitionId((string) $alias);
@@ -100,7 +102,8 @@ protected function processValue($value, $isRoot = false)
100102
$targetDefinition,
101103
$value,
102104
$this->lazy || ($this->hasProxyDumper && $targetDefinition && $targetDefinition->isLazy()),
103-
ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE === $value->getInvalidBehavior()
105+
ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE === $value->getInvalidBehavior(),
106+
$this->byConstructor
104107
);
105108

106109
return $value;
@@ -118,8 +121,11 @@ protected function processValue($value, $isRoot = false)
118121
}
119122
$this->lazy = false;
120123

124+
$byConstructor = $this->byConstructor;
125+
$this->byConstructor = true;
121126
$this->processValue($value->getFactory());
122127
$this->processValue($value->getArguments());
128+
$this->byConstructor = $byConstructor;
123129

124130
if (!$this->onlyConstructorArguments) {
125131
$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: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,16 @@ public function dump(array $options = array())
155155
}
156156
}
157157

158-
(new AnalyzeServiceReferencesPass(false))->process($this->container);
158+
(new AnalyzeServiceReferencesPass(false, !$this->getProxyDumper() instanceof NullDumper))->process($this->container);
159159
$this->circularReferences = array();
160-
$checkedNodes = array();
161-
foreach ($this->container->getCompiler()->getServiceReferenceGraph()->getNodes() as $id => $node) {
162-
$currentPath = array($id => $id);
163-
$this->analyzeCircularReferences($node->getOutEdges(), $checkedNodes, $currentPath);
160+
foreach (array(true, false) as $byConstructor) {
161+
foreach ($this->container->getCompiler()->getServiceReferenceGraph()->getNodes() as $id => $node) {
162+
if (!$node->getValue() instanceof Definition) {
163+
continue;
164+
}
165+
$currentPath = array($id => true);
166+
$this->analyzeCircularReferences($node->getOutEdges(), $currentPath, $id, $byConstructor);
167+
}
164168
}
165169
$this->container->getCompiler()->getServiceReferenceGraph()->clear();
166170

@@ -303,27 +307,31 @@ private function getProxyDumper(): ProxyDumper
303307
return $this->proxyDumper;
304308
}
305309

306-
private function analyzeCircularReferences(array $edges, &$checkedNodes, &$currentPath)
310+
private function analyzeCircularReferences(array $edges, &$currentPath, $sourceId, $byConstructor)
307311
{
308312
foreach ($edges as $edge) {
313+
if ($byConstructor && !$edge->isReferencedByConstructor()) {
314+
continue;
315+
}
309316
$node = $edge->getDestNode();
310317
$id = $node->getId();
311318

312-
if ($node->getValue() && ($edge->isLazy() || $edge->isWeak())) {
319+
if (!$node->getValue() instanceof Definition || $sourceId === $id || $edge->isLazy() || $edge->isWeak()) {
313320
// no-op
314321
} elseif (isset($currentPath[$id])) {
315322
$currentId = $id;
316323
foreach (array_reverse($currentPath) as $parentId) {
317-
$this->circularReferences[$parentId][$currentId] = $currentId;
324+
if (!isset($this->circularReferences[$parentId][$currentId])) {
325+
$this->circularReferences[$parentId][$currentId] = $byConstructor;
326+
}
318327
if ($parentId === $id) {
319328
break;
320329
}
321330
$currentId = $parentId;
322331
}
323-
} elseif (!isset($checkedNodes[$id])) {
324-
$checkedNodes[$id] = true;
332+
} else {
325333
$currentPath[$id] = $id;
326-
$this->analyzeCircularReferences($node->getOutEdges(), $checkedNodes, $currentPath);
334+
$this->analyzeCircularReferences($node->getOutEdges(), $currentPath, $id, $byConstructor);
327335
unset($currentPath[$id]);
328336
}
329337
}
@@ -667,8 +675,14 @@ private function addInlineVariables(string $id, Definition $definition, array $a
667675

668676
private function addInlineReference(string $id, Definition $definition, string $targetId, bool $forConstructor): string
669677
{
678+
list($callCount, $behavior) = $this->serviceCalls[$targetId];
679+
680+
while ($this->container->hasAlias($targetId)) {
681+
$targetId = (string) $this->container->getAlias($targetId);
682+
}
683+
670684
if ($id === $targetId) {
671-
return $this->addInlineService($id, $definition, $definition, $forConstructor);
685+
return $this->addInlineService($id, $definition, $definition);
672686
}
673687

674688
if ('service_container' === $targetId || isset($this->referenceVariables[$targetId])) {
@@ -677,9 +691,7 @@ private function addInlineReference(string $id, Definition $definition, string $
677691

678692
$hasSelfRef = isset($this->circularReferences[$id][$targetId]);
679693
$forConstructor = $forConstructor && !isset($this->definitionVariables[$definition]);
680-
list($callCount, $behavior) = $this->serviceCalls[$targetId];
681-
682-
$code = $hasSelfRef && !$forConstructor ? $this->addInlineService($id, $definition, $definition, $forConstructor) : '';
694+
$code = $hasSelfRef && $this->circularReferences[$id][$targetId] && !$forConstructor ? $this->addInlineService($id, $definition, $definition) : '';
683695

684696
if (isset($this->referenceVariables[$targetId]) || (2 > $callCount && (!$hasSelfRef || !$forConstructor))) {
685697
return $code;
@@ -1222,7 +1234,7 @@ public function getParameterBag()
12221234
/*{$this->docStar}
12231235
* Computes a dynamic parameter.
12241236
*
1225-
* @param string The name of the dynamic parameter to load
1237+
* @param string \$name The name of the dynamic parameter to load
12261238
*
12271239
* @return mixed The value of the dynamic parameter
12281240
*

Tests/Fixtures/php/services10.php

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

Tests/Fixtures/php/services12.php

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

Tests/Fixtures/php/services19.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public function getParameterBag()
132132
/**
133133
* Computes a dynamic parameter.
134134
*
135-
* @param string The name of the dynamic parameter to load
135+
* @param string $name The name of the dynamic parameter to load
136136
*
137137
* @return mixed The value of the dynamic parameter
138138
*

Tests/Fixtures/php/services26.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public function getParameterBag()
138138
/**
139139
* Computes a dynamic parameter.
140140
*
141-
* @param string The name of the dynamic parameter to load
141+
* @param string $name The name of the dynamic parameter to load
142142
*
143143
* @return mixed The value of the dynamic parameter
144144
*

Tests/Fixtures/php/services8.php

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

0 commit comments

Comments
 (0)