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
5 changes: 5 additions & 0 deletions docs/blink.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
```bash
vendor/bin/ecs check --blink # report only (like a normal check)
vendor/bin/ecs check --blink --fix # rewrite files in place
vendor/bin/ecs check --blink --commit # apply rules one by one, one git commit per rule
vendor/bin/ecs check src --blink # limit to a path - pass the path before the flag
```

Expand All @@ -17,6 +18,10 @@ ECS resolves the configuration from `ecs.php` (paths, rules and skips) exactly a

The exit code of `ecs-go` is passed through.

## Commit per rule

`--blink --commit` applies the resolved rules one at a time, each in its own fix run, and turns every rule that actually changed files into a single git commit (`[Group] RuleName`, from the rule's namespace). Rules that change nothing are skipped. It requires `--blink`; used alone it errors. Run it on a clean working tree, since each commit stages tracked changes with `git add --update`.

## Dumping the config

The JSON ecs-go consumes can also be inspected on its own:
Expand Down
93 changes: 93 additions & 0 deletions src/Blink/BlinkCommitRunner.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

declare(strict_types=1);

namespace Symplify\EasyCodingStandard\Blink;

use Symplify\EasyCodingStandard\Console\ExitCode;
use Symplify\EasyCodingStandard\Console\Style\EasyCodingStandardStyle;

/**
* Drives --blink --commit: applies the resolved rules one at a time, each in its
* own ecs-go fix run, and turns every rule that changed files into a single git
* commit. One rule = one commit.
*
* @see \Symplify\EasyCodingStandard\Tests\Blink\BlinkCommitRunnerTest
*/
final readonly class BlinkCommitRunner
{
public function __construct(
private BlinkRunner $blinkRunner,
private EasyCodingStandardStyle $easyCodingStandardStyle,
) {
}

/**
* @param array{paths: string[], rules: array<array{class: string, config: object|array<string, mixed>}>, skips: array<mixed>} $configData
* @return ExitCode::*
*/
public function run(array $configData): int
{
if ($configData['rules'] === []) {
$this->easyCodingStandardStyle->warning('No rules resolved, nothing to commit.');
return ExitCode::SUCCESS;
}

$committedCount = 0;

foreach ($configData['rules'] as $rule) {
$singleRuleConfig = [
'paths' => $configData['paths'],
'rules' => [$rule],
'skips' => $configData['skips'],
];

$this->blinkRunner->run($singleRuleConfig, true);

if (! $this->hasChanges()) {
continue;
}

$message = $this->createCommitMessage($rule['class']);
$this->commit($message);

$this->easyCodingStandardStyle->writeln('<fg=green>✓</> ' . $message);
++$committedCount;
}

$this->easyCodingStandardStyle->success(sprintf('%d rule(s) committed', $committedCount));

return ExitCode::SUCCESS;
}

/**
* Builds "[Group] ShortName" from the rule class, where the group is the
* namespace segment just above the class (e.g. ...\Fixer\Import\OrderedImportsFixer
* yields "[Import] OrderedImportsFixer"). Falls back to the bare short name.
*/
public function createCommitMessage(string $ruleClass): string
{
$parts = explode('\\', $ruleClass);
$shortName = array_pop($parts);

$group = array_pop($parts);
if ($group === null) {
return $shortName;
}

return sprintf('[%s] %s', $group, $shortName);
}

private function hasChanges(): bool
{
exec('git status --porcelain --untracked-files=all 2>/dev/null', $statusLines, $exitCode);

return $exitCode === 0 && array_filter($statusLines, static fn (string $line): bool => $line !== '') !== [];
}

private function commit(string $message): void
{
exec('git add --update 2>/dev/null');
exec(sprintf('git commit --message %s 2>/dev/null', escapeshellarg($message)));
}
}
17 changes: 17 additions & 0 deletions src/Console/Command/CheckCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Entropy\Console\Contract\CommandInterface;
use Entropy\Console\Contract\DefaultCommandInterface;
use Symplify\EasyCodingStandard\Application\EasyCodingStandardApplication;
use Symplify\EasyCodingStandard\Blink\BlinkCommitRunner;
use Symplify\EasyCodingStandard\Blink\BlinkConfigDumper;
use Symplify\EasyCodingStandard\Blink\BlinkRunner;
use Symplify\EasyCodingStandard\Configuration\ConfigInitializer;
Expand All @@ -27,6 +28,7 @@ public function __construct(
private EasyCodingStandardApplication $easyCodingStandardApplication,
private ConfigurationFactory $configurationFactory,
private BlinkRunner $blinkRunner,
private BlinkCommitRunner $blinkCommitRunner,
private BlinkConfigDumper $blinkConfigDumper,
private EasyCodingStandardStyle $easyCodingStandardStyle,
) {
Expand All @@ -45,6 +47,7 @@ public function getDescription(): string
/**
* @param bool $dirty Check only files with uncommitted git changes
* @param bool $blink [EXPERIMENTAL] run the ecs-go Go binary instead of the PHP engine
* @param bool $commit [EXPERIMENTAL] with --blink, apply rules one by one and commit each separately
* @param string $config Path to config file
* @param string $outputFormat Select output format
* @param string $memoryLimit Memory limit for check
Expand All @@ -71,13 +74,22 @@ public function run(
bool $debug = false,
bool $dirty = false,
bool $blink = false,
bool $commit = false,
string $config = '',
string $outputFormat = ConsoleOutputFormatter::NAME,
string $memoryLimit = '',
string $port = '',
string $identifier = '',
string ...$paths,
): int {
if ($commit && ! $blink) {
$this->easyCodingStandardStyle->error(
'The --commit option works only together with --blink. It runs the ecs-go engine rule by rule and creates one git commit per rule. Re-run as: vendor/bin/ecs check --blink --commit'
);

return ExitCode::CHANGED_CODE_OR_FOUND_ERRORS;
}

// create ecs.php config file if does not exist yet, unless an explicit --config was provided
if ($config === '' && ! $this->configInitializer->areSomeCheckersRegistered()) {
$this->configInitializer->createConfig((string) getcwd());
Expand All @@ -103,6 +115,11 @@ public function run(
// experimental: hand the resolved config to the ecs-go Go binary and skip the PHP engine
if ($blink) {
$configData = $this->blinkConfigDumper->dump($configuration->getSources());

if ($commit) {
return $this->blinkCommitRunner->run($configData);
}

$blinkExitCode = $this->blinkRunner->run($configData, $fix);
return $blinkExitCode === ExitCode::SUCCESS ? ExitCode::SUCCESS : ExitCode::CHANGED_CODE_OR_FOUND_ERRORS;
}
Expand Down
37 changes: 37 additions & 0 deletions tests/Blink/BlinkCommitRunnerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace Symplify\EasyCodingStandard\Tests\Blink;

use PhpCsFixer\Fixer\Import\OrderedImportsFixer;
use PHPUnit\Framework\TestCase;
use ReflectionClass;
use Symplify\EasyCodingStandard\Blink\BlinkCommitRunner;

final class BlinkCommitRunnerTest extends TestCase
{
private BlinkCommitRunner $blinkCommitRunner;

protected function setUp(): void
{
// createCommitMessage() is pure and touches no dependency
$this->blinkCommitRunner = new ReflectionClass(BlinkCommitRunner::class)->newInstanceWithoutConstructor();
}

public function testCreateCommitMessagePrefixesNamespaceGroup(): void
{
$message = $this->blinkCommitRunner->createCommitMessage(
OrderedImportsFixer::class
);

$this->assertSame('[Import] OrderedImportsFixer', $message);
}

public function testCreateCommitMessageFallsBackToShortName(): void
{
$message = $this->blinkCommitRunner->createCommitMessage('OrderedImportsFixer');

$this->assertSame('OrderedImportsFixer', $message);
}
}
Loading