From 5e2ab0657b157412b562fca5c58b1a012b1a0ca1 Mon Sep 17 00:00:00 2001 From: Denis Smet Date: Thu, 9 May 2024 12:10:43 +0300 Subject: [PATCH] Adjust `Utils::getVersion` to return null if version is not found (#193) This commit updates the Utils::getVersion method to return null when the version file is not found, instead of a string error message. --- csv-blueprint.php | 2 +- src/CliApplication.php | 3 ++- src/Commands/AbstractValidate.php | 5 ++++- src/Utils.php | 10 +++++----- 4 files changed, 12 insertions(+), 8 deletions(-) diff --git a/csv-blueprint.php b/csv-blueprint.php index 76b788ae..bbd02d9b 100644 --- a/csv-blueprint.php +++ b/csv-blueprint.php @@ -43,6 +43,6 @@ Utils::init(); -(new CliApplication('CSV Blueprint', Utils::getVersion(true))) +(new CliApplication('CSV Blueprint', (string)Utils::getVersion(true))) ->registerCommandsByPath(PATH_ROOT . '/src/Commands', __NAMESPACE__) ->run(); diff --git a/src/CliApplication.php b/src/CliApplication.php index 0fc1c670..8bb264af 100644 --- a/src/CliApplication.php +++ b/src/CliApplication.php @@ -33,7 +33,8 @@ final class CliApplication extends \JBZoo\Cli\CliApplication public function getLongVersion(): string { $logo = '' . \implode("\n", $this->appLogo) . ''; + $version = Utils::getVersion(true); - return "{$logo}\n" . Utils::getVersion(true); + return $version === null ? $logo : "{$logo}\n{$version}"; } } diff --git a/src/Commands/AbstractValidate.php b/src/Commands/AbstractValidate.php index c360e0dc..47156c6c 100644 --- a/src/Commands/AbstractValidate.php +++ b/src/Commands/AbstractValidate.php @@ -86,7 +86,10 @@ protected function preparation(bool $showIntro = true): void { if ($showIntro) { if ($this->isHumanReadableMode()) { - $this->_('CSV Blueprint: ' . Utils::getVersion(true)); + $version = Utils::getVersion(true); + if ($version !== null) { + $this->_("CSV Blueprint: {$version}"); + } } $threads = $this->getNumberOfThreads(); diff --git a/src/Utils.php b/src/Utils.php index 604bc7ed..06a2b75f 100644 --- a/src/Utils.php +++ b/src/Utils.php @@ -463,11 +463,11 @@ public static function printFile(string $fullpath, string $tag = 'bright-blue'): /** * Retrieves the version of the software. - * @param bool $showFull Whether to display the full version information or not. Default is false. - * @return string the version of the software as a string, or an error message if the version file is - * not found + * @param bool $showFull Whether to display the full version information or not. Default is false. + * @return null|string the version of the software as a string, or an error message if the version file is + * not found */ - public static function getVersion(bool $showFull): string + public static function getVersion(bool $showFull): ?string { if (self::isPhpUnit()) { return 'Unknown version (PhpUnit)'; @@ -475,7 +475,7 @@ public static function getVersion(bool $showFull): string $versionFile = __DIR__ . '/../.version'; if (!\file_exists($versionFile)) { - return 'Version file not found'; + return null; } return self::parseVersion((string)\file_get_contents($versionFile), $showFull);