Skip to content

Commit

Permalink
Fix more deprecations warnings and fix edge case when Composer 2 is i…
Browse files Browse the repository at this point in the history
…n vendor but project was installed with Composer 1, fixes #27
  • Loading branch information
Seldaek committed Aug 17, 2021
1 parent c6522af commit fff576a
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 11 deletions.
25 changes: 23 additions & 2 deletions src/PackageVersions/Installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ private function __construct()
*/
public static function rootPackageName() : string
{
if (!class_exists(InstalledVersions::class, false) || !(method_exists(InstalledVersions::class, 'getAllRawData') ? InstalledVersions::getAllRawData() : InstalledVersions::getRawData())) {
if (!self::composer2ApiUsable()) {
return self::ROOT_PACKAGE_NAME;
}
Expand All @@ -100,7 +100,7 @@ public static function rootPackageName() : string
*/
public static function getVersion(string $packageName): string
{
if (class_exists(InstalledVersions::class, false) && (method_exists(InstalledVersions::class, 'getAllRawData') ? InstalledVersions::getAllRawData() : InstalledVersions::getRawData())) {
if (self::composer2ApiUsable()) {
return InstalledVersions::getPrettyVersion($packageName)
. '@' . InstalledVersions::getReference($packageName);
}
Expand All @@ -113,6 +113,27 @@ public static function getVersion(string $packageName): string
'Required package "' . $packageName . '" is not installed: check your ./vendor/composer/installed.json and/or ./composer.lock files'
);
}
private static function composer2ApiUsable(): bool
{
if (!class_exists(InstalledVersions::class, false)) {
return false;
}
if (method_exists(InstalledVersions::class, 'getAllRawData')) {
$rawData = InstalledVersions::getAllRawData();
if (count($rawData) === 1 && count($rawData[0]) === 0) {
return false;
}
} else {
$rawData = InstalledVersions::getRawData();
if ($rawData === []) {
return false;
}
}
return true;
}
}

PHP;
Expand Down
23 changes: 22 additions & 1 deletion src/PackageVersions/Versions.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public static function rootPackageName() : string
*/
public static function getVersion(string $packageName): string
{
if (!class_exists(InstalledVersions::class, false) || !InstalledVersions::getRawData()) {
if (!self::composer2ApiUsable()) {
return FallbackVersions::getVersion($packageName);
}

Expand All @@ -68,4 +68,25 @@ public static function getVersion(string $packageName): string
return InstalledVersions::getPrettyVersion($packageName)
. '@' . InstalledVersions::getReference($packageName);
}

private static function composer2ApiUsable(): bool
{
if (!class_exists(InstalledVersions::class, false)) {
return false;
}

if (method_exists(InstalledVersions::class, 'getAllRawData')) {
$rawData = InstalledVersions::getAllRawData();
if (count($rawData) === 1 && count($rawData[0]) === 0) {
return false;
}
} else {
$rawData = InstalledVersions::getRawData();
if ($rawData === []) {
return false;
}
}

return true;
}
}
100 changes: 92 additions & 8 deletions test/PackageVersionsTest/InstallerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ private function __construct()
*/
public static function rootPackageName() : string
{
if (!class_exists(InstalledVersions::class, false) || !(method_exists(InstalledVersions::class, 'getAllRawData') ? InstalledVersions::getAllRawData() : InstalledVersions::getRawData())) {
if (!self::composer2ApiUsable()) {
return self::ROOT_PACKAGE_NAME;
}
Expand All @@ -333,7 +333,7 @@ public static function rootPackageName() : string
*/
public static function getVersion(string $packageName): string
{
if (class_exists(InstalledVersions::class, false) && (method_exists(InstalledVersions::class, 'getAllRawData') ? InstalledVersions::getAllRawData() : InstalledVersions::getRawData())) {
if (self::composer2ApiUsable()) {
return InstalledVersions::getPrettyVersion($packageName)
. '@' . InstalledVersions::getReference($packageName);
}
Expand All @@ -346,6 +346,27 @@ public static function getVersion(string $packageName): string
'Required package "' . $packageName . '" is not installed: check your ./vendor/composer/installed.json and/or ./composer.lock files'
);
}
private static function composer2ApiUsable(): bool
{
if (!class_exists(InstalledVersions::class, false)) {
return false;
}
if (method_exists(InstalledVersions::class, 'getAllRawData')) {
$rawData = InstalledVersions::getAllRawData();
if (count($rawData) === 1 && count($rawData[0]) === 0) {
return false;
}
} else {
$rawData = InstalledVersions::getRawData();
if ($rawData === []) {
return false;
}
}
return true;
}
}

PHP;
Expand Down Expand Up @@ -463,7 +484,7 @@ private function __construct()
*/
public static function rootPackageName() : string
{
if (!class_exists(InstalledVersions::class, false) || !(method_exists(InstalledVersions::class, 'getAllRawData') ? InstalledVersions::getAllRawData() : InstalledVersions::getRawData())) {
if (!self::composer2ApiUsable()) {
return self::ROOT_PACKAGE_NAME;
}
Expand All @@ -481,7 +502,7 @@ public static function rootPackageName() : string
*/
public static function getVersion(string $packageName): string
{
if (class_exists(InstalledVersions::class, false) && (method_exists(InstalledVersions::class, 'getAllRawData') ? InstalledVersions::getAllRawData() : InstalledVersions::getRawData())) {
if (self::composer2ApiUsable()) {
return InstalledVersions::getPrettyVersion($packageName)
. '@' . InstalledVersions::getReference($packageName);
}
Expand All @@ -494,6 +515,27 @@ public static function getVersion(string $packageName): string
'Required package "' . $packageName . '" is not installed: check your ./vendor/composer/installed.json and/or ./composer.lock files'
);
}
private static function composer2ApiUsable(): bool
{
if (!class_exists(InstalledVersions::class, false)) {
return false;
}
if (method_exists(InstalledVersions::class, 'getAllRawData')) {
$rawData = InstalledVersions::getAllRawData();
if (count($rawData) === 1 && count($rawData[0]) === 0) {
return false;
}
} else {
$rawData = InstalledVersions::getRawData();
if ($rawData === []) {
return false;
}
}
return true;
}
}

PHP;
Expand Down Expand Up @@ -615,7 +657,7 @@ private function __construct()
*/
public static function rootPackageName() : string
{
if (!class_exists(InstalledVersions::class, false) || !(method_exists(InstalledVersions::class, 'getAllRawData') ? InstalledVersions::getAllRawData() : InstalledVersions::getRawData())) {
if (!self::composer2ApiUsable()) {
return self::ROOT_PACKAGE_NAME;
}
Expand All @@ -633,7 +675,7 @@ public static function rootPackageName() : string
*/
public static function getVersion(string $packageName): string
{
if (class_exists(InstalledVersions::class, false) && (method_exists(InstalledVersions::class, 'getAllRawData') ? InstalledVersions::getAllRawData() : InstalledVersions::getRawData())) {
if (self::composer2ApiUsable()) {
return InstalledVersions::getPrettyVersion($packageName)
. '@' . InstalledVersions::getReference($packageName);
}
Expand All @@ -646,6 +688,27 @@ public static function getVersion(string $packageName): string
'Required package "' . $packageName . '" is not installed: check your ./vendor/composer/installed.json and/or ./composer.lock files'
);
}
private static function composer2ApiUsable(): bool
{
if (!class_exists(InstalledVersions::class, false)) {
return false;
}
if (method_exists(InstalledVersions::class, 'getAllRawData')) {
$rawData = InstalledVersions::getAllRawData();
if (count($rawData) === 1 && count($rawData[0]) === 0) {
return false;
}
} else {
$rawData = InstalledVersions::getRawData();
if ($rawData === []) {
return false;
}
}
return true;
}
}

PHP;
Expand Down Expand Up @@ -1058,7 +1121,7 @@ private function __construct()
*/
public static function rootPackageName() : string
{
if (!class_exists(InstalledVersions::class, false) || !(method_exists(InstalledVersions::class, 'getAllRawData') ? InstalledVersions::getAllRawData() : InstalledVersions::getRawData())) {
if (!self::composer2ApiUsable()) {
return self::ROOT_PACKAGE_NAME;
}
Expand All @@ -1076,7 +1139,7 @@ public static function rootPackageName() : string
*/
public static function getVersion(string $packageName): string
{
if (class_exists(InstalledVersions::class, false) && (method_exists(InstalledVersions::class, 'getAllRawData') ? InstalledVersions::getAllRawData() : InstalledVersions::getRawData())) {
if (self::composer2ApiUsable()) {
return InstalledVersions::getPrettyVersion($packageName)
. '@' . InstalledVersions::getReference($packageName);
}
Expand All @@ -1089,6 +1152,27 @@ public static function getVersion(string $packageName): string
'Required package "' . $packageName . '" is not installed: check your ./vendor/composer/installed.json and/or ./composer.lock files'
);
}
private static function composer2ApiUsable(): bool
{
if (!class_exists(InstalledVersions::class, false)) {
return false;
}
if (method_exists(InstalledVersions::class, 'getAllRawData')) {
$rawData = InstalledVersions::getAllRawData();
if (count($rawData) === 1 && count($rawData[0]) === 0) {
return false;
}
} else {
$rawData = InstalledVersions::getRawData();
if ($rawData === []) {
return false;
}
}
return true;
}
}

PHP;
Expand Down

0 comments on commit fff576a

Please sign in to comment.