Skip to content

Commit

Permalink
Merge pull request #1131 from antoniovj1/v2.x
Browse files Browse the repository at this point in the history
feat: Add task for Twig-CS-Fixer
  • Loading branch information
veewee committed May 17, 2024
2 parents 24c1609 + 3fc91de commit c9e3209
Show file tree
Hide file tree
Showing 6 changed files with 428 additions and 1 deletion.
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
80 changes: 80 additions & 0 deletions doc/tasks/twigcsfixer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# 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: ~
config: ~
report: 'text'
no-cache: false
verbose: false
triggered_by: ['twig']
```

**paths**

*Default: []*

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.

**no-cache**

*Default: false*

Do not use cache.

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

$resolver->addAllowedTypes('paths', ['array']);
$resolver->addAllowedTypes('level', ['null', 'string']);
$resolver->addAllowedTypes('config', ['null', 'string']);
$resolver->addAllowedTypes('report', ['null', 'string']);
$resolver->addAllowedTypes('no-cache', ['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) {
$arguments->addArgumentArray('%s', $config['paths']);
}

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

$arguments->addOptionalArgument('--no-cache', $config['no-cache']);
$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

0 comments on commit c9e3209

Please sign in to comment.