diff --git a/.github/workflows/create-release.yml b/.github/workflows/create-release.yml index eef7915a..d6408be3 100644 --- a/.github/workflows/create-release.yml +++ b/.github/workflows/create-release.yml @@ -21,7 +21,7 @@ jobs: with: token: ${{ secrets.GITHUB_TOKEN }} config-file: .github/.release-please-config.json - manifest-file: src/version.json + manifest-file: resources/version.json target-branch: master ... diff --git a/src/version.json b/resources/version.json similarity index 100% rename from src/version.json rename to resources/version.json diff --git a/src/Info.php b/src/Info.php index ee97a046..2decc2ab 100644 --- a/src/Info.php +++ b/src/Info.php @@ -28,21 +28,31 @@ class Info public const TRAP_ROOT = __DIR__ . '/..'; private const VERSION = 'experimental'; + /** + * Returns the version of the Trap. + * + * @return non-empty-string + */ public static function version(): string { - $versionPath = self::TRAP_ROOT . '/src/version.json'; - $versionContents = file_get_contents($versionPath); + /** @var non-empty-string|null $cache */ + static $cache = null; - if ($versionContents === false) { - return self::VERSION; + if ($cache !== null) { + return $cache; } - $versionData = json_decode($versionContents, true); + $fileContent = \file_get_contents(self::TRAP_ROOT . '/resources/version.json'); - if (!is_array($versionData) || !isset($versionData['.']) || !is_string($versionData['.'])) { - return self::VERSION; + if ($fileContent === false) { + return $cache = self::VERSION; } - return $versionData['.']; + /** @var mixed $version */ + $version = \json_decode($fileContent, true)['.'] ?? null; + + return $cache = \is_string($version) && $version !== '' + ? $version + : self::VERSION; } } diff --git a/tests/Unit/InfoTest.php b/tests/Unit/InfoTest.php new file mode 100644 index 00000000..ce1678db --- /dev/null +++ b/tests/Unit/InfoTest.php @@ -0,0 +1,15 @@ +