clean handling of :: passed to find()
Commands cannot have a double colon in their name (for example, a command can't be named `foo::bar`). However, one might try to retrieve a `foo::bar` command from the application like this: ```php $command = $application->find('foo::bar'); ``` The `findAlternatives()` method of the `Application` class fails to handle these strings when there are commands registered with a name consisting of at least three parts (e.g. a command is named `foo:bar:baz`). In this case, an empty string is passed to `strpos()` causing PHP to raise a warning.
This commit is contained in:
parent
adbde8ac7a
commit
baab4edf35
@ -1076,7 +1076,7 @@ class Application
|
|||||||
}
|
}
|
||||||
|
|
||||||
$lev = levenshtein($subname, $parts[$i]);
|
$lev = levenshtein($subname, $parts[$i]);
|
||||||
if ($lev <= strlen($subname) / 3 || false !== strpos($parts[$i], $subname)) {
|
if ($lev <= strlen($subname) / 3 || '' !== $subname && false !== strpos($parts[$i], $subname)) {
|
||||||
$alternatives[$collectionName] = $exists ? $alternatives[$collectionName] + $lev : $lev;
|
$alternatives[$collectionName] = $exists ? $alternatives[$collectionName] + $lev : $lev;
|
||||||
} elseif ($exists) {
|
} elseif ($exists) {
|
||||||
$alternatives[$collectionName] += $threshold;
|
$alternatives[$collectionName] += $threshold;
|
||||||
|
@ -445,6 +445,18 @@ class ApplicationTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertEquals('foo:sublong', $application->findNamespace('f:sub'));
|
$this->assertEquals('foo:sublong', $application->findNamespace('f:sub'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @expectedException \InvalidArgumentException
|
||||||
|
* @expectedExceptionMessage Command "foo::bar" is not defined.
|
||||||
|
*/
|
||||||
|
public function testFindWithDoubleColonInNameThrowsException()
|
||||||
|
{
|
||||||
|
$application = new Application();
|
||||||
|
$application->add(new \FooCommand());
|
||||||
|
$application->add(new \Foo4Command());
|
||||||
|
$application->find('foo::bar');
|
||||||
|
}
|
||||||
|
|
||||||
public function testSetCatchExceptions()
|
public function testSetCatchExceptions()
|
||||||
{
|
{
|
||||||
$application = $this->getMock('Symfony\Component\Console\Application', array('getTerminalWidth'));
|
$application = $this->getMock('Symfony\Component\Console\Application', array('getTerminalWidth'));
|
||||||
|
Reference in New Issue
Block a user