diff --git a/src/Composer/Advisory/Auditor.php b/src/Composer/Advisory/Auditor.php index 7d55be5dc..00ac610e6 100644 --- a/src/Composer/Advisory/Auditor.php +++ b/src/Composer/Advisory/Auditor.php @@ -86,7 +86,10 @@ class Auditor if ($ignoredAdvisories !== []) { $json['ignored-advisories'] = $ignoredAdvisories; } - $json['abandoned'] = $abandonedPackages; + $json['abandoned'] = array_reduce($abandonedPackages, static function(array $carry, CompletePackageInterface $package): array { + $carry[$package->getPrettyName()] = $package->getReplacementPackage(); + return $carry; + }, []); $io->write(JsonFile::encode($json)); @@ -127,11 +130,11 @@ class Auditor /** * @param array $packages - * @return array + * @return array */ private function filterAbandonedPackages(array $packages): array { - return array_filter($packages, function (PackageInterface $pkg) { + return array_filter($packages, static function (PackageInterface $pkg) { return $pkg instanceof CompletePackageInterface && $pkg->isAbandoned(); }); } @@ -305,7 +308,7 @@ class Auditor } /** - * @param array $packages + * @param array $packages * @param self::FORMAT_PLAIN|self::FORMAT_TABLE $format */ private function outputAbandonedPackages(IOInterface $io, array $packages, string $format): void @@ -314,10 +317,6 @@ class Auditor if ($format === self::FORMAT_PLAIN) { foreach ($packages as $pkg) { - if (!$pkg instanceof CompletePackageInterface) { - continue; - } - $replacement = $pkg->getReplacementPackage() !== null ? 'Use '.$pkg->getReplacementPackage().' instead' : 'No replacement was suggested'; @@ -341,10 +340,6 @@ class Auditor ->setColumnMaxWidth(1, 80); foreach ($packages as $pkg) { - if (!$pkg instanceof CompletePackageInterface) { - continue; - } - $replacement = $pkg->getReplacementPackage() !== null ? $pkg->getReplacementPackage() : 'none'; $table->addRow([$this->getPackageNameWithLink($pkg), $replacement]); } diff --git a/tests/Composer/Test/Advisory/AuditorTest.php b/tests/Composer/Test/Advisory/AuditorTest.php index e42c95e68..656505844 100644 --- a/tests/Composer/Test/Advisory/AuditorTest.php +++ b/tests/Composer/Test/Advisory/AuditorTest.php @@ -122,6 +122,26 @@ Found 2 abandoned packages: | vendor/abandoned2 | none | +-------------------+----------------------------------------------------------------------------------+', ]; + + yield 'abandoned packages fails with json format' => [ + 'data' => [ + 'packages' => [ + $abandonedWithReplacement, + $abandonedNoReplacement, + ], + 'warningOnly' => false, + 'abandoned' => Auditor::ABANDONED_FAIL, + 'format' => Auditor::FORMAT_JSON, + ], + 'expected' => 2, + 'output' => '{ + "advisories": [], + "abandoned": { + "vendor/abandoned": "foo/bar", + "vendor/abandoned2": null + } +}', + ]; } /**