src/Composer/Command/InitCommand.php
private function findBestVersionAndNameForPackage(InputInterface $input, $name, $phpVersion, $preferredStability = 'stable', $requiredVersion = null, $minimumStability = null)
    {
        // find the latest version allowed in this pool
        $versionSelector = new VersionSelector($this->getPool($input, $minimumStability));
        $ignorePlatformReqs = $input->hasOption('ignore-platform-reqs') && $input->getOption('ignore-platform-reqs');

        // ignore phpVersion if platform requirements are ignored
        if ($ignorePlatformReqs) {
            $phpVersion = null;
        }

        $package = $versionSelector->findBestCandidate($name, $requiredVersion, $phpVersion, $preferredStability);

        if (!$package) {
            // platform packages can not be found in the pool in versions other than the local platform's has
            // so if platform reqs are ignored we just take the user's word for it
            if ($ignorePlatformReqs && preg_match(PlatformRepository::PLATFORM_PACKAGE_REGEX, $name)) {
                return array($name, $requiredVersion ?: '*');
            }

            // Check whether the PHP version was the problem
            if ($phpVersion && $versionSelector->findBestCandidate($name, $requiredVersion, null, $preferredStability)) {
                throw new \InvalidArgumentException(sprintf(
                    'Package %s at version %s has a PHP requirement incompatible with your PHP version (%s)',
                    $name,
                    $requiredVersion,
                    $phpVersion
                ));
            }
            // Check whether the required version was the problem
            if ($requiredVersion && $versionSelector->findBestCandidate($name, null, $phpVersion, $preferredStability)) {
                throw new \InvalidArgumentException(sprintf(
                    'Could not find package %s in a version matching %s',
                    $name,
                    $requiredVersion
                ));
            }
            // Check whether the PHP version was the problem
            if ($phpVersion && $versionSelector->findBestCandidate($name)) {
                throw new \InvalidArgumentException(sprintf(
                    'Could not find package %s in any version matching your PHP version (%s)',
                    $name,
                    $phpVersion
                ));
            }

            // Check for similar names/typos
            $similar = $this->findSimilar($name);
            if ($similar) {
                // Check whether the minimum stability was the problem but the package exists
                if ($requiredVersion === null && in_array($name, $similar, true)) {
                    throw new \InvalidArgumentException(sprintf(
                        'Could not find a version of package %s matching your minimum-stability (%s). Require it with an explicit version constraint allowing its desired stability.',
                        $name,
                        $this->getMinimumStability($input)
                    ));
                }

                throw new \InvalidArgumentException(sprintf(
                    "Could not find package %s.\n\nDid you mean " . (count($similar) > 1 ? 'one of these' : 'this') . "?\n    %s",
                    $name,
                    implode("\n    ", $similar)
                ));
            }

            throw new \InvalidArgumentException(sprintf(
                'Could not find a matching version of package %s. Check the package spelling, your version constraint and that the package is available in a stability which matches your minimum-stability (%s).',
                $name,
                $this->getMinimumStability($input)
            ));
        }

        return array(
            $package->getPrettyName(),
            $versionSelector->findRecommendedRequireVersion($package),
        );
    }
private function findBestVersionAndNameForPackage(InputInterface $input, $name, $phpVersion, $preferredStability = 'stable', $requiredVersion = null, $minimumStability = null)
    {
        // find the latest version allowed in this pool
        $versionSelector = new VersionSelector($this->getPool($input, $minimumStability));
        $ignorePlatformReqs = $input->hasOption('ignore-platform-reqs') && $input->getOption('ignore-platform-reqs');

        // ignore phpVersion if platform requirements are ignored
        if ($ignorePlatformReqs) {
            $phpVersion = null;
        }

        $package = $versionSelector->findBestCandidate($name, $requiredVersion, $phpVersion, $preferredStability);

        if (!$package) {
            // platform packages can not be found in the pool in versions other than the local platform's has
            // so if platform reqs are ignored we just take the user's word for it
            if ($ignorePlatformReqs && preg_match(PlatformRepository::PLATFORM_PACKAGE_REGEX, $name)) {
                return array($name, $requiredVersion ?: '*');
            }

            // Check whether the PHP version was the problem
            if ($phpVersion && $versionSelector->findBestCandidate($name, $requiredVersion, null, $preferredStability)) {
                throw new \InvalidArgumentException(sprintf(
                    'Package %s at version %s has a PHP requirement incompatible with your PHP version (%s)',
                    $name,
                    $requiredVersion,
                    $phpVersion
                ));
            }
            // Check whether the required version was the problem
            if ($requiredVersion && $versionSelector->findBestCandidate($name, null, $phpVersion, $preferredStability)) {
                throw new \InvalidArgumentException(sprintf(
                    'Could not find package %s in a version matching %s',
                    $name,
                    $requiredVersion
                ));
            }
            // Check whether the PHP version was the problem
            if ($phpVersion && $versionSelector->findBestCandidate($name)) {
                throw new \InvalidArgumentException(sprintf(
                    'Could not find package %s in any version matching your PHP version (%s)',
                    $name,
                    $phpVersion
                ));
            }

            // Check for similar names/typos
            $similar = $this->findSimilar($name);
            if ($similar) {
                // Check whether the minimum stability was the problem but the package exists
                if ($requiredVersion === null && in_array($name, $similar, true)) {
                    throw new \InvalidArgumentException(sprintf(
                        'Could not find a version of package %s matching your minimum-stability (%s). Require it with an explicit version constraint allowing its desired stability.',
                        $name,
                        $this->getMinimumStability($input)
                    ));
                }

                throw new \InvalidArgumentException(sprintf(
                    "Could not find package %s.\n\nDid you mean " . (count($similar) > 1 ? 'one of these' : 'this') . "?\n    %s",
                    $name,
                    implode("\n    ", $similar)
                ));
            }

            throw new \InvalidArgumentException(sprintf(
                'Could not find a matching version of package %s. Check the package spelling, your version constraint and that the package is available in a stability which matches your minimum-stability (%s).',
                $name,
                $this->getMinimumStability($input)
            ));
        }

        return array(
            $package->getPrettyName(),
            $versionSelector->findRecommendedRequireVersion($package),
        );
    }
Given a package name, this determines the best version to use in the require key.

This returns a version with the ~ operator prefixed when possible.

@param  InputInterface            $input
@param  string                    $name
@param  string|null               $phpVersion
@param  string                    $preferredStability
@param  string|null               $requiredVersion
@param  string                    $minimumStability
@throws \InvalidArgumentException
@return array                     name version