Skip to content

Commit

Permalink
extract composer jso nresolver
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Jan 16, 2025
1 parent 7f13181 commit 803e63a
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 75 deletions.
52 changes: 10 additions & 42 deletions src/Command/MultiPackageComposerStatsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace Rector\SwissKnife\Command;

use Nette\Utils\FileSystem;
use Rector\SwissKnife\Sorting\ArraySorter;
use Rector\SwissKnife\Composer\ComposerJsonResolver;
use Rector\SwissKnife\Sorting\ArrayFilter;
use Rector\SwissKnife\ValueObject\ComposerJson;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
Expand All @@ -16,13 +16,9 @@

final class MultiPackageComposerStatsCommand extends Command
{
/**
* @var string
*/
private const TEMP_PROJECT_COMPOSER_JSON = 'temp-project-composer.json';

public function __construct(
private readonly SymfonyStyle $symfonyStyle,
private readonly ComposerJsonResolver $composerJsonResolver,
) {
parent::__construct();
}
Expand All @@ -47,9 +43,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$repositories = (array) $input->getArgument('repositories');
Assert::allString($repositories);

$this->symfonyStyle->newLine();
$this->symfonyStyle->note(sprintf(
'Downloading "composer.json" files for %d repositories',
count($repositories)
));

$projectsComposerJsons = $this->resolveProjectComposerJsons($repositories);
$projectsComposerJsons = $this->composerJsonResolver->resolveFromRepositories($repositories);
$tableHeadlines = $this->resolveTableHeadlines($projectsComposerJsons);

$requiredPackageNames = $this->resolveProjectsRequiredPackageNames($projectsComposerJsons);
Expand All @@ -66,44 +65,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}

// sort and put those with both values first
$tableRows = ArraySorter::putSharedFirst($tableRows);
$tableRows = ArrayFilter::filterOnlyAtLeast2($tableRows);

$this->symfonyStyle->table($tableHeadlines, $tableRows);

return self::SUCCESS;
}


/**
* @param string[] $repositories
* @return ComposerJson[]
*/
private function resolveProjectComposerJsons(array $repositories): array
{
Assert::allString($repositories);

$projectsComposerJsons = [];
foreach ($repositories as $repository) {
// clones only "composer.json" file
exec(sprintf(
'git archive --remote=%s HEAD composer.json | tar -xO composer.json > %s',
$repository,
self::TEMP_PROJECT_COMPOSER_JSON
));

$projectsComposerJsonContents = FileSystem::read(self::TEMP_PROJECT_COMPOSER_JSON);

Assert::string($projectsComposerJsonContents);
Assert::notEmpty($projectsComposerJsonContents);

$projectsComposerJsons[] = new ComposerJson($repository, $projectsComposerJsonContents);
}

// tidy up temporary file
FileSystem::delete(self::TEMP_PROJECT_COMPOSER_JSON);

return $projectsComposerJsons;
}

/**
* @param ComposerJson[] $projectsComposerJsons
* @return string[]
Expand Down
48 changes: 48 additions & 0 deletions src/Composer/ComposerJsonResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace Rector\SwissKnife\Composer;

use Nette\Utils\FileSystem;
use Rector\SwissKnife\ValueObject\ComposerJson;
use Webmozart\Assert\Assert;

final class ComposerJsonResolver
{
/**
* @var string
*/
private const TEMP_PROJECT_COMPOSER_JSON = 'temp-project-composer.json';

/**
* @param string[] $repositories
* @return ComposerJson[]
*/
public function resolveFromRepositories(array $repositories): array
{
Assert::allString($repositories);

$projectsComposerJsons = [];
foreach ($repositories as $repository) {
// clones only "composer.json" file
exec(sprintf(
'git archive --remote=%s HEAD composer.json | tar -xO composer.json > %s',
$repository,
self::TEMP_PROJECT_COMPOSER_JSON
));

$projectsComposerJsonContents = FileSystem::read(self::TEMP_PROJECT_COMPOSER_JSON);

Assert::string($projectsComposerJsonContents);
Assert::notEmpty($projectsComposerJsonContents);

$projectsComposerJsons[] = new ComposerJson($repository, $projectsComposerJsonContents);
}

// tidy up temporary file
FileSystem::delete(self::TEMP_PROJECT_COMPOSER_JSON);

return $projectsComposerJsons;
}
}
26 changes: 26 additions & 0 deletions src/Sorting/ArrayFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Rector\SwissKnife\Sorting;

use Webmozart\Assert\Assert;

final class ArrayFilter
{
/**
* @param array<mixed[]> $items
* @return array<mixed[]>
*/
public static function filterOnlyAtLeast2(array $items): array
{
Assert::allIsArray($items);

return array_filter($items, function (array $item): bool {
$nonEmptyValues = array_filter($item);

// +1 for the package name
return count($nonEmptyValues) >= 3;
});
}
}
31 changes: 0 additions & 31 deletions src/Sorting/ArraySorter.php

This file was deleted.

6 changes: 4 additions & 2 deletions src/ValueObject/ComposerJson.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ final class ComposerJson
*/
private readonly array $json;

public function __construct(private readonly string $repositoryGit, string $composerJsonContents)
{
public function __construct(
private readonly string $repositoryGit,
string $composerJsonContents
) {
$this->json = Json::decode($composerJsonContents, forceArrays: true);
}

Expand Down

0 comments on commit 803e63a

Please sign in to comment.