Skip to content

Commit 05624c3

Browse files
committed
[Config] Fix using null values with config builders
1 parent d65e1bd commit 05624c3

File tree

26 files changed

+253
-110
lines changed

26 files changed

+253
-110
lines changed

Builder/ClassBuilder.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public function build(): string
9393
USE
9494
9595
/**
96-
* This class is automatically generated to help creating config.
96+
* This class is automatically generated to help in creating a config.
9797
*/
9898
class CLASS IMPLEMENTS
9999
{
@@ -124,14 +124,15 @@ public function addMethod(string $name, string $body, array $params = []): void
124124
$this->methods[] = new Method(strtr($body, ['NAME' => $this->camelCase($name)] + $params));
125125
}
126126

127-
public function addProperty(string $name, string $classType = null): Property
127+
public function addProperty(string $name, string $classType = null, string $defaultValue = null): Property
128128
{
129129
$property = new Property($name, '_' !== $name[0] ? $this->camelCase($name) : $name);
130130
if (null !== $classType) {
131131
$property->setType($classType);
132132
}
133133
$this->properties[] = $property;
134-
$property->setContent(sprintf('private $%s;', $property->getName()));
134+
$defaultValue = null !== $defaultValue ? sprintf(' = %s', $defaultValue) : '';
135+
$property->setContent(sprintf('private $%s%s;', $property->getName(), $defaultValue));
135136

136137
return $property;
137138
}

Builder/ConfigBuilderGenerator.php

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
*/
3232
class ConfigBuilderGenerator implements ConfigBuilderGeneratorInterface
3333
{
34+
/**
35+
* @var ClassBuilder[]
36+
*/
3437
private $classes;
3538
private $outputDir;
3639

@@ -89,6 +92,9 @@ private function writeClasses(): void
8992
foreach ($this->classes as $class) {
9093
$this->buildConstructor($class);
9194
$this->buildToArray($class);
95+
if ($class->getProperties()) {
96+
$class->addProperty('_usedProperties', null, '[]');
97+
}
9298
$this->buildSetExtraKey($class);
9399

94100
file_put_contents($this->getFullPath($class), $class->build());
@@ -135,6 +141,7 @@ private function handleArrayNode(ArrayNode $node, ClassBuilder $class, string $n
135141
public function NAME(array $value = []): CLASS
136142
{
137143
if (null === $this->PROPERTY) {
144+
$this->_usedProperties[\'PROPERTY\'] = true;
138145
$this->PROPERTY = new CLASS($value);
139146
} elseif ([] !== $value) {
140147
throw new InvalidConfigurationException(\'The node created by "NAME()" has already been initialized. You cannot pass values the second time you call NAME().\');
@@ -160,6 +167,7 @@ private function handleVariableNode(VariableNode $node, ClassBuilder $class): vo
160167
*/
161168
public function NAME($valueDEFAULT): self
162169
{
170+
$this->_usedProperties[\'PROPERTY\'] = true;
163171
$this->PROPERTY = $value;
164172
165173
return $this;
@@ -186,6 +194,7 @@ private function handlePrototypedArrayNode(PrototypedArrayNode $node, ClassBuild
186194
*/
187195
public function NAME($value): self
188196
{
197+
$this->_usedProperties[\'PROPERTY\'] = true;
189198
$this->PROPERTY = $value;
190199
191200
return $this;
@@ -200,6 +209,7 @@ public function NAME($value): self
200209
*/
201210
public function NAME(string $VAR, $VALUE): self
202211
{
212+
$this->_usedProperties[\'PROPERTY\'] = true;
203213
$this->PROPERTY[$VAR] = $VALUE;
204214
205215
return $this;
@@ -223,6 +233,8 @@ public function NAME(string $VAR, $VALUE): self
223233
$body = '
224234
public function NAME(array $value = []): CLASS
225235
{
236+
$this->_usedProperties[\'PROPERTY\'] = true;
237+
226238
return $this->PROPERTY[] = new CLASS($value);
227239
}';
228240
$class->addMethod($methodName, $body, ['PROPERTY' => $property->getName(), 'CLASS' => $childClass->getFqcn()]);
@@ -231,9 +243,11 @@ public function NAME(array $value = []): CLASS
231243
public function NAME(string $VAR, array $VALUE = []): CLASS
232244
{
233245
if (!isset($this->PROPERTY[$VAR])) {
234-
return $this->PROPERTY[$VAR] = new CLASS($value);
246+
$this->_usedProperties[\'PROPERTY\'] = true;
247+
248+
return $this->PROPERTY[$VAR] = new CLASS($VALUE);
235249
}
236-
if ([] === $value) {
250+
if ([] === $VALUE) {
237251
return $this->PROPERTY[$VAR];
238252
}
239253
@@ -258,6 +272,7 @@ private function handleScalarNode(ScalarNode $node, ClassBuilder $class): void
258272
*/
259273
public function NAME($value): self
260274
{
275+
$this->_usedProperties[\'PROPERTY\'] = true;
261276
$this->PROPERTY = $value;
262277
263278
return $this;
@@ -367,7 +382,7 @@ private function buildToArray(ClassBuilder $class): void
367382
}
368383

369384
$body .= strtr('
370-
if (null !== $this->PROPERTY) {
385+
if (isset($this->_usedProperties[\'PROPERTY\'])) {
371386
$output[\'ORG_NAME\'] = '.$code.';
372387
}', ['PROPERTY' => $p->getName(), 'ORG_NAME' => $p->getOriginalName()]);
373388
}
@@ -397,7 +412,8 @@ private function buildConstructor(ClassBuilder $class): void
397412
}
398413

399414
$body .= strtr('
400-
if (isset($value[\'ORG_NAME\'])) {
415+
if (array_key_exists(\'ORG_NAME\', $value)) {
416+
$this->_usedProperties[\'PROPERTY\'] = true;
401417
$this->PROPERTY = '.$code.';
402418
unset($value[\'ORG_NAME\']);
403419
}
@@ -441,11 +457,7 @@ private function buildSetExtraKey(ClassBuilder $class): void
441457
*/
442458
public function NAME(string $key, $value): self
443459
{
444-
if (null === $value) {
445-
unset($this->_extraKeys[$key]);
446-
} else {
447-
$this->_extraKeys[$key] = $value;
448-
}
460+
$this->_extraKeys[$key] = $value;
449461
450462
return $this;
451463
}');

Tests/Builder/Fixtures/AddToList/Symfony/Config/AddToList/Messenger/ReceivingConfig.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88

99

1010
/**
11-
* This class is automatically generated to help creating config.
11+
* This class is automatically generated to help in creating a config.
1212
*/
1313
class ReceivingConfig
1414
{
1515
private $priority;
1616
private $color;
17+
private $_usedProperties = [];
1718

1819
/**
1920
* @default null
@@ -22,6 +23,7 @@ class ReceivingConfig
2223
*/
2324
public function priority($value): self
2425
{
26+
$this->_usedProperties['priority'] = true;
2527
$this->priority = $value;
2628

2729
return $this;
@@ -34,6 +36,7 @@ public function priority($value): self
3436
*/
3537
public function color($value): self
3638
{
39+
$this->_usedProperties['color'] = true;
3740
$this->color = $value;
3841

3942
return $this;
@@ -42,12 +45,14 @@ public function color($value): self
4245
public function __construct(array $value = [])
4346
{
4447

45-
if (isset($value['priority'])) {
48+
if (array_key_exists('priority', $value)) {
49+
$this->_usedProperties['priority'] = true;
4650
$this->priority = $value['priority'];
4751
unset($value['priority']);
4852
}
4953

50-
if (isset($value['color'])) {
54+
if (array_key_exists('color', $value)) {
55+
$this->_usedProperties['color'] = true;
5156
$this->color = $value['color'];
5257
unset($value['color']);
5358
}
@@ -60,10 +65,10 @@ public function __construct(array $value = [])
6065
public function toArray(): array
6166
{
6267
$output = [];
63-
if (null !== $this->priority) {
68+
if (isset($this->_usedProperties['priority'])) {
6469
$output['priority'] = $this->priority;
6570
}
66-
if (null !== $this->color) {
71+
if (isset($this->_usedProperties['color'])) {
6772
$output['color'] = $this->color;
6873
}
6974

Tests/Builder/Fixtures/AddToList/Symfony/Config/AddToList/Messenger/RoutingConfig.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,20 @@
88

99

1010
/**
11-
* This class is automatically generated to help creating config.
11+
* This class is automatically generated to help in creating a config.
1212
*/
1313
class RoutingConfig
1414
{
1515
private $senders;
16+
private $_usedProperties = [];
1617

1718
/**
1819
* @param ParamConfigurator|list<mixed|ParamConfigurator> $value
1920
* @return $this
2021
*/
2122
public function senders($value): self
2223
{
24+
$this->_usedProperties['senders'] = true;
2325
$this->senders = $value;
2426

2527
return $this;
@@ -28,7 +30,8 @@ public function senders($value): self
2830
public function __construct(array $value = [])
2931
{
3032

31-
if (isset($value['senders'])) {
33+
if (array_key_exists('senders', $value)) {
34+
$this->_usedProperties['senders'] = true;
3235
$this->senders = $value['senders'];
3336
unset($value['senders']);
3437
}
@@ -41,7 +44,7 @@ public function __construct(array $value = [])
4144
public function toArray(): array
4245
{
4346
$output = [];
44-
if (null !== $this->senders) {
47+
if (isset($this->_usedProperties['senders'])) {
4548
$output['senders'] = $this->senders;
4649
}
4750

Tests/Builder/Fixtures/AddToList/Symfony/Config/AddToList/MessengerConfig.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,19 @@
99

1010

1111
/**
12-
* This class is automatically generated to help creating config.
12+
* This class is automatically generated to help in creating a config.
1313
*/
1414
class MessengerConfig
1515
{
1616
private $routing;
1717
private $receiving;
18+
private $_usedProperties = [];
1819

1920
public function routing(string $message_class, array $value = []): \Symfony\Config\AddToList\Messenger\RoutingConfig
2021
{
2122
if (!isset($this->routing[$message_class])) {
23+
$this->_usedProperties['routing'] = true;
24+
2225
return $this->routing[$message_class] = new \Symfony\Config\AddToList\Messenger\RoutingConfig($value);
2326
}
2427
if ([] === $value) {
@@ -30,18 +33,22 @@ public function routing(string $message_class, array $value = []): \Symfony\Conf
3033

3134
public function receiving(array $value = []): \Symfony\Config\AddToList\Messenger\ReceivingConfig
3235
{
36+
$this->_usedProperties['receiving'] = true;
37+
3338
return $this->receiving[] = new \Symfony\Config\AddToList\Messenger\ReceivingConfig($value);
3439
}
3540

3641
public function __construct(array $value = [])
3742
{
3843

39-
if (isset($value['routing'])) {
44+
if (array_key_exists('routing', $value)) {
45+
$this->_usedProperties['routing'] = true;
4046
$this->routing = array_map(function ($v) { return new \Symfony\Config\AddToList\Messenger\RoutingConfig($v); }, $value['routing']);
4147
unset($value['routing']);
4248
}
4349

44-
if (isset($value['receiving'])) {
50+
if (array_key_exists('receiving', $value)) {
51+
$this->_usedProperties['receiving'] = true;
4552
$this->receiving = array_map(function ($v) { return new \Symfony\Config\AddToList\Messenger\ReceivingConfig($v); }, $value['receiving']);
4653
unset($value['receiving']);
4754
}
@@ -54,10 +61,10 @@ public function __construct(array $value = [])
5461
public function toArray(): array
5562
{
5663
$output = [];
57-
if (null !== $this->routing) {
64+
if (isset($this->_usedProperties['routing'])) {
5865
$output['routing'] = array_map(function ($v) { return $v->toArray(); }, $this->routing);
5966
}
60-
if (null !== $this->receiving) {
67+
if (isset($this->_usedProperties['receiving'])) {
6168
$output['receiving'] = array_map(function ($v) { return $v->toArray(); }, $this->receiving);
6269
}
6370

Tests/Builder/Fixtures/AddToList/Symfony/Config/AddToList/TranslatorConfig.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,21 @@
88

99

1010
/**
11-
* This class is automatically generated to help creating config.
11+
* This class is automatically generated to help in creating a config.
1212
*/
1313
class TranslatorConfig
1414
{
1515
private $fallbacks;
1616
private $sources;
17+
private $_usedProperties = [];
1718

1819
/**
1920
* @param ParamConfigurator|list<mixed|ParamConfigurator> $value
2021
* @return $this
2122
*/
2223
public function fallbacks($value): self
2324
{
25+
$this->_usedProperties['fallbacks'] = true;
2426
$this->fallbacks = $value;
2527

2628
return $this;
@@ -32,6 +34,7 @@ public function fallbacks($value): self
3234
*/
3335
public function source(string $source_class, $value): self
3436
{
37+
$this->_usedProperties['sources'] = true;
3538
$this->sources[$source_class] = $value;
3639

3740
return $this;
@@ -40,12 +43,14 @@ public function source(string $source_class, $value): self
4043
public function __construct(array $value = [])
4144
{
4245

43-
if (isset($value['fallbacks'])) {
46+
if (array_key_exists('fallbacks', $value)) {
47+
$this->_usedProperties['fallbacks'] = true;
4448
$this->fallbacks = $value['fallbacks'];
4549
unset($value['fallbacks']);
4650
}
4751

48-
if (isset($value['sources'])) {
52+
if (array_key_exists('sources', $value)) {
53+
$this->_usedProperties['sources'] = true;
4954
$this->sources = $value['sources'];
5055
unset($value['sources']);
5156
}
@@ -58,10 +63,10 @@ public function __construct(array $value = [])
5863
public function toArray(): array
5964
{
6065
$output = [];
61-
if (null !== $this->fallbacks) {
66+
if (isset($this->_usedProperties['fallbacks'])) {
6267
$output['fallbacks'] = $this->fallbacks;
6368
}
64-
if (null !== $this->sources) {
69+
if (isset($this->_usedProperties['sources'])) {
6570
$output['sources'] = $this->sources;
6671
}
6772

0 commit comments

Comments
 (0)