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
1 change: 1 addition & 0 deletions e2e/rules-summary/cli-options.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--rules-summary
26 changes: 26 additions & 0 deletions e2e/rules-summary/expected-output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

1 file with changes
===================

1) rules-summary/result/fixture.typoscript:1

---------- begin diff ----------
@@ @@
-config.xhtmlDoctype = 1
-config.metaCharset = utf-8
+config.doctype = 1
----------- end diff -----------

Applied rules:
* RenameConfigXhtmlDoctypeToDoctypeFractor
* RemoveConfigMetaCharsetFractor


Rules Summary
-------------

* RenameConfigXhtmlDoctypeToDoctypeFractor was applied 1 time
* RemoveConfigMetaCharsetFractor was applied 1 time

[OK] 1 file has been changed by Fractor

1 change: 1 addition & 0 deletions e2e/rules-summary/expected-result/fixture.typoscript
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
config.doctype = 1
2 changes: 2 additions & 0 deletions e2e/rules-summary/fixtures/fixture.typoscript
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
config.xhtmlDoctype = 1
config.metaCharset = utf-8
10 changes: 10 additions & 0 deletions e2e/rules-summary/fractor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

use a9f\Fractor\Configuration\FractorConfiguration;
use a9f\Typo3Fractor\Set\Typo3LevelSetList;

return FractorConfiguration::configure()
->withPaths([__DIR__ . '/result/'])
->withSets([Typo3LevelSetList::UP_TO_TYPO3_14]);
1 change: 1 addition & 0 deletions e2e/rules-summary/result/fixture.typoscript
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
config.doctype = 1
2 changes: 1 addition & 1 deletion e2e/run-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ cd $TESTS_BASE_DIR
rm -r composer.lock vendor || true
composer install

for TEST_DIR in only-option typo3-extension typo3-typoscript typo3-xml typo3-yaml
for TEST_DIR in only-option rules-summary typo3-extension typo3-typoscript typo3-xml typo3-yaml
do
set +x
echo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use a9f\Fractor\Configuration\ValueObject\Configuration;
use a9f\Fractor\Differ\ValueObject\FileDiff;
use a9f\Fractor\ValueObject\ProcessResult;
use Nette\Utils\Strings;
use Symfony\Component\Console\Style\SymfonyStyle;

final readonly class ConsoleOutputFormatter implements OutputFormatterInterface
Expand Down Expand Up @@ -38,6 +39,10 @@ public function report(ProcessResult $processResult, Configuration $configuratio
$this->symfonyStyle->newLine();
}

if ($configuration->shouldShowRulesSummary()) {
$this->reportRulesSummary($processResult, $configuration);
}

$message = $this->createSuccessMessage($processResult, $configuration);
$this->symfonyStyle->success($message);
}
Expand Down Expand Up @@ -93,4 +98,29 @@ private function createSuccessMessage(ProcessResult $processResult, Configuratio
: ($changeCount === 1 ? 'has' : 'have') . ' been changed'
);
}

private function reportRulesSummary(ProcessResult $processResult, Configuration $configuration): void
{
$ruleApplicationCounts = $processResult->getRuleApplicationCounts();
if ($ruleApplicationCounts === []) {
return;
}

$verb = $configuration->isDryRun() ? 'would have been applied' : 'was applied';

$this->symfonyStyle->section('Rules Summary');

foreach ($ruleApplicationCounts as $ruleClass => $count) {
$ruleShortClass = (string) Strings::after($ruleClass, '\\', -1);
$this->symfonyStyle->writeln(sprintf(
' * <info>%s</info> %s <comment>%d</comment> time%s',
$ruleShortClass,
$verb,
$count,
$count > 1 ? 's' : ''
));
}

$this->symfonyStyle->newLine();
}
}
3 changes: 3 additions & 0 deletions packages/fractor/src/Configuration/ConfigurationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public function createFromInput(InputInterface $input): Configuration

$memoryLimit = $this->resolveMemoryLimit($input);

$showRulesSummary = (bool) $input->getOption(Option::RULES_SUMMARY);

return new Configuration(
$dryRun,
$showProgressBar,
Expand All @@ -51,6 +53,7 @@ public function createFromInput(InputInterface $input): Configuration
$showDiffs,
$memoryLimit,
$onlyRule,
$showRulesSummary,
$showChangelog
);
}
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
* @var string
*/
public const MEMORY_LIMIT = 'memory-limit';

/**
* @var string
*/
public const RULES_SUMMARY = 'rules-summary';
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public function __construct(
private bool $showDiffs = true,
private string|null $memoryLimit = null,
private ?string $onlyRule = null,
private bool $showRulesSummary = false,
private bool $showChangelog = false,
) {
Assert::allStringNotEmpty($this->paths, 'No directories given');
Expand Down Expand Up @@ -76,6 +77,11 @@ public function getOnlyRule(): ?string
return $this->onlyRule;
}

public function shouldShowRulesSummary(): bool
{
return $this->showRulesSummary;
}

public function shouldShowChangelog(): bool
{
return $this->showChangelog;
Expand Down
7 changes: 7 additions & 0 deletions packages/fractor/src/Console/Command/ProcessCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ protected function configure(): void

// filter by rule and path
$this->addOption(Option::ONLY, null, InputOption::VALUE_REQUIRED, 'Fully qualified rule class name');

$this->addOption(
Option::RULES_SUMMARY,
null,
InputOption::VALUE_NONE,
'Show summary of rules applied during the run.'
);
}

protected function execute(InputInterface $input, OutputInterface $output): int
Expand Down
22 changes: 22 additions & 0 deletions packages/fractor/src/ValueObject/ProcessResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace a9f\Fractor\ValueObject;

use a9f\Fractor\Application\Contract\FractorRule;
use a9f\Fractor\Differ\ValueObject\FileDiff;
use Webmozart\Assert\Assert;

Expand Down Expand Up @@ -34,4 +35,25 @@ public function getTotalChanged(): int
{
return $this->totalChanged;
}

/**
* @return array<class-string<FractorRule>, int>
*/
public function getRuleApplicationCounts(): array
{
$ruleCounts = [];

foreach ($this->fileDiffs as $fileDiff) {
foreach ($fileDiff->getFractorClasses() as $fractorClass) {
if (! isset($ruleCounts[$fractorClass])) {
$ruleCounts[$fractorClass] = 0;
}

++$ruleCounts[$fractorClass];
}
}

arsort($ruleCounts);
return $ruleCounts;
}
}