Skip to content

Commit

Permalink
Discover phpunit-bridge automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Dec 13, 2022
1 parent 11a1039 commit c760b1f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/Internal/ComposerHelper.php
Expand Up @@ -56,6 +56,16 @@ public static function getVendorDirFromComposerConfig(string $root, array $compo
return $root . '/' . trim($vendorDirectory, '/');
}

/**
* @param array<string, mixed> $composerConfig
*/
public static function getBinDirFromComposerConfig(string $root, array $composerConfig): string
{
$vendorDirectory = $composerConfig['config']['bin-dir'] ?? 'vendor/bin';

return $root . '/' . trim($vendorDirectory, '/');
}

public static function getPhpStanVersion(): string
{
if (self::$phpstanVersion !== null) {
Expand Down
Expand Up @@ -11,15 +11,20 @@
use PHPStan\File\CouldNotReadFileException;
use PHPStan\File\FileReader;
use PHPStan\Internal\ComposerHelper;
use PHPStan\Php\PhpVersion;
use function array_filter;
use function array_key_exists;
use function array_map;
use function array_merge;
use function array_merge_recursive;
use function array_reverse;
use function count;
use function dirname;
use function glob;
use function is_dir;
use function is_file;
use function strpos;
use const GLOB_ONLYDIR;

class ComposerJsonAndInstalledJsonSourceLocatorMaker
{
Expand All @@ -28,6 +33,7 @@ public function __construct(
private OptimizedDirectorySourceLocatorRepository $optimizedDirectorySourceLocatorRepository,
private OptimizedPsrAutoloaderLocatorFactory $optimizedPsrAutoloaderLocatorFactory,
private OptimizedDirectorySourceLocatorFactory $optimizedDirectorySourceLocatorFactory,
private PhpVersion $phpVersion,
)
{
}
Expand Down Expand Up @@ -125,6 +131,39 @@ public function create(string $projectInstallationPath): ?SourceLocator
$locators[] = $this->optimizedDirectorySourceLocatorFactory->createByFiles($files);
}

$binDir = ComposerHelper::getBinDirFromComposerConfig($projectInstallationPath, $composer);
$phpunitBridgeDir = $binDir . '/.phpunit';
if (!is_dir($vendorDirectory . '/phpunit/phpunit') && is_dir($phpunitBridgeDir)) {
// from https://github.com/composer/composer/blob/8ff237afb61b8766efa576b8ae1cc8560c8aed96/phpstan/locate-phpunit-autoloader.php
$bestDirFound = null;
$phpunitBridgeDirectories = glob($phpunitBridgeDir . '/phpunit-*', GLOB_ONLYDIR);
if ($phpunitBridgeDirectories !== false) {
foreach (array_reverse($phpunitBridgeDirectories) as $dir) {
$bestDirFound = $dir;
if ($this->phpVersion->getVersionId() >= 80100 && strpos($dir, 'phpunit-10') !== false) {
break;
}
if ($this->phpVersion->getVersionId() >= 80000) {
if (strpos($dir, 'phpunit-9') !== false) {
break;
}
continue;
}

if (strpos($dir, 'phpunit-8') !== false || strpos($dir, 'phpunit-7') !== false) {
break;
}
}

if ($bestDirFound !== null) {
$phpunitBridgeLocator = $this->create($bestDirFound);
if ($phpunitBridgeLocator !== null) {
$locators[] = $phpunitBridgeLocator;
}
}
}
}

return new AggregateSourceLocator($locators);
}

Expand Down

0 comments on commit c760b1f

Please sign in to comment.