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 native return types where possible #10547

Merged
merged 9 commits into from Feb 21, 2022
  •  
  •  
  •  
1 change: 0 additions & 1 deletion .php-cs-fixer.php
Expand Up @@ -71,7 +71,6 @@
'random_api_migration' => true,
'ternary_to_null_coalescing' => true,
//'declare_strict_types' => true,
//'void_return' => true,
])
->setUsingCache(true)
->setRiskyAllowed(true)
Expand Down
6 changes: 1 addition & 5 deletions phpstan/config.neon
Expand Up @@ -31,15 +31,11 @@ parameters:
- '~^Undefined variable: \$vendorDir$~'
- '~^Undefined variable: \$baseDir$~'

# variable defined in eval
- '~^Undefined variable: \$res$~'

# we don't have different constructors for parent/child
- '~^Unsafe usage of new static\(\)\.$~'

# Ignore some irrelevant errors in test files
- '~Method Composer\\Test\\[^:]+::(setUp(BeforeClass)?|tearDown(AfterClass)?|test[^(]+)\(\) has no return type specified.~'
- '~Method Composer\\Test\\[^:]+::(data\w+|provide\w+|\w+?Provider)\(\) has no return type specified.~'
- '~Method Composer\\Test\\[^:]+::(data\w+|provide\w+|\w+?Provider)\(\) (has no return type specified.|return type has no value type specified in iterable type array.)~'

# PHPUnit assertions as instance methods
- '~Dynamic call to static method PHPUnit\\Framework\\Assert::\w+\(\)~'
Expand Down
16 changes: 8 additions & 8 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 @@ -441,7 +441,7 @@ public static function autoload(\$class)
* @param array<string, true> $scannedFiles
* @return array<class-string, string>
*/
private function addClassMapCode(Filesystem $filesystem, $basePath, $vendorPath, $dir, $excluded, $namespaceFilter, $autoloadType, array $classMap, array &$ambiguousClasses, array &$scannedFiles)
private function addClassMapCode(Filesystem $filesystem, $basePath, $vendorPath, $dir, $excluded, $namespaceFilter, $autoloadType, array $classMap, array &$ambiguousClasses, array &$scannedFiles): array
{
foreach ($this->generateClassMap($dir, $excluded, $namespaceFilter, $autoloadType, true, $scannedFiles) as $class => $path) {
$pathCode = $this->getPathCode($filesystem, $basePath, $vendorPath, $path).",\n";
Expand All @@ -464,7 +464,7 @@ private function addClassMapCode(Filesystem $filesystem, $basePath, $vendorPath,
* @param array<string, true> $scannedFiles
* @return array<class-string, string>
*/
private function generateClassMap($dir, $excluded, $namespaceFilter, $autoloadType, $showAmbiguousWarning, array &$scannedFiles)
private function generateClassMap($dir, $excluded, $namespaceFilter, $autoloadType, $showAmbiguousWarning, array &$scannedFiles): array
{
if ($excluded) {
// filter excluded patterns here to only use those matching $dir
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
10 changes: 5 additions & 5 deletions src/Composer/Autoload/ClassMapGenerator.php
Expand Up @@ -38,7 +38,7 @@ class ClassMapGenerator
* @param string $file The name of the class map file
* @return void
*/
public static function dump($dirs, $file)
public static function dump($dirs, $file): void
{
$maps = array();

Expand All @@ -61,7 +61,7 @@ public static function dump($dirs, $file)
* @return array<class-string, string> A class map array
* @throws \RuntimeException When the path is neither an existing file nor directory
*/
public static function createMap($path, $excluded = null, IOInterface $io = null, $namespace = null, $autoloadType = null, &$scannedFiles = array())
public static function createMap($path, $excluded = null, IOInterface $io = null, $namespace = null, $autoloadType = null, &$scannedFiles = array()): array
{
$basePath = $path;
if (is_string($path)) {
Expand Down Expand Up @@ -157,7 +157,7 @@ public static function createMap($path, $excluded = null, IOInterface $io = null
* @param ?IOInterface $io IO object
* @return array<int, class-string> valid classes
*/
private static function filterByNamespace($classes, $filePath, $baseNamespace, $namespaceType, $basePath, $io)
private static function filterByNamespace($classes, $filePath, $baseNamespace, $namespaceType, $basePath, $io): array
{
$validClasses = array();
$rejectedClasses = array();
Expand Down Expand Up @@ -215,7 +215,7 @@ private static function filterByNamespace($classes, $filePath, $baseNamespace, $
* @throws \RuntimeException
* @return array<int, class-string> The found classes
*/
private static function findClasses($path)
private static function findClasses($path): array
{
$extraTypes = self::getExtraTypes();

Expand Down Expand Up @@ -296,7 +296,7 @@ private static function findClasses($path)
/**
* @return string
*/
private static function getExtraTypes()
private static function getExtraTypes(): string
{
static $extraTypes = null;

Expand Down
18 changes: 9 additions & 9 deletions src/Composer/Autoload/PhpFileCleaner.php
Expand Up @@ -51,7 +51,7 @@ class PhpFileCleaner
* @param string[] $types
* @return void
*/
public static function setTypeConfig($types)
public static function setTypeConfig($types): void
{
foreach ($types as $type) {
self::$typeConfig[$type[0]] = array(
Expand All @@ -78,7 +78,7 @@ public function __construct($contents, $maxMatches)
/**
* @return string
*/
public function clean()
public function clean(): string
{
$clean = '';

Expand Down Expand Up @@ -152,7 +152,7 @@ public function clean()
/**
* @return void
*/
private function skipToPhp()
private function skipToPhp(): void
{
while ($this->index < $this->len) {
if ($this->contents[$this->index] === '<' && $this->peek('?')) {
Expand All @@ -168,7 +168,7 @@ private function skipToPhp()
* @param string $delimiter
* @return void
*/
private function skipString($delimiter)
private function skipString($delimiter): void
{
$this->index += 1;
while ($this->index < $this->len) {
Expand All @@ -187,7 +187,7 @@ private function skipString($delimiter)
/**
* @return void
*/
private function skipComment()
private function skipComment(): void
{
$this->index += 2;
while ($this->index < $this->len) {
Expand All @@ -203,7 +203,7 @@ private function skipComment()
/**
* @return void
*/
private function skipToNewline()
private function skipToNewline(): void
{
while ($this->index < $this->len) {
if ($this->contents[$this->index] === "\r" || $this->contents[$this->index] === "\n") {
Expand All @@ -217,7 +217,7 @@ private function skipToNewline()
* @param string $delimiter
* @return void
*/
private function skipHeredoc($delimiter)
private function skipHeredoc($delimiter): void
{
$firstDelimiterChar = $delimiter[0];
$delimiterLength = \strlen($delimiter);
Expand Down Expand Up @@ -260,7 +260,7 @@ private function skipHeredoc($delimiter)
* @param string $char
* @return bool
*/
private function peek($char)
private function peek($char): bool
{
return $this->index + 1 < $this->len && $this->contents[$this->index + 1] === $char;
}
Expand All @@ -270,7 +270,7 @@ private function peek($char)
* @param ?array<int, string> $match
* @return bool
*/
private function match($regex, array &$match = null)
private function match($regex, array &$match = null): bool
{
return Preg::isMatch($regex, $this->contents, $match, 0, $this->index);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Composer/Command/AboutCommand.php
Expand Up @@ -24,7 +24,7 @@ class AboutCommand extends BaseCommand
/**
* @return void
*/
protected function configure()
protected function configure(): void
{
$this
->setName('about')
Expand Down
4 changes: 2 additions & 2 deletions src/Composer/Command/ArchiveCommand.php
Expand Up @@ -41,7 +41,7 @@ class ArchiveCommand extends BaseCommand
/**
* @return void
*/
protected function configure()
protected function configure(): void
{
$this
->setName('archive')
Expand Down 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
12 changes: 6 additions & 6 deletions src/Composer/Command/BaseDependencyCommand.php
Expand Up @@ -50,7 +50,7 @@ class BaseDependencyCommand extends BaseCommand
* @param bool $inverted Whether to invert matching process (why-not vs why behaviour)
* @return int Exit code of the operation.
*/
protected function doExecute(InputInterface $input, OutputInterface $output, $inverted = false)
protected function doExecute(InputInterface $input, OutputInterface $output, $inverted = false): int
{
// Emit command event on startup
$composer = $this->requireComposer();
Expand Down 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 Expand Up @@ -136,7 +136,7 @@ protected function doExecute(InputInterface $input, OutputInterface $output, $in
*
* @return void
*/
protected function printTable(OutputInterface $output, $results)
protected function printTable(OutputInterface $output, $results): void
{
$table = array();
$doubles = array();
Expand Down Expand Up @@ -172,7 +172,7 @@ protected function printTable(OutputInterface $output, $results)
*
* @return void
*/
protected function initStyles(OutputInterface $output)
protected function initStyles(OutputInterface $output): void
{
$this->colors = array(
'green',
Expand All @@ -197,7 +197,7 @@ protected function initStyles(OutputInterface $output)
*
* @return void
*/
protected function printTree($results, $prefix = '', $level = 1)
protected function printTree($results, $prefix = '', $level = 1): void
{
$count = count($results);
$idx = 0;
Expand All @@ -223,7 +223,7 @@ protected function printTree($results, $prefix = '', $level = 1)
*
* @return void
*/
private function writeTreeLine($line)
private function writeTreeLine($line): void
{
$io = $this->getIO();
if (!$io->isDecorated()) {
Expand Down
4 changes: 2 additions & 2 deletions src/Composer/Command/CheckPlatformReqsCommand.php
Expand Up @@ -26,7 +26,7 @@ class CheckPlatformReqsCommand extends BaseCommand
/**
* @return void
*/
protected function configure()
protected function configure(): void
{
$this->setName('check-platform-reqs')
->setDescription('Check that platform requirements are satisfied.')
Expand Down Expand Up @@ -174,7 +174,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
*
* @return void
*/
protected function printTable(OutputInterface $output, $results)
protected function printTable(OutputInterface $output, $results): void
{
$rows = array();
foreach ($results as $result) {
Expand Down
2 changes: 1 addition & 1 deletion src/Composer/Command/ClearCacheCommand.php
Expand Up @@ -25,7 +25,7 @@ class ClearCacheCommand extends BaseCommand
/**
* @return void
*/
protected function configure()
protected function configure(): void
{
$this
->setName('clear-cache')
Expand Down