Skip to content

Commit

Permalink
Upgrade to friendsoftwig/twigcs v5
Browse files Browse the repository at this point in the history
  • Loading branch information
joelambert committed Mar 5, 2021
1 parent e527711 commit f349df0
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 51 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ This is a custom ruleset for [Twigcs](https://github.com/friendsoftwig/twigcs).
It follows the [official Twig coding style](http://twig.sensiolabs.org/doc/coding_standards.html) with the following exceptions:

- Use `lowerCamelCase` variables instead of `snake_case`
- Enforces 1 space inside a hash e.g. `{ key: expr, key: expr }` instead of the default `{key: expr, key: expr}`

## Installation

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
],
"require": {
"php": ">=7.2",
"friendsoftwig/twigcs": "^3.2.2"
"friendsoftwig/twigcs": "^5.0"
},
"require-dev": {},
"autoload": {
Expand Down
19 changes: 10 additions & 9 deletions src/Rule/LowerCamelCaseVariable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,24 @@

namespace Rareloop\Twigcs\Rule;

use Allocine\Twigcs\Lexer;
use Allocine\Twigcs\Token;
use Allocine\Twigcs\Rule\AbstractRule;
use Allocine\Twigcs\Rule\RuleInterface;
use FriendsOfTwig\Twigcs\Lexer;
use FriendsOfTwig\Twigcs\TwigPort\Token;
use FriendsOfTwig\Twigcs\Rule\AbstractRule;
use FriendsOfTwig\Twigcs\Rule\RuleInterface;
use FriendsOfTwig\Twigcs\TwigPort\TokenStream;

class LowerCamelCaseVariable extends AbstractRule implements RuleInterface
{
public function check(\Twig\TokenStream $tokens)
public function check(TokenStream $tokens)
{
$this->reset();
$violations = [];

while (!$tokens->isEOF()) {
$token = $tokens->getCurrent();

if ($token->getType() === \Twig\Token::NAME_TYPE && $this->isNotLowerCamelCase($token->getValue())) {
if ($token->getType() === Token::NAME_TYPE && $this->isNotLowerCamelCase($token->getValue())) {
if ($tokens->look(Lexer::PREVIOUS_TOKEN)->getType() === Token::WHITESPACE_TYPE && $tokens->look(-2)->getValue() === 'set') {
$this->addViolation(
$violations[] = $this->createViolation(
$tokens->getSourceContext()->getPath(),
$token->getLine(),
$token->columnno,
Expand All @@ -30,7 +31,7 @@ public function check(\Twig\TokenStream $tokens)
$tokens->next();
}

return $this->violations;
return $violations;
}

private function isNotLowerCamelCase(string $string): bool
Expand Down
62 changes: 21 additions & 41 deletions src/Ruleset/Rareloop.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,11 @@

namespace Rareloop\Twigcs\Ruleset;

use Allocine\Twigcs\Rule\UnusedMacro;
use Allocine\Twigcs\Rule\TrailingSpace;
use Allocine\Twigcs\Rule\TernarySpacing;
use Allocine\Twigcs\Rule\UnusedVariable;
use Allocine\Twigcs\Validator\Violation;
use Allocine\Twigcs\Rule\OperatorSpacing;
use Allocine\Twigcs\Rule\DelimiterSpacing;
use Allocine\Twigcs\Rule\ParenthesisSpacing;
use Allocine\Twigcs\Rule\PunctuationSpacing;
use Allocine\Twigcs\Ruleset\RulesetInterface;
use Allocine\Twigcs\Whitelist\TokenWhitelist;
use Allocine\Twigcs\Rule\HashSeparatorSpacing;
use Allocine\Twigcs\Rule\ArraySeparatorSpacing;
use Allocine\Twigcs\Rule\SliceShorthandSpacing;
use FriendsOfTwig\Twigcs\Rule;
use FriendsOfTwig\Twigcs\Validator\Violation;
use FriendsOfTwig\Twigcs\RegEngine\RulesetBuilder;
use FriendsOfTwig\Twigcs\Ruleset\RulesetInterface;
use FriendsOfTwig\Twigcs\RegEngine\RulesetConfigurator;
use Rareloop\Twigcs\Rule\LowerCamelCaseVariable;

/**
Expand All @@ -30,40 +21,29 @@
*/
class Rareloop implements RulesetInterface
{
private $twigMajorVersion;

public function __construct(int $twigMajorVersion)
{
$this->twigMajorVersion = $twigMajorVersion;
}

/**
* {@inheritdoc}
*/
public function getRules()
{
$configurator = new RulesetConfigurator();
$configurator->setTwigMajorVersion($this->twigMajorVersion);
$configurator->setHashSpacingPattern('{ key: expr, key: expr }');
$builder = new RulesetBuilder($configurator);

return [
new DelimiterSpacing(Violation::SEVERITY_ERROR, 1),
new ParenthesisSpacing(Violation::SEVERITY_ERROR, 0, 1),
new ArraySeparatorSpacing(Violation::SEVERITY_ERROR, 0, 1),
new HashSeparatorSpacing(Violation::SEVERITY_ERROR, 0, 1),
new OperatorSpacing(Violation::SEVERITY_ERROR, [
'==', '!=', '<', '>', '>=', '<=',
'+', '-', '/', '*', '%', '//', '**',
'not', 'and', 'or',
'~',
'is', 'in'
], 1),
new PunctuationSpacing(
Violation::SEVERITY_ERROR,
['|', '.', '..', '[', ']'],
0,
new TokenWhitelist([
')',
\Twig\Token::NAME_TYPE,
\Twig\Token::NUMBER_TYPE,
\Twig\Token::STRING_TYPE
], [2])
),
new TernarySpacing(Violation::SEVERITY_ERROR, 1),
new LowerCamelCaseVariable(Violation::SEVERITY_ERROR),
new UnusedVariable(Violation::SEVERITY_WARNING),
new UnusedMacro(Violation::SEVERITY_WARNING),
new SliceShorthandSpacing(Violation::SEVERITY_ERROR),
new TrailingSpace(Violation::SEVERITY_ERROR),
new Rule\RegEngineRule(Violation::SEVERITY_ERROR, $builder->build()),
new Rule\TrailingSpace(Violation::SEVERITY_ERROR),
new Rule\UnusedMacro(Violation::SEVERITY_WARNING),
new Rule\UnusedVariable(Violation::SEVERITY_WARNING),
];
}
}

0 comments on commit f349df0

Please sign in to comment.