From bd8b39fe01b55b3afba8058afb78adfd0fbb4737 Mon Sep 17 00:00:00 2001 From: Martin Herndl Date: Mon, 25 Oct 2021 13:45:09 +0200 Subject: [PATCH] Add types to `Package` (#10210) --- src/Composer/Package/AliasPackage.php | 4 + src/Composer/Package/BasePackage.php | 8 ++ src/Composer/Package/Locker.php | 54 ++++++++++--- src/Composer/Package/Package.php | 76 ++++++++++++++++++- src/Composer/Package/PackageInterface.php | 2 + .../Test/Autoload/AutoloadGeneratorTest.php | 2 +- 6 files changed, 131 insertions(+), 15 deletions(-) diff --git a/src/Composer/Package/AliasPackage.php b/src/Composer/Package/AliasPackage.php index 7b9ab2c14..0e737a916 100644 --- a/src/Composer/Package/AliasPackage.php +++ b/src/Composer/Package/AliasPackage.php @@ -29,6 +29,7 @@ class AliasPackage extends BasePackage /** @var bool */ protected $rootPackageAlias = false; /** @var string */ + /** @phpstan-var 'stable'|'RC'|'beta'|'alpha'|'dev' */ protected $stability; /** @var bool */ protected $hasSelfVersionRequires = false; @@ -214,6 +215,9 @@ class AliasPackage extends BasePackage return $links; } + /** + * @return bool + */ public function hasSelfVersionRequires() { return $this->hasSelfVersionRequires; diff --git a/src/Composer/Package/BasePackage.php b/src/Composer/Package/BasePackage.php index 72025b9d5..b61c4de7e 100644 --- a/src/Composer/Package/BasePackage.php +++ b/src/Composer/Package/BasePackage.php @@ -168,6 +168,9 @@ abstract class BasePackage implements PackageInterface return $this->getName().'-'.$this->getVersion(); } + /** + * @return bool + */ public function equals(PackageInterface $package) { $self = $this; @@ -231,6 +234,11 @@ abstract class BasePackage implements PackageInterface return $this->getPrettyVersion() . ' ' . $reference; } + /** + * @return int + * + * @phpstan-return self::STABILITY_* + */ public function getStabilityPriority() { return self::$stabilities[$this->getStability()]; diff --git a/src/Composer/Package/Locker.php b/src/Composer/Package/Locker.php index 49894ffc3..924bd50c1 100644 --- a/src/Composer/Package/Locker.php +++ b/src/Composer/Package/Locker.php @@ -251,6 +251,9 @@ class Locker return $requirements; } + /** + * @return string + */ public function getMinimumStability() { $lockData = $this->getLockData(); @@ -258,6 +261,9 @@ class Locker return isset($lockData['minimum-stability']) ? $lockData['minimum-stability'] : 'stable'; } + /** + * @return array + */ public function getStabilityFlags() { $lockData = $this->getLockData(); @@ -265,6 +271,9 @@ class Locker return isset($lockData['stability-flags']) ? $lockData['stability-flags'] : array(); } + /** + * @return bool|null + */ public function getPreferStable() { $lockData = $this->getLockData(); @@ -274,6 +283,9 @@ class Locker return isset($lockData['prefer-stable']) ? $lockData['prefer-stable'] : null; } + /** + * @return bool|null + */ public function getPreferLowest() { $lockData = $this->getLockData(); @@ -283,6 +295,9 @@ class Locker return isset($lockData['prefer-lowest']) ? $lockData['prefer-lowest'] : null; } + /** + * @return array + */ public function getPlatformOverrides() { $lockData = $this->getLockData(); @@ -290,6 +305,11 @@ class Locker return isset($lockData['platform-overrides']) ? $lockData['platform-overrides'] : array(); } + /** + * @return string[][] + * + * @phpstan-return list> + */ public function getAliases() { $lockData = $this->getLockData(); @@ -297,6 +317,9 @@ class Locker return isset($lockData['aliases']) ? $lockData['aliases'] : array(); } + /** + * @return array + */ public function getLockData() { if (null !== $this->lockDataCache) { @@ -313,19 +336,21 @@ class Locker /** * Locks provided data into lockfile. * - * @param array $packages array of packages - * @param mixed $devPackages array of dev packages or null if installed without --dev - * @param array $platformReqs array of package name => constraint for required platform packages - * @param mixed $platformDevReqs array of package name => constraint for dev-required platform packages - * @param array $aliases array of aliases - * @param string $minimumStability - * @param array $stabilityFlags - * @param bool $preferStable - * @param bool $preferLowest - * @param array $platformOverrides - * @param bool $write Whether to actually write data to disk, useful in tests and for --dry-run + * @param PackageInterface[] $packages array of packages + * @param PackageInterface[]|null $devPackages array of dev packages or null if installed without --dev + * @param array $platformReqs array of package name => constraint for required platform packages + * @param array $platformDevReqs array of package name => constraint for dev-required platform packages + * @param string[][] $aliases array of aliases + * @param string $minimumStability + * @param array $stabilityFlags + * @param bool $preferStable + * @param bool $preferLowest + * @param array $platformOverrides + * @param bool $write Whether to actually write data to disk, useful in tests and for --dry-run * * @return bool + * + * @phpstan-param list> $aliases */ public function setLockData(array $packages, $devPackages, array $platformReqs, $platformDevReqs, array $aliases, $minimumStability, array $stabilityFlags, $preferStable, $preferLowest, array $platformOverrides, $write = true) { @@ -386,6 +411,13 @@ class Locker return false; } + /** + * @param PackageInterface[] $packages + * + * @return mixed[][] + * + * @phpstan-return list> + */ private function lockPackages(array $packages) { $locked = array(); diff --git a/src/Composer/Package/Package.php b/src/Composer/Package/Package.php index 7881978c1..419d8231d 100644 --- a/src/Composer/Package/Package.php +++ b/src/Composer/Package/Package.php @@ -59,6 +59,7 @@ class Package extends BasePackage /** @var bool */ protected $dev; /** @var string */ + /** @phpstan-var 'stable'|'RC'|'beta'|'alpha'|'dev' */ protected $stability; /** @var ?string */ protected $notificationUrl; @@ -114,6 +115,8 @@ class Package extends BasePackage /** * @param string $type + * + * @return void */ public function setType($type) { @@ -138,6 +141,8 @@ class Package extends BasePackage /** * @param string $targetDir + * + * @return void */ public function setTargetDir($targetDir) { @@ -157,7 +162,9 @@ class Package extends BasePackage } /** - * @param array $extra + * @param mixed[] $extra + * + * @return void */ public function setExtra(array $extra) { @@ -173,7 +180,9 @@ class Package extends BasePackage } /** - * @param array $binaries + * @param string[] $binaries + * + * @return void */ public function setBinaries(array $binaries) { @@ -190,6 +199,8 @@ class Package extends BasePackage /** * {@inheritDoc} + * + * @return void */ public function setInstallationSource($type) { @@ -206,6 +217,8 @@ class Package extends BasePackage /** * @param string $type + * + * @return void */ public function setSourceType($type) { @@ -222,6 +235,8 @@ class Package extends BasePackage /** * @param string $url + * + * @return void */ public function setSourceUrl($url) { @@ -238,6 +253,8 @@ class Package extends BasePackage /** * @param string $reference + * + * @return void */ public function setSourceReference($reference) { @@ -254,6 +271,8 @@ class Package extends BasePackage /** * {@inheritDoc} + * + * @return void */ public function setSourceMirrors($mirrors) { @@ -278,6 +297,8 @@ class Package extends BasePackage /** * @param string $type + * + * @return void */ public function setDistType($type) { @@ -294,6 +315,8 @@ class Package extends BasePackage /** * @param string $url + * + * @return void */ public function setDistUrl($url) { @@ -310,6 +333,8 @@ class Package extends BasePackage /** * @param string $reference + * + * @return void */ public function setDistReference($reference) { @@ -326,6 +351,8 @@ class Package extends BasePackage /** * @param string $sha1checksum + * + * @return void */ public function setDistSha1Checksum($sha1checksum) { @@ -342,6 +369,8 @@ class Package extends BasePackage /** * {@inheritDoc} + * + * @return void */ public function setDistMirrors($mirrors) { @@ -400,6 +429,8 @@ class Package extends BasePackage * Set the releaseDate * * @param \DateTime $releaseDate + * + * @return void */ public function setReleaseDate(\DateTime $releaseDate) { @@ -418,6 +449,8 @@ class Package extends BasePackage * Set the required packages * * @param array $requires A set of package links + * + * @return void */ public function setRequires(array $requires) { @@ -436,6 +469,8 @@ class Package extends BasePackage * Set the conflicting packages * * @param array $conflicts A set of package links + * + * @return void */ public function setConflicts(array $conflicts) { @@ -455,6 +490,8 @@ class Package extends BasePackage * Set the provided virtual packages * * @param array $provides A set of package links + * + * @return void */ public function setProvides(array $provides) { @@ -474,6 +511,8 @@ class Package extends BasePackage * Set the packages this one replaces * * @param array $replaces A set of package links + * + * @return void */ public function setReplaces(array $replaces) { @@ -493,6 +532,8 @@ class Package extends BasePackage * Set the recommended packages * * @param array $devRequires A set of package links + * + * @return void */ public function setDevRequires(array $devRequires) { @@ -511,6 +552,8 @@ class Package extends BasePackage * Set the suggested packages * * @param array $suggests A set of package names/comments + * + * @return void */ public function setSuggests(array $suggests) { @@ -529,6 +572,10 @@ class Package extends BasePackage * Set the autoload mapping * * @param array $autoload Mapping of autoloading rules + * + * @return void + * + * @phpstan-param array{psr-0?: array, psr-4?: array, classmap?: list, files?: list} $autoload */ public function setAutoload(array $autoload) { @@ -547,6 +594,10 @@ class Package extends BasePackage * Set the dev autoload mapping * * @param array $devAutoload Mapping of dev autoloading rules + * + * @return void + * + * @phpstan-param array{psr-0?: array, psr-4?: array, classmap?: list, files?: list} $devAutoload */ public function setDevAutoload(array $devAutoload) { @@ -564,7 +615,9 @@ class Package extends BasePackage /** * Sets the list of paths added to PHP's include path. * - * @param array $includePaths List of directories. + * @param string[] $includePaths List of directories. + * + * @return void */ public function setIncludePaths(array $includePaths) { @@ -583,6 +636,8 @@ class Package extends BasePackage * Sets the notification URL * * @param string $notificationUrl + * + * @return void */ public function setNotificationUrl($notificationUrl) { @@ -599,6 +654,8 @@ class Package extends BasePackage /** * @param bool $defaultBranch + * + * @return void */ public function setIsDefaultBranch($defaultBranch) { @@ -639,6 +696,8 @@ class Package extends BasePackage * * @param string $version The package's normalized version * @param string $prettyVersion The package's non-normalized version + * + * @return void */ public function replaceVersion($version, $prettyVersion) { @@ -649,6 +708,17 @@ class Package extends BasePackage $this->dev = $this->stability === 'dev'; } + /** + * @param string|null $url + * @param mixed[]|null $mirrors + * @param string|null $ref + * @param string|null $type + * @param string $urlType + * + * @return string[] + * + * @phpstan-param list|null $mirrors + */ protected function getUrls($url, $mirrors, $ref, $type, $urlType) { if (!$url) { diff --git a/src/Composer/Package/PackageInterface.php b/src/Composer/Package/PackageInterface.php index cad79a8e0..89dce5101 100644 --- a/src/Composer/Package/PackageInterface.php +++ b/src/Composer/Package/PackageInterface.php @@ -240,6 +240,8 @@ interface PackageInterface * Returns the stability of this package: one of (dev, alpha, beta, RC, stable) * * @return string + * + * @phpstan-return 'stable'|'RC'|'beta'|'alpha'|'dev' */ public function getStability(); diff --git a/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php b/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php index 521e762fb..d777fab51 100644 --- a/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php +++ b/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php @@ -1292,7 +1292,7 @@ EOF; ->with(ScriptEvents::POST_AUTOLOAD_DUMP, false); $package = new RootPackage('root/a', '1.0', '1.0'); - $package->setAutoload(array('psr-0' => array('foo/bar/non/existing/'))); + $package->setAutoload(array('psr-0' => array('Prefix' => 'foo/bar/non/existing/'))); $this->repository->expects($this->once()) ->method('getCanonicalPackages')