Skip to content

Commit fb34692

Browse files
Consistently throw exceptions on a single line
1 parent a145664 commit fb34692

File tree

8 files changed

+23
-81
lines changed

8 files changed

+23
-81
lines changed

Definition/ArrayNode.php

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -219,15 +219,13 @@ public function addChild(NodeInterface $node)
219219
protected function finalizeValue($value)
220220
{
221221
if (false === $value) {
222-
$msg = sprintf('Unsetting key for path "%s", value: %s', $this->getPath(), json_encode($value));
223-
throw new UnsetKeyException($msg);
222+
throw new UnsetKeyException(sprintf('Unsetting key for path "%s", value: %s', $this->getPath(), json_encode($value)));
224223
}
225224

226225
foreach ($this->children as $name => $child) {
227226
if (!array_key_exists($name, $value)) {
228227
if ($child->isRequired()) {
229-
$msg = sprintf('The child node "%s" at path "%s" must be configured.', $name, $this->getPath());
230-
$ex = new InvalidConfigurationException($msg);
228+
$ex = new InvalidConfigurationException(sprintf('The child node "%s" at path "%s" must be configured.', $name, $this->getPath()));
231229
$ex->setPath($this->getPath());
232230

233231
throw $ex;
@@ -260,11 +258,7 @@ protected function finalizeValue($value)
260258
protected function validateType($value)
261259
{
262260
if (!\is_array($value) && (!$this->allowFalse || false !== $value)) {
263-
$ex = new InvalidTypeException(sprintf(
264-
'Invalid type for path "%s". Expected array, but got %s',
265-
$this->getPath(),
266-
\gettype($value)
267-
));
261+
$ex = new InvalidTypeException(sprintf('Invalid type for path "%s". Expected array, but got %s', $this->getPath(), \gettype($value)));
268262
if ($hint = $this->getInfo()) {
269263
$ex->addHint($hint);
270264
}
@@ -303,8 +297,7 @@ protected function normalizeValue($value)
303297

304298
// if extra fields are present, throw exception
305299
if (\count($value) && !$this->ignoreExtraKeys) {
306-
$msg = sprintf('Unrecognized option%s "%s" under "%s"', 1 === \count($value) ? '' : 's', implode(', ', array_keys($value)), $this->getPath());
307-
$ex = new InvalidConfigurationException($msg);
300+
$ex = new InvalidConfigurationException(sprintf('Unrecognized option%s "%s" under "%s"', 1 === \count($value) ? '' : 's', implode(', ', array_keys($value)), $this->getPath()));
308301
$ex->setPath($this->getPath());
309302

310303
throw $ex;
@@ -363,13 +356,7 @@ protected function mergeValues($leftSide, $rightSide)
363356
// no conflict
364357
if (!array_key_exists($k, $leftSide)) {
365358
if (!$this->allowNewKeys) {
366-
$ex = new InvalidConfigurationException(sprintf(
367-
'You are not allowed to define new elements for path "%s". '
368-
.'Please define all elements for this path in one config file. '
369-
.'If you are trying to overwrite an element, make sure you redefine it '
370-
.'with the same name.',
371-
$this->getPath()
372-
));
359+
$ex = new InvalidConfigurationException(sprintf('You are not allowed to define new elements for path "%s". Please define all elements for this path in one config file. If you are trying to overwrite an element, make sure you redefine it with the same name.', $this->getPath()));
373360
$ex->setPath($this->getPath());
374361

375362
throw $ex;

Definition/BaseNode.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -205,12 +205,7 @@ public function getPath()
205205
final public function merge($leftSide, $rightSide)
206206
{
207207
if (!$this->allowOverwrite) {
208-
throw new ForbiddenOverwriteException(sprintf(
209-
'Configuration path "%s" cannot be overwritten. You have to '
210-
.'define all options for this path, and any of its sub-paths in '
211-
.'one configuration section.',
212-
$this->getPath()
213-
));
208+
throw new ForbiddenOverwriteException(sprintf('Configuration path "%s" cannot be overwritten. You have to define all options for this path, and any of its sub-paths in one configuration section.', $this->getPath()));
214209
}
215210

216211
$this->validateType($leftSide);

Definition/BooleanNode.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,7 @@ class BooleanNode extends ScalarNode
2626
protected function validateType($value)
2727
{
2828
if (!\is_bool($value)) {
29-
$ex = new InvalidTypeException(sprintf(
30-
'Invalid type for path "%s". Expected boolean, but got %s.',
31-
$this->getPath(),
32-
\gettype($value)
33-
));
29+
$ex = new InvalidTypeException(sprintf('Invalid type for path "%s". Expected boolean, but got %s.', $this->getPath(), \gettype($value)));
3430
if ($hint = $this->getInfo()) {
3531
$ex->addHint($hint);
3632
}

Definition/Builder/ArrayNodeDefinition.php

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -411,27 +411,19 @@ protected function validateConcreteNode(ArrayNode $node)
411411
$path = $node->getPath();
412412

413413
if (null !== $this->key) {
414-
throw new InvalidDefinitionException(
415-
sprintf('->useAttributeAsKey() is not applicable to concrete nodes at path "%s"', $path)
416-
);
414+
throw new InvalidDefinitionException(sprintf('->useAttributeAsKey() is not applicable to concrete nodes at path "%s"', $path));
417415
}
418416

419417
if (true === $this->atLeastOne) {
420-
throw new InvalidDefinitionException(
421-
sprintf('->requiresAtLeastOneElement() is not applicable to concrete nodes at path "%s"', $path)
422-
);
418+
throw new InvalidDefinitionException(sprintf('->requiresAtLeastOneElement() is not applicable to concrete nodes at path "%s"', $path));
423419
}
424420

425421
if ($this->default) {
426-
throw new InvalidDefinitionException(
427-
sprintf('->defaultValue() is not applicable to concrete nodes at path "%s"', $path)
428-
);
422+
throw new InvalidDefinitionException(sprintf('->defaultValue() is not applicable to concrete nodes at path "%s"', $path));
429423
}
430424

431425
if (false !== $this->addDefaultChildren) {
432-
throw new InvalidDefinitionException(
433-
sprintf('->addDefaultChildrenIfNoneSet() is not applicable to concrete nodes at path "%s"', $path)
434-
);
426+
throw new InvalidDefinitionException(sprintf('->addDefaultChildrenIfNoneSet() is not applicable to concrete nodes at path "%s"', $path));
435427
}
436428
}
437429

@@ -445,28 +437,20 @@ protected function validatePrototypeNode(PrototypedArrayNode $node)
445437
$path = $node->getPath();
446438

447439
if ($this->addDefaults) {
448-
throw new InvalidDefinitionException(
449-
sprintf('->addDefaultsIfNotSet() is not applicable to prototype nodes at path "%s"', $path)
450-
);
440+
throw new InvalidDefinitionException(sprintf('->addDefaultsIfNotSet() is not applicable to prototype nodes at path "%s"', $path));
451441
}
452442

453443
if (false !== $this->addDefaultChildren) {
454444
if ($this->default) {
455-
throw new InvalidDefinitionException(
456-
sprintf('A default value and default children might not be used together at path "%s"', $path)
457-
);
445+
throw new InvalidDefinitionException(sprintf('A default value and default children might not be used together at path "%s"', $path));
458446
}
459447

460448
if (null !== $this->key && (null === $this->addDefaultChildren || \is_int($this->addDefaultChildren) && $this->addDefaultChildren > 0)) {
461-
throw new InvalidDefinitionException(
462-
sprintf('->addDefaultChildrenIfNoneSet() should set default children names as ->useAttributeAsKey() is used at path "%s"', $path)
463-
);
449+
throw new InvalidDefinitionException(sprintf('->addDefaultChildrenIfNoneSet() should set default children names as ->useAttributeAsKey() is used at path "%s"', $path));
464450
}
465451

466452
if (null === $this->key && (\is_string($this->addDefaultChildren) || \is_array($this->addDefaultChildren))) {
467-
throw new InvalidDefinitionException(
468-
sprintf('->addDefaultChildrenIfNoneSet() might not set default children names as ->useAttributeAsKey() is not used at path "%s"', $path)
469-
);
453+
throw new InvalidDefinitionException(sprintf('->addDefaultChildrenIfNoneSet() might not set default children names as ->useAttributeAsKey() is not used at path "%s"', $path));
470454
}
471455
}
472456
}

Definition/EnumNode.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,7 @@ protected function finalizeValue($value)
4343
$value = parent::finalizeValue($value);
4444

4545
if (!\in_array($value, $this->values, true)) {
46-
$ex = new InvalidConfigurationException(sprintf(
47-
'The value %s is not allowed for path "%s". Permissible values: %s',
48-
json_encode($value),
49-
$this->getPath(),
50-
implode(', ', array_map('json_encode', $this->values))));
46+
$ex = new InvalidConfigurationException(sprintf('The value %s is not allowed for path "%s". Permissible values: %s', json_encode($value), $this->getPath(), implode(', ', array_map('json_encode', $this->values))));
5147
$ex->setPath($this->getPath());
5248

5349
throw $ex;

Definition/PrototypedArrayNode.php

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,7 @@ public function addChild(NodeInterface $node)
185185
protected function finalizeValue($value)
186186
{
187187
if (false === $value) {
188-
$msg = sprintf('Unsetting key for path "%s", value: %s', $this->getPath(), json_encode($value));
189-
throw new UnsetKeyException($msg);
188+
throw new UnsetKeyException(sprintf('Unsetting key for path "%s", value: %s', $this->getPath(), json_encode($value)));
190189
}
191190

192191
foreach ($value as $k => $v) {
@@ -199,8 +198,7 @@ protected function finalizeValue($value)
199198
}
200199

201200
if (\count($value) < $this->minNumberOfElements) {
202-
$msg = sprintf('The path "%s" should have at least %d element(s) defined.', $this->getPath(), $this->minNumberOfElements);
203-
$ex = new InvalidConfigurationException($msg);
201+
$ex = new InvalidConfigurationException(sprintf('The path "%s" should have at least %d element(s) defined.', $this->getPath(), $this->minNumberOfElements));
204202
$ex->setPath($this->getPath());
205203

206204
throw $ex;
@@ -232,8 +230,7 @@ protected function normalizeValue($value)
232230
foreach ($value as $k => $v) {
233231
if (null !== $this->keyAttribute && \is_array($v)) {
234232
if (!isset($v[$this->keyAttribute]) && \is_int($k) && !$isAssoc) {
235-
$msg = sprintf('The attribute "%s" must be set for path "%s".', $this->keyAttribute, $this->getPath());
236-
$ex = new InvalidConfigurationException($msg);
233+
$ex = new InvalidConfigurationException(sprintf('The attribute "%s" must be set for path "%s".', $this->keyAttribute, $this->getPath()));
237234
$ex->setPath($this->getPath());
238235

239236
throw $ex;
@@ -262,8 +259,7 @@ protected function normalizeValue($value)
262259
}
263260

264261
if (array_key_exists($k, $normalized)) {
265-
$msg = sprintf('Duplicate key "%s" for path "%s".', $k, $this->getPath());
266-
$ex = new DuplicateKeyException($msg);
262+
$ex = new DuplicateKeyException(sprintf('Duplicate key "%s" for path "%s".', $k, $this->getPath()));
267263
$ex->setPath($this->getPath());
268264

269265
throw $ex;
@@ -314,11 +310,7 @@ protected function mergeValues($leftSide, $rightSide)
314310
// no conflict
315311
if (!array_key_exists($k, $leftSide)) {
316312
if (!$this->allowNewKeys) {
317-
$ex = new InvalidConfigurationException(sprintf(
318-
'You are not allowed to define new elements for path "%s". '.
319-
'Please define all elements for this path in one config file.',
320-
$this->getPath()
321-
));
313+
$ex = new InvalidConfigurationException(sprintf('You are not allowed to define new elements for path "%s". Please define all elements for this path in one config file.', $this->getPath()));
322314
$ex->setPath($this->getPath());
323315

324316
throw $ex;

Definition/ScalarNode.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,7 @@ class ScalarNode extends VariableNode
3333
protected function validateType($value)
3434
{
3535
if (!is_scalar($value) && null !== $value) {
36-
$ex = new InvalidTypeException(sprintf(
37-
'Invalid type for path "%s". Expected scalar, but got %s.',
38-
$this->getPath(),
39-
\gettype($value)
40-
));
36+
$ex = new InvalidTypeException(sprintf('Invalid type for path "%s". Expected scalar, but got %s.', $this->getPath(), \gettype($value)));
4137
if ($hint = $this->getInfo()) {
4238
$ex->addHint($hint);
4339
}

Definition/VariableNode.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,7 @@ protected function validateType($value)
8282
protected function finalizeValue($value)
8383
{
8484
if (!$this->allowEmptyValue && $this->isValueEmpty($value)) {
85-
$ex = new InvalidConfigurationException(sprintf(
86-
'The path "%s" cannot contain an empty value, but got %s.',
87-
$this->getPath(),
88-
json_encode($value)
89-
));
85+
$ex = new InvalidConfigurationException(sprintf('The path "%s" cannot contain an empty value, but got %s.', $this->getPath(), json_encode($value)));
9086
if ($hint = $this->getInfo()) {
9187
$ex->addHint($hint);
9288
}

0 commit comments

Comments
 (0)