Ad

How Does Composer Resolve The `dumpautoload` command?

Using Composer version 1.8.6 2019-06-11 15:03:05 the dumpautoload command works with all the following variations

composer dump-autoload
composer dumpautoload
composer dumpautoloa
composer dumpautolo
composer dumpautol
composer dumpauto
composer dumpaut
composer dumpau
composer dumpa
composer dump
composer dum
composer du

Until composer d which throws

[Symfony\Component\Console\Exception\CommandNotFoundException]
Command "d" is ambiguous (depends, dump-autoload and 1 more).

How are the commands resolved? I understand that dumpautoload is set as an alias to dump-autoload in composer source here

class DumpAutoloadCommand extends BaseCommand
{
   protected function configure()
   {
      $this->setName('dump-autoload')->setAliases(array('dumpautoload'))

and setAliases in the composer/RootPackage.phpsource here only assign the aliases to the private $aliases; property

public function setAliases(array $aliases)
{
    $this->aliases = $aliases;
}

But how are the other variations resolved?

Ad

Answer

Composer builds up upon symfony/console, and the code to search for a command is located in Symfony\Component\Console\Application::find, which can be found at https://github.com/symfony/console/blob/master/Application.php#L596. Basically, all registered commands are checked until the shortest, clearly identifiable length - so the approach with similar_text is already too sophisticated ;)

Ad
source: stackoverflow.com
Ad