Skip to content

Commit

Permalink
Utility: Review Package
Browse files Browse the repository at this point in the history
- Fix issue where `Package::getPackagePath()` doesn't use
  `File::realpath()` to return the canonical path
- Make `Package::isInstalled()` public
- Fix issue where package versions like `dev-f0c162d8d462...@f0c162d`
  may be returned
  • Loading branch information
lkrms committed Jan 26, 2025
1 parent 8ee5ded commit 624a6d9
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 35 deletions.
54 changes: 28 additions & 26 deletions src/Toolkit/Utility/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,16 @@ public static function getPackagePath(string $package): ?string
return null;
}

return self::filterData(
$path = self::filterData(
Installed::getInstallPath($package),
Installed::class,
'getInstallPath',
$package,
);

return $path !== null
? File::realpath($path)
: null;
}

/**
Expand Down Expand Up @@ -189,7 +193,7 @@ public static function getNamespacePath(string $namespace): ?string
// Sort prefixes from longest to shortest
uksort(
$prefixes,
fn(string $p1, string $p2): int => strlen($p2) <=> strlen($p1)
fn($p1, $p2) => strlen($p2) <=> strlen($p1)
);

foreach ($prefixes as $prefix => $dirs) {
Expand All @@ -198,18 +202,15 @@ public static function getNamespacePath(string $namespace): ?string
}

foreach ((array) $dirs as $dir) {
if (!is_dir($dir)) {
// @codeCoverageIgnoreStart
continue;
// @codeCoverageIgnoreEnd
}
$dir = File::realpath($dir);
$subdir = strtr(substr($namespace, strlen($prefix)), '\\', '/');
$path = Arr::implode('/', [$dir, $subdir], '');
if (is_dir($path)) {
return $path;
if (is_dir($dir)) {
$dir = File::realpath($dir);
$subdir = strtr(substr($namespace, strlen($prefix)), '\\', '/');
$path = Arr::implode('/', [$dir, $subdir], '');
if (is_dir($path)) {
return $path;
}
$fallback ??= $path;
}
$fallback ??= $path;
}
}

Expand All @@ -229,16 +230,20 @@ private static function getRootPackageValue(string $key)

if (!array_key_exists($key, $values)) {
// @codeCoverageIgnoreStart
throw new ShouldNotHappenException(
sprintf('Value not found in root package: %s', $key)
);
throw new ShouldNotHappenException(sprintf(
'Value not found in root package: %s',
$key,
));
// @codeCoverageIgnoreEnd
}

return $values[$key];
}

private static function isInstalled(string $package): bool
/**
* Check if a package is installed
*/
public static function isInstalled(string $package): bool
{
return self::filterData(
Installed::isInstalled($package),
Expand Down Expand Up @@ -282,13 +287,11 @@ private static function getRegisteredLoaders(): array
*/
private static function filterData($data, string $class, string $method, ...$args)
{
if (!class_exists(Event::class) || !Event::isLoaded()) {
// @codeCoverageIgnoreStart
return $data;
// @codeCoverageIgnoreEnd
if (class_exists(Event::class) && Event::isLoaded()) {
$event = new PackageDataReceivedEvent($data, $class, $method, ...$args);
$data = Event::getInstance()->dispatch($event)->getData();
}
$event = new PackageDataReceivedEvent($data, $class, $method, ...$args);
return Event::getInstance()->dispatch($event)->getData();
return $data;
}

/**
Expand All @@ -299,14 +302,13 @@ private static function formatVersion(
?bool $withRef,
Closure $refCallback
): string {
if ($withRef !== false && Regex::match('/(?:^dev-|-dev$)/', $version)) {
if ($withRef !== false && Regex::match('/(?:^dev-|-dev$)/D', $version)) {
$ref = $refCallback();
if ($ref !== null) {
if ($ref !== null && !Str::startsWith($version, ['dev-' . $ref, $ref])) {
return $version . "@$ref";
}
return $version;
}

if ($withRef) {
$ref = $refCallback();
if ($ref !== null) {
Expand Down
5 changes: 0 additions & 5 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
<?php declare(strict_types=1);

use PHPStan\Testing\PHPStanTestCase;

PHPStanTestCase::getContainer();

require_once __DIR__ . '/fixtures/Toolkit/PHPStan/Core/Rules/TypesAssignedByHasMutatorRuleFailures.php';
require_once __DIR__ . '/fixtures/Toolkit/Utility/Debug/GetCallerFile1.php';
require_once __DIR__ . '/fixtures/Toolkit/Utility/Debug/GetCallerFile2.php';
39 changes: 35 additions & 4 deletions tests/unit/Toolkit/Utility/PackageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,17 @@ final class PackageTest extends TestCase
'dev' => false,
];

private const DEV_REF_PACKAGE = [
'name' => 'acme/sync',
'pretty_version' => 'dev-87effbba67b4cf767ff832f6f77bbca3f0dea67d',
'version' => 'dev-87effbba67b4cf767ff832f6f77bbca3f0dea67d',
'reference' => '87effbba67b4cf767ff832f6f77bbca3f0dea67d',
'type' => 'library',
'install_path' => __DIR__ . '/../../',
'aliases' => [],
'dev' => true,
];

/** @var array<string,mixed>|null */
private static ?array $RootPackage = null;
private static int $ListenerId;
Expand Down Expand Up @@ -184,6 +195,27 @@ public static function versionProvider(): array
false,
true,
],
[
'dev-87effbba67b4cf767ff832f6f77bbca3f0dea67d',
self::DEV_REF_PACKAGE,
],
[
'dev-87effbba67b4cf767ff832f6f77bbca3f0dea67d',
self::DEV_REF_PACKAGE,
false,
],
[
'dev-87effbba67b4cf767ff832f6f77bbca3f0dea67d',
self::DEV_REF_PACKAGE,
true,
true,
],
[
'dev-87effbba67b4cf767ff832f6f77bbca3f0dea67d',
self::DEV_REF_PACKAGE,
false,
true,
],
];
}

Expand Down Expand Up @@ -229,10 +261,9 @@ public function testGetPackageVersion(): void

public function testGetPackagePath(): void
{
// `InstalledVersions::getInstallPath()` may return a non-existent
// location in phpstan.phar, so don't perform any filesystem tests here
$this->assertNotNull($dir = InstalledVersions::getInstallPath('phpunit/phpunit'));
$this->assertSame($dir, Package::getPackagePath('phpunit/phpunit'));
$this->assertNotNull($dir1 = InstalledVersions::getInstallPath('phpunit/phpunit'));
$this->assertNotNull($dir2 = Package::getPackagePath('phpunit/phpunit'));
$this->assertTrue(File::same($dir1, $dir2));
$this->assertNull(Package::getPackagePath('composer/composer'));
}

Expand Down

0 comments on commit 624a6d9

Please sign in to comment.