Skip to content

Commit

Permalink
Allow to skip non fixable rules
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet committed Jan 26, 2024
1 parent 7139f95 commit f111b6e
Show file tree
Hide file tree
Showing 22 changed files with 180 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
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 $useOnlyFixableRules = true;

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 useOnlyFixableRules(bool $useOnlyFixableRules = true): self
{
$this->useOnlyFixableRules = $useOnlyFixableRules;

return $this;
}

public function getUseOnlyFixableRules(): bool
{
return $this->useOnlyFixableRules;
}
}
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()->useOnlyFixableRules($config->getUseOnlyFixableRules());

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;
}
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;
}
7 changes: 3 additions & 4 deletions src/Rules/Variable/VariableNameRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use TwigCsFixer\Rules\AbstractRule;
use TwigCsFixer\Rules\ConfigurableRuleInterface;
use TwigCsFixer\Token\Token;
use Webmozart\Assert\Assert;

final class VariableNameRule extends AbstractRule implements ConfigurableRuleInterface
{
Expand Down Expand Up @@ -38,10 +39,8 @@ protected function process(int $tokenPosition, array $tokens): void
return;
}

$nameTokenPosition = $this->findNext(Token::NAME_TYPE, $tokens, $tokenPosition + 1);
if (false === $nameTokenPosition) {
return;
}
$nameTokenPosition = $this->findNext(Token::NAME_TYPE, $tokens, $tokenPosition);
Assert::notFalse($nameTokenPosition, 'A BLOCK_NAME_TYPE set must be followed by a name');
$name = $tokens[$nameTokenPosition]->getValue();

$expected = match ($this->case) {
Expand Down
4 changes: 2 additions & 2 deletions src/Rules/Whitespace/BlankEOFRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

namespace TwigCsFixer\Rules\Whitespace;

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

/**
* Ensures that files end with one blank line.
*/
final class BlankEOFRule extends AbstractRule
final class BlankEOFRule extends AbstractFixableRule
{
protected function process(int $tokenPosition, array $tokens): void
{
Expand Down
4 changes: 2 additions & 2 deletions src/Rules/Whitespace/EmptyLinesRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

namespace TwigCsFixer\Rules\Whitespace;

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

/**
* Ensures that 2 empty lines do not follow each other.
*/
final class EmptyLinesRule extends AbstractRule
final class EmptyLinesRule extends AbstractFixableRule
{
protected function process(int $tokenPosition, array $tokens): void
{
Expand Down
4 changes: 2 additions & 2 deletions src/Rules/Whitespace/IndentRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

namespace TwigCsFixer\Rules\Whitespace;

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

/**
* Ensures that files are not indented with tabs.
*/
final class IndentRule extends AbstractRule implements ConfigurableRuleInterface
final class IndentRule extends AbstractFixableRule implements ConfigurableRuleInterface
{
public function __construct(private int $spaceRatio = 4)
{
Expand Down
Loading

0 comments on commit f111b6e

Please sign in to comment.