Skip to content

Commit

Permalink
Add color on phpcsfixer
Browse files Browse the repository at this point in the history
  • Loading branch information
oallain committed Sep 23, 2023
1 parent 3ec61c1 commit 49da91d
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 15 deletions.
8 changes: 8 additions & 0 deletions doc/tasks/phpcsfixer.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ grumphp:
allow_risky: ~
cache_file: ~
config: ~
format: 'txt'
rules: []
using_cache: ~
config_contains_finder: true
Expand Down Expand Up @@ -50,6 +51,13 @@ When no cache_file is set, the default file `.php_cs.cache` will be used.
By default, the `.php_cs` wil be used.
You can specify an alternate location for this file by changing this option.

**format**

*Default: txt*

By default, the `txt` format wil be used.
You can find more reporters on [PHPCSFixer repository](https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/tree/master/src/Console/Report/FixReport).

**rules**

*Default: []*
Expand Down
6 changes: 4 additions & 2 deletions src/Task/PhpCsFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public static function getConfigurableOptions(): OptionsResolver
'allow_risky' => null,
'cache_file' => null,
'config' => null,
'format' => 'txt',
'rules' => [],
'using_cache' => null,
'config_contains_finder' => true,
Expand All @@ -39,6 +40,7 @@ public static function getConfigurableOptions(): OptionsResolver
$resolver->addAllowedTypes('allow_risky', ['null', 'bool']);
$resolver->addAllowedTypes('cache_file', ['null', 'string']);
$resolver->addAllowedTypes('config', ['null', 'string']);
$resolver->addAllowedTypes('format', ['null', 'string']);
$resolver->addAllowedTypes('rules', ['array']);
$resolver->addAllowedTypes('using_cache', ['null', 'bool']);
$resolver->addAllowedTypes('config_contains_finder', ['bool']);
Expand Down Expand Up @@ -71,11 +73,12 @@ public function run(ContextInterface $context): TaskResultInterface
$this->formatter->resetCounter();

$arguments = $this->processBuilder->createArgumentsForCommand('php-cs-fixer');
$arguments->add('--format=json');
$arguments->add('--dry-run');
$arguments->addOptionalArgument('--format=%s', $config['format']);
$arguments->addOptionalBooleanArgument('--allow-risky=%s', $config['allow_risky'], 'yes', 'no');
$arguments->addOptionalArgument('--cache-file=%s', $config['cache_file']);
$arguments->addOptionalArgument('--config=%s', $config['config']);
$arguments->addOptionalArgument('--ansi', true);

if ($rules = $config['rules']) {
$arguments->add(sprintf(
Expand Down Expand Up @@ -104,7 +107,6 @@ public function run(ContextInterface $context): TaskResultInterface
return FixableProcessResultProvider::provide(
TaskResult::createFailed($this, $context, $this->formatter->format($process)),
function () use ($arguments): Process {
$arguments->removeElement('--format=json');
$arguments->removeElement('--dry-run');
return $this->processBuilder->buildProcess($arguments);
}
Expand Down
54 changes: 41 additions & 13 deletions test/Unit/Task/PhpCsFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public function provideConfigurableOptions(): iterable
'allow_risky' => null,
'cache_file' => null,
'config' => null,
'format' => 'txt',
'rules' => [],
'using_cache' => null,
'config_contains_finder' => true,
Expand Down Expand Up @@ -115,8 +116,9 @@ public function provideExternalTaskRuns(): iterable
$this->mockContext(RunContext::class, ['hello.php', 'hello2.php']),
'php-cs-fixer',
[
'--format=json',
'--dry-run',
'--format=txt',
'--ansi',
'--verbose',
'fix',
]
Expand All @@ -128,9 +130,10 @@ public function provideExternalTaskRuns(): iterable
$this->mockContext(RunContext::class, ['hello.php', 'hello2.php']),
'php-cs-fixer',
[
'--format=json',
'--dry-run',
'--format=txt',
'--allow-risky=yes',
'--ansi',
'--verbose',
'fix',
]
Expand All @@ -142,9 +145,10 @@ public function provideExternalTaskRuns(): iterable
$this->mockContext(RunContext::class, ['hello.php', 'hello2.php']),
'php-cs-fixer',
[
'--format=json',
'--dry-run',
'--format=txt',
'--cache-file=cachefile',
'--ansi',
'--verbose',
'fix',
]
Expand All @@ -156,9 +160,24 @@ public function provideExternalTaskRuns(): iterable
$this->mockContext(RunContext::class, ['hello.php', 'hello2.php']),
'php-cs-fixer',
[
'--format=json',
'--dry-run',
'--format=txt',
'--config=config.php',
'--ansi',
'--verbose',
'fix',
]
];
yield 'format' => [
[
'format' => 'json'
],
$this->mockContext(RunContext::class, ['hello.php', 'hello2.php']),
'php-cs-fixer',
[
'--dry-run',
'--format=json',
'--ansi',
'--verbose',
'fix',
]
Expand All @@ -170,8 +189,9 @@ public function provideExternalTaskRuns(): iterable
$this->mockContext(RunContext::class, ['hello.php', 'hello2.php']),
'php-cs-fixer',
[
'--format=json',
'--dry-run',
'--format=txt',
'--ansi',
'--rules=foo,bar',
'--verbose',
'fix',
Expand All @@ -188,8 +208,9 @@ public function provideExternalTaskRuns(): iterable
$this->mockContext(RunContext::class, ['hello.php', 'hello2.php']),
'php-cs-fixer',
[
'--format=json',
'--dry-run',
'--format=txt',
'--ansi',
'--rules='.json_encode($rules),
'--verbose',
'fix',
Expand All @@ -202,8 +223,9 @@ public function provideExternalTaskRuns(): iterable
$this->mockContext(RunContext::class, ['hello.php', 'hello2.php']),
'php-cs-fixer',
[
'--format=json',
'--dry-run',
'--format=txt',
'--ansi',
'--using-cache=yes',
'--verbose',
'fix',
Expand All @@ -216,8 +238,9 @@ public function provideExternalTaskRuns(): iterable
$this->mockContext(RunContext::class, ['hello.php', 'hello2.php']),
'php-cs-fixer',
[
'--format=json',
'--dry-run',
'--format=txt',
'--ansi',
'fix',
]
];
Expand All @@ -228,8 +251,9 @@ public function provideExternalTaskRuns(): iterable
$this->mockContext(RunContext::class, ['hello.php', 'hello2.php']),
'php-cs-fixer',
[
'--format=json',
'--dry-run',
'--format=txt',
'--ansi',
'--verbose',
'--diff',
'fix',
Expand All @@ -242,8 +266,9 @@ public function provideExternalTaskRuns(): iterable
$this->mockContext(RunContext::class, ['hello.php', 'hello2.php']),
'php-cs-fixer',
[
'--format=json',
'--dry-run',
'--format=txt',
'--ansi',
'--verbose',
'fix',
]
Expand All @@ -255,8 +280,9 @@ public function provideExternalTaskRuns(): iterable
$this->mockContext(RunContext::class, ['hello.php', 'hello2.php']),
'php-cs-fixer',
[
'--format=json',
'--dry-run',
'--format=txt',
'--ansi',
'--verbose',
'fix',
'hello.php',
Expand All @@ -270,8 +296,9 @@ public function provideExternalTaskRuns(): iterable
$this->mockContext(GitPreCommitContext::class, ['hello.php', 'hello2.php']),
'php-cs-fixer',
[
'--format=json',
'--dry-run',
'--format=txt',
'--ansi',
'--verbose',
'fix',
'hello.php',
Expand All @@ -285,8 +312,9 @@ public function provideExternalTaskRuns(): iterable
$this->mockContext(GitPreCommitContext::class, ['hello.php', 'hello2.php']),
'php-cs-fixer',
[
'--format=json',
'--dry-run',
'--format=txt',
'--ansi',
'--path-mode=intersection',
'--verbose',
'fix',
Expand Down

0 comments on commit 49da91d

Please sign in to comment.