Skip to content

Commit

Permalink
Implement FixerGenerator::mergeWith()
Browse files Browse the repository at this point in the history
  • Loading branch information
paulbalandan committed Jul 28, 2024
1 parent b508ccb commit e1f33c4
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
37 changes: 33 additions & 4 deletions src/FixerGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,15 @@
*/
final class FixerGenerator implements \IteratorAggregate
{
private function __construct(private string $path, private string $vendor) {}
/**
* @var list<\IteratorAggregate<FixerInterface>>
*/
private array $fixerIterators = [];

private function __construct(
private string $path,
private string $vendor,
) {}

/**
* @throws \RuntimeException
Expand All @@ -50,6 +58,18 @@ public static function create(string $path, string $vendor): self
return new self($path, $vendor);
}

/**
* Merge other iterators that yield `FixerInterface` custom fixers.
*
* @param \IteratorAggregate<FixerInterface> ...$fixerIterators
*/
public function mergeWith(\IteratorAggregate ...$fixerIterators): self
{
$this->fixerIterators = array_values($fixerIterators);

return $this;
}

/**
* @return \Traversable<FixerInterface>
*/
Expand All @@ -63,7 +83,16 @@ public function getIterator(): \Traversable
->sortByName()
;

$fixers = array_filter(array_map(
$otherFixers = [];

foreach ($this->fixerIterators as $fixerIterator) {
$otherFixers = array_merge(
$otherFixers,
iterator_to_array($fixerIterator->getIterator(), false),
);
}

$fixers = array_values(array_filter(array_map(
function (SplFileInfo $file): object {
$fixer = \sprintf(
'%s\\%s%s%s',
Expand All @@ -76,8 +105,8 @@ function (SplFileInfo $file): object {
return new $fixer();
},
iterator_to_array($finder, false),
), static fn(object $fixer): bool => $fixer instanceof FixerInterface);
), static fn(object $fixer): bool => $fixer instanceof FixerInterface));

yield from $fixers;
yield from array_merge($fixers, $otherFixers);
}
}
12 changes: 12 additions & 0 deletions tests/FixerGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Nexus\CsConfig\Tests;

use Nexus\CsConfig\FixerGenerator;
use PhpCsFixer\Fixer\FixerInterface;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -53,4 +54,15 @@ public function testTraversableReturnedAreInstancesOfFixerInterface(): void
$generator = FixerGenerator::create('vendor/friendsofphp/php-cs-fixer/src/Fixer', 'PhpCsFixer\\Fixer');
self::assertNotEmpty(iterator_to_array($generator));
}

public function testMergeWithJoinsIteratorsTogether(): void
{
$generator1 = FixerGenerator::create('src/Fixer', 'Nexus\\CsConfig\\Fixer');
$generator2 = FixerGenerator::create('vendor/friendsofphp/php-cs-fixer/src/Fixer', 'PhpCsFixer\\Fixer');

/** @var list<FixerInterface> $joinedGenerators */
$joinedGenerators = iterator_to_array($generator1->mergeWith($generator2)->getIterator(), false);
self::assertStringStartsWith('Nexus\\CsConfig\\Fixer', $joinedGenerators[0]::class);
self::assertStringStartsWith('PhpCsFixer\\Fixer', $joinedGenerators[\count($joinedGenerators) - 1]::class);
}
}

0 comments on commit e1f33c4

Please sign in to comment.