diff --git a/.travis.yml b/.travis.yml index 215bc3f..8c37c6c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -40,7 +40,7 @@ before_script: # aliasing current branch as `master`, since otherwise composer cannot determine the current branch-alias - git branch -D master || true - git checkout -b master - - composer self-update + - if [[ $DEPENDENCIES = '--no-scripts' ]]; then composer self-update --2; else composer self-update; fi - if [[ $LOCKED_DEPENDENCIES = '1' ]]; then composer install; fi - if [[ $LOCKED_DEPENDENCIES = '' ]]; then composer update $DEPENDENCIES; fi diff --git a/composer.json b/composer.json index 82a535e..d5a40da 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,7 @@ "composer-plugin-api": "^1.1.0 || ^2.0" }, "replace": { - "ocramius/package-versions": "1.10.99" + "ocramius/package-versions": "1.11.99" }, "require-dev": { "phpunit/phpunit": "^6.5 || ^7", diff --git a/src/PackageVersions/Installer.php b/src/PackageVersions/Installer.php index a0a2616..b87a7b1 100644 --- a/src/PackageVersions/Installer.php +++ b/src/PackageVersions/Installer.php @@ -43,6 +43,8 @@ final class Installer implements PluginInterface, EventSubscriberInterface use Composer\InstalledVersions; use OutOfBoundsException; +class_exists(InstalledVersions::class); + /** * This class is generated by composer/package-versions-deprecated, specifically by * @see \PackageVersions\Installer @@ -54,9 +56,7 @@ final class Installer implements PluginInterface, EventSubscriberInterface %s { /** - * @deprecated please use {@see \Composer\InstalledVersions::getRootPackage()} instead. The - * equivalent expression for this constant's contents is - * `\Composer\InstalledVersions::getRootPackage()['name']`. + * @deprecated please use {@see self::rootPackageName()} instead. * This constant will be removed in version 2.0.0. */ const ROOT_PACKAGE_NAME = '%s'; @@ -72,7 +72,21 @@ final class Installer implements PluginInterface, EventSubscriberInterface private function __construct() { - class_exists(InstalledVersions::class); + } + + /** + * @psalm-pure + * + * @psalm-suppress ImpureMethodCall we know that {@see InstalledVersions} interaction does not + * cause any side effects here. + */ + public static function rootPackageName() : string + { + if (!class_exists(InstalledVersions::class, false) || !InstalledVersions::getRawData()) { + return self::ROOT_PACKAGE_NAME; + } + + return InstalledVersions::getRootPackage()['name']; } /** @@ -86,7 +100,7 @@ class_exists(InstalledVersions::class); */ public static function getVersion(string $packageName): string { - if (class_exists(InstalledVersions::class, false)) { + if (class_exists(InstalledVersions::class, false) && InstalledVersions::getRawData()) { return InstalledVersions::getPrettyVersion($packageName) . '@' . InstalledVersions::getReference($packageName); } diff --git a/src/PackageVersions/Versions.php b/src/PackageVersions/Versions.php index 2446cc1..e1cca5d 100644 --- a/src/PackageVersions/Versions.php +++ b/src/PackageVersions/Versions.php @@ -8,6 +8,8 @@ use OutOfBoundsException; use UnexpectedValueException; +class_exists(InstalledVersions::class); + /** * This is a stub class: it is in place only for scenarios where PackageVersions * is installed with a `--no-scripts` flag, in which scenarios the Versions class @@ -19,17 +21,31 @@ final class Versions { /** - * @deprecated please use {@see \Composer\InstalledVersions::getRootPackage()} instead. The - * equivalent expression for this constant's contents is - * `\Composer\InstalledVersions::getRootPackage()['name']`. + * @deprecated please use {@see self::rootPackageName()} instead. * This constant will be removed in version 2.0.0. */ - const ROOT_PACKAGE_NAME = FallbackVersions::ROOT_PACKAGE_NAME; + const ROOT_PACKAGE_NAME = 'unknown/root-package@UNKNOWN'; + + /** @internal */ const VERSIONS = []; private function __construct() { - class_exists(InstalledVersions::class); + } + + /** + * @psalm-pure + * + * @psalm-suppress ImpureMethodCall we know that {@see InstalledVersions} interaction does not + * cause any side effects here. + */ + public static function rootPackageName() : string + { + if (!class_exists(InstalledVersions::class, false) || !InstalledVersions::getRawData()) { + return self::ROOT_PACKAGE_NAME; + } + + return InstalledVersions::getRootPackage()['name']; } /** @@ -38,10 +54,17 @@ class_exists(InstalledVersions::class); */ public static function getVersion(string $packageName): string { - if (!class_exists(InstalledVersions::class, false)) { + if (!class_exists(InstalledVersions::class, false) || !InstalledVersions::getRawData()) { return FallbackVersions::getVersion($packageName); } + /** @psalm-suppress DeprecatedConstant */ + if ($packageName === self::ROOT_PACKAGE_NAME) { + $rootPackage = InstalledVersions::getRootPackage(); + + return $rootPackage['pretty_version'] . '@' . $rootPackage['reference']; + } + return InstalledVersions::getPrettyVersion($packageName) . '@' . InstalledVersions::getReference($packageName); } diff --git a/test/PackageVersionsTest/InstallerTest.php b/test/PackageVersionsTest/InstallerTest.php index a9312b9..cd06171 100644 --- a/test/PackageVersionsTest/InstallerTest.php +++ b/test/PackageVersionsTest/InstallerTest.php @@ -269,6 +269,8 @@ public function testDumpVersionsClass() use Composer\InstalledVersions; use OutOfBoundsException; +class_exists(InstalledVersions::class); + /** * This class is generated by composer/package-versions-deprecated, specifically by * @see \PackageVersions\Installer @@ -280,9 +282,7 @@ public function testDumpVersionsClass() final class Versions { /** - * @deprecated please use {@see \Composer\InstalledVersions::getRootPackage()} instead. The - * equivalent expression for this constant's contents is - * `\Composer\InstalledVersions::getRootPackage()['name']`. + * @deprecated please use {@see self::rootPackageName()} instead. * This constant will be removed in version 2.0.0. */ const ROOT_PACKAGE_NAME = 'root/package'; @@ -305,7 +305,21 @@ final class Versions private function __construct() { - class_exists(InstalledVersions::class); + } + + /** + * @psalm-pure + * + * @psalm-suppress ImpureMethodCall we know that {@see InstalledVersions} interaction does not + * cause any side effects here. + */ + public static function rootPackageName() : string + { + if (!class_exists(InstalledVersions::class, false) || !InstalledVersions::getRawData()) { + return self::ROOT_PACKAGE_NAME; + } + + return InstalledVersions::getRootPackage()['name']; } /** @@ -319,7 +333,7 @@ class_exists(InstalledVersions::class); */ public static function getVersion(string $packageName): string { - if (class_exists(InstalledVersions::class, false)) { + if (class_exists(InstalledVersions::class, false) && InstalledVersions::getRawData()) { return InstalledVersions::getPrettyVersion($packageName) . '@' . InstalledVersions::getReference($packageName); } @@ -404,6 +418,8 @@ public function testDumpVersionsClassNoDev() use Composer\InstalledVersions; use OutOfBoundsException; +class_exists(InstalledVersions::class); + /** * This class is generated by composer/package-versions-deprecated, specifically by * @see \PackageVersions\Installer @@ -415,9 +431,7 @@ public function testDumpVersionsClassNoDev() final class Versions { /** - * @deprecated please use {@see \Composer\InstalledVersions::getRootPackage()} instead. The - * equivalent expression for this constant's contents is - * `\Composer\InstalledVersions::getRootPackage()['name']`. + * @deprecated please use {@see self::rootPackageName()} instead. * This constant will be removed in version 2.0.0. */ const ROOT_PACKAGE_NAME = 'root/package'; @@ -439,7 +453,21 @@ final class Versions private function __construct() { - class_exists(InstalledVersions::class); + } + + /** + * @psalm-pure + * + * @psalm-suppress ImpureMethodCall we know that {@see InstalledVersions} interaction does not + * cause any side effects here. + */ + public static function rootPackageName() : string + { + if (!class_exists(InstalledVersions::class, false) || !InstalledVersions::getRawData()) { + return self::ROOT_PACKAGE_NAME; + } + + return InstalledVersions::getRootPackage()['name']; } /** @@ -453,7 +481,7 @@ class_exists(InstalledVersions::class); */ public static function getVersion(string $packageName): string { - if (class_exists(InstalledVersions::class, false)) { + if (class_exists(InstalledVersions::class, false) && InstalledVersions::getRawData()) { return InstalledVersions::getPrettyVersion($packageName) . '@' . InstalledVersions::getReference($packageName); } @@ -542,6 +570,8 @@ public function testDumpVersionsWithoutPackageSourceDetails() use Composer\InstalledVersions; use OutOfBoundsException; +class_exists(InstalledVersions::class); + /** * This class is generated by composer/package-versions-deprecated, specifically by * @see \PackageVersions\Installer @@ -553,9 +583,7 @@ public function testDumpVersionsWithoutPackageSourceDetails() final class Versions { /** - * @deprecated please use {@see \Composer\InstalledVersions::getRootPackage()} instead. The - * equivalent expression for this constant's contents is - * `\Composer\InstalledVersions::getRootPackage()['name']`. + * @deprecated please use {@see self::rootPackageName()} instead. * This constant will be removed in version 2.0.0. */ const ROOT_PACKAGE_NAME = 'root/package'; @@ -577,7 +605,21 @@ final class Versions private function __construct() { - class_exists(InstalledVersions::class); + } + + /** + * @psalm-pure + * + * @psalm-suppress ImpureMethodCall we know that {@see InstalledVersions} interaction does not + * cause any side effects here. + */ + public static function rootPackageName() : string + { + if (!class_exists(InstalledVersions::class, false) || !InstalledVersions::getRawData()) { + return self::ROOT_PACKAGE_NAME; + } + + return InstalledVersions::getRootPackage()['name']; } /** @@ -591,7 +633,7 @@ class_exists(InstalledVersions::class); */ public static function getVersion(string $packageName): string { - if (class_exists(InstalledVersions::class, false)) { + if (class_exists(InstalledVersions::class, false) && InstalledVersions::getRawData()) { return InstalledVersions::getPrettyVersion($packageName) . '@' . InstalledVersions::getReference($packageName); } @@ -973,6 +1015,8 @@ public function testGetVersionsIsNotNormalizedForRootPackage() use Composer\InstalledVersions; use OutOfBoundsException; +class_exists(InstalledVersions::class); + /** * This class is generated by composer/package-versions-deprecated, specifically by * @see \PackageVersions\Installer @@ -984,9 +1028,7 @@ public function testGetVersionsIsNotNormalizedForRootPackage() final class Versions { /** - * @deprecated please use {@see \Composer\InstalledVersions::getRootPackage()} instead. The - * equivalent expression for this constant's contents is - * `\Composer\InstalledVersions::getRootPackage()['name']`. + * @deprecated please use {@see self::rootPackageName()} instead. * This constant will be removed in version 2.0.0. */ const ROOT_PACKAGE_NAME = 'root/package'; @@ -1006,7 +1048,21 @@ final class Versions private function __construct() { - class_exists(InstalledVersions::class); + } + + /** + * @psalm-pure + * + * @psalm-suppress ImpureMethodCall we know that {@see InstalledVersions} interaction does not + * cause any side effects here. + */ + public static function rootPackageName() : string + { + if (!class_exists(InstalledVersions::class, false) || !InstalledVersions::getRawData()) { + return self::ROOT_PACKAGE_NAME; + } + + return InstalledVersions::getRootPackage()['name']; } /** @@ -1020,7 +1076,7 @@ class_exists(InstalledVersions::class); */ public static function getVersion(string $packageName): string { - if (class_exists(InstalledVersions::class, false)) { + if (class_exists(InstalledVersions::class, false) && InstalledVersions::getRawData()) { return InstalledVersions::getPrettyVersion($packageName) . '@' . InstalledVersions::getReference($packageName); } diff --git a/test/PackageVersionsTest/VersionsTest.php b/test/PackageVersionsTest/VersionsTest.php index f059bdc..3f63dc4 100644 --- a/test/PackageVersionsTest/VersionsTest.php +++ b/test/PackageVersionsTest/VersionsTest.php @@ -13,11 +13,7 @@ use function json_decode; use function uniqid; -/** - * @uses \PackageVersions\FallbackVersions - * - * @covers \PackageVersions\Versions - */ +/** @covers \PackageVersions\Versions */ final class VersionsTest extends TestCase { public function testValidVersions() @@ -36,6 +32,19 @@ public function testValidVersions() } } + /** @group #148 */ + public function testCanRetrieveRootPackageVersion() + { + /** @psalm-suppress DeprecatedConstant */ + self::assertRegExp('/^.+\@[0-9a-f]+$/', Versions::getVersion(Versions::ROOT_PACKAGE_NAME)); + } + + /** @group #153 */ + public function testCanRetrieveRootPackageName() + { + self::assertRegExp('/^[a-z0-9\\-]+\\/[a-z0-9\\-]+$/', Versions::rootPackageName()); + } + public function testInvalidVersionsAreRejected() { $this->expectException(OutOfBoundsException::class);