Skip to content

Commit

Permalink
[DependencyInjection] Add suggestion on ParameterNotFoundException
Browse files Browse the repository at this point in the history
  • Loading branch information
lyrixx committed Apr 30, 2013
1 parent 4dcee0a commit f44db48
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 6 deletions.
Expand Up @@ -21,20 +21,23 @@ class ParameterNotFoundException extends InvalidArgumentException
private $key;
private $sourceId;
private $sourceKey;
private $alternatives;

/**
* Constructor.
*
* @param string $key The requested parameter key
* @param string $sourceId The service id that references the non-existent parameter
* @param string $sourceKey The parameter key that references the non-existent parameter
* @param \Exception $previous The previous exception
* @param string $key The requested parameter key
* @param string $sourceId The service id that references the non-existent parameter
* @param string $sourceKey The parameter key that references the non-existent parameter
* @param \Exception $previous The previous exception
* @param string[] $alternatives Some parameter name alternatives
*/
public function __construct($key, $sourceId = null, $sourceKey = null, \Exception $previous = null)
public function __construct($key, $sourceId = null, $sourceKey = null, \Exception $previous = null, array $alternatives = array())
{
$this->key = $key;
$this->sourceId = $sourceId;
$this->sourceKey = $sourceKey;
$this->alternatives = $alternatives;

parent::__construct('', 0, $previous);

Expand All @@ -50,6 +53,16 @@ public function updateRepr()
} else {
$this->message = sprintf('You have requested a non-existent parameter "%s".', $this->key);
}

if ($this->alternatives) {
if (1 == count($this->alternatives)) {
$this->message .= ' Did you mean this: "';
} elseif (1 < count($this->alternatives)) {
$this->message .= ' Did you mean one of these: "';
}
$this->message .= implode('", "', $this->alternatives);
$this->message .= '" ?';
}
}

public function getKey()
Expand Down
Expand Up @@ -93,7 +93,15 @@ public function get($name)
$name = strtolower($name);

if (!array_key_exists($name, $this->parameters)) {
throw new ParameterNotFoundException($name);
$alternatives = array();
foreach (array_keys($this->parameters) as $key) {
$lev = levenshtein($name, $key);
if ($lev <= strlen($name) / 3 || false !== strpos($key, $name)) {
$alternatives[] = $key;
}
}

throw new ParameterNotFoundException($name, null, null, null, $alternatives);
}

return $this->parameters[$name];
Expand Down
Expand Up @@ -84,6 +84,31 @@ public function testGetSet()
}
}

public function testGetThrowParameterNotFoundException()
{
$bag = new ParameterBag(array(
'foo' => 'foo',
'bar' => 'bar',
'baz' => 'baz',
));

try {
$bag->get('foo1');
$this->fail('->get() throws an Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException if the key does not exist');
} catch (\Exception $e) {
$this->assertInstanceOf('Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException', $e, '->get() throws an Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException if the key does not exist');
$this->assertEquals('You have requested a non-existent parameter "foo1". Did you mean this: "foo" ?', $e->getMessage(), '->get() throws an \InvalidArgumentException with some advices');
}

try {
$bag->get('bag');
$this->fail('->get() throws an Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException if the key does not exist');
} catch (\Exception $e) {
$this->assertInstanceOf('Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException', $e, '->get() throws an Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException if the key does not exist');
$this->assertEquals('You have requested a non-existent parameter "bag". Did you mean one of these: "bar", "baz" ?', $e->getMessage(), '->get() throws an \InvalidArgumentException with some advices');
}
}

/**
* @covers Symfony\Component\DependencyInjection\ParameterBag\ParameterBag::has
*/
Expand Down

0 comments on commit f44db48

Please sign in to comment.