8000 [Config] Throw exceptions on invalid definition · symfony/symfony@6745b28 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6745b28

Browse files
committed
[Config] Throw exceptions on invalid definition
1 parent fb27de0 commit 6745b28

File tree

5 files changed

+84
-3
lines changed

5 files changed

+84
-3
lines changed

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

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Config\Definition\ArrayNode;
1515
use Symfony\Component\Config\Definition\PrototypedArrayNode;
16+
use Symfony\Component\Config\Definition\Exception\InvalidDefinitionException;
1617

1718
/**
1819
* This class provides a fluent interface for defining an array node.
@@ -259,13 +260,38 @@ protected function getNodeBuilder()
259260
protected function createNode()
260261
{
261262
if (null === $this->prototype) {
262-
$node = new ArrayNode($this->name, $this->parent);
263+
if (null !== $this->key) {
264+
throw new InvalidDefinitionException(
265+
sprintf('%s::useAttributeAsKey() is not applicable to concrete nodes.', __CLASS__)
266+
);
267+
}
268+
269+
if (true === $this->atLeastOne) {
270+
throw new InvalidDefinitionException(
271+
sprintf('%s::requiresAtLeastOneElement() is not applicable to concrete nodes.', __CLASS__)
272+
);
273+
}
263274

275+
if ($this->default) {
276+
throw new InvalidDefinitionException(
277+
sprintf('%s::defaultValue() is not applicable to concrete nodes.', __CLASS__)
278+
);
279+
}
280+
281+
$node = new ArrayNode($this->name, $this->parent);
282+
$node->setAddIfNotSet($this->addDefaults);
283+
264284
foreach ($this->children as $child) {
265285
$child->parent = $node;
266286
$node->addChild($child->getNode());
267287
}
268288
} else {
289+
if ($this->addDefaults) {
290+
throw new InvalidDefinitionException(
291+
sprintf('%s::addDefaultsIfNotSet() is not applicable to prototype nodes.', __CLASS__)
292+
);
293+
}
294+
269295
$node = new PrototypedArrayNode($this->name, $this->parent);
270296

271297
if (null !== $this->key) {
@@ -284,7 +310,6 @@ protected function createNode()
284310
$node->setPrototype($this->prototype->getNode());
285311
}
286312

287-
$node->setAddIfNotSet($this->addDefaults);
288313
$node->setAllowNewKeys($this->allowNewKeys);
289314
$node->addEquivalentValue(null, $this->nullEquivalent);
290315
$node->addEquivalentValue(true, $this->trueEquivalent);

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,8 @@ protected function normalization()
325325
* Instantiate and configure the node according to this definition
326326
*
327327
* @return NodeInterface $node The node instance
328+
*
329+
* @throws Symfony\Component\Config\Definition\Exception\InvalidDefinitionException When the definition is invalid
328330
*/
329331
abstract protected function createNode();
330332

Lines changed: 21 additions & 0 deletions
< D7AE button class="Button Button--iconOnly Button--invisible" aria-label="More options" id=":R14dtlab:" aria-haspopup="true" aria-expanded="false" tabindex="0">
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Config\Definition\Exception;
13+
14+
/**
15+
* Thrown when an error is detected in a node Definition.
16+
*
17+
* @author Victor Berchet <victor.berchet@suumit.com>
18+
*/
19+
class InvalidDefinitionException extends Exception
20+
{
21+
}

src/Symfony/Component/Config/Definition/PrototypedArrayNode.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
1515
use Symfony\Component\Config\Definition\Exception\DuplicateKeyException;
1616
use Symfony\Component\Config\Definition\Exception\UnsetKeyException;
17+
use Symfony\Component\Config\Definition\Exception\Exception;
1718

1819
/**
1920
* Represents a prototyped Array node in the config tree.
@@ -158,7 +159,7 @@ public function getPrototype()
158159
*/
159160
public function addChild(NodeInterface $node)
160161
{
161-
throw new \RuntimeException('A prototyped array node can not have concrete children.');
162+
throw new Exception('A prototyped array node can not have concrete children.');
162163
}
163164

164165
/**

tests/Symfony/Tests/Component/Config/Definition/Builder/ArrayNodeDefinitionTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,38 @@ public function testAppendingSomeNode()
3232
$this->assertTrue(in_array($child, $this->getField($parent, 'children')));
3333
}
3434

35+
/**
36+
* @expectedException Symfony\Component\Config\Definition\Exception\InvalidDefinitionException
37+
* @dataProvider providePrototypeNodeSpecificCalls
38+
*/
39+
public function testPrototypeNodeSpecificOption($method, $args)
40+
{
41+
$node = new ArrayNodeDefinition('root');
42+
43+
call_user_method_array($method, $node, $args);
44+
45+
$node->getNode();
46+
}
47+
48+
public function providePrototypeNodeSpecificCalls()
49+
{
50+
return array(
51+
array('defaultValue', array(array())),
52+
array('requiresAtLeastOneElement', array()),
53+
array('useAttributeAsKey', array('foo'))
54+
);
55+
}
56+
57+
/**
58+
* @expectedException Symfony\Component\Config\Definition\Exception\InvalidDefinitionException
59+
*/
60+
public function testConcreteNodeSpecificOption()
61+
{
62+
$node = new ArrayNodeDefinition('root');
63+
$node->addDefaultsIfNotSet()->prototype('array');
64+
$node->getNode();
65+
}
66+
3567
protected function getField($object, $field)
3668
{
3769
$reflection = new \ReflectionProperty($object, $field);

0 commit comments

Comments
 (0)
0