Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 15f69a3

Browse files
committed
[Config] show proposals when unsupported option is provided
1 parent 37a3f0b commit 15f69a3

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,30 @@ protected function normalizeValue($value)
307307

308308
// if extra fields are present, throw exception
309309
if (\count($value) && !$this->ignoreExtraKeys) {
310+
$proposals = array_keys($this->children);
311+
sort($proposals);
312+
$guesses = array();
313+
314+
foreach (array_keys($value) as $subject) {
315+
$minScore = INF;
316+
foreach ($proposals as $proposal) {
317+
$distance = levenshtein($subject, $proposal);
318+
if ($distance <= $minScore && $distance < 3) {
319+
$guesses[$proposal] = $distance;
320+
$minScore = $distance;
321+
}
322+
}
323+
}
324+
310325
$msg = sprintf('Unrecognized option%s "%s" under "%s"', 1 === \count($value) ? '' : 's', implode(', ', array_keys($value)), $this->getPath());
326+
327+
if (\count($guesses)) {
328+
asort($guesses);
329+
$msg .= sprintf('. Did you mean "%s"?', implode('", "', array_keys($guesses)));
330+
} else {
331+
$msg .= sprintf('. Available option%s %s "%s".', 1 === \count($proposals) ? '' : 's', 1 === \count($proposals) ? 'is' : 'are', implode('", "', $proposals));
332+
}
333+
311334
$ex = new InvalidConfigurationException($msg);
312335
$ex->setPath($this->getPath());
313336

src/Symfony/Component/Config/Tests/Definition/ArrayNodeTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,31 @@ public function testExceptionThrownOnUnrecognizedChild()
3737
$node->normalize(array('foo' => 'bar'));
3838
}
3939

40+
/**
41+
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
42+
* @expectedExceptionMessage Did you mean "alpha1", "alpha2"?
43+
*/
44+
public function testNormalizeWithProposals()
45+
{
46+
$node = new ArrayNode('root');
47+
$node->addChild(new ArrayNode('alpha1'));
48+
$node->addChild(new ArrayNode('alpha2'));
49+
$node->addChild(new ArrayNode('beta'));
50+
$node->normalize(array('alpha3' => 'foo'));
51+
}
52+
53+
/**
54+
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
55+
* @expectedExceptionMessage Available options are "alpha1", "alpha2".
56+
*/
57+
public function testNormalizeWithoutProposals()
58+
{
59+
$node = new ArrayNode('root');
60+
$node->addChild(new ArrayNode('alpha1'));
61+
$node->addChild(new ArrayNode('alpha2'));
62+
$node->normalize(array('beta' => 'foo'));
63+
}
64+
4065
public function ignoreAndRemoveMatrixProvider()
4166
{
4267
$unrecognizedOptionException = new InvalidConfigurationException('Unrecognized option "foo" under "root"');

0 commit comments

Comments
 (0)