Skip to content

Commit

Permalink
Fix abandoned package list JSON serialization (#11647)
Browse files Browse the repository at this point in the history
Co-authored-by: Jordi Boggiano <j.boggiano@seld.be>
  • Loading branch information
mxr576 and Seldaek committed Sep 26, 2023
1 parent 1f9f91c commit 755de04
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
19 changes: 7 additions & 12 deletions src/Composer/Advisory/Auditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,10 @@ public function audit(IOInterface $io, RepositorySet $repoSet, array $packages,
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));

Expand Down Expand Up @@ -127,11 +130,11 @@ public function audit(IOInterface $io, RepositorySet $repoSet, array $packages,

/**
* @param array<PackageInterface> $packages
* @return array<PackageInterface>
* @return array<CompletePackageInterface>
*/
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();
});
}
Expand Down Expand Up @@ -305,7 +308,7 @@ private function outputAdvisoriesPlain(IOInterface $io, array $advisories): void
}

/**
* @param array<PackageInterface> $packages
* @param array<CompletePackageInterface> $packages
* @param self::FORMAT_PLAIN|self::FORMAT_TABLE $format
*/
private function outputAbandonedPackages(IOInterface $io, array $packages, string $format): void
Expand All @@ -314,10 +317,6 @@ private function outputAbandonedPackages(IOInterface $io, array $packages, strin

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';
Expand All @@ -341,10 +340,6 @@ private function outputAbandonedPackages(IOInterface $io, array $packages, strin
->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]);
}
Expand Down
20 changes: 20 additions & 0 deletions tests/Composer/Test/Advisory/AuditorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,26 @@ public static function auditProvider()
| 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
}
}',
];
}

/**
Expand Down

0 comments on commit 755de04

Please sign in to comment.