Skip to content

Commit

Permalink
Fallback to unknown version if not running under Composer API 2
Browse files Browse the repository at this point in the history
  • Loading branch information
sanmai committed Dec 6, 2022
1 parent 966a7b2 commit 4ea0da6
Showing 1 changed file with 25 additions and 14 deletions.
39 changes: 25 additions & 14 deletions src/Console/Application.php
Expand Up @@ -36,6 +36,7 @@
namespace Infection\Console;

use function array_merge;
use function class_exists;
use Composer\InstalledVersions;
use Infection\Command\ConfigureCommand;
use Infection\Command\DescribeCommand;
Expand Down Expand Up @@ -74,20 +75,7 @@ final class Application extends BaseApplication

public function __construct(Container $container)
{
try {
$version = (string) InstalledVersions::getPrettyVersion(self::PACKAGE_NAME);
// @codeCoverageIgnoreStart
} catch (OutOfBoundsException $e) {
if (preg_match('#package .*' . preg_quote(self::PACKAGE_NAME, '#') . '.* not installed#i', $e->getMessage()) === 0) {
throw $e;
}

// We have a bogus exception: how can Infection be not installed if we're here?
$version = 'not-installed';
}
// @codeCoverageIgnoreEnd

parent::__construct(self::NAME, $version);
parent::__construct(self::NAME, self::getPrettyVersion());

$this->container = $container;
$this->setDefaultCommand('run');
Expand Down Expand Up @@ -136,4 +124,27 @@ protected function configureIO(InputInterface $input, OutputInterface $output):

OutputFormatterStyleConfigurator::configure($output);
}

private static function getPrettyVersion(): string
{
// Pre 2.0 Composer runtime didn't have this class.
// @codeCoverageIgnoreStart
if (!class_exists(InstalledVersions::class)) {
return 'unknown';
}
// @codeCoverageIgnoreEnd

try {
return (string) InstalledVersions::getPrettyVersion(self::PACKAGE_NAME);
// @codeCoverageIgnoreStart
} catch (OutOfBoundsException $e) {
if (preg_match('#package .*' . preg_quote(self::PACKAGE_NAME, '#') . '.* not installed#i', $e->getMessage()) === 0) {
throw $e;
}

// We have a bogus exception: how can Infection be not installed if we're here?
return 'not-installed';
}
// @codeCoverageIgnoreEnd
}
}

0 comments on commit 4ea0da6

Please sign in to comment.