From a921d9b233b622ffb10ea1a5fce6be0cff6e1369 Mon Sep 17 00:00:00 2001 From: Paolo Rossi Date: Mon, 25 Oct 2021 10:44:29 +0200 Subject: [PATCH] Reaching phpstan level 6 in EventDispatcher Exception and Installer folders (#10192) --- src/Composer/EventDispatcher/Event.php | 2 + .../EventDispatcher/EventDispatcher.php | 39 +++++++---- src/Composer/Installer/BinaryInstaller.php | 51 +++++++++++++++ .../Installer/InstallationManager.php | 65 +++++++++++++++++-- src/Composer/Installer/LibraryInstaller.php | 12 ++++ src/Composer/Installer/PluginInstaller.php | 2 + src/Composer/Installer/ProjectInstaller.php | 3 + .../Installer/SuggestedPackagesReporter.php | 2 +- .../Test/Mock/InstallationManagerMock.php | 16 +++++ 9 files changed, 172 insertions(+), 20 deletions(-) diff --git a/src/Composer/EventDispatcher/Event.php b/src/Composer/EventDispatcher/Event.php index 51cfda4f8..8563809f5 100644 --- a/src/Composer/EventDispatcher/Event.php +++ b/src/Composer/EventDispatcher/Event.php @@ -95,6 +95,8 @@ class Event /** * Prevents the event from being passed to further listeners + * + * @return void */ public function stopPropagation() { diff --git a/src/Composer/EventDispatcher/EventDispatcher.php b/src/Composer/EventDispatcher/EventDispatcher.php index c273fdf59..06e30792c 100644 --- a/src/Composer/EventDispatcher/EventDispatcher.php +++ b/src/Composer/EventDispatcher/EventDispatcher.php @@ -106,12 +106,12 @@ class EventDispatcher /** * Dispatch a script event. * - * @param string $eventName The constant in ScriptEvents - * @param bool $devMode - * @param array $additionalArgs Arguments passed by the user - * @param array $flags Optional flags to pass data not as argument - * @return int return code of the executed script if any, for php scripts a false return - * value is changed to 1, anything else to 0 + * @param string $eventName The constant in ScriptEvents + * @param bool $devMode + * @param array $additionalArgs Arguments passed by the user + * @param array $flags Optional flags to pass data not as argument + * @return int return code of the executed script if any, for php scripts a false return + * value is changed to 1, anything else to 0 */ public function dispatchScript($eventName, $devMode = false, $additionalArgs = array(), $flags = array()) { @@ -121,11 +121,11 @@ class EventDispatcher /** * Dispatch a package event. * - * @param string $eventName The constant in PackageEvents - * @param bool $devMode Whether or not we are in dev mode - * @param RepositoryInterface $localRepo The installed repository - * @param array $operations The list of operations - * @param OperationInterface $operation The package being installed/updated/removed + * @param string $eventName The constant in PackageEvents + * @param bool $devMode Whether or not we are in dev mode + * @param RepositoryInterface $localRepo The installed repository + * @param OperationInterface[] $operations The list of operations + * @param OperationInterface $operation The package being installed/updated/removed * * @return int return code of the executed script if any, for php scripts a false return * value is changed to 1, anything else to 0 @@ -338,6 +338,8 @@ class EventDispatcher } /** + * @param string $exec + * * @return int */ protected function executeTty($exec) @@ -372,6 +374,8 @@ class EventDispatcher * @param string $className * @param string $methodName * @param Event $event Event invoking the PHP callable + * + * @return mixed */ protected function executeEventPhpScript($className, $methodName, Event $event) { @@ -390,6 +394,8 @@ class EventDispatcher * @param string $eventName The event name - typically a constant * @param callable $listener A callable expecting an event argument * @param int $priority A higher value represents a higher priority + * + * @return void */ public function addListener($eventName, $listener, $priority = 0) { @@ -398,6 +404,8 @@ class EventDispatcher /** * @param callable|object $listener A callable or an object instance for which all listeners should be removed + * + * @return void */ public function removeListener($listener) { @@ -418,6 +426,8 @@ class EventDispatcher * @see EventSubscriberInterface * * @param EventSubscriberInterface $subscriber + * + * @return void */ public function addSubscriber(EventSubscriberInterface $subscriber) { @@ -438,7 +448,7 @@ class EventDispatcher * Retrieves all listeners for a given event * * @param Event $event - * @return array All listeners: callables and scripts + * @return array All listeners: callables and scripts */ protected function getListeners(Event $event) { @@ -472,7 +482,7 @@ class EventDispatcher * Finds all listeners defined as scripts in the package * * @param Event $event Event object - * @return array Listeners + * @return string[] Listeners */ protected function getScriptListeners(Event $event) { @@ -550,6 +560,9 @@ class EventDispatcher return array_pop($this->eventStack); } + /** + * @return void + */ private function ensureBinDirIsInPath() { $pathStr = 'PATH'; diff --git a/src/Composer/Installer/BinaryInstaller.php b/src/Composer/Installer/BinaryInstaller.php index c692e72b3..19a48eada 100644 --- a/src/Composer/Installer/BinaryInstaller.php +++ b/src/Composer/Installer/BinaryInstaller.php @@ -51,6 +51,12 @@ class BinaryInstaller $this->filesystem = $filesystem ?: new Filesystem(); } + /** + * @param string $installPath + * @param bool $warnOnOverwrite + * + * @return void + */ public function installBinaries(PackageInterface $package, $installPath, $warnOnOverwrite = true) { $binaries = $this->getBinaries($package); @@ -103,6 +109,9 @@ class BinaryInstaller } } + /** + * @return void + */ public function removeBinaries(PackageInterface $package) { $this->initializeBinDir(); @@ -127,6 +136,11 @@ class BinaryInstaller } } + /** + * @param string $bin + * + * @return string + */ public static function determineBinaryCaller($bin) { if ('.bat' === substr($bin, -4) || '.exe' === substr($bin, -4)) { @@ -143,11 +157,21 @@ class BinaryInstaller return 'php'; } + /** + * @return string[] + */ protected function getBinaries(PackageInterface $package) { return $package->getBinaries(); } + /** + * @param string $binPath + * @param string $link + * @param string $bin + * + * @return void + */ protected function installFullBinaries($binPath, $link, $bin, PackageInterface $package) { // add unixy support for cygwin and similar environments @@ -164,6 +188,12 @@ class BinaryInstaller } } + /** + * @param string $binPath + * @param string $link + * + * @return void + */ protected function installSymlinkBinaries($binPath, $link) { if (!$this->filesystem->relativeSymlink($binPath, $link)) { @@ -171,18 +201,33 @@ class BinaryInstaller } } + /** + * @param string $binPath + * @param string $link + * + * @return void + */ protected function installUnixyProxyBinaries($binPath, $link) { file_put_contents($link, $this->generateUnixyProxyCode($binPath, $link)); Silencer::call('chmod', $link, 0777 & ~umask()); } + /** + * @return void + */ protected function initializeBinDir() { $this->filesystem->ensureDirectoryExists($this->binDir); $this->binDir = realpath($this->binDir); } + /** + * @param string $bin + * @param string $link + * + * @return string + */ protected function generateWindowsProxyCode($bin, $link) { $binPath = $this->filesystem->findShortestPath($link, $bin); @@ -194,6 +239,12 @@ class BinaryInstaller "{$caller} \"%BIN_TARGET%\" %*\r\n"; } + /** + * @param string $bin + * @param string $link + * + * @return string + */ protected function generateUnixyProxyCode($bin, $link) { $binPath = $this->filesystem->findShortestPath($link, $bin); diff --git a/src/Composer/Installer/InstallationManager.php b/src/Composer/Installer/InstallationManager.php index 5b254384e..3448a96a6 100644 --- a/src/Composer/Installer/InstallationManager.php +++ b/src/Composer/Installer/InstallationManager.php @@ -59,6 +59,9 @@ class InstallationManager $this->eventDispatcher = $eventDispatcher; } + /** + * @return void + */ public function reset() { $this->notifiablePackages = array(); @@ -68,6 +71,8 @@ class InstallationManager * Adds installer * * @param InstallerInterface $installer installer instance + * + * @return void */ public function addInstaller(InstallerInterface $installer) { @@ -79,6 +84,8 @@ class InstallationManager * Removes installer * * @param InstallerInterface $installer installer instance + * + * @return void */ public function removeInstaller(InstallerInterface $installer) { @@ -94,6 +101,8 @@ class InstallationManager * We prevent any plugins from being instantiated by simply * deactivating the installer for them. This ensure that no third-party * code is ever executed. + * + * @return void */ public function disablePlugins() { @@ -153,6 +162,8 @@ class InstallationManager * If the installer associated to this package doesn't handle that function, it'll do nothing. * * @param PackageInterface $package Package instance + * + * @return void */ public function ensureBinariesPresence(PackageInterface $package) { @@ -176,6 +187,8 @@ class InstallationManager * @param OperationInterface[] $operations operations to execute * @param bool $devMode whether the install is being run in dev mode * @param bool $runScripts whether to dispatch script events + * + * @return void */ public function execute(InstalledRepositoryInterface $repo, array $operations, $devMode = true, $runScripts = true) { @@ -293,8 +306,13 @@ class InstallationManager } /** - * @param array $operations List of operations to execute in this batch - * @param array $allOperations Complete list of operations to be executed in the install job, used for event listeners + * @param OperationInterface[] $operations List of operations to execute in this batch + * @param PromiseInterface[] $cleanupPromises + * @param bool $devMode + * @param bool $runScripts + * @param OperationInterface[] $allOperations Complete list of operations to be executed in the install job, used for event listeners + * + * @return void */ private function downloadAndExecuteBatch(InstalledRepositoryInterface $repo, array $operations, array &$cleanupPromises, $devMode, $runScripts, array $allOperations) { @@ -309,9 +327,11 @@ class InstallationManager } if ($opType === 'update') { + /** @var UpdateOperation $operation */ $package = $operation->getTargetPackage(); $initialPackage = $operation->getInitialPackage(); } else { + /** @var InstallOperation|MarkAliasInstalledOperation|MarkAliasUninstalledOperation|UninstallOperation $operation */ $package = $operation->getPackage(); $initialPackage = null; } @@ -345,8 +365,8 @@ class InstallationManager $batches = array(); $batch = array(); foreach ($operations as $index => $operation) { - if (in_array($operation->getOperationType(), array('update', 'install'), true)) { - $package = $operation->getOperationType() === 'update' ? $operation->getTargetPackage() : $operation->getPackage(); + if ($operation instanceof InstallOperation || $operation instanceof UpdateOperation) { + $package = $operation instanceof UpdateOperation ? $operation->getTargetPackage() : $operation->getPackage(); if ($package->getType() === 'composer-plugin' || $package->getType() === 'composer-installer') { if ($batch) { $batches[] = $batch; @@ -370,8 +390,13 @@ class InstallationManager } /** - * @param array $operations List of operations to execute in this batch - * @param array $allOperations Complete list of operations to be executed in the install job, used for event listeners + * @param OperationInterface[] $operations List of operations to execute in this batch + * @param PromiseInterface[] $cleanupPromises + * @param bool $devMode + * @param bool $runScripts + * @param OperationInterface[] $allOperations Complete list of operations to be executed in the install job, used for event listeners + * + * @return void */ private function executeBatch(InstalledRepositoryInterface $repo, array $operations, array $cleanupPromises, $devMode, $runScripts, array $allOperations) { @@ -393,9 +418,11 @@ class InstallationManager } if ($opType === 'update') { + /** @var UpdateOperation $operation */ $package = $operation->getTargetPackage(); $initialPackage = $operation->getInitialPackage(); } else { + /** @var InstallOperation|MarkAliasInstalledOperation|MarkAliasUninstalledOperation|UninstallOperation $operation */ $package = $operation->getPackage(); $initialPackage = null; } @@ -448,6 +475,11 @@ class InstallationManager } } + /** + * @param PromiseInterface[] $promises + * + * @return void + */ private function waitOnPromises(array $promises) { $progress = null; @@ -475,6 +507,8 @@ class InstallationManager * * @param InstalledRepositoryInterface $repo repository in which to check * @param InstallOperation $operation operation instance + * + * @return PromiseInterface|null */ public function install(InstalledRepositoryInterface $repo, InstallOperation $operation) { @@ -491,6 +525,8 @@ class InstallationManager * * @param InstalledRepositoryInterface $repo repository in which to check * @param UpdateOperation $operation operation instance + * + * @return PromiseInterface|null */ public function update(InstalledRepositoryInterface $repo, UpdateOperation $operation) { @@ -524,6 +560,8 @@ class InstallationManager * * @param InstalledRepositoryInterface $repo repository in which to check * @param UninstallOperation $operation operation instance + * + * @return PromiseInterface|null */ public function uninstall(InstalledRepositoryInterface $repo, UninstallOperation $operation) { @@ -538,6 +576,8 @@ class InstallationManager * * @param InstalledRepositoryInterface $repo repository in which to check * @param MarkAliasInstalledOperation $operation operation instance + * + * @return void */ public function markAliasInstalled(InstalledRepositoryInterface $repo, MarkAliasInstalledOperation $operation) { @@ -553,6 +593,8 @@ class InstallationManager * * @param InstalledRepositoryInterface $repo repository in which to check * @param MarkAliasUninstalledOperation $operation operation instance + * + * @return void */ public function markAliasUninstalled(InstalledRepositoryInterface $repo, MarkAliasUninstalledOperation $operation) { @@ -574,11 +616,19 @@ class InstallationManager return $installer->getInstallPath($package); } + /** + * @param bool $outputProgress + * + * @return void + */ public function setOutputProgress($outputProgress) { $this->outputProgress = $outputProgress; } + /** + * @return void + */ public function notifyInstalls(IOInterface $io) { $promises = array(); @@ -638,6 +688,9 @@ class InstallationManager $this->reset(); } + /** + * @return void + */ private function markForNotification(PackageInterface $package) { if ($package->getNotificationUrl()) { diff --git a/src/Composer/Installer/LibraryInstaller.php b/src/Composer/Installer/LibraryInstaller.php index 239e5d129..73d6481c2 100644 --- a/src/Composer/Installer/LibraryInstaller.php +++ b/src/Composer/Installer/LibraryInstaller.php @@ -258,6 +258,9 @@ class LibraryInstaller implements InstallerInterface, BinaryPresenceInterface return $installPath; } + /** + * @return PromiseInterface|null + */ protected function installCode(PackageInterface $package) { $downloadPath = $this->getInstallPath($package); @@ -265,6 +268,9 @@ class LibraryInstaller implements InstallerInterface, BinaryPresenceInterface return $this->downloadManager->install($package, $downloadPath); } + /** + * @return PromiseInterface|null + */ protected function updateCode(PackageInterface $initial, PackageInterface $target) { $initialDownloadPath = $this->getInstallPath($initial); @@ -298,6 +304,9 @@ class LibraryInstaller implements InstallerInterface, BinaryPresenceInterface return $this->downloadManager->update($initial, $target, $targetDownloadPath); } + /** + * @return PromiseInterface|null + */ protected function removeCode(PackageInterface $package) { $downloadPath = $this->getPackageBasePath($package); @@ -305,6 +314,9 @@ class LibraryInstaller implements InstallerInterface, BinaryPresenceInterface return $this->downloadManager->remove($package, $downloadPath); } + /** + * @return void + */ protected function initializeVendorDir() { $this->filesystem->ensureDirectoryExists($this->vendorDir); diff --git a/src/Composer/Installer/PluginInstaller.php b/src/Composer/Installer/PluginInstaller.php index 120253216..f760be6a9 100644 --- a/src/Composer/Installer/PluginInstaller.php +++ b/src/Composer/Installer/PluginInstaller.php @@ -117,6 +117,8 @@ class PluginInstaller extends LibraryInstaller /** * TODO v3 should make this private once we can drop PHP 5.3 support * @private + * + * @return void */ public function rollbackInstall(\Exception $e, InstalledRepositoryInterface $repo, PackageInterface $package) { diff --git a/src/Composer/Installer/ProjectInstaller.php b/src/Composer/Installer/ProjectInstaller.php index d03e97521..678da7f13 100644 --- a/src/Composer/Installer/ProjectInstaller.php +++ b/src/Composer/Installer/ProjectInstaller.php @@ -32,6 +32,9 @@ class ProjectInstaller implements InstallerInterface /** @var Filesystem */ private $filesystem; + /** + * @param string $installPath + */ public function __construct($installPath, DownloadManager $dm, Filesystem $fs) { $this->installPath = rtrim(strtr($installPath, '\\', '/'), '/').'/'; diff --git a/src/Composer/Installer/SuggestedPackagesReporter.php b/src/Composer/Installer/SuggestedPackagesReporter.php index 4db5931d3..901852307 100644 --- a/src/Composer/Installer/SuggestedPackagesReporter.php +++ b/src/Composer/Installer/SuggestedPackagesReporter.php @@ -44,7 +44,7 @@ class SuggestedPackagesReporter } /** - * @return array Suggested packages with source, target and reason keys. + * @return array Suggested packages with source, target and reason keys. */ public function getPackages() { diff --git a/tests/Composer/Test/Mock/InstallationManagerMock.php b/tests/Composer/Test/Mock/InstallationManagerMock.php index 9e235b917..6271d3da6 100644 --- a/tests/Composer/Test/Mock/InstallationManagerMock.php +++ b/tests/Composer/Test/Mock/InstallationManagerMock.php @@ -21,6 +21,7 @@ use Composer\DependencyResolver\Operation\UpdateOperation; use Composer\DependencyResolver\Operation\UninstallOperation; use Composer\DependencyResolver\Operation\MarkAliasInstalledOperation; use Composer\DependencyResolver\Operation\MarkAliasUninstalledOperation; +use React\Promise\PromiseInterface; class InstallationManagerMock extends InstallationManager { @@ -64,13 +65,21 @@ class InstallationManagerMock extends InstallationManager return $repo->hasPackage($package); } + /** + * {@inheritdoc} + */ public function install(InstalledRepositoryInterface $repo, InstallOperation $operation) { $this->installed[] = $operation->getPackage(); $this->trace[] = strip_tags((string) $operation); $repo->addPackage(clone $operation->getPackage()); + + return null; } + /** + * {@inheritdoc} + */ public function update(InstalledRepositoryInterface $repo, UpdateOperation $operation) { $this->updated[] = array($operation->getInitialPackage(), $operation->getTargetPackage()); @@ -79,13 +88,20 @@ class InstallationManagerMock extends InstallationManager if (!$repo->hasPackage($operation->getTargetPackage())) { $repo->addPackage(clone $operation->getTargetPackage()); } + + return null; } + /** + * {@inheritdoc} + */ public function uninstall(InstalledRepositoryInterface $repo, UninstallOperation $operation) { $this->uninstalled[] = $operation->getPackage(); $this->trace[] = strip_tags((string) $operation); $repo->removePackage($operation->getPackage()); + + return null; } public function markAliasInstalled(InstalledRepositoryInterface $repo, MarkAliasInstalledOperation $operation)