From a6f1e803b1d674ae844dd2662c39c806e4063710 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Sun, 17 Oct 2021 14:43:34 +0200 Subject: [PATCH] Use more BasePackage instead of PackageInterface --- src/Composer/DependencyResolver/Request.php | 30 +++++++++---------- src/Composer/DependencyResolver/Solver.php | 4 +-- src/Composer/Installer.php | 7 +++-- .../DependencyResolver/DefaultPolicyTest.php | 10 +++---- tests/Composer/Test/InstallerTest.php | 2 ++ .../Repository/InstalledRepositoryTest.php | 4 +-- tests/Composer/Test/TestCase.php | 15 +++++++++- 7 files changed, 44 insertions(+), 28 deletions(-) diff --git a/src/Composer/DependencyResolver/Request.php b/src/Composer/DependencyResolver/Request.php index dc5ba0365..7378d9cfd 100644 --- a/src/Composer/DependencyResolver/Request.php +++ b/src/Composer/DependencyResolver/Request.php @@ -13,7 +13,7 @@ namespace Composer\DependencyResolver; use Composer\Package\Package; -use Composer\Package\PackageInterface; +use Composer\Package\BasePackage; use Composer\Repository\LockArrayRepository; use Composer\Semver\Constraint\ConstraintInterface; use Composer\Semver\Constraint\MatchAllConstraint; @@ -44,11 +44,11 @@ class Request protected $lockedRepository; /** @var array */ protected $requires = array(); - /** @var array */ + /** @var array */ protected $fixedPackages = array(); - /** @var array */ + /** @var array */ protected $lockedPackages = array(); - /** @var array */ + /** @var array */ protected $fixedLockedPackages = array(); /** @var string[] */ protected $updateAllowList = array(); @@ -79,7 +79,7 @@ class Request * This is used for platform packages which cannot be modified by Composer. A rule enforcing their installation is * generated for dependency resolution. Partial updates with dependencies cannot in any way modify these packages. */ - public function fixPackage(PackageInterface $package) + public function fixPackage(BasePackage $package) { $this->fixedPackages[spl_object_hash($package)] = $package; } @@ -94,7 +94,7 @@ class Request * if nothing requires these packages they will be removed. Additionally in a partial update these packages can be * unlocked, meaning other versions can be installed if explicitly requested as part of the update. */ - public function lockPackage(PackageInterface $package) + public function lockPackage(BasePackage $package) { $this->lockedPackages[spl_object_hash($package)] = $package; } @@ -106,13 +106,13 @@ class Request * removal of any packages. At the same time lock packages there cannot simply be marked fixed, as error reporting * would then report them as platform packages, so this still marks them as locked packages at the same time. */ - public function fixLockedPackage(PackageInterface $package) + public function fixLockedPackage(BasePackage $package) { $this->fixedPackages[spl_object_hash($package)] = $package; $this->fixedLockedPackages[spl_object_hash($package)] = $package; } - public function unlockPackage(PackageInterface $package) + public function unlockPackage(BasePackage $package) { unset($this->lockedPackages[spl_object_hash($package)]); } @@ -156,7 +156,7 @@ class Request } /** - * @return array + * @return array */ public function getFixedPackages() { @@ -166,13 +166,13 @@ class Request /** * @return bool */ - public function isFixedPackage(PackageInterface $package) + public function isFixedPackage(BasePackage $package) { return isset($this->fixedPackages[spl_object_hash($package)]); } /** - * @return array + * @return array */ public function getLockedPackages() { @@ -182,13 +182,13 @@ class Request /** * @return bool */ - public function isLockedPackage(PackageInterface $package) + public function isLockedPackage(BasePackage $package) { return isset($this->lockedPackages[spl_object_hash($package)]) || isset($this->fixedLockedPackages[spl_object_hash($package)]); } /** - * @return array + * @return array */ public function getFixedOrLockedPackages() { @@ -198,7 +198,7 @@ class Request // TODO look into removing the packageIds option, the only place true is used is for the installed map in the solver problems // some locked packages may not be in the pool so they have a package->id of -1 /** - * @return array + * @return array */ public function getPresentMap($packageIds = false) { @@ -218,7 +218,7 @@ class Request } /** - * @return PackageInterface[] + * @return BasePackage[] */ public function getFixedPackagesMap() { diff --git a/src/Composer/DependencyResolver/Solver.php b/src/Composer/DependencyResolver/Solver.php index af474240c..830ee826c 100644 --- a/src/Composer/DependencyResolver/Solver.php +++ b/src/Composer/DependencyResolver/Solver.php @@ -13,7 +13,7 @@ namespace Composer\DependencyResolver; use Composer\IO\IOInterface; -use Composer\Package\PackageInterface; +use Composer\Package\BasePackage; use Composer\Repository\PlatformRepository; /** @@ -36,7 +36,7 @@ class Solver protected $watchGraph; /** @var Decisions */ protected $decisions; - /** @var PackageInterface[] */ + /** @var BasePackage[] */ protected $fixedMap; /** @var int */ diff --git a/src/Composer/Installer.php b/src/Composer/Installer.php index 421427afd..e9d06e746 100644 --- a/src/Composer/Installer.php +++ b/src/Composer/Installer.php @@ -84,13 +84,13 @@ class Installer protected $config; /** - * @var RootPackageInterface + * @var RootPackageInterface&BasePackage */ protected $package; // TODO can we get rid of the below and just use the package itself? /** - * @var RootPackageInterface + * @var RootPackageInterface&BasePackage */ protected $fixedRootPackage; @@ -187,7 +187,7 @@ class Installer * * @param IOInterface $io * @param Config $config - * @param RootPackageInterface $package + * @param RootPackageInterface&BasePackage $package * @param DownloadManager $downloadManager * @param RepositoryManager $repositoryManager * @param Locker $locker @@ -845,6 +845,7 @@ class Installer } /** + * @param RootPackageInterface&BasePackage $rootPackage * @return Request */ private function createRequest(RootPackageInterface $rootPackage, PlatformRepository $platformRepo, LockArrayRepository $lockedRepository = null) diff --git a/tests/Composer/Test/DependencyResolver/DefaultPolicyTest.php b/tests/Composer/Test/DependencyResolver/DefaultPolicyTest.php index 42a8f2da5..cae72c66d 100644 --- a/tests/Composer/Test/DependencyResolver/DefaultPolicyTest.php +++ b/tests/Composer/Test/DependencyResolver/DefaultPolicyTest.php @@ -190,8 +190,8 @@ class DefaultPolicyTest extends TestCase $this->repo->addPackage($packageA = $this->getPackage('A', '1.0')); $this->repo->addPackage($packageB = $this->getPackage('B', '2.0')); - $packageA->setProvides(array(new Link('A', 'X', new Constraint('==', '1.0'), Link::TYPE_PROVIDE))); - $packageB->setProvides(array(new Link('B', 'X', new Constraint('==', '1.0'), Link::TYPE_PROVIDE))); + $packageA->setProvides(array('x' => new Link('A', 'X', new Constraint('==', '1.0'), Link::TYPE_PROVIDE))); + $packageB->setProvides(array('x' => new Link('B', 'X', new Constraint('==', '1.0'), Link::TYPE_PROVIDE))); $this->repositorySet->addRepository($this->repo); @@ -210,7 +210,7 @@ class DefaultPolicyTest extends TestCase $this->repo->addPackage($packageA = $this->getPackage('A', '1.0')); $this->repo->addPackage($packageB = $this->getPackage('B', '2.0')); - $packageB->setReplaces(array(new Link('B', 'A', new Constraint('==', '1.0'), Link::TYPE_REPLACE))); + $packageB->setReplaces(array('a' => new Link('B', 'A', new Constraint('==', '1.0'), Link::TYPE_REPLACE))); $this->repositorySet->addRepository($this->repo); @@ -230,8 +230,8 @@ class DefaultPolicyTest extends TestCase $this->repo->addPackage($packageB = $this->getPackage('vendor-b/replacer', '1.0')); $this->repo->addPackage($packageA = $this->getPackage('vendor-a/replacer', '1.0')); - $packageA->setReplaces(array(new Link('vendor-a/replacer', 'vendor-a/package', new Constraint('==', '1.0'), Link::TYPE_REPLACE))); - $packageB->setReplaces(array(new Link('vendor-b/replacer', 'vendor-a/package', new Constraint('==', '1.0'), Link::TYPE_REPLACE))); + $packageA->setReplaces(array('vendor-a/package' => new Link('vendor-a/replacer', 'vendor-a/package', new Constraint('==', '1.0'), Link::TYPE_REPLACE))); + $packageB->setReplaces(array('vendor-a/package' => new Link('vendor-b/replacer', 'vendor-a/package', new Constraint('==', '1.0'), Link::TYPE_REPLACE))); $this->repositorySet->addRepository($this->repo); diff --git a/tests/Composer/Test/InstallerTest.php b/tests/Composer/Test/InstallerTest.php index c4202511c..d5959daf8 100644 --- a/tests/Composer/Test/InstallerTest.php +++ b/tests/Composer/Test/InstallerTest.php @@ -26,6 +26,7 @@ use Composer\Repository\ArrayRepository; use Composer\Repository\RepositoryManager; use Composer\Repository\InstalledArrayRepository; use Composer\Package\RootPackageInterface; +use Composer\Package\BasePackage; use Composer\Package\Link; use Composer\Package\Locker; use Composer\Test\Mock\FactoryMock; @@ -58,6 +59,7 @@ class InstallerTest extends TestCase /** * @dataProvider provideInstaller + * @param RootPackageInterface&BasePackage $rootPackage */ public function testInstaller(RootPackageInterface $rootPackage, $repositories, array $options) { diff --git a/tests/Composer/Test/Repository/InstalledRepositoryTest.php b/tests/Composer/Test/Repository/InstalledRepositoryTest.php index 505278c3b..7b0d767ca 100644 --- a/tests/Composer/Test/Repository/InstalledRepositoryTest.php +++ b/tests/Composer/Test/Repository/InstalledRepositoryTest.php @@ -31,8 +31,8 @@ class InstalledRepositoryTest extends TestCase $arrayRepoTwo->addPackage($bar = $this->getPackage('bar', '1')); $arrayRepoTwo->addPackage($bar2 = $this->getPackage('bar', '2')); - $foo->setReplaces(array(new Link('foo', 'provided', new MatchAllConstraint()))); - $bar2->setProvides(array(new Link('bar', 'provided', new MatchAllConstraint()))); + $foo->setReplaces(array('provided' => new Link('foo', 'provided', new MatchAllConstraint()))); + $bar2->setProvides(array('provided' => new Link('bar', 'provided', new MatchAllConstraint()))); $repo = new InstalledRepository(array($arrayRepoOne, $arrayRepoTwo)); diff --git a/tests/Composer/Test/TestCase.php b/tests/Composer/Test/TestCase.php index 8a461644d..1e6e77c06 100644 --- a/tests/Composer/Test/TestCase.php +++ b/tests/Composer/Test/TestCase.php @@ -22,6 +22,10 @@ use Composer\Util\Silencer; use Symfony\Component\Process\ExecutableFinder; use Composer\Package\Loader\ArrayLoader; use Composer\Package\BasePackage; +use Composer\Package\RootPackage; +use Composer\Package\RootAliasPackage; +use Composer\Package\CompletePackage; +use Composer\Package\CompleteAliasPackage; abstract class TestCase extends PolyfillTestCase { @@ -74,7 +78,16 @@ abstract class TestCase extends PolyfillTestCase return $constraint; } - protected function getPackage($name, $version, $class = 'Composer\Package\Package') + /** + * @template PackageClass of PackageInterface + * + * @param string $class FQCN to be instantiated + * + * @return CompletePackage|CompleteAliasPackage|RootPackage|RootAliasPackage + * + * @phpstan-param class-string $class + */ + protected function getPackage($name, $version, $class = 'Composer\Package\CompletePackage') { $normVersion = self::getVersionParser()->normalize($version);