Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add return types to closures #9

Merged
merged 8 commits into from Feb 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/Composer/Autoload/AutoloadGenerator.php
Expand Up @@ -290,7 +290,7 @@ public function dump(Config $config, InstalledRepositoryInterface $localRepo, Ro
$mainAutoload = $rootPackage->getAutoload();
if ($rootPackage->getTargetDir() && !empty($mainAutoload['psr-0'])) {
$levels = substr_count($filesystem->normalizePath($rootPackage->getTargetDir()), '/') + 1;
$prefixes = implode(', ', array_map(function ($prefix) {
$prefixes = implode(', ', array_map(function ($prefix): string {
return var_export($prefix, true);
}, array_keys($mainAutoload['psr-0'])));
$baseDirFromTargetDirCode = $filesystem->findShortestPathCode($targetDir, $basePath, true);
Expand Down Expand Up @@ -554,7 +554,7 @@ public function parseAutoloads(array $packageMap, PackageInterface $rootPackage,
{
$rootPackageMap = array_shift($packageMap);
if (is_array($filteredDevPackages)) {
$packageMap = array_filter($packageMap, function ($item) use ($filteredDevPackages) {
$packageMap = array_filter($packageMap, function ($item) use ($filteredDevPackages): bool {
return !in_array($item[0]->getName(), $filteredDevPackages, true);
});
} elseif ($filteredDevPackages) {
Expand Down Expand Up @@ -808,7 +808,7 @@ protected function getPlatformCheck(array $packageMap, $checkPlatform, array $de

ksort($requiredExtensions);

$formatToPhpVersionId = function (Bound $bound) {
$formatToPhpVersionId = function (Bound $bound): int {
if ($bound->isZero()) {
return 0;
}
Expand Down Expand Up @@ -1229,7 +1229,7 @@ protected function parseAutoloadsType(array $packageMap, $type, RootPackageInter
$updir = null;
$path = Preg::replaceCallback(
'{^((?:(?:\\\\\\.){1,2}+/)+)}',
function ($matches) use (&$updir) {
function ($matches) use (&$updir): string {
if (isset($matches[1])) {
// undo preg_quote for the matched string
$updir = str_replace('\\.', '.', $matches[1]);
Expand Down Expand Up @@ -1300,7 +1300,7 @@ protected function filterPackageMap(array $packageMap, RootPackageInterface $roo
}
}

$add = function (PackageInterface $package) use (&$add, $packages, &$include, $replacedBy) {
$add = function (PackageInterface $package) use (&$add, $packages, &$include, $replacedBy): void {
foreach ($package->getRequires() as $link) {
$target = $link->getTarget();
if (isset($replacedBy[$target])) {
Expand All @@ -1318,7 +1318,7 @@ protected function filterPackageMap(array $packageMap, RootPackageInterface $roo

return array_filter(
$packageMap,
function ($item) use ($include) {
function ($item) use ($include): bool {
$package = $item[0];
foreach ($package->getNames() as $name) {
if (isset($include[$name])) {
Expand Down
2 changes: 1 addition & 1 deletion src/Composer/Command/ArchiveCommand.php
Expand Up @@ -168,7 +168,7 @@ protected function selectPackage(IOInterface $io, $packageName, $version = null)
if (count($packages) > 1) {
$package = reset($packages);
$io->writeError('<info>Found multiple matches, selected '.$package->getPrettyString().'.</info>');
$io->writeError('Alternatives were '.implode(', ', array_map(function ($p) {
$io->writeError('Alternatives were '.implode(', ', array_map(function ($p): string {
return $p->getPrettyString();
}, $packages)).'.');
$io->writeError('<comment>Please use a more specific constraint to pick a different package.</comment>');
Expand Down
2 changes: 1 addition & 1 deletion src/Composer/Command/BaseDependencyCommand.php
Expand Up @@ -90,7 +90,7 @@ protected function doExecute(InputInterface $input, OutputInterface $output, $in
$needles = array($needle);
if ($inverted) {
foreach ($packages as $package) {
$needles = array_merge($needles, array_map(function (Link $link) {
$needles = array_merge($needles, array_map(function (Link $link): string {
return $link->getTarget();
}, $package->getReplaces()));
}
Expand Down
28 changes: 14 additions & 14 deletions src/Composer/Command/ConfigCommand.php
Expand Up @@ -318,10 +318,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$values = $input->getArgument('setting-value'); // what the user is trying to add/change

$booleanValidator = function ($val) {
$booleanValidator = function ($val): bool {
return in_array($val, array('true', 'false', '1', '0'), true);
};
$booleanNormalizer = function ($val) {
$booleanNormalizer = function ($val): bool {
return $val !== 'false' && (bool) $val;
};

Expand All @@ -331,23 +331,23 @@ protected function execute(InputInterface $input, OutputInterface $output): int
'use-include-path' => array($booleanValidator, $booleanNormalizer),
'use-github-api' => array($booleanValidator, $booleanNormalizer),
'preferred-install' => array(
function ($val) {
function ($val): bool {
return in_array($val, array('auto', 'source', 'dist'), true);
},
function ($val) {
return $val;
},
),
'gitlab-protocol' => array(
function ($val) {
function ($val): bool {
return in_array($val, array('git', 'http', 'https'), true);
},
function ($val) {
return $val;
},
),
'store-auths' => array(
function ($val) {
function ($val): bool {
return in_array($val, array('true', 'false', 'prompt'), true);
},
function ($val) {
Expand Down Expand Up @@ -389,23 +389,23 @@ function ($val) {
'cache-ttl' => array('is_numeric', 'intval'),
'cache-files-ttl' => array('is_numeric', 'intval'),
'cache-files-maxsize' => array(
function ($val) {
function ($val): bool {
return Preg::isMatch('/^\s*([0-9.]+)\s*(?:([kmg])(?:i?b)?)?\s*$/i', $val);
},
function ($val) {
return $val;
},
),
'bin-compat' => array(
function ($val) {
function ($val): bool {
return in_array($val, array('auto', 'full', 'symlink'));
},
function ($val) {
return $val;
},
),
'discard-changes' => array(
function ($val) {
function ($val): bool {
return in_array($val, array('stash', 'true', 'false', '1', '0'), true);
},
function ($val) {
Expand All @@ -427,15 +427,15 @@ function ($val) {
'disable-tls' => array($booleanValidator, $booleanNormalizer),
'secure-http' => array($booleanValidator, $booleanNormalizer),
'cafile' => array(
function ($val) {
function ($val): bool {
return file_exists($val) && Filesystem::isReadable($val);
},
function ($val) {
return $val === 'null' ? null : $val;
},
),
'capath' => array(
function ($val) {
function ($val): bool {
return is_dir($val) && Filesystem::isReadable($val);
},
function ($val) {
Expand All @@ -447,7 +447,7 @@ function ($val) {
'lock' => array($booleanValidator, $booleanNormalizer),
'allow-plugins' => array($booleanValidator, $booleanNormalizer),
'platform-check' => array(
function ($val) {
function ($val): bool {
return in_array($val, array('php-only', 'true', 'false', '1', '0'), true);
},
function ($val) {
Expand All @@ -459,7 +459,7 @@ function ($val) {
},
),
'use-parent-dir' => array(
function ($val) {
function ($val): bool {
return in_array($val, array('true', 'false', 'prompt'), true);
},
function ($val) {
Expand Down Expand Up @@ -593,10 +593,10 @@ function ($vals) {
return $val;
}),
'minimum-stability' => array(
function ($val) {
function ($val): bool {
return isset(BasePackage::$stabilities[VersionParser::normalizeStability($val)]);
},
function ($val) {
function ($val): string {
return VersionParser::normalizeStability($val);
},
),
Expand Down
2 changes: 1 addition & 1 deletion src/Composer/Command/DiagnoseCommand.php
Expand Up @@ -511,7 +511,7 @@ private function outputResult($result): void
private function checkPlatform()
{
$output = '';
$out = function ($msg, $style) use (&$output) {
$out = function ($msg, $style) use (&$output): void {
$output .= '<'.$style.'>'.$msg.'</'.$style.'>'.PHP_EOL;
};

Expand Down
2 changes: 1 addition & 1 deletion src/Composer/Command/InitCommand.php
Expand Up @@ -511,7 +511,7 @@ public function namespaceFromPackageName($packageName)
}

$namespace = array_map(
function ($part) {
function ($part): string {
$part = Preg::replace('/[^a-z0-9]/i', ' ', $part);
$part = ucwords($part);

Expand Down
2 changes: 1 addition & 1 deletion src/Composer/Command/LicensesCommand.php
Expand Up @@ -170,7 +170,7 @@ private function filterRequiredPackages(RepositoryInterface $repo, PackageInterf
$packageListNames = array_keys($bucket);
$packages = array_filter(
$repo->getPackages(),
function ($package) use ($requires, $packageListNames) {
function ($package) use ($requires, $packageListNames): bool {
return in_array($package->getName(), $requires) && !in_array($package->getName(), $packageListNames);
}
);
Expand Down
2 changes: 1 addition & 1 deletion src/Composer/Command/ReinstallCommand.php
Expand Up @@ -123,7 +123,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$installOrder[$op->getPackage()->getName()] = $index;
}
}
usort($uninstallOperations, function ($a, $b) use ($installOrder) {
usort($uninstallOperations, function ($a, $b) use ($installOrder): int {
return $installOrder[$b->getPackage()->getName()] - $installOrder[$a->getPackage()->getName()];
});

Expand Down
2 changes: 1 addition & 1 deletion src/Composer/Command/RemoveCommand.php
Expand Up @@ -116,7 +116,7 @@ protected function interact(InputInterface $input, OutputInterface $output)

if (count($input->getArgument('packages')) === 0) {
$this->getIO()->writeError('<info>No unused packages to remove</info>');
$this->setCode(function () {
$this->setCode(function (): int {
return 0;
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/Composer/Command/SelfUpdateCommand.php
Expand Up @@ -354,7 +354,7 @@ protected function fetchKeys(IOInterface $io, Config $config): void

$io->write('Open <info>https://composer.github.io/pubkeys.html</info> to find the latest keys');

$validator = function ($value) {
$validator = function ($value): string {
if (!Preg::isMatch('{^-----BEGIN PUBLIC KEY-----$}', trim($value))) {
throw new \UnexpectedValueException('Invalid input');
}
Expand Down
4 changes: 2 additions & 2 deletions src/Composer/Command/ShowCommand.php
Expand Up @@ -223,7 +223,7 @@ protected function execute(InputInterface $input, OutputInterface $output)

if ($input->getOption('no-dev')) {
$packages = $this->filterRequiredPackages($installedRepo, $rootPkg);
$repos = $installedRepo = new InstalledRepository(array(new InstalledArrayRepository(array_map(function ($pkg) {
$repos = $installedRepo = new InstalledRepository(array(new InstalledArrayRepository(array_map(function ($pkg): \Composer\Package\PackageInterface {
return clone $pkg;
}, $packages))));
}
Expand Down Expand Up @@ -320,7 +320,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
if ($input->getOption('tree')) {
$rootRequires = $this->getRootRequires();
$packages = $installedRepo->getPackages();
usort($packages, function (BasePackage $a, BasePackage $b) {
usort($packages, function (BasePackage $a, BasePackage $b): int {
return strcmp((string) $a, (string) $b);
});
$arrayTree = array();
Expand Down
4 changes: 2 additions & 2 deletions src/Composer/Command/StatusCommand.php
Expand Up @@ -163,7 +163,7 @@ private function doExecute(InputInterface $input): int

foreach ($errors as $path => $changes) {
if ($input->getOption('verbose')) {
$indentedChanges = implode("\n", array_map(function ($line) {
$indentedChanges = implode("\n", array_map(function ($line): string {
return ' ' . ltrim($line);
}, explode("\n", $changes)));
$io->write('<info>'.$path.'</info>:');
Expand All @@ -179,7 +179,7 @@ private function doExecute(InputInterface $input): int

foreach ($unpushedChanges as $path => $changes) {
if ($input->getOption('verbose')) {
$indentedChanges = implode("\n", array_map(function ($line) {
$indentedChanges = implode("\n", array_map(function ($line): string {
return ' ' . ltrim($line);
}, explode("\n", $changes)));
$io->write('<info>'.$path.'</info>:');
Expand Down
4 changes: 2 additions & 2 deletions src/Composer/Command/UpdateCommand.php
Expand Up @@ -129,7 +129,7 @@ protected function execute(InputInterface $input, OutputInterface $output)

// extract --with shorthands from the allowlist
if (count($packages) > 0) {
$allowlistPackagesWithRequirements = array_filter($packages, function ($pkg) {
$allowlistPackagesWithRequirements = array_filter($packages, function ($pkg): bool {
return Preg::isMatch('{\S+[ =:]\S+}', $pkg);
});
foreach ($this->formatRequirements($allowlistPackagesWithRequirements) as $package => $constraint) {
Expand Down Expand Up @@ -180,7 +180,7 @@ protected function execute(InputInterface $input, OutputInterface $output)

// the arguments lock/nothing/mirrors are not package names but trigger a mirror update instead
// they are further mutually exclusive with listing actual package names
$filteredPackages = array_filter($packages, function ($package) {
$filteredPackages = array_filter($packages, function ($package): bool {
return !in_array($package, array('lock', 'nothing', 'mirrors'), true);
});
$updateMirrors = $input->getOption('lock') || count($filteredPackages) != count($packages);
Expand Down
6 changes: 3 additions & 3 deletions src/Composer/Command/ValidateCommand.php
Expand Up @@ -193,13 +193,13 @@ private function outputResult(IOInterface $io, $name, &$errors, &$warnings, $che
}

if ($errors) {
$errors = array_map(function ($err) {
$errors = array_map(function ($err): string {
return '- ' . $err;
}, $errors);
array_unshift($errors, '# General errors');
}
if ($warnings) {
$warnings = array_map(function ($err) {
$warnings = array_map(function ($err): string {
return '- ' . $err;
}, $warnings);
array_unshift($warnings, '# General warnings');
Expand All @@ -210,7 +210,7 @@ private function outputResult(IOInterface $io, $name, &$errors, &$warnings, $che

// If checking publish errors, display them as errors, otherwise just show them as warnings
if ($publishErrors) {
$publishErrors = array_map(function ($err) {
$publishErrors = array_map(function ($err): string {
return '- ' . $err;
}, $publishErrors);

Expand Down
2 changes: 1 addition & 1 deletion src/Composer/Compiler.php
Expand Up @@ -82,7 +82,7 @@ public function compile($pharFile = 'composer.phar'): void

$phar->startBuffering();

$finderSort = function ($a, $b) {
$finderSort = function ($a, $b): int {
return strcmp(strtr($a->getRealPath(), '\\', '/'), strtr($b->getRealPath(), '\\', '/'));
};

Expand Down