Skip to content

Commit baab4ed

Browse files
committed
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.
1 parent adbde8a commit baab4ed

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/Symfony/Component/Console/Application.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1076,7 +1076,7 @@ private function findAlternatives($name, $collection)
10761076
}
10771077

10781078
$lev = levenshtein($subname, $parts[$i]);
1079-
if ($lev <= strlen($subname) / 3 || false !== strpos($parts[$i], $subname)) {
1079+
if ($lev <= strlen($subname) / 3 || '' !== $subname && false !== strpos($parts[$i], $subname)) {
10801080
$alternatives[$collectionName] = $exists ? $alternatives[$collectionName] + $lev : $lev;
10811081
} elseif ($exists) {
10821082
$alternatives[$collectionName] += $threshold;

src/Symfony/Component/Console/Tests/ApplicationTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,18 @@ public function testFindNamespaceDoesNotFailOnDeepSimilarNamespaces()
445445
$this->assertEquals('foo:sublong', $application->findNamespace('f:sub'));
446446
}
447447

448+
/**
449+
* @expectedException \InvalidArgumentException
450+
* @expectedExceptionMessage Command "foo::bar" is not defined.
451+
*/
452+
public function testFindWithDoubleColonInNameThrowsException()
453+
{
454+
$application = new Application();
455+
$application->add(new \FooCommand());
456+
$application->add(new \Foo4Command());
457+
$application->find('foo::bar');
458+
}
459+
448460
public function testSetCatchExceptions()
449461
{
450462
$application = $this->getMock('Symfony\Component\Console\Application', array('getTerminalWidth'));

0 commit comments

Comments
 (0)