Skip to content

Commit 99b0883

Browse files
committed
Add comment on array methods
1 parent 8d13f4b commit 99b0883

File tree

2 files changed

+47
-21
lines changed

2 files changed

+47
-21
lines changed

src/Symfony/Component/Config/Builder/ConfigBuilderGenerator.php

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Config\Builder;
1313

1414
use Symfony\Component\Config\Definition\ArrayNode;
15+
use Symfony\Component\Config\Definition\BaseNode;
1516
use Symfony\Component\Config\Definition\BooleanNode;
1617
use Symfony\Component\Config\Definition\ConfigurationInterface;
1718
use Symfony\Component\Config\Definition\EnumNode;
@@ -125,14 +126,19 @@ private function buildNode(NodeInterface $node, ClassBuilder $class, string $nam
125126

126127
private function handleArrayNode(ArrayNode $node, ClassBuilder $class, string $namespace): void
127128
{
129+
$comment = $this->getComment($node);
130+
if ('' !== $comment) {
131+
$comment = "/**\n$comment*/\n";
132+
}
133+
128134
$childClass = new ClassBuilder($namespace, $node->getName());
129135
$childClass->setAllowExtraKeys($node->shouldIgnoreExtraKeys());
130136
$class->addRequire($childClass);
131137
$this->classes[] = $childClass;
132138

133139
$property = $class->addProperty($node->getName(), $childClass->getFqcn());
134140
$body = '
135-
public function NAME(array $value = []): CLASS
141+
COMMENTpublic function NAME(array $value = []): CLASS
136142
{
137143
if (null === $this->PROPERTY) {
138144
$this->PROPERTY = new CLASS($value);
@@ -143,7 +149,7 @@ public function NAME(array $value = []): CLASS
143149
return $this->PROPERTY;
144150
}';
145151
$class->addUse(InvalidConfigurationException::class);
146-
$class->addMethod($node->getName(), $body, ['PROPERTY' => $property->getName(), 'CLASS' => $childClass->getFqcn()]);
152+
$class->addMethod($node->getName(), $body, ['COMMENT' => $comment, 'PROPERTY' => $property->getName(), 'CLASS' => $childClass->getFqcn()]);
147153

148154
$this->buildNode($node, $childClass, $this->getSubNamespace($childClass));
149155
}
@@ -220,16 +226,21 @@ public function NAME(string $VAR, TYPE $VALUE): static
220226
$this->classes[] = $childClass;
221227
$property = $class->addProperty($node->getName(), $childClass->getFqcn().'[]');
222228

229+
$comment = $this->getComment($node);
230+
if ('' !== $comment) {
231+
$comment = "/**\n$comment*/\n";
232+
}
233+
223234
if (null === $key = $node->getKeyAttribute()) {
224235
$body = '
225-
public function NAME(array $value = []): CLASS
236+
COMMENTpublic function NAME(array $value = []): CLASS
226237
{
227238
return $this->PROPERTY[] = new CLASS($value);
228239
}';
229-
$class->addMethod($methodName, $body, ['PROPERTY' => $property->getName(), 'CLASS' => $childClass->getFqcn()]);
240+
$class->addMethod($methodName, $body, ['COMMENT' => $comment, 'PROPERTY' => $property->getName(), 'CLASS' => $childClass->getFqcn()]);
230241
} else {
231242
$body = '
232-
public function NAME(string $VAR, array $VALUE = []): CLASS
243+
COMMENTpublic function NAME(string $VAR, array $VALUE = []): CLASS
233244
{
234245
if (!isset($this->PROPERTY[$VAR])) {
235246
return $this->PROPERTY[$VAR] = new CLASS($value);
@@ -241,7 +252,7 @@ public function NAME(string $VAR, array $VALUE = []): CLASS
241252
throw new InvalidConfigurationException(\'The node created by "NAME()" has already been initialized. You cannot pass values the second time you call NAME().\');
242253
}';
243254
$class->addUse(InvalidConfigurationException::class);
244-
$class->addMethod($methodName, $body, ['PROPERTY' => $property->getName(), 'CLASS' => $childClass->getFqcn(), 'VAR' => '' === $key ? 'key' : $key, 'VALUE' => 'value' === $key ? 'data' : 'value']);
255+
$class->addMethod($methodName, $body, ['COMMENT' => $comment, 'PROPERTY' => $property->getName(), 'CLASS' => $childClass->getFqcn(), 'VAR' => '' === $key ? 'key' : $key, 'VALUE' => 'value' === $key ? 'data' : 'value']);
245256
}
246257

247258
$this->buildNode($prototype, $childClass, $namespace.'\\'.$childClass->getName());
@@ -298,31 +309,41 @@ private function getParameterType(NodeInterface $node): ?string
298309
return null;
299310
}
300311

301-
private function getComment(VariableNode $node): string
312+
private function getComment(BaseNode $node): string
302313
{
303314
$comment = '';
304315
if ('' !== $info = (string) $node->getInfo()) {
305316
$comment .= ' * '.$info."\n";
306317
}
307318

308-
foreach ((array) ($node->getExample() ?? []) as $example) {
309-
$comment .= ' * @example '.$example."\n";
310-
}
319+
if (!$node instanceof ArrayNode) {
320+
foreach ((array) ($node->getExample() ?? []) as $example) {
321+
$comment .= ' * @example '.$example."\n";
322+
}
311323

312-
if ('' !== $default = $node->getDefaultValue()) {
313-
$comment .= ' * @default '.(null === $default ? 'null' : var_export($default, true))."\n";
314-
}
324+
if ('' !== $default = $node->getDefaultValue()) {
325+
$comment .= ' * @default '.(null === $default ? 'null' : var_export($default, true))."\n";
326+
}
315327

316-
if ($node instanceof EnumNode) {
317-
$comment .= sprintf(' * @param ParamConfigurator|%s $value', implode('|', array_map(function ($a) {
318-
return var_export($a, true);
319-
}, $node->getValues())))."\n";
328+
if ($node instanceof EnumNode) {
329+
$comment .= sprintf(' * @param ParamConfigurator|%s $value', implode('|', array_map(function ($a) {
330+
return var_export($a, true);
331+
}, $node->getValues())))."\n";
332+
} else {
333+
$parameterType = $this->getParameterType($node);
334+
if (null === $parameterType || '' === $parameterType) {
335+
$parameterType = 'mixed';
336+
}
337+
$comment .= ' * @param ParamConfigurator|'.$parameterType.' $value'."\n";
338+
}
320339
} else {
321-
$parameterType = $this->getParameterType($node);
322-
if (null === $parameterType || '' === $parameterType) {
323-
$parameterType = 'mixed';
340+
foreach ((array) ($node->getExample() ?? []) as $example) {
341+
$comment .= ' * @example '.json_encode($example)."\n";
342+
}
343+
344+
if ($node->hasDefaultValue() && [] != $default = $node->getDefaultValue()) {
345+
$comment .= ' * @default '.json_encode($default)."\n";
324346
}
325-
$comment .= ' * @param ParamConfigurator|'.$parameterType.' $value'."\n";
326347
}
327348

328349
if ($node->isDeprecated()) {

src/Symfony/Component/Config/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
6.1
5+
---
6+
7+
* Add Node's information in generated Config
8+
49
6.0
510
---
611

0 commit comments

Comments
 (0)