Skip to content

Commit

Permalink
Add var rule and refacto rules (#172)
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet authored Jan 26, 2024
1 parent 76deacb commit b850eca
Show file tree
Hide file tree
Showing 33 changed files with 340 additions and 84 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/qa.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,5 @@ jobs:
dependency-versions: highest
composer-options: --prefer-dist --prefer-stable

- name: Run PHP Code Sniffer
- name: Run Psalm
run: vendor/bin/psalm --no-progress --output-format=github --shepherd
15 changes: 15 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,21 @@ If your config is not located in your current directory, you can specify its pat
vendor/bin/twig-cs-fixer lint --config=dir/.twig-cs-fixer.php /path/to/code
```

## Non fixable rules

Most of the rules are automatically fixable but some of them are not.
By default, the twig-cs-fixer disable all the non-fixable-rules, but you can still allow them in the config file:

```php
<?php

$config = new TwigCsFixer\Config\Config();
$config->allowNonFixableRules();

return $config;
```


## Files

By default, all `.twig` files in the current directory are linted, except the ones in the `vendor` directory.
Expand Down
11 changes: 9 additions & 2 deletions docs/rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

## Rules

### Fixable

- **BlockNameSpacingRule**: ensures there is one space before and after block names.
- **DelimiterSpacingRule**: ensures there is one space before '}}', '%}' and '#}', and after '{{', '{%', '{#'.
- **DirectoryNameRule**: ensures directory name is snake_case (configurable).
- **FileNameRule**: ensures file name is snake_case (configurable).
- **OperatorNameSpacingRule**: ensures there is no consecutive spaces inside operator names.
- **OperatorSpacingRule**: ensures there is one space before and after an operator except for '..'.
- **PunctuationSpacingRule**: ensures there is no space before and after a punctuation except for ':' and ','.
Expand All @@ -15,13 +15,20 @@
- **IndentRule**: ensures that files are not indented with tabs.
- **TrailingSpaceRule**: ensures that files have no trailing spaces.

### Non-fixable

- **DirectoryNameRule**: ensures that directory name use snake_case (configurable).
- **FileNameRule**: ensures that file name use snake_case (configurable).
- **VariableNameRule**: ensures that variable name use snake_case (configurable).

## Standards

**Twig**:
- DelimiterSpacingRule
- OperatorNameSpacingRule
- OperatorSpacingRule
- PunctuationSpacingRule
- VariableNameRule

**TwigCsFixer**:
- Twig
Expand Down
4 changes: 2 additions & 2 deletions src/Command/TwigCsFixerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ private function resolveConfig(InputInterface $input, OutputInterface $output):
$config = $configResolver->resolveConfig(
$input->getArgument('paths'),
$input->getOption('config'),
$input->getOption('no-cache')
$input->getOption('no-cache'),
);

$cacheFile = $config->getCacheFile();
Expand All @@ -130,7 +130,7 @@ private function runLinter(Config $config, InputInterface $input, OutputInterfac
$report = $linter->run(
$config->getFinder(),
$config->getRuleset(),
$input->getOption('fix') ? new Fixer($tokenizer) : null
$input->getOption('fix') ? new Fixer($tokenizer) : null,
);

$reporterFactory = new ReporterFactory();
Expand Down
17 changes: 17 additions & 0 deletions src/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ final class Config
*/
private array $tokenParsers = [];

private bool $allowNonFixableRules = false;

public function __construct(private string $name = 'Default')
{
$this->ruleset = new Ruleset();
Expand Down Expand Up @@ -143,4 +145,19 @@ public function getTokenParsers(): array
{
return $this->tokenParsers;
}

/**
* @return $this
*/
public function allowNonFixableRules(bool $allowNonFixableRules = true): self
{
$this->allowNonFixableRules = $allowNonFixableRules;

return $this;
}

public function areNonFixableRulesAllowed(): bool
{
return $this->allowNonFixableRules;
}
}
11 changes: 7 additions & 4 deletions src/Config/ConfigResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,22 @@ public function __construct(private string $workingDir)
public function resolveConfig(
array $paths = [],
?string $configPath = null,
bool $disableCache = false
bool $disableCache = false,
): Config {
$config = $this->getConfig($configPath);
$config->setFinder($this->resolveFinder($config->getFinder(), $paths));

// Override ruleset with config
$config->getRuleset()->allowNonFixableRules($config->areNonFixableRulesAllowed());

if ($disableCache) {
$config->setCacheFile(null);
$config->setCacheManager(null);
} else {
$config->setCacheManager($this->resolveCacheManager(
$config->getCacheManager(),
$config->getCacheFile(),
$config->getRuleset()
$config->getRuleset(),
));
}

Expand Down Expand Up @@ -132,7 +135,7 @@ private function resolveFinder(Finder $finder, array $paths): Finder
private function resolveCacheManager(
?CacheManagerInterface $cacheManager,
?string $cacheFile,
Ruleset $ruleset
Ruleset $ruleset,
): ?CacheManagerInterface {
if (null !== $cacheManager) {
return $cacheManager;
Expand All @@ -147,7 +150,7 @@ private function resolveCacheManager(
Signature::fromRuleset(
\PHP_VERSION,
InstalledVersions::getReference(self::PACKAGE_NAME) ?? '0',
$ruleset
$ruleset,
)
);
}
Expand Down
58 changes: 58 additions & 0 deletions src/Rules/AbstractFixableRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

namespace TwigCsFixer\Rules;

use TwigCsFixer\Report\Report;
use TwigCsFixer\Runner\FixerInterface;
use TwigCsFixer\Token\Token;

abstract class AbstractFixableRule extends AbstractRule implements FixableRuleInterface
{
private ?FixerInterface $fixer = null;

protected function init(
?Report $report,
array $ignoredViolations = [],
?FixerInterface $fixer = null
): void {
parent::init($report, $ignoredViolations);
$this->fixer = $fixer;
}

public function fixFile(array $stream, FixerInterface $fixer, array $ignoredViolations = []): void
{
$this->init(null, $ignoredViolations, $fixer);

foreach (array_keys($stream) as $index) {
$this->process($index, $stream);
}
}

protected function addFixableWarning(
string $message,
Token $token,
?string $messageId = null
): ?FixerInterface {
$added = $this->addWarning($message, $token, $messageId);
if (!$added) {
return null;
}

return $this->fixer;
}

protected function addFixableError(
string $message,
Token $token,
?string $messageId = null
): ?FixerInterface {
$added = $this->addError($message, $token, $messageId);
if (!$added) {
return null;
}

return $this->fixer;
}
}
45 changes: 6 additions & 39 deletions src/Rules/AbstractRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,12 @@
use TwigCsFixer\Report\Report;
use TwigCsFixer\Report\Violation;
use TwigCsFixer\Report\ViolationId;
use TwigCsFixer\Runner\FixerInterface;
use TwigCsFixer\Token\Token;

abstract class AbstractRule implements RuleInterface
{
private ?Report $report = null;

private ?FixerInterface $fixer = null;

/**
* @var list<ViolationId>
*/
Expand All @@ -36,24 +33,20 @@ public function getShortName(): string

public function lintFile(array $stream, Report $report, array $ignoredViolations = []): void
{
$this->report = $report;
$this->fixer = null;
$this->ignoredViolations = $ignoredViolations;
$this->init($report, $ignoredViolations);

foreach (array_keys($stream) as $index) {
$this->process($index, $stream);
}
}

public function fixFile(array $stream, FixerInterface $fixer, array $ignoredViolations = []): void
/**
* @param list<ViolationId> $ignoredViolations
*/
protected function init(?Report $report, array $ignoredViolations = []): void
{
$this->report = null;
$this->fixer = $fixer;
$this->report = $report;
$this->ignoredViolations = $ignoredViolations;

foreach (array_keys($stream) as $index) {
$this->process($index, $stream);
}
}

/**
Expand Down Expand Up @@ -170,32 +163,6 @@ protected function addFileError(string $message, Token $token, ?string $messageI
);
}

protected function addFixableWarning(
string $message,
Token $token,
?string $messageId = null
): ?FixerInterface {
$added = $this->addWarning($message, $token, $messageId);
if (!$added) {
return null;
}

return $this->fixer;
}

protected function addFixableError(
string $message,
Token $token,
?string $messageId = null
): ?FixerInterface {
$added = $this->addError($message, $token, $messageId);
if (!$added) {
return null;
}

return $this->fixer;
}

private function addMessage(
int $messageType,
string $message,
Expand Down
2 changes: 1 addition & 1 deletion src/Rules/AbstractSpacingRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
/**
* Ensures there is one space before or after some tokens
*/
abstract class AbstractSpacingRule extends AbstractRule
abstract class AbstractSpacingRule extends AbstractFixableRule
{
protected function process(int $tokenPosition, array $tokens): void
{
Expand Down
2 changes: 1 addition & 1 deletion src/Rules/ConfigurableRuleInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
interface ConfigurableRuleInterface extends RuleInterface
{
/**
* @return array<mixed>
* @return array<string, mixed>
*/
public function getConfiguration(): array;
}
2 changes: 1 addition & 1 deletion src/Rules/File/DirectoryNameRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use TwigCsFixer\Rules\ConfigurableRuleInterface;

/**
* Ensures directory name is snake_case (Configurable).
* Ensures that directory name use snake_case (Configurable).
*/
final class DirectoryNameRule extends AbstractRule implements ConfigurableRuleInterface
{
Expand Down
2 changes: 1 addition & 1 deletion src/Rules/File/FileNameRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use TwigCsFixer\Rules\ConfigurableRuleInterface;

/**
* Ensures file name is snake_case (Configurable).
* Ensures that file name use snake_case (Configurable).
*/
final class FileNameRule extends AbstractRule implements ConfigurableRuleInterface
{
Expand Down
18 changes: 18 additions & 0 deletions src/Rules/FixableRuleInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace TwigCsFixer\Rules;

use TwigCsFixer\Report\ViolationId;
use TwigCsFixer\Runner\FixerInterface;
use TwigCsFixer\Token\Token;

interface FixableRuleInterface
{
/**
* @param array<int, Token> $stream
* @param list<ViolationId> $ignoredViolations
*/
public function fixFile(array $stream, FixerInterface $fixer, array $ignoredViolations = []): void;
}
4 changes: 2 additions & 2 deletions src/Rules/Operator/OperatorNameSpacingRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

namespace TwigCsFixer\Rules\Operator;

use TwigCsFixer\Rules\AbstractRule;
use TwigCsFixer\Rules\AbstractFixableRule;
use TwigCsFixer\Token\Token;

/**
* Ensures there is no consecutive spaces inside operator names.
*/
final class OperatorNameSpacingRule extends AbstractRule
final class OperatorNameSpacingRule extends AbstractFixableRule
{
protected function process(int $tokenPosition, array $tokens): void
{
Expand Down
4 changes: 2 additions & 2 deletions src/Rules/Punctuation/TrailingCommaSingleLineRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

namespace TwigCsFixer\Rules\Punctuation;

use TwigCsFixer\Rules\AbstractRule;
use TwigCsFixer\Rules\AbstractFixableRule;
use TwigCsFixer\Token\Token;
use Webmozart\Assert\Assert;

/**
* Ensures that single-line arrays, objects and argument lists do not have a trailing comma.
*/
final class TrailingCommaSingleLineRule extends AbstractRule
final class TrailingCommaSingleLineRule extends AbstractFixableRule
{
protected function process(int $tokenPosition, array $tokens): void
{
Expand Down
7 changes: 0 additions & 7 deletions src/Rules/RuleInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use TwigCsFixer\Report\Report;
use TwigCsFixer\Report\ViolationId;
use TwigCsFixer\Runner\FixerInterface;
use TwigCsFixer\Token\Token;

interface RuleInterface
Expand All @@ -18,10 +17,4 @@ interface RuleInterface
* @param list<ViolationId> $ignoredViolations
*/
public function lintFile(array $stream, Report $report, array $ignoredViolations = []): void;

/**
* @param array<int, Token> $stream
* @param list<ViolationId> $ignoredViolations
*/
public function fixFile(array $stream, FixerInterface $fixer, array $ignoredViolations = []): void;
}
Loading

0 comments on commit b850eca

Please sign in to comment.