diff --git a/src/PackageVersions/Installer.php b/src/PackageVersions/Installer.php index 4994efa..d590f64 100644 --- a/src/PackageVersions/Installer.php +++ b/src/PackageVersions/Installer.php @@ -50,9 +50,7 @@ final class Installer implements ComposerV2Plugin, 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. */ public const ROOT_PACKAGE_NAME = '%s'; @@ -61,6 +59,17 @@ private function __construct() { } + /** + * @psalm-pure + * + * @psalm-suppress ImpureMethodCall we know that {@see InstalledVersions} interaction does not + * cause any side effects here. + */ + public static function rootPackageName() : string + { + return InstalledVersions::getRootPackage()['name']; + } + /** * @throws OutOfBoundsException If a version cannot be located. * diff --git a/src/PackageVersions/Versions.php b/src/PackageVersions/Versions.php index ad1c16a..83033ac 100644 --- a/src/PackageVersions/Versions.php +++ b/src/PackageVersions/Versions.php @@ -18,9 +18,7 @@ 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. */ public const ROOT_PACKAGE_NAME = 'unknown/root-package@UNKNOWN'; @@ -29,6 +27,17 @@ private function __construct() { } + /** + * @psalm-pure + * + * @psalm-suppress ImpureMethodCall we know that {@see InstalledVersions} interaction does not + * cause any side effects here. + */ + public static function rootPackageName() : string + { + return InstalledVersions::getRootPackage()['name']; + } + /** * @throws OutOfBoundsException if a version cannot be located. * diff --git a/test/PackageVersionsTest/InstallerTest.php b/test/PackageVersionsTest/InstallerTest.php index 587f618..6ed1d86 100644 --- a/test/PackageVersionsTest/InstallerTest.php +++ b/test/PackageVersionsTest/InstallerTest.php @@ -278,9 +278,7 @@ public function testDumpVersionsClass(): void 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. */ public const ROOT_PACKAGE_NAME = 'root/package'; @@ -289,6 +287,17 @@ private function __construct() { } + /** + * @psalm-pure + * + * @psalm-suppress ImpureMethodCall we know that {@see InstalledVersions} interaction does not + * cause any side effects here. + */ + public static function rootPackageName() : string + { + return InstalledVersions::getRootPackage()['name']; + } + /** * @throws OutOfBoundsException If a version cannot be located. * @@ -383,9 +392,7 @@ public function testDumpVersionsClassNoDev(): void 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. */ public const ROOT_PACKAGE_NAME = 'root/package'; @@ -394,6 +401,17 @@ private function __construct() { } + /** + * @psalm-pure + * + * @psalm-suppress ImpureMethodCall we know that {@see InstalledVersions} interaction does not + * cause any side effects here. + */ + public static function rootPackageName() : string + { + return InstalledVersions::getRootPackage()['name']; + } + /** * @throws OutOfBoundsException If a version cannot be located. * @@ -492,9 +510,7 @@ public function testDumpVersionsWithoutPackageSourceDetails(): void 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. */ public const ROOT_PACKAGE_NAME = 'root/package'; @@ -503,6 +519,17 @@ private function __construct() { } + /** + * @psalm-pure + * + * @psalm-suppress ImpureMethodCall we know that {@see InstalledVersions} interaction does not + * cause any side effects here. + */ + public static function rootPackageName() : string + { + return InstalledVersions::getRootPackage()['name']; + } + /** * @throws OutOfBoundsException If a version cannot be located. * @@ -902,9 +929,7 @@ public function testGetVersionsIsNotNormalizedForRootPackage(): void 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. */ public const ROOT_PACKAGE_NAME = 'root/package'; @@ -913,6 +938,17 @@ private function __construct() { } + /** + * @psalm-pure + * + * @psalm-suppress ImpureMethodCall we know that {@see InstalledVersions} interaction does not + * cause any side effects here. + */ + public static function rootPackageName() : string + { + return InstalledVersions::getRootPackage()['name']; + } + /** * @throws OutOfBoundsException If a version cannot be located. * diff --git a/test/PackageVersionsTest/VersionsTest.php b/test/PackageVersionsTest/VersionsTest.php index 3b2f605..63d5ce7 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(): void @@ -43,6 +39,12 @@ public function testCanRetrieveRootPackageVersion(): void self::assertMatchesRegularExpression('/^.+\@[0-9a-f]+$/', Versions::getVersion(Versions::ROOT_PACKAGE_NAME)); } + /** @group #153 */ + public function testCanRetrieveRootPackageName(): void + { + self::assertMatchesRegularExpression('/^[a-z0-9\\-]+\\/[a-z0-9\\-]+$/', Versions::rootPackageName()); + } + public function testInvalidVersionsAreRejected(): void { $this->expectException(OutOfBoundsException::class);