Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions packages/fractor/src/Application/FractorRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function run(Configuration $configuration): ProcessResult

// no files found
if ($filePaths === []) {
return new ProcessResult([]);
return new ProcessResult([], 0);
}

$shouldShowProgressBar = $configuration->shouldShowProgressBar();
Expand All @@ -63,6 +63,7 @@ public function run(Configuration $configuration): ProcessResult
/** @var FileDiff[] $fileDiffs */
$fileDiffs = [];

$totalChanged = 0;
foreach ($filePaths as $filePath) {
$file = new File($filePath, FileSystem::read($filePath));
$this->fileCollector->addFile($file);
Expand All @@ -89,7 +90,9 @@ public function run(Configuration $configuration): ProcessResult
continue;
}

$file->setFileDiff($this->fileDiffFactory->createFileDiff($file));
++$totalChanged;

$file->setFileDiff($this->fileDiffFactory->createFileDiff($configuration->shouldShowDiffs(), $file));

$fileProcessResult = new FileProcessResult($file->getFileDiff());
$currentFileDiff = $fileProcessResult->getFileDiff();
Expand All @@ -110,7 +113,7 @@ public function run(Configuration $configuration): ProcessResult
$this->fileWriter->write($file);
}

return new ProcessResult($fileDiffs);
return new ProcessResult($fileDiffs, $totalChanged);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ public function getName(): string

public function report(ProcessResult $processResult, Configuration $configuration): void
{
$this->reportFileDiffs($processResult->getFileDiffs(), false, $configuration->shouldShowChangelog());
if ($configuration->shouldShowDiffs()) {
$this->reportFileDiffs($processResult->getFileDiffs(), false, $configuration->shouldShowChangelog());
}

// to keep space between progress bar and success message
if ($configuration->shouldShowProgressBar() && $processResult->getFileDiffs() === []) {
Expand Down Expand Up @@ -78,7 +80,7 @@ private function reportFileDiffs(array $fileDiffs, bool $absoluteFilePath, bool

private function createSuccessMessage(ProcessResult $processResult, Configuration $configuration): string
{
$changeCount = \count($processResult->getFileDiffs());
$changeCount = $processResult->getTotalChanged();
if ($changeCount === 0) {
return 'Fractor is done!';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@ public function report(ProcessResult $processResult, Configuration $configuratio
foreach ($fileDiffs as $fileDiff) {
$filePath = $fileDiff->getRelativeFilePath();

$errorsJson['file_diffs'][] = [
'file' => $filePath,
'diff' => $fileDiff->getDiff(),
'applied_rules' => $configuration->shouldShowChangelog() ? $fileDiff->getChangelogsLines() : $fileDiff->getFractorClasses(),
];
if ($configuration->shouldShowDiffs() && $fileDiff->getDiff() !== '') {
$errorsJson['file_diffs'][] = [
'file' => $filePath,
'diff' => $fileDiff->getDiff(),
'applied_rules' => $configuration->shouldShowChangelog() ? $fileDiff->getChangelogsLines() : $fileDiff->getFractorClasses(),
];
}

// for CI
$errorsJson['changed_files'][] = $filePath;
Expand Down
13 changes: 13 additions & 0 deletions packages/fractor/src/Configuration/ConfigurationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public function createFromInput(InputInterface $input): Configuration

$outputFormat = (string) $input->getOption(Option::OUTPUT_FORMAT);
$showProgressBar = $this->shouldShowProgressBar($input, $outputFormat);
$showDiffs = $this->shouldShowDiffs($input);
$showChangelog = $this->shouldShowChangelog($input);

/** @var list<non-empty-string> $paths */
Expand All @@ -48,6 +49,7 @@ public function createFromInput(InputInterface $input): Configuration
$outputFormat,
$fileExtensions,
$paths,
$showDiffs,
(array) $this->parameterBag->get(Option::SKIP),
$onlyRule,
$showChangelog
Expand Down Expand Up @@ -81,6 +83,17 @@ private function shouldShowProgressBar(InputInterface $input, string $outputForm
return $outputFormat === ConsoleOutputFormatter::NAME;
}

private function shouldShowDiffs(InputInterface $input): bool
{
$noDiffs = (bool) $input->getOption(Option::NO_DIFFS);
if ($noDiffs) {
return false;
}

// fallback to parameter
return ! SimpleParameterProvider::provideBoolParameter(Option::NO_DIFFS, false);
}

private function shouldShowChangelog(InputInterface $input): bool
{
return (bool) $input->getOption(Option::SHOW_CHANGELOG);
Expand Down
12 changes: 12 additions & 0 deletions packages/fractor/src/Configuration/FractorConfigurationBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ final class FractorConfigurationBuilder

private ?string $containerCacheDirectory = null;

private bool $noDiffs = false;

public function __invoke(ContainerConfigurator $containerConfigurator): void
{
foreach ($this->imports as $import) {
Expand Down Expand Up @@ -104,6 +106,9 @@ public function __invoke(ContainerConfigurator $containerConfigurator): void
$parameters->set(Option::CONTAINER_CACHE_DIRECTORY, $this->containerCacheDirectory);
SimpleParameterProvider::setParameter(Option::CONTAINER_CACHE_DIRECTORY, $this->containerCacheDirectory);

$parameters->set(Option::NO_DIFFS, $this->noDiffs);
SimpleParameterProvider::setParameter(Option::NO_DIFFS, $this->noDiffs);

foreach ($this->options as $optionName => $optionValue) {
$parameters->set($optionName, $optionValue);
}
Expand Down Expand Up @@ -209,6 +214,13 @@ public function import(string $import): self
return $this;
}

public function withNoDiffs(): self
{
$this->noDiffs = true;

return $this;
}

/**
* @param array<string, string|int|bool|PrettyPrinterConditionTermination> $options
*/
Expand Down
5 changes: 5 additions & 0 deletions packages/fractor/src/Configuration/Option.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,9 @@ final class Option
* @internal to allow process file without extension if explicitly registered
*/
public const FILES_WITHOUT_EXTENSION = 'files_without_extension';

/**
* @var string
*/
public const NO_DIFFS = 'no-diffs';
}
48 changes: 27 additions & 21 deletions packages/fractor/src/Configuration/ValueObject/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,40 @@ public function __construct(
private string $outputFormat = ConsoleOutputFormatter::NAME,
private array $fileExtensions = [],
private array $paths = [],
private bool $showDiffs = true,
private array $skip = [],
private ?string $onlyRule = null,
private bool $showChangelog = false,
) {
Assert::allStringNotEmpty($this->paths, 'No directories given');
}

/**
* @return string[]
*/
public function getFileExtensions(): array
public function isDryRun(): bool
{
return $this->fileExtensions;
return $this->dryRun;
}

public function shouldShowProgressBar(): bool
{
return $this->showProgressBar;
}

public function isQuiet(): bool
{
return $this->quiet;
}

public function getOutputFormat(): string
{
return $this->outputFormat;
}

/**
* @return string[]
*/
public function getSkip(): array
public function getFileExtensions(): array
{
return $this->skip;
return $this->fileExtensions;
}

/**
Expand All @@ -55,31 +68,24 @@ public function getPaths(): array
return $this->paths;
}

public function isDryRun(): bool
public function shouldShowDiffs(): bool
{
return $this->dryRun;
return $this->showDiffs;
}

public function shouldShowProgressBar(): bool
{
return $this->showProgressBar;
}

public function isQuiet(): bool
/**
* @return string[]
*/
public function getSkip(): array
{
return $this->quiet;
return $this->skip;
}

public function getOnlyRule(): ?string
{
return $this->onlyRule;
}

public function getOutputFormat(): string
{
return $this->outputFormat;
}

public function shouldShowChangelog(): bool
{
return $this->showChangelog;
Expand Down
6 changes: 6 additions & 0 deletions packages/fractor/src/Console/Command/ProcessCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ protected function configure(): void
InputOption::VALUE_NONE,
'Hide progress bar. Useful e.g. for nicer CI output.'
);
$this->addOption(
Option::NO_DIFFS,
null,
InputOption::VALUE_NONE,
'Hide diffs of changed files. Useful e.g. for nicer CI output.'
);
$this->addOption(
Option::SHOW_CHANGELOG,
null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,20 @@ public function __construct(
) {
}

public function createFileDiff(File $file): FileDiff
public function createFileDiff(bool $shouldShowDiffs, File $file): FileDiff
{
$relativeFilePath = $this->filePathHelper->relativePath($file->getFilePath());

$diff = $shouldShowDiffs ? $this->defaultDiffer->diff($file->getDiff()) : '';
$consoleDiff = $shouldShowDiffs ? $this->consoleDiffer->diff($file->getDiff()) : '';

$fractorsChangelogsLines = $this->fractorsChangelogLinesResolver->createFractorChangelogLines(
$file->getAppliedRules()
);
return new FileDiff(
$relativeFilePath,
$this->defaultDiffer->diff($file->getDiff()),
$this->consoleDiffer->diff($file->getDiff()),
$diff,
$consoleDiff,
$file->getAppliedRules(),
$fractorsChangelogsLines
);
Expand Down
13 changes: 11 additions & 2 deletions packages/fractor/src/ValueObject/ProcessResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,25 @@
* @param FileDiff[] $fileDiffs
*/
public function __construct(
private array $fileDiffs
private array $fileDiffs,
private int $totalChanged,
) {
Assert::allIsInstanceOf($this->fileDiffs, FileDiff::class);
}

/**
* @return FileDiff[]
*/
public function getFileDiffs(): array
public function getFileDiffs(bool $onlyWithChanges = true): array
{
if ($onlyWithChanges) {
return array_filter($this->fileDiffs, static fn (FileDiff $fileDiff): bool => $fileDiff->getDiff() !== '');
}
return $this->fileDiffs;
}

public function getTotalChanged(): int
{
return $this->totalChanged;
}
}