8000 [Config] show proposals when unsupported option is provided by fmata · Pull Request #28085 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Config] show proposals when unsupported option is provided #28085

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/Symfony/Component/Config/Definition/ArrayNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,30 @@ protected function normalizeValue($value)

// if extra fields are present, throw exception
if (\count($value) && !$this->ignoreExtraKeys) {
$proposals = array_keys($this->children);
sort($proposals);
$guesses = array();

foreach (array_keys($value) as $subject) {
$minScore = INF;
foreach ($proposals as $proposal) {
$distance = levenshtein($subject, $proposal);
if ($distance <= $minScore && $distance < 3) {
$guesses[$proposal] = $distance;
$minScore = $distance;
}
}
}

$msg = sprintf('Unrecognized option%s "%s" under "%s"', 1 === \count($value) ? '' : 's', implode(', ', array_keys($value)), $this->getPath());

if (\count($guesses)) {
asort($guesses);
$msg .= sprintf('. Did you mean "%s"?', implode('", "', array_keys($guesses)));
} else {
$msg .= sprintf('. Available option%s %s "%s".', 1 === \count($proposals) ? '' : 's', 1 === \count($proposals) ? 'is' : 'are', implode('", "', $proposals));
}

$ex = new InvalidConfigurationException($msg);
$ex->setPath($this->getPath());

Expand Down
25 changes: 25 additions & 0 deletions src/Symfony/Component/Config/Tests/Definition/ArrayNodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,31 @@ public function testExceptionThrownOnUnrecognizedChild()
$node->normalize(array('foo' => 'bar'));
}

/**
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
* @expectedExceptionMessage Did you mean "alpha1", "alpha2"?
*/
public function testNormalizeWithProposals()
{
$node = new ArrayNode('root');
$node->addChild(new ArrayNode('alpha1'));
$node->addChild(new ArrayNode('alpha2'));
$node->addChild(new ArrayNode('beta'));
$node->normalize(array('alpha3' => 'foo'));
}

/**
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
* @expectedExceptionMessage Available options are "alpha1", "alpha2".
*/
public function testNormalizeWithoutProposals()
{
$node = new ArrayNode('root');
$node->addChild(new ArrayNode('alpha1'));
$node->addChild(new ArrayNode('alpha2'));
$node->normalize(array('beta' => 'foo'));
}

public function ignoreAndRemoveMatrixProvider()
{
$unrecognizedOptionException = new InvalidConfigurationException('Unrecognized option "foo" under "root"');
Expand Down
0