Skip to content

Commit

Permalink
Merge pull request #154 from Ocramius/feature/#153-add-alternative-to…
Browse files Browse the repository at this point in the history
…-root-package-name-constant

Implement #153 - added `PackageVersions\Versions::rootPackageName()` as replacement for `PackageVersions\Versions::ROOT_PACKAGE_NAME`
  • Loading branch information
Ocramius authored Aug 21, 2020
2 parents e19f578 + d043c87 commit f51ff2b
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 23 deletions.
15 changes: 12 additions & 3 deletions src/PackageVersions/Installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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.
*
Expand Down
15 changes: 12 additions & 3 deletions src/PackageVersions/Versions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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.
*
Expand Down
60 changes: 48 additions & 12 deletions test/PackageVersionsTest/InstallerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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.
*
Expand Down Expand Up @@ -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';
Expand All @@ -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.
*
Expand Down Expand Up @@ -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';
Expand All @@ -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.
*
Expand Down Expand Up @@ -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';
Expand All @@ -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.
*
Expand Down
12 changes: 7 additions & 5 deletions test/PackageVersionsTest/VersionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down

0 comments on commit f51ff2b

Please sign in to comment.