From 4ea0da65c9b2f7197eb8361f7bd9dae707e28629 Mon Sep 17 00:00:00 2001 From: Alexey Kopytko Date: Tue, 6 Dec 2022 18:06:22 +0900 Subject: [PATCH] Fallback to unknown version if not running under Composer API 2 Fixes #1770 --- src/Console/Application.php | 39 ++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/src/Console/Application.php b/src/Console/Application.php index 3f47f7194..a31bf4fda 100644 --- a/src/Console/Application.php +++ b/src/Console/Application.php @@ -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; @@ -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'); @@ -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 + } }