From 9599eb613b89980c72c89e2b1c2fecd725aa9047 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Sat, 16 Oct 2021 11:13:29 +0200 Subject: [PATCH] Type annotations --- src/Composer/Autoload/PhpFileCleaner.php | 9 +++++ src/Composer/Cache.php | 38 +++++++++++++++++++ src/Composer/Command/ArchiveCommand.php | 6 ++- src/Composer/Command/ConfigCommand.php | 9 ----- src/Composer/Command/CreateProjectCommand.php | 6 +++ src/Composer/Command/DiagnoseCommand.php | 37 +++++++++++++++--- src/Composer/Command/HomeCommand.php | 7 +++- src/Composer/Command/InitCommand.php | 29 +++++++++++++- src/Composer/Command/RequireCommand.php | 3 ++ src/Composer/Command/RunScriptCommand.php | 3 ++ src/Composer/Command/SelfUpdateCommand.php | 9 +++++ src/Composer/Command/UpdateCommand.php | 3 ++ .../Test/Autoload/AutoloadGeneratorTest.php | 3 ++ 13 files changed, 145 insertions(+), 17 deletions(-) diff --git a/src/Composer/Autoload/PhpFileCleaner.php b/src/Composer/Autoload/PhpFileCleaner.php index 9b0872aad..4d8944e00 100644 --- a/src/Composer/Autoload/PhpFileCleaner.php +++ b/src/Composer/Autoload/PhpFileCleaner.php @@ -54,6 +54,9 @@ class PhpFileCleaner $this->maxMatches = $maxMatches; } + /** + * @return string + */ public function clean() { $clean = ''; @@ -212,11 +215,17 @@ class PhpFileCleaner } } + /** + * @return bool + */ private function peek($char) { return $this->index + 1 < $this->len && $this->contents[$this->index + 1] === $char; } + /** + * @return bool + */ private function match($regex, array &$match = null) { if (\preg_match($regex, $this->contents, $match, 0, $this->index)) { diff --git a/src/Composer/Cache.php b/src/Composer/Cache.php index 8ee724357..b6fcf17e7 100644 --- a/src/Composer/Cache.php +++ b/src/Composer/Cache.php @@ -75,11 +75,17 @@ class Cache return $this->readOnly; } + /** + * @return bool + */ public static function isUsable($path) { return !preg_match('{(^|[\\\\/])(\$null|nul|NUL|/dev/null)([\\\\/]|$)}', $path); } + /** + * @return bool + */ public function isEnabled() { if ($this->enabled === null) { @@ -97,11 +103,17 @@ class Cache return $this->enabled; } + /** + * @return string + */ public function getRoot() { return $this->root; } + /** + * @return string|false + */ public function read($file) { if ($this->isEnabled()) { @@ -116,6 +128,9 @@ class Cache return false; } + /** + * @return bool + */ public function write($file, $contents) { if ($this->isEnabled() && !$this->readOnly) { @@ -154,6 +169,7 @@ class Cache /** * Copy a file into the cache + * @return bool */ public function copyFrom($file, $source) { @@ -175,6 +191,7 @@ class Cache /** * Copy a file out of the cache + * @return bool */ public function copyTo($file, $target) { @@ -198,6 +215,9 @@ class Cache return false; } + /** + * @return bool + */ public function gcIsNecessary() { if (self::$cacheCollected) { @@ -216,6 +236,9 @@ class Cache return !mt_rand(0, 50); } + /** + * @return bool + */ public function remove($file) { if ($this->isEnabled()) { @@ -228,6 +251,9 @@ class Cache return false; } + /** + * @return bool + */ public function clear() { if ($this->isEnabled()) { @@ -239,6 +265,9 @@ class Cache return false; } + /** + * @return bool + */ public function gc($ttl, $maxSize) { if ($this->isEnabled()) { @@ -269,6 +298,9 @@ class Cache return false; } + /** + * @return string|false + */ public function sha1($file) { if ($this->isEnabled()) { @@ -281,6 +313,9 @@ class Cache return false; } + /** + * @return string|false + */ public function sha256($file) { if ($this->isEnabled()) { @@ -293,6 +328,9 @@ class Cache return false; } + /** + * @return Finder + */ protected function getFinder() { return Finder::create()->in($this->root)->files(); diff --git a/src/Composer/Command/ArchiveCommand.php b/src/Composer/Command/ArchiveCommand.php index 3acf4fd04..379781a68 100644 --- a/src/Composer/Command/ArchiveCommand.php +++ b/src/Composer/Command/ArchiveCommand.php @@ -16,6 +16,7 @@ use Composer\Factory; use Composer\IO\IOInterface; use Composer\Config; use Composer\Composer; +use Composer\Package\BasePackage; use Composer\Package\CompletePackageInterface; use Composer\Repository\CompositeRepository; use Composer\Repository\RepositoryFactory; @@ -108,6 +109,9 @@ EOT return $returnCode; } + /** + * @return int + */ protected function archive(IOInterface $io, Config $config, $packageName = null, $version = null, $format = 'tar', $dest = '.', $fileName = null, $ignoreFilters = false, Composer $composer = null) { if ($composer) { @@ -142,7 +146,7 @@ EOT } /** - * @return CompletePackageInterface|false + * @return BasePackage&CompletePackageInterface|false */ protected function selectPackage(IOInterface $io, $packageName, $version = null) { diff --git a/src/Composer/Command/ConfigCommand.php b/src/Composer/Command/ConfigCommand.php index 39b4604d3..c96597dce 100644 --- a/src/Composer/Command/ConfigCommand.php +++ b/src/Composer/Command/ConfigCommand.php @@ -58,9 +58,6 @@ class ConfigCommand extends BaseCommand */ protected $authConfigSource; - /** - * {@inheritDoc} - */ protected function configure() { $this @@ -148,9 +145,6 @@ EOT ; } - /** - * {@inheritDoc} - */ protected function initialize(InputInterface $input, OutputInterface $output) { parent::initialize($input, $output); @@ -204,9 +198,6 @@ EOT } } - /** - * {@inheritDoc} - */ protected function execute(InputInterface $input, OutputInterface $output) { // Open file in editor diff --git a/src/Composer/Command/CreateProjectCommand.php b/src/Composer/Command/CreateProjectCommand.php index b64b01e18..e47945466 100644 --- a/src/Composer/Command/CreateProjectCommand.php +++ b/src/Composer/Command/CreateProjectCommand.php @@ -159,6 +159,9 @@ EOT ); } + /** + * @return int + */ public function installProject(IOInterface $io, Config $config, InputInterface $input, $packageName, $directory = null, $packageVersion = null, $stability = 'stable', $preferSource = false, $preferDist = false, $installDevPackages = false, $repositories = null, $disablePlugins = false, $noScripts = false, $noProgress = false, $noInstall = false, $ignorePlatformReqs = false, $secureHttp = true, $addRepository = false) { $oldCwd = getcwd(); @@ -304,6 +307,9 @@ EOT return 0; } + /** + * @return bool + */ protected function installRootPackage(IOInterface $io, Config $config, $packageName, $directory = null, $packageVersion = null, $stability = 'stable', $preferSource = false, $preferDist = false, $installDevPackages = false, array $repositories = null, $disablePlugins = false, $noScripts = false, $noProgress = false, $ignorePlatformReqs = false, $secureHttp = true) { if (!$secureHttp) { diff --git a/src/Composer/Command/DiagnoseCommand.php b/src/Composer/Command/DiagnoseCommand.php index 4f5eefeb3..b788c5e0f 100644 --- a/src/Composer/Command/DiagnoseCommand.php +++ b/src/Composer/Command/DiagnoseCommand.php @@ -65,9 +65,6 @@ EOT ; } - /** - * {@inheritdoc} - */ protected function execute(InputInterface $input, OutputInterface $output) { $composer = $this->getComposer(false); @@ -195,6 +192,9 @@ EOT return $this->exitCode; } + /** + * @return string|true + */ private function checkComposerSchema() { $validator = new ConfigValidator($this->getIO()); @@ -219,6 +219,9 @@ EOT return true; } + /** + * @return string|true + */ private function checkGit() { if (!function_exists('proc_open')) { @@ -233,6 +236,9 @@ EOT return true; } + /** + * @return string|string[]|true + */ private function checkHttp($proto, Config $config) { $result = $this->checkConnectivity(); @@ -268,6 +274,9 @@ EOT return true; } + /** + * @return string|true|\Exception + */ private function checkHttpProxy() { $result = $this->checkConnectivity(); @@ -293,6 +302,9 @@ EOT return true; } + /** + * @return string|true|\Exception + */ private function checkGithubOauth($domain, $token) { $result = $this->checkConnectivity(); @@ -322,7 +334,7 @@ EOT * @param string $domain * @param string $token * @throws TransportException - * @return array|string + * @return mixed|string */ private function getGithubRateLimit($domain, $token = null) { @@ -341,6 +353,9 @@ EOT return $data['resources']['core']; } + /** + * @return string|true + */ private function checkDiskSpace($config) { $minSpaceFree = 1024 * 1024; @@ -353,6 +368,9 @@ EOT return true; } + /** + * @return string[]|true + */ private function checkPubKeys($config) { $home = $config->get('home'); @@ -382,6 +400,9 @@ EOT return $errors ?: true; } + /** + * @return string|\Exception|true + */ private function checkVersion($config) { $result = $this->checkConnectivity(); @@ -403,6 +424,9 @@ EOT return true; } + /** + * @return string + */ private function getCurlVersion() { if (extension_loaded('curl')) { @@ -469,6 +493,9 @@ EOT } } + /** + * @return string|true + */ private function checkPlatform() { $output = ''; @@ -699,7 +726,7 @@ EOT /** * Check if allow_url_fopen is ON * - * @return true|string + * @return string|true */ private function checkConnectivity() { diff --git a/src/Composer/Command/HomeCommand.php b/src/Composer/Command/HomeCommand.php index 1a228b871..62c52e923 100644 --- a/src/Composer/Command/HomeCommand.php +++ b/src/Composer/Command/HomeCommand.php @@ -97,6 +97,9 @@ EOT return $return; } + /** + * @return bool + */ private function handlePackage(CompletePackageInterface $package, $showHomepage, $showOnly) { $support = $package->getSupport(); @@ -122,6 +125,7 @@ EOT * opens a url in your system default browser * * @param string $url + * @return void */ private function openBrowser($url) { @@ -129,7 +133,8 @@ EOT $process = new ProcessExecutor($this->getIO()); if (Platform::isWindows()) { - return $process->execute('start "web" explorer ' . $url, $output); + $process->execute('start "web" explorer ' . $url, $output); + return; } $linux = $process->execute('which xdg-open', $output); diff --git a/src/Composer/Command/InitCommand.php b/src/Composer/Command/InitCommand.php index c8888f07e..469df7ffb 100644 --- a/src/Composer/Command/InitCommand.php +++ b/src/Composer/Command/InitCommand.php @@ -461,7 +461,7 @@ EOT /** * @private * @param string $author - * @return array + * @return array{name: string, email: string} */ public function parseAuthorString($author) { @@ -485,6 +485,9 @@ EOT return $this->getRepos()->search($name); } + /** + * @return CompositeRepository + */ protected function getRepos() { if (!$this->repos) { @@ -663,6 +666,9 @@ EOT return $requires; } + /** + * @return array + */ protected function formatAuthors($author) { return array($this->parseAuthorString($author)); @@ -775,6 +781,9 @@ EOT file_put_contents($ignoreFile, $contents . $vendor. "\n"); } + /** + * @return bool + */ protected function isValidEmail($email) { // assume it's valid if we can't validate it @@ -790,6 +799,9 @@ EOT return false !== filter_var($email, FILTER_VALIDATE_EMAIL); } + /** + * @return RepositorySet + */ private function getRepositorySet(InputInterface $input, $minimumStability = null) { $key = $minimumStability ?: 'default'; @@ -802,6 +814,9 @@ EOT return $this->repositorySets[$key]; } + /** + * @return string + */ private function getMinimumStability(InputInterface $input) { if ($input->hasOption('stability')) { @@ -936,6 +951,9 @@ EOT ); } + /** + * @return string + */ private function getPlatformExceptionDetails(PackageInterface $candidate, PlatformRepository $platformRepo = null) { $details = array(); @@ -993,6 +1011,9 @@ EOT return array_keys(array_slice($similarPackages, 0, 5)); } + /** + * @return void + */ private function updateDependencies($output) { try { @@ -1004,6 +1025,9 @@ EOT } } + /** + * @return void + */ private function runDumpAutoloadCommand($output) { try { @@ -1015,6 +1039,9 @@ EOT } } + /** + * @return bool + */ private function hasDependencies($options) { $requires = (array) $options['require']; diff --git a/src/Composer/Command/RequireCommand.php b/src/Composer/Command/RequireCommand.php index ad65420af..9388af63d 100644 --- a/src/Composer/Command/RequireCommand.php +++ b/src/Composer/Command/RequireCommand.php @@ -417,6 +417,9 @@ EOT return $status; } + /** + * @return bool + */ private function updateFileCleanly($json, array $new, $requireKey, $removeKey, $sortPackages) { $contents = file_get_contents($json->getPath()); diff --git a/src/Composer/Command/RunScriptCommand.php b/src/Composer/Command/RunScriptCommand.php index 81b625786..052139f8b 100644 --- a/src/Composer/Command/RunScriptCommand.php +++ b/src/Composer/Command/RunScriptCommand.php @@ -109,6 +109,9 @@ EOT return $composer->getEventDispatcher()->dispatchScript($script, $devMode, $args); } + /** + * @return int + */ protected function listScripts(OutputInterface $output) { $scripts = $this->getComposer()->getPackage()->getScripts(); diff --git a/src/Composer/Command/SelfUpdateCommand.php b/src/Composer/Command/SelfUpdateCommand.php index 70e65d252..bfa1bd481 100644 --- a/src/Composer/Command/SelfUpdateCommand.php +++ b/src/Composer/Command/SelfUpdateCommand.php @@ -369,6 +369,9 @@ TAGSPUBKEY $io->write('Public keys stored in '.$config->get('home')); } + /** + * @return int + */ protected function rollback(OutputInterface $output, $rollbackDir, $localFilename) { $rollbackVersion = $this->getLastBackupVersion($rollbackDir); @@ -464,6 +467,9 @@ TAGSPUBKEY } } + /** + * @return string|false + */ protected function getLastBackupVersion($rollbackDir) { $finder = $this->getOldInstallationFinder($rollbackDir); @@ -477,6 +483,9 @@ TAGSPUBKEY return false; } + /** + * @return Finder + */ protected function getOldInstallationFinder($rollbackDir) { return Finder::create() diff --git a/src/Composer/Command/UpdateCommand.php b/src/Composer/Command/UpdateCommand.php index 0fe582299..7c2a7dcee 100644 --- a/src/Composer/Command/UpdateCommand.php +++ b/src/Composer/Command/UpdateCommand.php @@ -304,6 +304,9 @@ EOT throw new \RuntimeException('Installation aborted.'); } + /** + * @return Link + */ private function appendConstraintToLink(Link $link, $constraint) { $parser = new VersionParser; diff --git a/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php b/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php index 0e571f81a..521e762fb 100644 --- a/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php +++ b/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php @@ -1737,6 +1737,9 @@ EOF; } } + /** + * @return array + */ public function platformCheckProvider() { $versionParser = new VersionParser();