Skip to content

Commit cba2c33

Browse files
committed
[Config] Improve error messages & extensibility
1 parent bca2b0e commit cba2c33

File tree

1 file changed

+57
-29
lines changed

1 file changed

+57
-29
lines changed

src/Symfony/Component/Config/Definition/Builder/ArrayNodeDefinition.php

Lines changed: 57 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -278,45 +278,21 @@ protected function getNodeBuilder()
278278
protected function createNode()
279279
{
280280
if (null === $this->prototype) {
281-
if (null !== $this->key) {
282-
throw new InvalidDefinitionException(
283-
sprintf('%s::useAttributeAsKey() is not applicable to concrete nodes.', __CLASS__)
284-
);
285-
}
286-
287-
if (true === $this->atLeastOne) {
288-
throw new InvalidDefinitionException(
289-
sprintf('%s::requiresAtLeastOneElement() is not applicable to concrete nodes.', __CLASS__)
290-
);
291-
}
281+
$node = new ArrayNode($this->name, $this->parent);
292282

293-
if ($this->default) {
294-
throw new InvalidDefinitionException(
295-
sprintf('%s::defaultValue() is not applicable to concrete nodes.', __CLASS__)
296-
);
297-
}
283+
$this->validateConcreteNode($node);
298284

299-
$node = new ArrayNode($this->name, $this->parent);
300285
$node->setAddIfNotSet($this->addDefaults);
301286

302287
foreach ($this->children as $child) {
303288
$child->parent = $node;
304289
$node->addChild($child->getNode());
305290
}
306291
} else {
307-
if ($this->addDefaults) {
308-
throw new InvalidDefinitionException(
309-
sprintf('%s::addDefaultsIfNotSet() is not applicable to prototype nodes.', __CLASS__)
310-
);
311-
}
312-
313-
if ($this->default && false !== $this->addDefaultChildren) {
314-
throw new InvalidDefinitionException('A default value and default children might not be used together.');
315-
316-
}
317-
318292
$node = new PrototypedArrayNode($this->name, $this->parent);
319293

294+
$this->validatePrototypeNode($node);
295+
320296
if (null !== $this->key) {
321297
$node->setKeyAttribute($this->key, $this->removeKeyItem);
322298
}
@@ -338,7 +314,6 @@ protected function createNode()
338314

339315
$this->prototype->parent = $node;
340316
$node->setPrototype($this->prototype->getNode());
341-
342317
}
343318

344319
$node->setAllowNewKeys($this->allowNewKeys);
@@ -366,4 +341,57 @@ protected function createNode()
366341
return $node;
367342
}
368343

344+
/**
345+
* Validate the confifuration of a concrete node.
346+
*
347+
* @param NodeInterface $node The related node
348+
*
349+
* @throws InvalidDefinitionException When an error is detected in the configuration
350+
*/
351+
protected function validateConcreteNode(ArrayNode $node)
352+
{
353+
$path = $node->getPath();
354+
355+
if (null !== $this->key) {
356+
throw new InvalidDefinitionException(
357+
sprintf('->useAttributeAsKey() is not applicable to concrete nodes at path "%s"', $path)
358+
);
359+
}
360+
361+
if (true === $this->atLeastOne) {
362+
throw new InvalidDefinitionException(
363+
sprintf('->requiresAtLeastOneElement() is not applicable to concrete nodes at path "%s"', $path)
364+
);
365+
}
366+
367+
if ($this->default) {
368+
throw new InvalidDefinitionException(
369+
sprintf('->defaultValue() is not applicable to concrete nodes at path "%s"', $path)
370+
);
371+
}
372+
}
373+
374+
/**
375+
* Validate the configuration of a prototype node.
376+
*
377+
* @param NodeInterface $node The related node
378+
*
379+
* @throws InvalidDefinitionException When an error is detected in the configuration
380+
*/
381+
protected function validatePrototypeNode(PrototypedArrayNode $node)
382+
{
383+
$path = $node->getPath();
384+
385+
if ($this->addDefaults) {
386+
throw new InvalidDefinitionException(
387+
sprintf('->addDefaultsIfNotSet() is not applicable to prototype nodes at path "%s"', $path)
388+
);
389+
}
390+
391+
if ($this->default && false !== $this->addDefaultChildren) {
392+
throw new InvalidDefinitionException(
393+
sprintf('A default value and default children might not be used together at path "%s"', $path)
394+
);
395+
}
396+
}
369397
}

0 commit comments

Comments
 (0)