1
0
Fork 0

Reaching phpstan level 6 in EventDispatcher Exception and Installer folders (#10192)

pull/10211/head
Paolo Rossi 2021-10-25 10:44:29 +02:00 committed by GitHub
parent fa4d4e20e9
commit a921d9b233
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 172 additions and 20 deletions

View File

@ -95,6 +95,8 @@ class Event
/**
* Prevents the event from being passed to further listeners
*
* @return void
*/
public function stopPropagation()
{

View File

@ -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<int, mixed> $additionalArgs Arguments passed by the user
* @param array<string, mixed> $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<callable|string> 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';

View File

@ -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);

View File

@ -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()) {

View File

@ -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);

View File

@ -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)
{

View File

@ -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, '\\', '/'), '/').'/';

View File

@ -44,7 +44,7 @@ class SuggestedPackagesReporter
}
/**
* @return array Suggested packages with source, target and reason keys.
* @return array<array{source: string, target: string, reason: string}> Suggested packages with source, target and reason keys.
*/
public function getPackages()
{

View File

@ -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)