Remove files that are ignored before they are generated

This commit is contained in:
Oliver Davies 2024-10-31 12:00:00 +00:00
parent a56d9b2d5e
commit f0121f8bc6
2 changed files with 59 additions and 0 deletions

View file

@ -0,0 +1,34 @@
<?php
declare(strict_types=1);
namespace App\Command;
use App\DataTransferObject\ConfigDto;
use App\DataTransferObject\TemplateFile;
use Illuminate\Support\Collection;
use Symfony\Component\Filesystem\Filesystem;
use Twig\Environment;
final class RemoveIgnoredFilesCommand
{
public function __construct(private array $filenames)
{
}
public function execute(array $filesToGenerateAndConfigurationData, \Closure $next)
{
/**
* @var array<int,TemplateFile> $filesToGenerate
* @var ConfigDto $configurationDataDto,
* @var array<non-empty-string,mixed> $configurationData
*/
[$configurationData, $configurationDataDto, $filesToGenerate] = $filesToGenerateAndConfigurationData;
$filesToGenerate = array_filter($filesToGenerate, function (TemplateFile $templateFile): bool {
return !collect($this->filenames)->contains($templateFile->name);
});
return $next([$configurationDataDto, $filesToGenerate]);
}
}

View file

@ -0,0 +1,25 @@
<?php
use App\Command\RemoveIgnoredFilesCommand;
use App\DataTransferObject\TemplateFile;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
class RemoveIgnoredFilesCommandTest extends KernelTestCase
{
public function test_it_removes_any_ignored_files(): void
{
$filenamesToGenerate = [
new TemplateFile(data: '', name: 'phpcs.xml.dist'),
new TemplateFile(data: '', name: 'phpstan.neon.dist'),
];
$filenamesToIgnore = ['phpstan.neon.dist'];
$command = new RemoveIgnoredFilesCommand($filenamesToIgnore);
$command->execute([[], [], $filenamesToGenerate], function ($result) {
self::assertCount(1, $result[1]);
self::assertSame('phpcs.xml.dist', $result[1][0]->name);
});
}
}