1
0
Fork 0

Extract some common logic for filtering away dev requirements into a RepositoryUtils

pull/10908/head
Jordi Boggiano 2022-06-30 15:05:34 +02:00
parent de9996d4c4
commit efd426f8bb
No known key found for this signature in database
GPG Key ID: 7BBD42C429EC80BC
7 changed files with 171 additions and 100 deletions

View File

@ -4,11 +4,11 @@ namespace Composer\Command;
use Composer\Composer;
use Composer\Repository\RepositorySet;
use Composer\Repository\RepositoryUtils;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Composer\Package\PackageInterface;
use Composer\Repository\InstalledRepository;
use Composer\Repository\RepositoryInterface;
use Composer\Advisory\Auditor;
use Composer\Console\Input\InputOption;
@ -73,35 +73,9 @@ EOT
$installedRepo = new InstalledRepository(array($composer->getRepositoryManager()->getLocalRepository()));
if ($input->getOption('no-dev')) {
return $this->filterRequiredPackages($installedRepo, $rootPkg);
return RepositoryUtils::filterRequiredPackages($installedRepo->getPackages(), $rootPkg);
}
return $installedRepo->getPackages();
}
/**
* Find package requires and child requires.
* Effectively filters out dev dependencies.
*
* @param PackageInterface[] $bucket
* @return PackageInterface[]
*/
private function filterRequiredPackages(RepositoryInterface $repo, PackageInterface $package, array $bucket = array()): array
{
$requires = $package->getRequires();
foreach ($repo->getPackages() as $candidate) {
foreach ($candidate->getNames() as $name) {
if (isset($requires[$name])) {
if (!in_array($candidate, $bucket, true)) {
$bucket[] = $candidate;
$bucket = $this->filterRequiredPackages($repo, $candidate, $bucket);
}
break;
}
}
}
return $bucket;
}
}

View File

@ -19,7 +19,9 @@ use Composer\Plugin\CommandEvent;
use Composer\Plugin\PluginEvents;
use Composer\Package\PackageInterface;
use Composer\Repository\RepositoryInterface;
use Composer\Repository\RepositoryUtils;
use Composer\Util\PackageInfo;
use Composer\Util\PackageSorter;
use Symfony\Component\Console\Formatter\OutputFormatter;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputInterface;
@ -65,12 +67,12 @@ EOT
$repo = $composer->getRepositoryManager()->getLocalRepository();
if ($input->getOption('no-dev')) {
$packages = $this->filterRequiredPackages($repo, $root);
$packages = RepositoryUtils::filterRequiredPackages($repo->getPackages(), $root);
} else {
$packages = $this->appendPackages($repo->getPackages(), array());
$packages = $repo->getPackages();
}
ksort($packages);
$packages = PackageSorter::sortPackagesAlphabetically($packages);
$io = $this->getIO();
switch ($format = $input->getOption('format')) {
@ -153,47 +155,4 @@ EOT
return 0;
}
/**
* Find package requires and child requires
*
* @param array<string, PackageInterface> $bucket
* @return array<string, PackageInterface>
*/
private function filterRequiredPackages(RepositoryInterface $repo, PackageInterface $package, array $bucket = array()): array
{
$requires = array_keys($package->getRequires());
$packageListNames = array_keys($bucket);
$packages = array_filter(
$repo->getPackages(),
static function ($package) use ($requires, $packageListNames): bool {
return in_array($package->getName(), $requires) && !in_array($package->getName(), $packageListNames);
}
);
$bucket = $this->appendPackages($packages, $bucket);
foreach ($packages as $package) {
$bucket = $this->filterRequiredPackages($repo, $package, $bucket);
}
return $bucket;
}
/**
* Adds packages to the package list
*
* @param PackageInterface[] $packages the list of packages to add
* @param array<string, PackageInterface> $bucket the list to add packages to
* @return array<string, PackageInterface>
*/
public function appendPackages(array $packages, array $bucket): array
{
foreach ($packages as $package) {
$bucket[$package->getName()] = $package;
}
return $bucket;
}
}

View File

@ -36,6 +36,7 @@ use Composer\Repository\RepositoryFactory;
use Composer\Repository\InstalledRepository;
use Composer\Repository\RepositoryInterface;
use Composer\Repository\RepositorySet;
use Composer\Repository\RepositoryUtils;
use Composer\Repository\RootPackageRepository;
use Composer\Semver\Constraint\ConstraintInterface;
use Composer\Semver\Semver;
@ -248,7 +249,7 @@ EOT
$repos = $installedRepo = new InstalledRepository(array($composer->getRepositoryManager()->getLocalRepository()));
if ($input->getOption('no-dev')) {
$packages = $this->filterRequiredPackages($installedRepo, $rootPkg);
$packages = RepositoryUtils::filterRequiredPackages($installedRepo->getPackages(), $rootPkg);
$repos = $installedRepo = new InstalledRepository(array(new InstalledArrayRepository(array_map(static function ($pkg): PackageInterface {
return clone $pkg;
}, $packages))));
@ -1423,29 +1424,4 @@ EOT
return $this->repositorySet;
}
/**
* Find package requires and child requires
*
* @param array<PackageInterface> $bucket
* @return array<PackageInterface>
*/
private function filterRequiredPackages(RepositoryInterface $repo, PackageInterface $package, array $bucket = array()): array
{
$requires = $package->getRequires();
foreach ($repo->getPackages() as $candidate) {
foreach ($candidate->getNames() as $name) {
if (isset($requires[$name])) {
if (!in_array($candidate, $bucket, true)) {
$bucket[] = $candidate;
$bucket = $this->filterRequiredPackages($repo, $candidate, $bucket);
}
break;
}
}
}
return $bucket;
}
}

View File

@ -33,6 +33,8 @@ use Composer\Semver\Constraint\MatchAllConstraint;
/**
* @author Nils Adermann <naderman@naderman.de>
*
* @see RepositoryUtils for ways to work with single repos
*/
class RepositorySet
{

View File

@ -0,0 +1,52 @@
<?php declare(strict_types=1);
/*
* This file is part of Composer.
*
* (c) Nils Adermann <naderman@naderman.de>
* Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Composer\Repository;
use Composer\Package\PackageInterface;
/**
* @author Jordi Boggiano <j.boggiano@seld.be>
*
* @see RepositorySet for ways to work with sets of repos
*/
class RepositoryUtils
{
/**
* Find all of $packages which are required by $requirer, either directly or transitively
*
* Require-dev is ignored
*
* @template T of PackageInterface
* @param array<T> $packages
* @param array<T> $bucket Do not pass this in, only used to avoid recursion with circular deps
* @return list<T>
*/
public static function filterRequiredPackages(array $packages, PackageInterface $requirer, array $bucket = array()): array
{
$requires = $requirer->getRequires();
foreach ($packages as $candidate) {
foreach ($candidate->getNames() as $name) {
if (isset($requires[$name])) {
if (!in_array($candidate, $bucket, true)) {
$bucket[] = $candidate;
$bucket = self::filterRequiredPackages($packages, $candidate, $bucket);
}
break;
}
}
}
return $bucket;
}
}

View File

@ -17,6 +17,22 @@ use Composer\Package\RootPackageInterface;
class PackageSorter
{
/**
* Sorts packages by name
*
* @template T of PackageInterface
* @param array<T> $packages
* @return array<T>
*/
public static function sortPackagesAlphabetically(array $packages): array
{
usort($packages, static function (PackageInterface $a, PackageInterface $b) {
return $a->getName() <=> $b->getName();
});
return $packages;
}
/**
* Sorts packages by dependency weight
*

View File

@ -0,0 +1,92 @@
<?php declare(strict_types=1);
/*
* This file is part of Composer.
*
* (c) Nils Adermann <naderman@naderman.de>
* Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Composer\Test\Repository;
use Composer\Package\PackageInterface;
use Composer\Repository\RepositoryUtils;
use Composer\Test\TestCase;
use Generator;
class RepositoryUtilsTest extends TestCase
{
/**
* @dataProvider provideFilterRequireTests
* @param PackageInterface[] $pkgs
* @param PackageInterface $requirer
* @param string[] $expected
*/
public function testFilterRequiredPackages(array $pkgs, PackageInterface $requirer, array $expected): void
{
$expected = array_map(static function (string $name) use ($pkgs): PackageInterface {
return $pkgs[$name];
}, $expected);
self::assertSame($expected, RepositoryUtils::filterRequiredPackages($pkgs, $requirer));
}
/**
* @return array<PackageInterface>
*/
private function getPackages(): array
{
$packageA = $this->getPackage('required/a');
$packageB = $this->getPackage('required/b');
$this->configureLinks($packageB, ['require' => ['required/c' => '*']]);
$packageC = $this->getPackage('required/c');
$packageCAlias = $this->getAliasPackage($packageC, '2.0.0');
$packageCircular = $this->getPackage('required/circular');
$this->configureLinks($packageCircular, ['require' => ['required/circular-b' => '*']]);
$packageCircularB = $this->getPackage('required/circular-b');
$this->configureLinks($packageCircularB, ['require' => ['required/circular' => '*']]);
return [
$this->getPackage('dummy/pkg'),
$this->getPackage('dummy/pkg2', '2.0.0'),
'a' => $packageA,
'b' => $packageB,
'c' => $packageC,
'c-alias' => $packageCAlias,
'circular' => $packageCircular,
'circular-b' => $packageCircularB,
];
}
public function provideFilterRequireTests(): Generator
{
$pkgs = $this->getPackages();
$requirer = $this->getPackage('requirer/pkg');
yield 'no require' => [$pkgs, $requirer, []];
$requirer = $this->getPackage('requirer/pkg');
$this->configureLinks($requirer, ['require-dev' => ['required/a' => '*']]);
yield 'require-dev has no effect' => [$pkgs, $requirer, []];
$requirer = $this->getPackage('requirer/pkg');
$this->configureLinks($requirer, ['require' => ['required/a' => '*']]);
yield 'simple require' => [$pkgs, $requirer, ['a']];
$requirer = $this->getPackage('requirer/pkg');
$this->configureLinks($requirer, ['require' => ['required/a' => 'dev-lala']]);
yield 'require constraint is irrelevant' => [$pkgs, $requirer, ['a']];
$requirer = $this->getPackage('requirer/pkg');
$this->configureLinks($requirer, ['require' => ['required/b' => '*']]);
yield 'require transitive deps and aliases are included' => [$pkgs, $requirer, ['b', 'c', 'c-alias']];
$requirer = $this->getPackage('requirer/pkg');
$this->configureLinks($requirer, ['require' => ['required/circular' => '*']]);
yield 'circular deps are no problem' => [$pkgs, $requirer, ['circular', 'circular-b']];
}
}