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 5 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
130 changes: 130 additions & 0 deletions doc/tasks/twigcsfixer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
# Twig-CS-Fixer

Check and fix Twig coding standard using [VincentLanglet/Twig-CS-Fixer](https://github.com/VincentLanglet/Twig-CS-Fixer).

***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:
paths: ['.']
level: 'NOTICE'
config: ~
report: 'text'
fix: false
no-cache: false
debug: false
quiet: false
version: false
ansi: false
no-ansi: false
no-interaction: false
verbose: false
triggered_by: ['twig']
```

**paths**

*Default: null*

By default [`.`] (current folder) will be used.
On precommit only changed files that live in the paths will be passed as arguments.


**level**

*Default: 'NOTICE'*

The level of the messages to display (possibles values are : 'NOTICE', 'WARNING', 'ERROR').

**config**

*Default: null*

Path to a `.twig-cs-fixer.php` config file. If not set, the default config will be used.

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

**report**

*Default: 'text'*

The `--report` option allows to choose the output format for the linter report.

Supported formats are:
- `text` selected by default.
- `checkstyle` following the common checkstyle XML schema.
- `github` if you want annotations on GitHub actions.
- `junit` following JUnit schema XML from Jenkins.
- `null` if you don't want any reporting.


**fix**

*Default: false*

Fix the violations.

**no-cache**

*Default: false*

Do not use cache.

**debug**

*Default: false*

Display debugging information.

**quiet**

*Default: false*

Do not output any message.

**version**

*Default: false*

Display this application version.

**ansi**

*Default: false*

Force ANSI output.

**no-ansi**

*Default: false*

Disable ANSI output.

**no-interaction**

*Default: false*

Do not ask any interactive question.

**verbose**

*Default: false*

Increase the verbosity of messages.

**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
118 changes: 118 additions & 0 deletions src/Task/TwigCsFixer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?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([
'paths' => ['.'],
'level' => 'NOTICE',
'config' => null,
'report' => 'text',
'fix' => false,
'no-cache' => false,
'debug' => false,
'quiet' => false,
'version' => false,
'ansi' => false,
'no-ansi' => false,
'no-interaction' => false,
'verbose' => false,
'triggered_by' => ['twig'],
]);

$resolver->addAllowedTypes('paths', ['array']);
$resolver->addAllowedTypes('level', ['string']);
$resolver->addAllowedTypes('config', ['null', 'string']);
$resolver->addAllowedTypes('report', ['string']);
$resolver->addAllowedTypes('fix', ['bool']);
$resolver->addAllowedTypes('no-cache', ['bool']);
$resolver->addAllowedTypes('debug', ['bool']);
$resolver->addAllowedTypes('quiet', ['bool']);
$resolver->addAllowedTypes('version', ['bool']);
$resolver->addAllowedTypes('ansi', ['bool']);
$resolver->addAllowedTypes('no-ansi', ['bool']);
$resolver->addAllowedTypes('no-interaction', ['bool']);
$resolver->addAllowedTypes('verbose', ['bool']);

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'])
->paths($config['paths']);

if (\count($files) === 0) {
return TaskResult::createSkipped($this, $context);
}

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

if ($context instanceof GitPreCommitContext) {
$arguments->addFiles($files);
}

if ($context instanceof RunContext) {
foreach ($config['paths'] as $path) {
$arguments->add($path);
}
}

$arguments->addOptionalArgument('--level=%s', $config['level']);
$arguments->addOptionalArgument('--config=%s', $config['config']);
$arguments->addOptionalArgument('--report=%s', $config['report']);

$arguments->addOptionalArgument('--fix', $config['fix']);
$arguments->addOptionalArgument('--no-cache', $config['no-cache']);
$arguments->addOptionalArgument('--debug', $config['debug']);
$arguments->addOptionalArgument('--quiet', $config['quiet']);
$arguments->addOptionalArgument('--version', $config['version']);
$arguments->addOptionalArgument('--ansi', $config['ansi']);
$arguments->addOptionalArgument('--no-ansi', $config['no-ansi']);
$arguments->addOptionalArgument('--no-interaction', $config['no-interaction']);
$arguments->addOptionalArgument('--verbose', $config['verbose']);

$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);
}
}
Loading