Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add task for Twig-CS-Fixer #1131

Merged
merged 6 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@
"sstalle/php7cc": "Lets GrumPHP check PHP 5.3 - 5.6 code compatibility with PHP 7.",
"symfony/phpunit-bridge": "Lets GrumPHP run your unit tests with the phpunit-bridge of Symfony.",
"symplify/easy-coding-standard": "Lets GrumPHP check coding standard.",
"vimeo/psalm": "Lets GrumPHP discover errors in your code without running it."
"vimeo/psalm": "Lets GrumPHP discover errors in your code without running it.",
"vincentlanglet/twig-cs-fixer": "Lets GrumPHP check and fix twig coding standard."
},
"autoload": {
"psr-4": {
Expand Down
2 changes: 2 additions & 0 deletions doc/tasks.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ grumphp:
stylelint: ~
tester: ~
twigcs: ~
twigcsfixer: ~
xmllint: ~
yamllint: ~
```
Expand Down Expand Up @@ -129,6 +130,7 @@ Every task has its own default configuration. It is possible to overwrite the pa
- [Stylelint](tasks/stylelint.md)
- [Tester](tasks/tester.md)
- [TwigCs](tasks/twigcs.md)
- [Twig-CS-Fixer](tasks/twigcsfixer.md)
- [XmlLint](tasks/xmllint.md)
- [YamlLint](tasks/yamllint.md)

Expand Down
37 changes: 37 additions & 0 deletions doc/tasks/twigcsfixer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Twig CS Fixer
antoniovj1 marked this conversation as resolved.
Show resolved Hide resolved

Check and fix Twig coding standard using [VincentLanglet/Twig-CS-Fixer](https://github.com/VincentLanglet/Twig-CS-Fixer).
You can check config file [here](https://github.com/VincentLanglet/Twig-CS-Fixer/blob/main/docs/configuration.md).

***Composer***

```
composer require --dev "vincentlanglet/twig-cs-fixer:>=2"
```

***Config***

The task lives under the `twigcsfixer` namespace and has following configurable parameters:

```yaml
# grumphp.yml
grumphp:
tasks:
twigcsfixer:
path: '.'
triggered_by: ['twig']
```

**path**

*Default: null*

By default `.` (current folder) will be used.
On precommit the path will not be used, changed files will be passed as arguments instead.
You can specify an alternate location by changing this option. If the path doesn't exist or is not accessible an exception will be thrown.

**triggered_by**

*Default: [twig]*

This option will specify which file extensions will trigger this task.
7 changes: 7 additions & 0 deletions resources/config/tasks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,13 @@ services:
tags:
- {name: grumphp.task, task: twigcs}

GrumPHP\Task\TwigCsFixer:
arguments:
- '@process_builder'
- '@formatter.raw_process'
tags:
- {name: grumphp.task, task: twigcsfixer}

GrumPHP\Task\XmlLint:
arguments:
- '@linter.xmllint'
Expand Down
81 changes: 81 additions & 0 deletions src/Task/TwigCsFixer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

declare(strict_types=1);

namespace GrumPHP\Task;

use GrumPHP\Formatter\ProcessFormatterInterface;
use GrumPHP\Runner\TaskResult;
use GrumPHP\Runner\TaskResultInterface;
use GrumPHP\Task\Config\ConfigOptionsResolver;
use GrumPHP\Task\Context\ContextInterface;
use GrumPHP\Task\Context\GitPreCommitContext;
use GrumPHP\Task\Context\RunContext;
use Symfony\Component\OptionsResolver\OptionsResolver;
use GrumPHP\Fixer\Provider\FixableProcessResultProvider;
use Symfony\Component\Process\Process;

/**
* @extends AbstractExternalTask<ProcessFormatterInterface>
*/
class TwigCsFixer extends AbstractExternalTask
{
public static function getConfigurableOptions(): ConfigOptionsResolver
{
$resolver = new OptionsResolver();
$resolver->setDefaults([
'path' => '.',
antoniovj1 marked this conversation as resolved.
Show resolved Hide resolved
'triggered_by' => ['twig'],
]);

$resolver->addAllowedTypes('path', ['string']);

return ConfigOptionsResolver::fromOptionsResolver($resolver);
}

public function canRunInContext(ContextInterface $context): bool
{
return $context instanceof GitPreCommitContext || $context instanceof RunContext;
}

public function run(ContextInterface $context): TaskResultInterface
{
$config = $this->getConfig()->getOptions();

$files = $context->getFiles()->extensions($config['triggered_by']);
if (0 === \count($files)) {
return TaskResult::createSkipped($this, $context);
}

$arguments = $this->processBuilder->createArgumentsForCommand('twig-cs-fixer');
$arguments->add('lint');
$arguments->add('--report=text');

if ($context instanceof GitPreCommitContext) {
$arguments->addFiles($files);
veewee marked this conversation as resolved.
Show resolved Hide resolved
}

if ($context instanceof RunContext) {
$arguments->add($config['path']);
veewee marked this conversation as resolved.
Show resolved Hide resolved
}

$process = $this->processBuilder->buildProcess($arguments);
antoniovj1 marked this conversation as resolved.
Show resolved Hide resolved

$process->run();

$process = $this->processBuilder->buildProcess($arguments);
$process->run();

if (!$process->isSuccessful()) {
return FixableProcessResultProvider::provide(
TaskResult::createFailed($this, $context, $this->formatter->format($process)),
function () use ($arguments): Process {
$arguments->add('--fix');
return $this->processBuilder->buildProcess($arguments);
}
);
}

return TaskResult::createPassed($this, $context);
}
}
132 changes: 132 additions & 0 deletions test/Unit/Task/TwigCsFixerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?php

declare(strict_types=1);

namespace GrumPHPTest\Unit\Task;

use GrumPHP\Runner\FixableTaskResult;
use GrumPHP\Task\Context\GitPreCommitContext;
use GrumPHP\Task\Context\RunContext;
use GrumPHP\Task\TaskInterface;
use GrumPHP\Task\TwigCsFixer;
use GrumPHP\Test\Task\AbstractExternalTaskTestCase;

class TwigCsFixerTest extends AbstractExternalTaskTestCase
{
protected function provideTask(): TaskInterface
{
return new TwigCsFixer(
$this->processBuilder->reveal(),
$this->formatter->reveal()
);
}

public function provideConfigurableOptions(): iterable
{
yield 'defaults' => [
[],
[
'triggered_by' => ['twig'],
'path' => '.',
]
];
}

public function provideRunContexts(): iterable
{
yield 'run-context' => [
true,
$this->mockContext(RunContext::class)
];

yield 'pre-commit-context' => [
true,
$this->mockContext(GitPreCommitContext::class)
];

yield 'other' => [
false,
$this->mockContext()
];
}

public function provideFailsOnStuff(): iterable
{
yield 'exitCode1' => [
[],
$this->mockContext(RunContext::class, ['hello.twig']),
function () {
$this->mockProcessBuilder('twig-cs-fixer', $process = $this->mockProcess(1));
$this->formatter->format($process)->willReturn('nope');
},
'nope',
FixableTaskResult::class
];
}

public function providePassesOnStuff(): iterable
{
yield 'exitCode0' => [
[],
$this->mockContext(RunContext::class, ['hello.twig']),
function () {
$this->mockProcessBuilder('twig-cs-fixer', $this->mockProcess(0));
}
];
}

public function provideSkipsOnStuff(): iterable
{
yield 'no-files' => [
[],
$this->mockContext(RunContext::class),
function () {}
];
yield 'no-files-after-triggered-by' => [
[],
$this->mockContext(RunContext::class, ['notatwigfile.php']),
function () {}
];
}

public function provideExternalTaskRuns(): iterable
veewee marked this conversation as resolved.
Show resolved Hide resolved
{
yield 'defaults' => [
[],
$this->mockContext(RunContext::class, ['hello.twig', 'hello2.twig']),
'twig-cs-fixer',
[
'lint',
'--report=text',
'.',
]
];

yield 'path' => [
[
'path' => 'src',
],
$this->mockContext(RunContext::class, ['hello.twig', 'hello2.twig']),
'twig-cs-fixer',
[
'lint',
'--report=text',
'src',
]
];

yield 'precommit' => [
[
'path' => 'src',
],
$this->mockContext(GitPreCommitContext::class, ['hello.twig', 'hello2.twig']),
'twig-cs-fixer',
[
'lint',
'--report=text',
'hello.twig',
'hello2.twig',
]
];
}
}