From 23d1030c7338aaef41bf62db9f8a3c887b3c2229 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Thu, 14 Nov 2024 11:54:11 +0100 Subject: [PATCH] phpstan type fixes --- src/Composer/Autoload/AutoloadGenerator.php | 4 ++-- src/Composer/Command/BaseDependencyCommand.php | 12 ++++++------ src/Composer/Repository/InstalledRepository.php | 9 +++++---- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/Composer/Autoload/AutoloadGenerator.php b/src/Composer/Autoload/AutoloadGenerator.php index 44431128f..6db30b8fd 100644 --- a/src/Composer/Autoload/AutoloadGenerator.php +++ b/src/Composer/Autoload/AutoloadGenerator.php @@ -665,11 +665,11 @@ EOF; foreach ($package->getIncludePaths() as $includePath) { $includePath = trim($includePath, '/'); - $includePaths[] = empty($installPath) ? $includePath : $installPath.'/'.$includePath; + $includePaths[] = $installPath === '' ? $includePath : $installPath.'/'.$includePath; } } - if (!$includePaths) { + if (\count($includePaths) === 0) { return null; } diff --git a/src/Composer/Command/BaseDependencyCommand.php b/src/Composer/Command/BaseDependencyCommand.php index bb2a64233..55b502e03 100644 --- a/src/Composer/Command/BaseDependencyCommand.php +++ b/src/Composer/Command/BaseDependencyCommand.php @@ -197,9 +197,9 @@ abstract class BaseDependencyCommand extends BaseCommand /** * Assembles and prints a bottom-up table of the dependencies. * - * @param array{PackageInterface, Link, mixed}[] $results + * @param array{PackageInterface, Link, array|false}[] $results */ - protected function printTable(OutputInterface $output, $results): void + protected function printTable(OutputInterface $output, array $results): void { $table = []; $doubles = []; @@ -221,13 +221,13 @@ abstract class BaseDependencyCommand extends BaseCommand $packageUrl = PackageInfo::getViewSourceOrHomepageUrl($package); $nameWithLink = $packageUrl !== null ? '' . $package->getPrettyName() . '' : $package->getPrettyName(); $rows[] = [$nameWithLink, $version, $link->getDescription(), sprintf('%s (%s)', $link->getTarget(), $link->getPrettyConstraint())]; - if ($children) { + if (is_array($children)) { $queue = array_merge($queue, $children); } } $results = $queue; $table = array_merge($rows, $table); - } while (!empty($results)); + } while (\count($results) > 0); $this->renderTable($table, $output); } @@ -254,7 +254,7 @@ abstract class BaseDependencyCommand extends BaseCommand /** * Recursively prints a tree of the selected results. * - * @param array{PackageInterface, Link, mixed[]|bool}[] $results Results to be printed at this level. + * @param array{PackageInterface, Link, array|false}[] $results Results to be printed at this level. * @param string $prefix Prefix of the current tree level. * @param int $level Current level of recursion. */ @@ -275,7 +275,7 @@ abstract class BaseDependencyCommand extends BaseCommand $linkText = sprintf('%s <%s>%s %s', $link->getDescription(), $prevColor, $link->getTarget(), $link->getPrettyConstraint()); $circularWarn = $children === false ? '(circular dependency aborted here)' : ''; $this->writeTreeLine(rtrim(sprintf("%s%s%s (%s) %s", $prefix, $isLast ? '└──' : '├──', $packageText, $linkText, $circularWarn))); - if ($children) { + if (is_array($children)) { $this->printTree($children, $prefix . ($isLast ? ' ' : '│ '), $level + 1); } } diff --git a/src/Composer/Repository/InstalledRepository.php b/src/Composer/Repository/InstalledRepository.php index e3b52079e..3520fdec6 100644 --- a/src/Composer/Repository/InstalledRepository.php +++ b/src/Composer/Repository/InstalledRepository.php @@ -84,7 +84,7 @@ class InstalledRepository extends CompositeRepository * @param string[] $packagesFound Used internally when recurring * * @return array[] An associative array of arrays as described above. - * @phpstan-return array + * @phpstan-return array|false}> */ public function getDependents($needle, ?ConstraintInterface $constraint = null, bool $invert = false, bool $recurse = true, ?array $packagesFound = null): array { @@ -137,6 +137,7 @@ class InstalledRepository extends CompositeRepository } } } + unset($needle); } // Require-dev is only relevant for the root package @@ -163,7 +164,7 @@ class InstalledRepository extends CompositeRepository } // When inverting, we need to check for conflicts of the needles against installed packages - if ($invert && in_array($package->getName(), $needles)) { + if ($invert && in_array($package->getName(), $needles, true)) { foreach ($package->getConflicts() as $link) { foreach ($this->findPackages($link->getTarget()) as $pkg) { $version = new Constraint('=', $pkg->getVersion()); @@ -176,7 +177,7 @@ class InstalledRepository extends CompositeRepository // List conflicts against X as they may explain why the current version was selected, or explain why it is rejected if the conflict matched when inverting foreach ($package->getConflicts() as $link) { - if (in_array($link->getTarget(), $needles)) { + if (in_array($link->getTarget(), $needles, true)) { foreach ($this->findPackages($link->getTarget()) as $pkg) { $version = new Constraint('=', $pkg->getVersion()); if ($link->getConstraint()->matches($version) === $invert) { @@ -187,7 +188,7 @@ class InstalledRepository extends CompositeRepository } // When inverting, we need to check for conflicts of the needles' requirements against installed packages - if ($invert && $constraint && in_array($package->getName(), $needles) && $constraint->matches(new Constraint('=', $package->getVersion()))) { + if ($invert && $constraint && in_array($package->getName(), $needles, true) && $constraint->matches(new Constraint('=', $package->getVersion()))) { foreach ($package->getRequires() as $link) { if (PlatformRepository::isPlatformPackage($link->getTarget())) { if ($this->findPackage($link->getTarget(), $link->getConstraint())) {