From 995d2e6eabe04d60ae8105bbb4ffaa916575d090 Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Thu, 30 May 2019 22:50:20 +0200 Subject: [PATCH 01/13] :sparkles: Add Whitespace check for delimiter --- .../EnsureWhitespaceDelimiterSniff.php | 99 +++++++++++++++++++ .../Fixtures/ensureWhitespaceDelimiter.twig | 14 +++ .../Sniff/EnsureWhitespaceDelimiterTest.php | 22 +++++ TwigCS/Token/Tokenizer.php | 3 +- 4 files changed, 137 insertions(+), 1 deletion(-) create mode 100644 TwigCS/Sniff/Standard/EnsureWhitespaceDelimiterSniff.php create mode 100644 TwigCS/Tests/Fixtures/ensureWhitespaceDelimiter.twig create mode 100644 TwigCS/Tests/Sniff/EnsureWhitespaceDelimiterTest.php diff --git a/TwigCS/Sniff/Standard/EnsureWhitespaceDelimiterSniff.php b/TwigCS/Sniff/Standard/EnsureWhitespaceDelimiterSniff.php new file mode 100644 index 0000000..f0b3502 --- /dev/null +++ b/TwigCS/Sniff/Standard/EnsureWhitespaceDelimiterSniff.php @@ -0,0 +1,99 @@ +isTokenMatching($token, Token::VAR_START_TYPE) + || $this->isTokenMatching($token, Token::BLOCK_START_TYPE) + || $this->isTokenMatching($token, Token::COMMENT_START_TYPE) + ) { + $this->processStart($token, $tokenPosition, $tokens); + } + + if ($this->isTokenMatching($token, Token::VAR_END_TYPE) + || $this->isTokenMatching($token, Token::BLOCK_END_TYPE) + || $this->isTokenMatching($token, Token::COMMENT_END_TYPE) + ) { + $this->processEnd($token, $tokenPosition, $tokens); + } + + return $token; + } + + /** + * @param Token $token + * @param int $tokenPosition + * @param Token[] $tokens + * + * @throws Exception + */ + public function processStart(Token $token, $tokenPosition, $tokens) + { + $offset = 1; + while ($this->isTokenMatching($tokens[$tokenPosition + $offset], Token::WHITESPACE_TYPE)) { + ++$offset; + } + + // Ignore new line + if ($this->isTokenMatching($tokens[$tokenPosition + $offset], Token::EOL_TYPE)) { + return; + } + + $count = $offset - 1; + if (1 !== $count) { + $this->addMessage( + $this::MESSAGE_TYPE_ERROR, + sprintf('Expecting 1 whitespace AFTER start of expression eg. "{{" or "{%%"; found %d', $count), + $token + ); + } + } + + /** + * @param Token $token + * @param int $tokenPosition + * @param Token[] $tokens + * + * @throws Exception + */ + public function processEnd(Token $token, $tokenPosition, $tokens) + { + $offset = 1; + while ($this->isTokenMatching($tokens[$tokenPosition - $offset], Token::WHITESPACE_TYPE)) { + ++$offset; + } + + // Ignore new line + if ($this->isTokenMatching($tokens[$tokenPosition - $offset], Token::EOL_TYPE)) { + return; + } + + $count = $offset - 1; + if (1 !== $count) { + $this->addMessage( + $this::MESSAGE_TYPE_ERROR, + sprintf('Expecting 1 whitespace BEFORE end of expression eg. "}}" or "%%}"; found %d', $count), + $token + ); + } + } +} diff --git a/TwigCS/Tests/Fixtures/ensureWhitespaceDelimiter.twig b/TwigCS/Tests/Fixtures/ensureWhitespaceDelimiter.twig new file mode 100644 index 0000000..273ea3d --- /dev/null +++ b/TwigCS/Tests/Fixtures/ensureWhitespaceDelimiter.twig @@ -0,0 +1,14 @@ +{{ foo }} +{# comment #} +{% if foo %}{% endif %} + +{{- foo -}} +{#- comment -#} +{%- if foo -%}{%- endif -%} + +{{ + shouldNotCareAboutNewLine +}} +{%-if foo -%}{%- endif-%} + +{{ foo({'bar': {'baz': 'shouldNotCareAboutDoubleHashes'}}) }} diff --git a/TwigCS/Tests/Sniff/EnsureWhitespaceDelimiterTest.php b/TwigCS/Tests/Sniff/EnsureWhitespaceDelimiterTest.php new file mode 100644 index 0000000..8d47e94 --- /dev/null +++ b/TwigCS/Tests/Sniff/EnsureWhitespaceDelimiterTest.php @@ -0,0 +1,22 @@ +checkGenericSniff('ensureWhitespaceDelimiter.twig', new EnsureWhitespaceDelimiterSniff(), [ + [12, 1], + [12, 12], + [12, 15], + [12, 25], + ]); + } +} diff --git a/TwigCS/Token/Tokenizer.php b/TwigCS/Token/Tokenizer.php index 68aa19d..536de34 100644 --- a/TwigCS/Token/Tokenizer.php +++ b/TwigCS/Token/Tokenizer.php @@ -279,7 +279,7 @@ protected function lex($endType, $endRegex) { preg_match($endRegex, $this->code, $match, PREG_OFFSET_CAPTURE, $this->cursor); - if (!isset($match[0])) { + if (!empty($this->brackets) || !isset($match[0])) { $this->lexExpression(); } elseif ($match[0][1] === $this->cursor) { $this->pushToken($endType, $match[0][0]); @@ -290,6 +290,7 @@ protected function lex($endType, $endRegex) // Parse as text until the end position. $this->lexData($match[0][1]); } else { + // Should not happen while ($this->cursor < $match[0][1]) { $this->lexExpression(); } From e9737225d14aba9bc482148b4dc3e383c35ff5b4 Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Thu, 30 May 2019 23:45:21 +0200 Subject: [PATCH 02/13] :package: Rename sniff --- ...eBlankAtEOFSniff.php => BlankEOFSniff.php} | 2 +- ...terSniff.php => DelimiterSpacingSniff.php} | 2 +- ...Sniff.php => ForbidCommentedCodeSniff.php} | 2 +- .../{ensureBlankAtEOF.twig => blankEOF.twig} | 0 ...ceDelimiter.twig => delimiterSpacing.twig} | 0 ...ntedCode.twig => forbidCommentedCode.twig} | 0 TwigCS/Tests/Sniff/BlankEOFTest.php | 19 ++++++++++++++++ TwigCS/Tests/Sniff/DelimiterSpacingTest.php | 22 +++++++++++++++++++ .../Tests/Sniff/DisallowCommentedCodeTest.php | 19 ---------------- TwigCS/Tests/Sniff/EnsureBlankAtEOFTest.php | 19 ---------------- .../Sniff/EnsureWhitespaceDelimiterTest.php | 22 ------------------- .../Tests/Sniff/ForbidCommentedCodeTest.php | 19 ++++++++++++++++ 12 files changed, 63 insertions(+), 63 deletions(-) rename TwigCS/Sniff/Standard/{EnsureBlankAtEOFSniff.php => BlankEOFSniff.php} (94%) rename TwigCS/Sniff/Standard/{EnsureWhitespaceDelimiterSniff.php => DelimiterSpacingSniff.php} (97%) rename TwigCS/Sniff/Standard/{DisallowCommentedCodeSniff.php => ForbidCommentedCodeSniff.php} (95%) rename TwigCS/Tests/Fixtures/{ensureBlankAtEOF.twig => blankEOF.twig} (100%) rename TwigCS/Tests/Fixtures/{ensureWhitespaceDelimiter.twig => delimiterSpacing.twig} (100%) rename TwigCS/Tests/Fixtures/{disallowCommentedCode.twig => forbidCommentedCode.twig} (100%) create mode 100644 TwigCS/Tests/Sniff/BlankEOFTest.php create mode 100644 TwigCS/Tests/Sniff/DelimiterSpacingTest.php delete mode 100644 TwigCS/Tests/Sniff/DisallowCommentedCodeTest.php delete mode 100644 TwigCS/Tests/Sniff/EnsureBlankAtEOFTest.php delete mode 100644 TwigCS/Tests/Sniff/EnsureWhitespaceDelimiterTest.php create mode 100644 TwigCS/Tests/Sniff/ForbidCommentedCodeTest.php diff --git a/TwigCS/Sniff/Standard/EnsureBlankAtEOFSniff.php b/TwigCS/Sniff/Standard/BlankEOFSniff.php similarity index 94% rename from TwigCS/Sniff/Standard/EnsureBlankAtEOFSniff.php rename to TwigCS/Sniff/Standard/BlankEOFSniff.php index cbbfc81..3093e86 100644 --- a/TwigCS/Sniff/Standard/EnsureBlankAtEOFSniff.php +++ b/TwigCS/Sniff/Standard/BlankEOFSniff.php @@ -9,7 +9,7 @@ /** * Ensure that files ends with one blank line. */ -class EnsureBlankAtEOFSniff extends AbstractPreParserSniff +class BlankEOFSniff extends AbstractPreParserSniff { /** * @param Token $token diff --git a/TwigCS/Sniff/Standard/EnsureWhitespaceDelimiterSniff.php b/TwigCS/Sniff/Standard/DelimiterSpacingSniff.php similarity index 97% rename from TwigCS/Sniff/Standard/EnsureWhitespaceDelimiterSniff.php rename to TwigCS/Sniff/Standard/DelimiterSpacingSniff.php index f0b3502..d1f4cf0 100644 --- a/TwigCS/Sniff/Standard/EnsureWhitespaceDelimiterSniff.php +++ b/TwigCS/Sniff/Standard/DelimiterSpacingSniff.php @@ -9,7 +9,7 @@ /** * Ensure there is one space before and after a delimiter {{, {%, {#, }}, %} and #} */ -class EnsureWhitespaceDelimiterSniff extends AbstractPreParserSniff +class DelimiterSpacingSniff extends AbstractPreParserSniff { /** * @param Token $token diff --git a/TwigCS/Sniff/Standard/DisallowCommentedCodeSniff.php b/TwigCS/Sniff/Standard/ForbidCommentedCodeSniff.php similarity index 95% rename from TwigCS/Sniff/Standard/DisallowCommentedCodeSniff.php rename to TwigCS/Sniff/Standard/ForbidCommentedCodeSniff.php index 3845c3c..3d09e1e 100644 --- a/TwigCS/Sniff/Standard/DisallowCommentedCodeSniff.php +++ b/TwigCS/Sniff/Standard/ForbidCommentedCodeSniff.php @@ -11,7 +11,7 @@ * * This will be triggered if `{{` or `{%` is found inside a comment. */ -class DisallowCommentedCodeSniff extends AbstractPreParserSniff +class ForbidCommentedCodeSniff extends AbstractPreParserSniff { /** * @param Token $token diff --git a/TwigCS/Tests/Fixtures/ensureBlankAtEOF.twig b/TwigCS/Tests/Fixtures/blankEOF.twig similarity index 100% rename from TwigCS/Tests/Fixtures/ensureBlankAtEOF.twig rename to TwigCS/Tests/Fixtures/blankEOF.twig diff --git a/TwigCS/Tests/Fixtures/ensureWhitespaceDelimiter.twig b/TwigCS/Tests/Fixtures/delimiterSpacing.twig similarity index 100% rename from TwigCS/Tests/Fixtures/ensureWhitespaceDelimiter.twig rename to TwigCS/Tests/Fixtures/delimiterSpacing.twig diff --git a/TwigCS/Tests/Fixtures/disallowCommentedCode.twig b/TwigCS/Tests/Fixtures/forbidCommentedCode.twig similarity index 100% rename from TwigCS/Tests/Fixtures/disallowCommentedCode.twig rename to TwigCS/Tests/Fixtures/forbidCommentedCode.twig diff --git a/TwigCS/Tests/Sniff/BlankEOFTest.php b/TwigCS/Tests/Sniff/BlankEOFTest.php new file mode 100644 index 0000000..ee8152e --- /dev/null +++ b/TwigCS/Tests/Sniff/BlankEOFTest.php @@ -0,0 +1,19 @@ +checkGenericSniff('blankEOF.twig', new BlankEOFSniff(), [ + [4, 1], + ]); + } +} diff --git a/TwigCS/Tests/Sniff/DelimiterSpacingTest.php b/TwigCS/Tests/Sniff/DelimiterSpacingTest.php new file mode 100644 index 0000000..890c6a3 --- /dev/null +++ b/TwigCS/Tests/Sniff/DelimiterSpacingTest.php @@ -0,0 +1,22 @@ +checkGenericSniff('delimiterSpacing.twig', new DelimiterSpacingSniff(), [ + [12, 1], + [12, 12], + [12, 15], + [12, 25], + ]); + } +} diff --git a/TwigCS/Tests/Sniff/DisallowCommentedCodeTest.php b/TwigCS/Tests/Sniff/DisallowCommentedCodeTest.php deleted file mode 100644 index 40e5b53..0000000 --- a/TwigCS/Tests/Sniff/DisallowCommentedCodeTest.php +++ /dev/null @@ -1,19 +0,0 @@ -checkGenericSniff('disallowCommentedCode.twig', new DisallowCommentedCodeSniff(), [ - [2, 5], - ]); - } -} diff --git a/TwigCS/Tests/Sniff/EnsureBlankAtEOFTest.php b/TwigCS/Tests/Sniff/EnsureBlankAtEOFTest.php deleted file mode 100644 index 7b8b7c4..0000000 --- a/TwigCS/Tests/Sniff/EnsureBlankAtEOFTest.php +++ /dev/null @@ -1,19 +0,0 @@ -checkGenericSniff('ensureBlankAtEOF.twig', new EnsureBlankAtEOFSniff(), [ - [4, 1], - ]); - } -} diff --git a/TwigCS/Tests/Sniff/EnsureWhitespaceDelimiterTest.php b/TwigCS/Tests/Sniff/EnsureWhitespaceDelimiterTest.php deleted file mode 100644 index 8d47e94..0000000 --- a/TwigCS/Tests/Sniff/EnsureWhitespaceDelimiterTest.php +++ /dev/null @@ -1,22 +0,0 @@ -checkGenericSniff('ensureWhitespaceDelimiter.twig', new EnsureWhitespaceDelimiterSniff(), [ - [12, 1], - [12, 12], - [12, 15], - [12, 25], - ]); - } -} diff --git a/TwigCS/Tests/Sniff/ForbidCommentedCodeTest.php b/TwigCS/Tests/Sniff/ForbidCommentedCodeTest.php new file mode 100644 index 0000000..37ecd5d --- /dev/null +++ b/TwigCS/Tests/Sniff/ForbidCommentedCodeTest.php @@ -0,0 +1,19 @@ +checkGenericSniff('forbidCommentedCode.twig', new ForbidCommentedCodeSniff(), [ + [2, 5], + ]); + } +} From 4b3a88798f956cb6543871c3f8ca675842d569a3 Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Thu, 30 May 2019 23:53:47 +0200 Subject: [PATCH 03/13] :lipstick: Improve error message --- TwigCS/Sniff/Standard/DelimiterSpacingSniff.php | 4 ++-- TwigCS/Sniff/Standard/ForbidCommentedCodeSniff.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/TwigCS/Sniff/Standard/DelimiterSpacingSniff.php b/TwigCS/Sniff/Standard/DelimiterSpacingSniff.php index d1f4cf0..8f5c57f 100644 --- a/TwigCS/Sniff/Standard/DelimiterSpacingSniff.php +++ b/TwigCS/Sniff/Standard/DelimiterSpacingSniff.php @@ -62,7 +62,7 @@ public function processStart(Token $token, $tokenPosition, $tokens) if (1 !== $count) { $this->addMessage( $this::MESSAGE_TYPE_ERROR, - sprintf('Expecting 1 whitespace AFTER start of expression eg. "{{" or "{%%"; found %d', $count), + sprintf('Expecting 1 whitespace after "%s"; found %d', $token->getValue(), $count), $token ); } @@ -91,7 +91,7 @@ public function processEnd(Token $token, $tokenPosition, $tokens) if (1 !== $count) { $this->addMessage( $this::MESSAGE_TYPE_ERROR, - sprintf('Expecting 1 whitespace BEFORE end of expression eg. "}}" or "%%}"; found %d', $count), + sprintf('Expecting 1 whitespace before "%s"; found %d', $token->getValue(), $count), $token ); } diff --git a/TwigCS/Sniff/Standard/ForbidCommentedCodeSniff.php b/TwigCS/Sniff/Standard/ForbidCommentedCodeSniff.php index 3d09e1e..9f9116d 100644 --- a/TwigCS/Sniff/Standard/ForbidCommentedCodeSniff.php +++ b/TwigCS/Sniff/Standard/ForbidCommentedCodeSniff.php @@ -44,7 +44,7 @@ public function process(Token $token, $tokenPosition, $tokens) if ($found) { $this->addMessage( $this::MESSAGE_TYPE_WARNING, - 'Probable commented code found; keeping commented code is usually not advised', + 'Probable commented code found; keeping commented code is not advised', $token ); } From b747e36cd1502e6c3099d9876581f1890aad5915 Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Sat, 1 Jun 2019 13:02:20 +0200 Subject: [PATCH 04/13] :sparkles: Group multiple Tab or multiple Whitespace in one token --- .../Sniff/Standard/DelimiterSpacingSniff.php | 28 +++++++++---------- TwigCS/Token/Tokenizer.php | 24 +++++++++++++--- 2 files changed, 34 insertions(+), 18 deletions(-) diff --git a/TwigCS/Sniff/Standard/DelimiterSpacingSniff.php b/TwigCS/Sniff/Standard/DelimiterSpacingSniff.php index 8f5c57f..2f720cd 100644 --- a/TwigCS/Sniff/Standard/DelimiterSpacingSniff.php +++ b/TwigCS/Sniff/Standard/DelimiterSpacingSniff.php @@ -48,17 +48,17 @@ public function process(Token $token, $tokenPosition, $tokens) */ public function processStart(Token $token, $tokenPosition, $tokens) { - $offset = 1; - while ($this->isTokenMatching($tokens[$tokenPosition + $offset], Token::WHITESPACE_TYPE)) { - ++$offset; - } - // Ignore new line - if ($this->isTokenMatching($tokens[$tokenPosition + $offset], Token::EOL_TYPE)) { + if ($this->isTokenMatching($tokens[$tokenPosition + 1], Token::EOL_TYPE)) { return; } - $count = $offset - 1; + if ($this->isTokenMatching($tokens[$tokenPosition + 1], Token::WHITESPACE_TYPE)) { + $count = strlen($tokens[$tokenPosition + 1]->getValue()); + } else { + $count = 0; + } + if (1 !== $count) { $this->addMessage( $this::MESSAGE_TYPE_ERROR, @@ -77,17 +77,17 @@ public function processStart(Token $token, $tokenPosition, $tokens) */ public function processEnd(Token $token, $tokenPosition, $tokens) { - $offset = 1; - while ($this->isTokenMatching($tokens[$tokenPosition - $offset], Token::WHITESPACE_TYPE)) { - ++$offset; - } - // Ignore new line - if ($this->isTokenMatching($tokens[$tokenPosition - $offset], Token::EOL_TYPE)) { + if ($this->isTokenMatching($tokens[$tokenPosition - 1], Token::EOL_TYPE)) { return; } - $count = $offset - 1; + if ($this->isTokenMatching($tokens[$tokenPosition - 1], Token::WHITESPACE_TYPE)) { + $count = strlen($tokens[$tokenPosition - 1]->getValue()); + } else { + $count = 0; + } + if (1 !== $count) { $this->addMessage( $this::MESSAGE_TYPE_ERROR, diff --git a/TwigCS/Token/Tokenizer.php b/TwigCS/Token/Tokenizer.php index 536de34..0d3a48d 100644 --- a/TwigCS/Token/Tokenizer.php +++ b/TwigCS/Token/Tokenizer.php @@ -435,14 +435,30 @@ protected function lexStart() protected function lexTab() { - $this->pushToken(Token::TAB_TYPE); - $this->moveCursor($this->code[$this->cursor]); + $currentToken = $this->code[$this->cursor]; + $whitespace = ''; + + while (preg_match('/\t/', $currentToken)) { + $whitespace .= $currentToken; + $this->moveCursor($currentToken); + $currentToken = $this->code[$this->cursor]; + } + + $this->pushToken(Token::TAB_TYPE, $whitespace); } protected function lexWhitespace() { - $this->pushToken(Token::WHITESPACE_TYPE, $this->code[$this->cursor]); - $this->moveCursor($this->code[$this->cursor]); + $currentToken = $this->code[$this->cursor]; + $whitespace = ''; + + while (' ' === $currentToken) { + $whitespace .= $currentToken; + $this->moveCursor($currentToken); + $currentToken = $this->code[$this->cursor]; + } + + $this->pushToken(Token::WHITESPACE_TYPE, $whitespace); } protected function lexEOL() From 584335e770ecc9b0f1ed9b73b01e52aae4967544 Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Sat, 1 Jun 2019 20:18:30 +0200 Subject: [PATCH 05/13] :rotating_light: Improve tests --- TwigCS/Tests/AbstractSniffTest.php | 9 +++++---- .../Tests/Fixtures/{blankEOF.twig => BlankEOFTest.twig} | 0 .../{delimiterSpacing.twig => DelimiterSpacingTest.twig} | 0 ...idCommentedCode.twig => ForbidCommentedCodeTest.twig} | 0 TwigCS/Tests/Sniff/BlankEOFTest.php | 4 ++-- TwigCS/Tests/Sniff/DelimiterSpacingTest.php | 4 ++-- TwigCS/Tests/Sniff/ForbidCommentedCodeTest.php | 4 ++-- 7 files changed, 11 insertions(+), 10 deletions(-) rename TwigCS/Tests/Fixtures/{blankEOF.twig => BlankEOFTest.twig} (100%) rename TwigCS/Tests/Fixtures/{delimiterSpacing.twig => DelimiterSpacingTest.twig} (100%) rename TwigCS/Tests/Fixtures/{forbidCommentedCode.twig => ForbidCommentedCodeTest.twig} (100%) diff --git a/TwigCS/Tests/AbstractSniffTest.php b/TwigCS/Tests/AbstractSniffTest.php index 5701d16..c5bd14d 100644 --- a/TwigCS/Tests/AbstractSniffTest.php +++ b/TwigCS/Tests/AbstractSniffTest.php @@ -3,6 +3,7 @@ namespace TwigCS\Tests; use \Exception; +use \ReflectionClass; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Twig\Loader\LoaderInterface; @@ -43,16 +44,16 @@ public function setUp() } /** - * @param string $filename * @param SniffInterface $sniff * @param array $expects */ - protected function checkGenericSniff($filename, SniffInterface $sniff, array $expects) + protected function checkGenericSniff(SniffInterface $sniff, array $expects) { - $file = __DIR__.'/Fixtures/'.$filename; - $ruleset = new Ruleset(); try { + $class = new ReflectionClass(get_called_class()); + $file = __DIR__.'/Fixtures/'.$class->getShortName().'.twig'; + $ruleset->addSniff($sniff); $report = $this->lint->run($file, $ruleset); } catch (Exception $e) { diff --git a/TwigCS/Tests/Fixtures/blankEOF.twig b/TwigCS/Tests/Fixtures/BlankEOFTest.twig similarity index 100% rename from TwigCS/Tests/Fixtures/blankEOF.twig rename to TwigCS/Tests/Fixtures/BlankEOFTest.twig diff --git a/TwigCS/Tests/Fixtures/delimiterSpacing.twig b/TwigCS/Tests/Fixtures/DelimiterSpacingTest.twig similarity index 100% rename from TwigCS/Tests/Fixtures/delimiterSpacing.twig rename to TwigCS/Tests/Fixtures/DelimiterSpacingTest.twig diff --git a/TwigCS/Tests/Fixtures/forbidCommentedCode.twig b/TwigCS/Tests/Fixtures/ForbidCommentedCodeTest.twig similarity index 100% rename from TwigCS/Tests/Fixtures/forbidCommentedCode.twig rename to TwigCS/Tests/Fixtures/ForbidCommentedCodeTest.twig diff --git a/TwigCS/Tests/Sniff/BlankEOFTest.php b/TwigCS/Tests/Sniff/BlankEOFTest.php index ee8152e..5042e77 100644 --- a/TwigCS/Tests/Sniff/BlankEOFTest.php +++ b/TwigCS/Tests/Sniff/BlankEOFTest.php @@ -10,9 +10,9 @@ */ class BlankEOFTest extends AbstractSniffTest { - public function testSniff1() + public function testSniff() { - $this->checkGenericSniff('blankEOF.twig', new BlankEOFSniff(), [ + $this->checkGenericSniff(new BlankEOFSniff(), [ [4, 1], ]); } diff --git a/TwigCS/Tests/Sniff/DelimiterSpacingTest.php b/TwigCS/Tests/Sniff/DelimiterSpacingTest.php index 890c6a3..dd7ec9f 100644 --- a/TwigCS/Tests/Sniff/DelimiterSpacingTest.php +++ b/TwigCS/Tests/Sniff/DelimiterSpacingTest.php @@ -10,9 +10,9 @@ */ class DelimiterSpacingTest extends AbstractSniffTest { - public function testSniff1() + public function testSniff() { - $this->checkGenericSniff('delimiterSpacing.twig', new DelimiterSpacingSniff(), [ + $this->checkGenericSniff(new DelimiterSpacingSniff(), [ [12, 1], [12, 12], [12, 15], diff --git a/TwigCS/Tests/Sniff/ForbidCommentedCodeTest.php b/TwigCS/Tests/Sniff/ForbidCommentedCodeTest.php index 37ecd5d..ac9cad1 100644 --- a/TwigCS/Tests/Sniff/ForbidCommentedCodeTest.php +++ b/TwigCS/Tests/Sniff/ForbidCommentedCodeTest.php @@ -10,9 +10,9 @@ */ class ForbidCommentedCodeTest extends AbstractSniffTest { - public function testSniff1() + public function testSniff() { - $this->checkGenericSniff('forbidCommentedCode.twig', new ForbidCommentedCodeSniff(), [ + $this->checkGenericSniff(new ForbidCommentedCodeSniff(), [ [2, 5], ]); } From fe9837d293ee1e2aa1900abc5b9d6cb045de1701 Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Sat, 1 Jun 2019 21:11:28 +0200 Subject: [PATCH 06/13] :package: Remove not used functionnality --- TwigCS/Command/TwigCSCommand.php | 2 +- TwigCS/Environment/StubbedEnvironment.php | 37 +--- TwigCS/Extension/SniffsExtension.php | 64 ------- TwigCS/Extension/SniffsNodeVisitor.php | 139 -------------- TwigCS/Linter.php | 22 +-- TwigCS/Ruleset/Ruleset.php | 69 +------ TwigCS/Ruleset/RulesetFactory.php | 3 - TwigCS/Sniff/AbstractPostParserSniff.php | 171 ------------------ TwigCS/Sniff/AbstractPreParserSniff.php | 81 --------- TwigCS/Sniff/AbstractSniff.php | 54 +++++- TwigCS/Sniff/PostParserSniffInterface.php | 18 -- TwigCS/Sniff/PreParserSniffInterface.php | 18 -- TwigCS/Sniff/SniffInterface.php | 16 +- TwigCS/Sniff/Standard/BlankEOFSniff.php | 4 +- .../Sniff/Standard/DelimiterSpacingSniff.php | 4 +- .../Standard/ForbidCommentedCodeSniff.php | 4 +- TwigCS/Tests/AbstractSniffTest.php | 8 +- 17 files changed, 79 insertions(+), 635 deletions(-) delete mode 100644 TwigCS/Extension/SniffsExtension.php delete mode 100644 TwigCS/Extension/SniffsNodeVisitor.php delete mode 100644 TwigCS/Sniff/AbstractPostParserSniff.php delete mode 100644 TwigCS/Sniff/AbstractPreParserSniff.php delete mode 100644 TwigCS/Sniff/PostParserSniffInterface.php delete mode 100644 TwigCS/Sniff/PreParserSniffInterface.php diff --git a/TwigCS/Command/TwigCSCommand.php b/TwigCS/Command/TwigCSCommand.php index d8b472b..42e3286 100644 --- a/TwigCS/Command/TwigCSCommand.php +++ b/TwigCS/Command/TwigCSCommand.php @@ -81,7 +81,7 @@ protected function execute(InputInterface $input, OutputInterface $output) 'workingDirectory' => $currentDir, ]); - $twig = new StubbedEnvironment(new ArrayLoader(), ['stub_tags' => $config->get('stub')]); + $twig = new StubbedEnvironment(new ArrayLoader()); $linter = new Linter($twig, new Tokenizer($twig)); $factory = new RulesetFactory(); $reporter = new TextFormatter($input, $output); diff --git a/TwigCS/Environment/StubbedEnvironment.php b/TwigCS/Environment/StubbedEnvironment.php index 53596ae..a398e98 100644 --- a/TwigCS/Environment/StubbedEnvironment.php +++ b/TwigCS/Environment/StubbedEnvironment.php @@ -14,8 +14,6 @@ use Twig\TwigFilter; use Twig\TwigFunction; use Twig\TwigTest; -use TwigCS\Extension\SniffsExtension; -use TwigCS\Token\TokenParser; /** * Provide stubs for all filters, functions, tests and tags that are not defined in twig's core. @@ -56,28 +54,6 @@ public function __construct(LoaderInterface $loader = null, $options = []) $this->addTokenParser(new TransChoiceTokenParser()); $this->addTokenParser(new TransDefaultDomainTokenParser()); $this->addTokenParser(new TransTokenParser()); - - $this->stubCallable = function () { - /* This will be used as stub filter, function or test */ - }; - - $this->stubFilters = []; - $this->stubFunctions = []; - - if (isset($options['stub_tags'])) { - foreach ($options['stub_tags'] as $tag) { - $this->addTokenParser(new TokenParser($tag)); - } - } - - $this->stubTests = []; - if (isset($options['stub_tests'])) { - foreach ($options['stub_tests'] as $test) { - $this->stubTests[$test] = new TwigTest('stub', $this->stubCallable); - } - } - - $this->addExtension(new SniffsExtension()); } /** @@ -111,19 +87,14 @@ public function getFunction($name) /** * @param string $name * - * @return false|TwigTest + * @return TwigTest */ public function getTest($name) { - $test = parent::getTest($name); - if ($test) { - return $test; - } - - if (isset($this->stubTests[$name])) { - return $this->stubTests[$name]; + if (!isset($this->stubTests[$name])) { + $this->stubTests[$name] = new TwigTest('stub', $this->stubCallable); } - return false; + return $this->stubTests[$name]; } } diff --git a/TwigCS/Extension/SniffsExtension.php b/TwigCS/Extension/SniffsExtension.php deleted file mode 100644 index 1947436..0000000 --- a/TwigCS/Extension/SniffsExtension.php +++ /dev/null @@ -1,64 +0,0 @@ -nodeVisitor = new SniffsNodeVisitor(); - } - - /** - * @return NodeVisitorInterface[] - */ - public function getNodeVisitors() - { - return [$this->nodeVisitor]; - } - - /** - * Register a sniff in the node visitor. - * - * @param PostParserSniffInterface $sniff - * - * @return self - */ - public function addSniff(PostParserSniffInterface $sniff) - { - $this->nodeVisitor->addSniff($sniff); - - return $this; - } - - /** - * Remove a sniff from the node visitor. - * - * @param PostParserSniffInterface $sniff - * - * @return self - */ - public function removeSniff(PostParserSniffInterface $sniff) - { - $this->nodeVisitor->removeSniff($sniff); - - return $this; - } -} diff --git a/TwigCS/Extension/SniffsNodeVisitor.php b/TwigCS/Extension/SniffsNodeVisitor.php deleted file mode 100644 index f9c6bab..0000000 --- a/TwigCS/Extension/SniffsNodeVisitor.php +++ /dev/null @@ -1,139 +0,0 @@ -sniffs = []; - $this->enabled = true; - } - - /** - * @return int - */ - public function getPriority() - { - return 0; - } - - /** - * Register a sniff to be executed. - * - * @param PostParserSniffInterface $sniff - */ - public function addSniff(PostParserSniffInterface $sniff) - { - $this->sniffs[] = $sniff; - } - - /** - * Remove a sniff from the node visitor. - * - * @param PostParserSniffInterface $toBeRemovedSniff - * - * @return self - */ - public function removeSniff(PostParserSniffInterface $toBeRemovedSniff) - { - foreach ($this->sniffs as $index => $sniff) { - if ($toBeRemovedSniff === $sniff) { - unset($this->sniffs[$index]); - } - } - - return $this; - } - - /** - * Get all registered sniffs. - * - * @return array - */ - public function getSniffs() - { - return $this->sniffs; - } - - /** - * Enable this node visitor. - * - * @return self - */ - public function enable() - { - $this->enabled = true; - - return $this; - } - - /** - * Disable this node visitor. - * - * @return self - */ - public function disable() - { - $this->enabled = false; - - return $this; - } - - /** - * @param Node $node - * @param Environment $env - * - * @return Node - */ - protected function doEnterNode(Node $node, Environment $env) - { - if (!$this->enabled) { - return $node; - } - - foreach ($this->getSniffs() as $sniff) { - $sniff->process($node, $env); - } - - return $node; - } - - /** - * @param Node $node - * @param Environment $env - * - * @return Node - */ - protected function doLeaveNode(Node $node, Environment $env) - { - return $node; - } -} diff --git a/TwigCS/Linter.php b/TwigCS/Linter.php index b762e74..418a010 100644 --- a/TwigCS/Linter.php +++ b/TwigCS/Linter.php @@ -7,12 +7,9 @@ use Twig\Environment; use Twig\Error\Error; use Twig\Source; -use TwigCS\Extension\SniffsExtension; use TwigCS\Report\Report; use TwigCS\Report\SniffViolation; use TwigCS\Ruleset\Ruleset; -use TwigCS\Sniff\PostParserSniffInterface; -use TwigCS\Sniff\PreParserSniffInterface; use TwigCS\Sniff\SniffInterface; use TwigCS\Token\Tokenizer; @@ -26,11 +23,6 @@ class Linter */ protected $env; - /** - * @var SniffsExtension - */ - protected $sniffsExtension; - /** * @var Tokenizer */ @@ -44,7 +36,6 @@ public function __construct(Environment $env, Tokenizer $tokenizer) { $this->env = $env; - $this->sniffsExtension = $this->env->getExtension('TwigCS\Extension\SniffsExtension'); $this->tokenizer = $tokenizer; } @@ -68,13 +59,8 @@ public function run($files, Ruleset $ruleset) throw new Exception('No files to process, provide at least one file to be linted'); } - // setUp $report = new Report(); foreach ($ruleset->getSniffs() as $sniff) { - if ($sniff instanceof PostParserSniffInterface) { - $this->sniffsExtension->addSniff($sniff); - } - $sniff->enable($report); } @@ -90,10 +76,6 @@ public function run($files, Ruleset $ruleset) // tearDown foreach ($ruleset->getSniffs() as $sniff) { - if ($sniff instanceof PostParserSniffInterface) { - $this->sniffsExtension->removeSniff($sniff); - } - $sniff->disable(); } @@ -144,8 +126,8 @@ public function processTemplate($file, $ruleset, $report) return false; } - /** @var PreParserSniffInterface[] $sniffs */ - $sniffs = $ruleset->getSniffs(SniffInterface::TYPE_PRE_PARSER); + /** @var SniffInterface[] $sniffs */ + $sniffs = $ruleset->getSniffs(); foreach ($sniffs as $sniff) { foreach ($stream as $index => $token) { $sniff->process($token, $index, $stream); diff --git a/TwigCS/Ruleset/Ruleset.php b/TwigCS/Ruleset/Ruleset.php index 485b044..da8126d 100644 --- a/TwigCS/Ruleset/Ruleset.php +++ b/TwigCS/Ruleset/Ruleset.php @@ -2,13 +2,10 @@ namespace TwigCS\Ruleset; -use \Exception; -use TwigCS\Sniff\PostParserSniffInterface; -use TwigCS\Sniff\PreParserSniffInterface; use TwigCS\Sniff\SniffInterface; /** - * Set of rules to be used by TwigCS and contains all sniffs (pre or post). + * Set of rules to be used by TwigCS and contains all sniffs. */ class Ruleset { @@ -23,79 +20,23 @@ public function __construct() } /** - * @param array|null $types - * * @return SniffInterface[] */ - public function getSniffs($types = null) + public function getSniffs() { - if (null === $types) { - $types = [SniffInterface::TYPE_PRE_PARSER, SniffInterface::TYPE_POST_PARSER]; - } - - if (null !== $types && !is_array($types)) { - $types = [$types]; - } - - return array_filter($this->sniffs, function (SniffInterface $sniff) use ($types) { - return in_array($sniff->getType(), $types); - }); - } - - /** - * @param PreParserSniffInterface $sniff - * - * @return $this - */ - public function addPreParserSniff(PreParserSniffInterface $sniff) - { - $this->sniffs[get_class($sniff)] = $sniff; - - return $this; - } - - /** - * @param PostParserSniffInterface $sniff - * - * @return $this - */ - public function addPostParserSniff(PostParserSniffInterface $sniff) - { - $this->sniffs[get_class($sniff)] = $sniff; - - return $this; + return $this->sniffs; } /** * @param SniffInterface $sniff * * @return $this - * - * @throws Exception */ public function addSniff(SniffInterface $sniff) { - if (SniffInterface::TYPE_PRE_PARSER === $sniff->getType()) { - // Store this type of sniff locally. - /** @var PreParserSniffInterface $sniff */ - $this->addPreParserSniff($sniff); - - return $this; - } - - if (SniffInterface::TYPE_POST_PARSER === $sniff->getType()) { - // Store this type of sniff locally. - /** @var PostParserSniffInterface $sniff */ - $this->addPostParserSniff($sniff); - - return $this; - } + $this->sniffs[get_class($sniff)] = $sniff; - throw new Exception(sprintf( - 'Unknown type of sniff "%s", expected one of: "%s"', - $sniff->getType(), - implode(', ', [SniffInterface::TYPE_PRE_PARSER, SniffInterface::TYPE_POST_PARSER]) - )); + return $this; } /** diff --git a/TwigCS/Ruleset/RulesetFactory.php b/TwigCS/Ruleset/RulesetFactory.php index b85ebfc..273250b 100644 --- a/TwigCS/Ruleset/RulesetFactory.php +++ b/TwigCS/Ruleset/RulesetFactory.php @@ -2,7 +2,6 @@ namespace TwigCS\Ruleset; -use \Exception; use \SplFileInfo; use Symfony\Component\Finder\Finder; @@ -15,8 +14,6 @@ class RulesetFactory * Create a new set of rule. * * @return Ruleset - * - * @throws Exception */ public function createStandardRuleset() { diff --git a/TwigCS/Sniff/AbstractPostParserSniff.php b/TwigCS/Sniff/AbstractPostParserSniff.php deleted file mode 100644 index ab6f6b5..0000000 --- a/TwigCS/Sniff/AbstractPostParserSniff.php +++ /dev/null @@ -1,171 +0,0 @@ -getTemplateName($node), - $this->getTemplateLine($node) - ); - - $this->getReport()->addMessage($sniffViolation); - - return $this; - } - - /** - * @param Node $node - * - * @return int|null - */ - public function getTemplateLine(Node $node) - { - if (method_exists($node, 'getTemplateLine')) { - return $node->getTemplateLine(); - } - - if (method_exists($node, 'getLine')) { - return $node->getLine(); - } - - return null; - } - - /** - * @param Node $node - * - * @return string - */ - public function getTemplateName(Node $node) - { - if (method_exists($node, 'getTemplateName')) { - return $node->getTemplateName(); - } - - if (method_exists($node, 'getFilename')) { - return $node->getFilename(); - } - - if ($node->hasAttribute('filename')) { - return $node->getAttribute('filename'); - } - - return ''; - } - - /** - * @param Node $node - * @param string $type - * @param string|null $name - * - * @return bool - */ - public function isNodeMatching(Node $node, $type, $name = null) - { - $typeToClass = [ - 'filter' => function (Node $node, $type, $name) { - return $node instanceof FilterExpression - && $name === $node->getNode($type)->getAttribute('value'); - }, - 'function' => function (Node $node, $type, $name) { - return $node instanceof FunctionExpression - && $name === $node->getAttribute('name'); - }, - 'include' => function (Node $node, $type, $name) { - return $node instanceof IncludeNode; - }, - 'tag' => function (Node $node, $type, $name) { - return $node->getNodeTag() === $name; - }, - ]; - - if (!isset($typeToClass[$type])) { - return false; - } - - return $typeToClass[$type]($node, $type, $name); - } - - /** - * @param mixed $value - * - * @return string - */ - public function stringifyValue($value) - { - if (null === $value) { - return 'null'; - } - - if (is_bool($value)) { - return ($value) ? 'true' : 'false'; - } - - return (string) $value; - } - - /** - * @param Node $node - * - * @return mixed|string - */ - public function stringifyNode(Node $node) - { - $stringValue = ''; - - if ($node instanceof GetAttrExpression) { - return $node->getNode('node')->getAttribute('name').'.'.$this->stringifyNode($node->getNode('attribute')); - } - if ($node instanceof ConcatBinary) { - return $this->stringifyNode($node->getNode('left')).' ~ '.$this->stringifyNode($node->getNode('right')); - } - if ($node instanceof ConstantExpression) { - return $node->getAttribute('value'); - } - - return $stringValue; - } -} diff --git a/TwigCS/Sniff/AbstractPreParserSniff.php b/TwigCS/Sniff/AbstractPreParserSniff.php deleted file mode 100644 index 930d0fd..0000000 --- a/TwigCS/Sniff/AbstractPreParserSniff.php +++ /dev/null @@ -1,81 +0,0 @@ -getType() === $type - && (null === $value || (null !== $value && $token->getValue() === $value)); - } - - /** - * Adds a violation to the current report for the given token. - * - * @param int $messageType - * @param string $message - * @param Token $token - * - * @return self - * - * @throws Exception - */ - public function addMessage($messageType, $message, Token $token) - { - $sniffViolation = new SniffViolation( - $messageType, - $message, - $token->getFilename(), - $token->getLine() - ); - $sniffViolation->setLinePosition($token->getPosition()); - - $this->getReport()->addMessage($sniffViolation); - - return $this; - } - - /** - * @param Token $token - * - * @return string - */ - public function stringifyValue($token) - { - if ($token->getType() === Token::STRING_TYPE) { - return $token->getValue(); - } - - return '\''.$token->getValue().'\''; - } -} diff --git a/TwigCS/Sniff/AbstractSniff.php b/TwigCS/Sniff/AbstractSniff.php index 0b58109..bec5089 100644 --- a/TwigCS/Sniff/AbstractSniff.php +++ b/TwigCS/Sniff/AbstractSniff.php @@ -4,6 +4,8 @@ use \Exception; use TwigCS\Report\Report; +use TwigCS\Report\SniffViolation; +use TwigCS\Token\Token; /** * Base for all sniff. @@ -65,7 +67,57 @@ public function getReport() } /** + * Helper method to match a token of a given type and value. + * + * @param Token $token + * @param int $type + * @param string $value + * + * @return bool + */ + public function isTokenMatching(Token $token, $type, $value = null) + { + return $token->getType() === $type + && (null === $value || (null !== $value && $token->getValue() === $value)); + } + + /** + * Adds a violation to the current report for the given token. + * + * @param int $messageType + * @param string $message + * @param Token $token + * + * @return self + * + * @throws Exception + */ + public function addMessage($messageType, $message, Token $token) + { + $sniffViolation = new SniffViolation( + $messageType, + $message, + $token->getFilename(), + $token->getLine() + ); + $sniffViolation->setLinePosition($token->getPosition()); + + $this->getReport()->addMessage($sniffViolation); + + return $this; + } + + /** + * @param Token $token + * * @return string */ - abstract public function getType(); + public function stringifyValue($token) + { + if ($token->getType() === Token::STRING_TYPE) { + return $token->getValue(); + } + + return '\''.$token->getValue().'\''; + } } diff --git a/TwigCS/Sniff/PostParserSniffInterface.php b/TwigCS/Sniff/PostParserSniffInterface.php deleted file mode 100644 index 5997e95..0000000 --- a/TwigCS/Sniff/PostParserSniffInterface.php +++ /dev/null @@ -1,18 +0,0 @@ -getMockBuilder(LoaderInterface::class)->getMock(); - $this->env = new StubbedEnvironment( - $twigLoaderInterface, - [ - 'stub_tags' => ['render', 'some_other_block', 'stylesheets'], - 'stub_tests' => ['some_test'], - ] - ); + $this->env = new StubbedEnvironment($twigLoaderInterface); $this->lint = new Linter($this->env, new Tokenizer($this->env)); } From ac90631760c0eb65d4cf90b0728b882c34366a9d Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Sat, 1 Jun 2019 21:17:26 +0200 Subject: [PATCH 07/13] :lipstick: Clean condition --- TwigCS/Sniff/AbstractSniff.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/TwigCS/Sniff/AbstractSniff.php b/TwigCS/Sniff/AbstractSniff.php index bec5089..144bb44 100644 --- a/TwigCS/Sniff/AbstractSniff.php +++ b/TwigCS/Sniff/AbstractSniff.php @@ -77,8 +77,7 @@ public function getReport() */ public function isTokenMatching(Token $token, $type, $value = null) { - return $token->getType() === $type - && (null === $value || (null !== $value && $token->getValue() === $value)); + return $token->getType() === $type && (null === $value || $token->getValue() === $value); } /** From b1d3cc92c0f11c81db7fd0bddda69d43159f731e Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Sun, 2 Jun 2019 00:13:42 +0200 Subject: [PATCH 08/13] :lipstick: Clean StubbedEnvironment --- TwigCS/Environment/StubbedEnvironment.php | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/TwigCS/Environment/StubbedEnvironment.php b/TwigCS/Environment/StubbedEnvironment.php index a398e98..11fc858 100644 --- a/TwigCS/Environment/StubbedEnvironment.php +++ b/TwigCS/Environment/StubbedEnvironment.php @@ -2,7 +2,6 @@ namespace TwigCS\Environment; -use \Closure; use Symfony\Bridge\Twig\TokenParser\DumpTokenParser; use Symfony\Bridge\Twig\TokenParser\FormThemeTokenParser; use Symfony\Bridge\Twig\TokenParser\StopwatchTokenParser; @@ -35,11 +34,6 @@ class StubbedEnvironment extends Environment */ private $stubTests; - /** - * @var Closure - */ - private $stubCallable; - /** * @param LoaderInterface|null $loader * @param array $options @@ -54,6 +48,10 @@ public function __construct(LoaderInterface $loader = null, $options = []) $this->addTokenParser(new TransChoiceTokenParser()); $this->addTokenParser(new TransDefaultDomainTokenParser()); $this->addTokenParser(new TransTokenParser()); + + $this->stubFilters = []; + $this->stubFunctions = []; + $this->stubTests = []; } /** @@ -64,7 +62,7 @@ public function __construct(LoaderInterface $loader = null, $options = []) public function getFilter($name) { if (!isset($this->stubFilters[$name])) { - $this->stubFilters[$name] = new TwigFilter('stub', $this->stubCallable); + $this->stubFilters[$name] = new TwigFilter('stub'); } return $this->stubFilters[$name]; @@ -78,7 +76,7 @@ public function getFilter($name) public function getFunction($name) { if (!isset($this->stubFunctions[$name])) { - $this->stubFunctions[$name] = new TwigFunction('stub', $this->stubCallable); + $this->stubFunctions[$name] = new TwigFunction('stub'); } return $this->stubFunctions[$name]; @@ -92,7 +90,7 @@ public function getFunction($name) public function getTest($name) { if (!isset($this->stubTests[$name])) { - $this->stubTests[$name] = new TwigTest('stub', $this->stubCallable); + $this->stubTests[$name] = new TwigTest('stub'); } return $this->stubTests[$name]; From f8e18ef78663aa77a0b9d01385704f587889e412 Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Sun, 2 Jun 2019 00:16:00 +0200 Subject: [PATCH 09/13] :package: Rename to be similar to Phpcs --- ...iff.php => DisallowCommentedCodeSniff.php} | 2 +- ...st.twig => DisallowCommentedCodeTest.twig} | 0 .../Tests/Sniff/DisallowCommentedCodeTest.php | 19 +++++++++++++++++++ .../Tests/Sniff/ForbidCommentedCodeTest.php | 19 ------------------- 4 files changed, 20 insertions(+), 20 deletions(-) rename TwigCS/Sniff/Standard/{ForbidCommentedCodeSniff.php => DisallowCommentedCodeSniff.php} (96%) rename TwigCS/Tests/Fixtures/{ForbidCommentedCodeTest.twig => DisallowCommentedCodeTest.twig} (100%) create mode 100644 TwigCS/Tests/Sniff/DisallowCommentedCodeTest.php delete mode 100644 TwigCS/Tests/Sniff/ForbidCommentedCodeTest.php diff --git a/TwigCS/Sniff/Standard/ForbidCommentedCodeSniff.php b/TwigCS/Sniff/Standard/DisallowCommentedCodeSniff.php similarity index 96% rename from TwigCS/Sniff/Standard/ForbidCommentedCodeSniff.php rename to TwigCS/Sniff/Standard/DisallowCommentedCodeSniff.php index 2c8c7c8..c447c68 100644 --- a/TwigCS/Sniff/Standard/ForbidCommentedCodeSniff.php +++ b/TwigCS/Sniff/Standard/DisallowCommentedCodeSniff.php @@ -11,7 +11,7 @@ * * This will be triggered if `{{` or `{%` is found inside a comment. */ -class ForbidCommentedCodeSniff extends AbstractSniff +class DisallowCommentedCodeSniff extends AbstractSniff { /** * @param Token $token diff --git a/TwigCS/Tests/Fixtures/ForbidCommentedCodeTest.twig b/TwigCS/Tests/Fixtures/DisallowCommentedCodeTest.twig similarity index 100% rename from TwigCS/Tests/Fixtures/ForbidCommentedCodeTest.twig rename to TwigCS/Tests/Fixtures/DisallowCommentedCodeTest.twig diff --git a/TwigCS/Tests/Sniff/DisallowCommentedCodeTest.php b/TwigCS/Tests/Sniff/DisallowCommentedCodeTest.php new file mode 100644 index 0000000..2a78dcf --- /dev/null +++ b/TwigCS/Tests/Sniff/DisallowCommentedCodeTest.php @@ -0,0 +1,19 @@ +checkGenericSniff(new DisallowCommentedCodeSniff(), [ + [2, 5], + ]); + } +} diff --git a/TwigCS/Tests/Sniff/ForbidCommentedCodeTest.php b/TwigCS/Tests/Sniff/ForbidCommentedCodeTest.php deleted file mode 100644 index ac9cad1..0000000 --- a/TwigCS/Tests/Sniff/ForbidCommentedCodeTest.php +++ /dev/null @@ -1,19 +0,0 @@ -checkGenericSniff(new ForbidCommentedCodeSniff(), [ - [2, 5], - ]); - } -} From 2464b63074e528443d25fbfd8ac6dbcf09c8ae52 Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Sun, 2 Jun 2019 14:56:40 +0200 Subject: [PATCH 10/13] :package: More cleaning --- TwigCS/Command/TwigCSCommand.php | 27 +++++++--------- TwigCS/Config/Config.php | 3 +- TwigCS/Environment/StubbedEnvironment.php | 10 ++---- .../Generic}/BlankEOFSniff.php | 2 +- .../Generic}/DelimiterSpacingSniff.php | 2 +- .../Generic}/DisallowCommentedCodeSniff.php | 2 +- TwigCS/Ruleset/Ruleset.php | 28 +++++++++++++--- TwigCS/Ruleset/RulesetFactory.php | 32 ------------------- TwigCS/Tests/AbstractSniffTest.php | 11 ++++--- .../Generic}/BlankEOFTest.php | 4 +-- .../Generic}/DelimiterSpacingTest.php | 4 +-- .../Generic}/DisallowCommentedCodeTest.php | 4 +-- composer.lock | 2 +- 13 files changed, 55 insertions(+), 76 deletions(-) rename TwigCS/{Sniff/Standard => Ruleset/Generic}/BlankEOFSniff.php (96%) rename TwigCS/{Sniff/Standard => Ruleset/Generic}/DelimiterSpacingSniff.php (98%) rename TwigCS/{Sniff/Standard => Ruleset/Generic}/DisallowCommentedCodeSniff.php (97%) delete mode 100644 TwigCS/Ruleset/RulesetFactory.php rename TwigCS/Tests/{Sniff => Ruleset/Generic}/BlankEOFTest.php (75%) rename TwigCS/Tests/{Sniff => Ruleset/Generic}/DelimiterSpacingTest.php (79%) rename TwigCS/Tests/{Sniff => Ruleset/Generic}/DisallowCommentedCodeTest.php (75%) diff --git a/TwigCS/Command/TwigCSCommand.php b/TwigCS/Command/TwigCSCommand.php index 42e3286..4aa710d 100644 --- a/TwigCS/Command/TwigCSCommand.php +++ b/TwigCS/Command/TwigCSCommand.php @@ -8,12 +8,11 @@ use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; -use Twig\Loader\ArrayLoader; use TwigCS\Config\Config; use TwigCS\Environment\StubbedEnvironment; use TwigCS\Linter; use TwigCS\Report\TextFormatter; -use TwigCS\Ruleset\RulesetFactory; +use TwigCS\Ruleset\Ruleset; use TwigCS\Token\Tokenizer; /** @@ -33,16 +32,16 @@ protected function configure() new InputOption( 'exclude', 'e', - InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, + InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'Excludes, based on regex, paths of files and folders from parsing', - ['vendor/'] + [] ), new InputOption( 'level', 'l', InputOption::VALUE_OPTIONAL, - 'Allowed values are: warning, error', - 'warning' + 'Allowed values are notice, warning or error', + 'notice' ), new InputOption( 'working-dir', @@ -81,26 +80,24 @@ protected function execute(InputInterface $input, OutputInterface $output) 'workingDirectory' => $currentDir, ]); - $twig = new StubbedEnvironment(new ArrayLoader()); - $linter = new Linter($twig, new Tokenizer($twig)); - $factory = new RulesetFactory(); - $reporter = new TextFormatter($input, $output); - $exitCode = 0; - // Get the rules to apply. - $ruleset = $factory->createStandardRuleset(); + $ruleset = new Ruleset(); + $ruleset->addStandard(); // Execute the linter. + $twig = new StubbedEnvironment(); + $linter = new Linter($twig, new Tokenizer($twig)); $report = $linter->run($config->findFiles(), $ruleset); // Format the output. + $reporter = new TextFormatter($input, $output); $reporter->display($report, $level); // Return a meaningful error code. if ($report->getTotalErrors()) { - $exitCode = 1; + return 1; } - return $exitCode; + return 0; } } diff --git a/TwigCS/Config/Config.php b/TwigCS/Config/Config.php index 00bca4f..aad24d3 100644 --- a/TwigCS/Config/Config.php +++ b/TwigCS/Config/Config.php @@ -16,10 +16,9 @@ class Config * @var array */ public static $defaultConfig = [ - 'exclude' => [], + 'exclude' => ['vendor/'], 'pattern' => '*.twig', 'paths' => [], - 'stub' => [], 'workingDirectory' => '', ]; diff --git a/TwigCS/Environment/StubbedEnvironment.php b/TwigCS/Environment/StubbedEnvironment.php index 11fc858..04cbde4 100644 --- a/TwigCS/Environment/StubbedEnvironment.php +++ b/TwigCS/Environment/StubbedEnvironment.php @@ -9,7 +9,7 @@ use Symfony\Bridge\Twig\TokenParser\TransDefaultDomainTokenParser; use Symfony\Bridge\Twig\TokenParser\TransTokenParser; use Twig\Environment; -use Twig\Loader\LoaderInterface; +use Twig\Loader\ArrayLoader; use Twig\TwigFilter; use Twig\TwigFunction; use Twig\TwigTest; @@ -34,13 +34,9 @@ class StubbedEnvironment extends Environment */ private $stubTests; - /** - * @param LoaderInterface|null $loader - * @param array $options - */ - public function __construct(LoaderInterface $loader = null, $options = []) + public function __construct() { - parent::__construct($loader, $options); + parent::__construct(new ArrayLoader()); $this->addTokenParser(new DumpTokenParser()); $this->addTokenParser(new FormThemeTokenParser()); diff --git a/TwigCS/Sniff/Standard/BlankEOFSniff.php b/TwigCS/Ruleset/Generic/BlankEOFSniff.php similarity index 96% rename from TwigCS/Sniff/Standard/BlankEOFSniff.php rename to TwigCS/Ruleset/Generic/BlankEOFSniff.php index 60b6688..051ebd2 100644 --- a/TwigCS/Sniff/Standard/BlankEOFSniff.php +++ b/TwigCS/Ruleset/Generic/BlankEOFSniff.php @@ -1,6 +1,6 @@ sniffs[$sniffClass])) { - unset($this->sniffs[$sniffClass]); + try { + $finder = Finder::create()->in(__DIR__.'/'.$standardName)->files(); + } catch (Exception $e) { + throw new Exception(sprintf('The standard "%s" is not found.', $standardName)); + } + + /** @var SplFileInfo $file */ + foreach ($finder as $file) { + $class = __NAMESPACE__.'\\'.$standardName.'\\'.$file->getBasename('.php'); + + if (class_exists($class)) { + $this->addSniff(new $class()); + } } return $this; diff --git a/TwigCS/Ruleset/RulesetFactory.php b/TwigCS/Ruleset/RulesetFactory.php deleted file mode 100644 index 273250b..0000000 --- a/TwigCS/Ruleset/RulesetFactory.php +++ /dev/null @@ -1,32 +0,0 @@ -in(__DIR__.'/../Sniff/Standard')->files(); - - /** @var SplFileInfo $file */ - foreach ($finder as $file) { - $class = 'TwigCS\Sniff\Standard\\'.explode('.', $file->getFilename())[0]; - $ruleset->addSniff(new $class()); - } - - return $ruleset; - } -} diff --git a/TwigCS/Tests/AbstractSniffTest.php b/TwigCS/Tests/AbstractSniffTest.php index 2877262..eac2b68 100644 --- a/TwigCS/Tests/AbstractSniffTest.php +++ b/TwigCS/Tests/AbstractSniffTest.php @@ -4,9 +4,7 @@ use \Exception; use \ReflectionClass; -use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; -use Twig\Loader\LoaderInterface; use TwigCS\Environment\StubbedEnvironment; use TwigCS\Linter; use TwigCS\Report\SniffViolation; @@ -31,12 +29,15 @@ abstract class AbstractSniffTest extends TestCase public function setUp() { - /** @var LoaderInterface|MockObject $twigLoaderInterface */ - $twigLoaderInterface = $this->getMockBuilder(LoaderInterface::class)->getMock(); - $this->env = new StubbedEnvironment($twigLoaderInterface); + $this->env = new StubbedEnvironment(); $this->lint = new Linter($this->env, new Tokenizer($this->env)); } + /** + * Should call $this->checkGenericSniff(new Sniff(), [...]); + */ + abstract public function testSniff(); + /** * @param SniffInterface $sniff * @param array $expects diff --git a/TwigCS/Tests/Sniff/BlankEOFTest.php b/TwigCS/Tests/Ruleset/Generic/BlankEOFTest.php similarity index 75% rename from TwigCS/Tests/Sniff/BlankEOFTest.php rename to TwigCS/Tests/Ruleset/Generic/BlankEOFTest.php index 5042e77..0599d12 100644 --- a/TwigCS/Tests/Sniff/BlankEOFTest.php +++ b/TwigCS/Tests/Ruleset/Generic/BlankEOFTest.php @@ -1,8 +1,8 @@ Date: Sun, 2 Jun 2019 21:42:37 +0200 Subject: [PATCH 11/13] :package: Remove lex function --- TwigCS/Token/Tokenizer.php | 67 ++++++++++++++++++++------------------ 1 file changed, 36 insertions(+), 31 deletions(-) diff --git a/TwigCS/Token/Tokenizer.php b/TwigCS/Token/Tokenizer.php index 0d3a48d..5a14fe5 100644 --- a/TwigCS/Token/Tokenizer.php +++ b/TwigCS/Token/Tokenizer.php @@ -269,34 +269,6 @@ protected function pushToken($type, $value = null) $this->tokens[] = new Token($type, $this->line, $tokenPositionInLine, $this->filename, $value); } - /** - * @param int $endType - * @param string $endRegex - * - * @throws Exception - */ - protected function lex($endType, $endRegex) - { - preg_match($endRegex, $this->code, $match, PREG_OFFSET_CAPTURE, $this->cursor); - - if (!empty($this->brackets) || !isset($match[0])) { - $this->lexExpression(); - } elseif ($match[0][1] === $this->cursor) { - $this->pushToken($endType, $match[0][0]); - $this->moveCursor($match[0][0]); - $this->moveCurrentPosition(); - $this->popState(); - } elseif ($this->getState() === self::STATE_COMMENT) { - // Parse as text until the end position. - $this->lexData($match[0][1]); - } else { - // Should not happen - while ($this->cursor < $match[0][1]) { - $this->lexExpression(); - } - } - } - /** * @throws Exception */ @@ -357,7 +329,17 @@ protected function lexExpression() */ protected function lexBlock() { - $this->lex(Token::BLOCK_END_TYPE, $this->regexes['lex_block']); + $endRegex = $this->regexes['lex_block']; + preg_match($endRegex, $this->code, $match, PREG_OFFSET_CAPTURE, $this->cursor); + + if (!empty($this->brackets) || !isset($match[0])) { + $this->lexExpression(); + } else { + $this->pushToken(Token::BLOCK_END_TYPE, $match[0][0]); + $this->moveCursor($match[0][0]); + $this->moveCurrentPosition(); + $this->popState(); + } } /** @@ -365,7 +347,17 @@ protected function lexBlock() */ protected function lexVariable() { - $this->lex(Token::VAR_END_TYPE, $this->regexes['lex_variable']); + $endRegex = $this->regexes['lex_variable']; + preg_match($endRegex, $this->code, $match, PREG_OFFSET_CAPTURE, $this->cursor); + + if (!empty($this->brackets) || !isset($match[0])) { + $this->lexExpression(); + } else { + $this->pushToken(Token::VAR_END_TYPE, $match[0][0]); + $this->moveCursor($match[0][0]); + $this->moveCurrentPosition(); + $this->popState(); + } } /** @@ -373,7 +365,20 @@ protected function lexVariable() */ protected function lexComment() { - $this->lex(Token::COMMENT_END_TYPE, $this->regexes['lex_comment']); + $endRegex = $this->regexes['lex_comment']; + preg_match($endRegex, $this->code, $match, PREG_OFFSET_CAPTURE, $this->cursor); + + if (!isset($match[0])) { + throw new Exception('Unclosed comment'); + } elseif ($match[0][1] === $this->cursor) { + $this->pushToken(Token::COMMENT_END_TYPE, $match[0][0]); + $this->moveCursor($match[0][0]); + $this->moveCurrentPosition(); + $this->popState(); + } else { + // Parse as text until the end position. + $this->lexData($match[0][1]); + } } /** From 15c1c3c0dda2d20103c9ba40a0f0af6f4cd981c7 Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Sun, 2 Jun 2019 23:38:24 +0200 Subject: [PATCH 12/13] :lipstick: Typeint --- .../Sniffs/Commenting/FunctionCommentSniff.php | 2 +- TwigCS/Command/TwigCSCommand.php | 2 +- TwigCS/Config/Config.php | 18 ++++++++---------- TwigCS/Linter.php | 17 ++++++----------- TwigCS/Report/Report.php | 4 ++-- TwigCS/Report/SniffViolation.php | 10 +++++----- TwigCS/Report/TextFormatter.php | 6 +++--- TwigCS/Ruleset/Generic/BlankEOFSniff.php | 2 +- .../Ruleset/Generic/DelimiterSpacingSniff.php | 2 +- .../Generic/DisallowCommentedCodeSniff.php | 2 +- TwigCS/Ruleset/Ruleset.php | 2 +- TwigCS/Sniff/AbstractSniff.php | 6 +++--- TwigCS/Sniff/SniffInterface.php | 2 +- TwigCS/Tests/AbstractSniffTest.php | 2 +- TwigCS/Token/Token.php | 9 +++++++-- TwigCS/Token/TokenParser.php | 2 +- TwigCS/Token/Tokenizer.php | 14 +++++++------- 17 files changed, 50 insertions(+), 52 deletions(-) diff --git a/SymfonyCustom/Sniffs/Commenting/FunctionCommentSniff.php b/SymfonyCustom/Sniffs/Commenting/FunctionCommentSniff.php index 15bc6f7..1e6a81b 100644 --- a/SymfonyCustom/Sniffs/Commenting/FunctionCommentSniff.php +++ b/SymfonyCustom/Sniffs/Commenting/FunctionCommentSniff.php @@ -269,7 +269,7 @@ protected function processParams( * * @return bool True if the return does not return anything */ - protected function isMatchingReturn($tokens, $returnPos) + protected function isMatchingReturn(array $tokens, $returnPos) { do { $returnPos++; diff --git a/TwigCS/Command/TwigCSCommand.php b/TwigCS/Command/TwigCSCommand.php index 4aa710d..fe4b044 100644 --- a/TwigCS/Command/TwigCSCommand.php +++ b/TwigCS/Command/TwigCSCommand.php @@ -34,7 +34,7 @@ protected function configure() 'e', InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'Excludes, based on regex, paths of files and folders from parsing', - [] + ['vendor/'] ), new InputOption( 'level', diff --git a/TwigCS/Config/Config.php b/TwigCS/Config/Config.php index aad24d3..b0648d3 100644 --- a/TwigCS/Config/Config.php +++ b/TwigCS/Config/Config.php @@ -16,7 +16,7 @@ class Config * @var array */ public static $defaultConfig = [ - 'exclude' => ['vendor/'], + 'exclude' => [], 'pattern' => '*.twig', 'paths' => [], 'workingDirectory' => '', @@ -29,14 +29,12 @@ class Config */ protected $config; - public function __construct() + /** + * @param array $config + */ + public function __construct(array $config = []) { - $args = func_get_args(); - - $this->config = $this::$defaultConfig; - foreach ($args as $arg) { - $this->config = array_merge($this->config, $arg); - } + $this->config = array_merge($this::$defaultConfig, $config); } /** @@ -67,7 +65,7 @@ public function findFiles() $files->exclude($exclude); } - return $files; + return iterator_to_array($files, false); } /** @@ -79,7 +77,7 @@ public function findFiles() * * @throws Exception */ - public function get($key) + public function get(string $key) { if (!isset($this->config[$key])) { throw new Exception(sprintf('Configuration key "%s" does not exist', $key)); diff --git a/TwigCS/Linter.php b/TwigCS/Linter.php index 418a010..63f2c8d 100644 --- a/TwigCS/Linter.php +++ b/TwigCS/Linter.php @@ -3,7 +3,6 @@ namespace TwigCS; use \Exception; -use \Traversable; use Twig\Environment; use Twig\Error\Error; use Twig\Source; @@ -42,19 +41,15 @@ public function __construct(Environment $env, Tokenizer $tokenizer) /** * Run the linter on the given $files against the given $ruleset. * - * @param array|string $files List of files to process. - * @param Ruleset $ruleset Set of rules to check. + * @param array $files List of files to process. + * @param Ruleset $ruleset Set of rules to check. * * @return Report an object with all violations and stats. * * @throws Exception */ - public function run($files, Ruleset $ruleset) + public function run(array $files, Ruleset $ruleset) { - if (!is_array($files) && !$files instanceof Traversable) { - $files = [$files]; - } - if (empty($files)) { throw new Exception('No files to process, provide at least one file to be linted'); } @@ -91,7 +86,7 @@ public function run($files, Ruleset $ruleset) * * @return bool */ - public function processTemplate($file, $ruleset, $report) + public function processTemplate(string $file, Ruleset $ruleset, Report $report) { $twigSource = new Source(file_get_contents($file), $file, $file); @@ -118,7 +113,7 @@ public function processTemplate($file, $ruleset, $report) $sniffViolation = new SniffViolation( SniffInterface::MESSAGE_TYPE_ERROR, sprintf('Unable to tokenize file'), - (string) $file + $file ); $report->addMessage($sniffViolation); @@ -141,7 +136,7 @@ public function processTemplate($file, $ruleset, $report) * @param Report $report * @param string|null $file */ - protected function setErrorHandler(Report $report, $file = null) + protected function setErrorHandler(Report $report, string $file = null) { set_error_handler(function ($type, $message) use ($report, $file) { if (E_USER_DEPRECATED === $type) { diff --git a/TwigCS/Report/Report.php b/TwigCS/Report/Report.php index f507067..6b74751 100644 --- a/TwigCS/Report/Report.php +++ b/TwigCS/Report/Report.php @@ -75,7 +75,7 @@ public function addMessage(SniffViolation $sniffViolation) * * @return SniffViolation[] */ - public function getMessages($filters = []) + public function getMessages(array $filters = []) { if (empty($filters)) { // Return all messages, without filtering. @@ -100,7 +100,7 @@ public function getMessages($filters = []) /** * @param string $file */ - public function addFile($file) + public function addFile(string $file) { $this->files[] = $file; } diff --git a/TwigCS/Report/SniffViolation.php b/TwigCS/Report/SniffViolation.php index 29644eb..070f9a9 100644 --- a/TwigCS/Report/SniffViolation.php +++ b/TwigCS/Report/SniffViolation.php @@ -57,7 +57,7 @@ class SniffViolation * @param string $filename * @param int|null $line */ - public function __construct($level, $message, $filename, $line = null) + public function __construct(int $level, string $message, string $filename, int $line = null) { $this->level = $level; $this->message = $message; @@ -99,11 +99,11 @@ public function getLevelAsString() /** * Get the integer value for a given string $level. * - * @param int $level + * @param string $level * * @return int */ - public static function getLevelAsInt($level) + public static function getLevelAsInt(string $level) { switch (strtoupper($level)) { case 'NOTICE': @@ -143,7 +143,7 @@ public function getLine() */ public function getFilename() { - return (string) $this->filename; + return $this->filename; } /** @@ -153,7 +153,7 @@ public function getFilename() * * @return self */ - public function setLinePosition($linePosition) + public function setLinePosition(int $linePosition) { $this->linePosition = $linePosition; diff --git a/TwigCS/Report/TextFormatter.php b/TwigCS/Report/TextFormatter.php index ed765ab..079fcd8 100644 --- a/TwigCS/Report/TextFormatter.php +++ b/TwigCS/Report/TextFormatter.php @@ -29,7 +29,7 @@ class TextFormatter * @param InputInterface $input * @param OutputInterface $output */ - public function __construct($input, $output) + public function __construct(InputInterface $input, OutputInterface $output) { $this->io = new SymfonyStyle($input, $output); } @@ -38,7 +38,7 @@ public function __construct($input, $output) * @param Report $report * @param string|null $level */ - public function display(Report $report, $level = null) + public function display(Report $report, string $level = null) { foreach ($report->getFiles() as $file) { $fileMessages = $report->getMessages([ @@ -106,7 +106,7 @@ public function display(Report $report, $level = null) * * @return array */ - protected function getContext($template, $line, $context) + protected function getContext(string $template, int $line, int $context) { $lines = explode("\n", $template); diff --git a/TwigCS/Ruleset/Generic/BlankEOFSniff.php b/TwigCS/Ruleset/Generic/BlankEOFSniff.php index 051ebd2..876ce32 100644 --- a/TwigCS/Ruleset/Generic/BlankEOFSniff.php +++ b/TwigCS/Ruleset/Generic/BlankEOFSniff.php @@ -20,7 +20,7 @@ class BlankEOFSniff extends AbstractSniff * * @throws Exception */ - public function process(Token $token, $tokenPosition, $tokens) + public function process(Token $token, int $tokenPosition, array $tokens) { if ($this->isTokenMatching($token, Token::EOF_TYPE)) { $i = 0; diff --git a/TwigCS/Ruleset/Generic/DelimiterSpacingSniff.php b/TwigCS/Ruleset/Generic/DelimiterSpacingSniff.php index 77e3cba..82671c1 100644 --- a/TwigCS/Ruleset/Generic/DelimiterSpacingSniff.php +++ b/TwigCS/Ruleset/Generic/DelimiterSpacingSniff.php @@ -20,7 +20,7 @@ class DelimiterSpacingSniff extends AbstractSniff * * @throws Exception */ - public function process(Token $token, $tokenPosition, $tokens) + public function process(Token $token, int $tokenPosition, array $tokens) { if ($this->isTokenMatching($token, Token::VAR_START_TYPE) || $this->isTokenMatching($token, Token::BLOCK_START_TYPE) diff --git a/TwigCS/Ruleset/Generic/DisallowCommentedCodeSniff.php b/TwigCS/Ruleset/Generic/DisallowCommentedCodeSniff.php index c225ab4..f9523d8 100644 --- a/TwigCS/Ruleset/Generic/DisallowCommentedCodeSniff.php +++ b/TwigCS/Ruleset/Generic/DisallowCommentedCodeSniff.php @@ -22,7 +22,7 @@ class DisallowCommentedCodeSniff extends AbstractSniff * * @throws Exception */ - public function process(Token $token, $tokenPosition, $tokens) + public function process(Token $token, int $tokenPosition, array $tokens) { if ($this->isTokenMatching($token, Token::COMMENT_START_TYPE)) { $i = $tokenPosition; diff --git a/TwigCS/Ruleset/Ruleset.php b/TwigCS/Ruleset/Ruleset.php index c3f1015..9866dca 100644 --- a/TwigCS/Ruleset/Ruleset.php +++ b/TwigCS/Ruleset/Ruleset.php @@ -51,7 +51,7 @@ public function addSniff(SniffInterface $sniff) * * @throws Exception */ - public function addStandard($standardName = 'Generic') + public function addStandard(string $standardName = 'Generic') { try { $finder = Finder::create()->in(__DIR__.'/'.$standardName)->files(); diff --git a/TwigCS/Sniff/AbstractSniff.php b/TwigCS/Sniff/AbstractSniff.php index 144bb44..beccd90 100644 --- a/TwigCS/Sniff/AbstractSniff.php +++ b/TwigCS/Sniff/AbstractSniff.php @@ -75,7 +75,7 @@ public function getReport() * * @return bool */ - public function isTokenMatching(Token $token, $type, $value = null) + public function isTokenMatching(Token $token, int $type, string $value = null) { return $token->getType() === $type && (null === $value || $token->getValue() === $value); } @@ -91,7 +91,7 @@ public function isTokenMatching(Token $token, $type, $value = null) * * @throws Exception */ - public function addMessage($messageType, $message, Token $token) + public function addMessage(int $messageType, string $message, Token $token) { $sniffViolation = new SniffViolation( $messageType, @@ -111,7 +111,7 @@ public function addMessage($messageType, $message, Token $token) * * @return string */ - public function stringifyValue($token) + public function stringifyValue(Token $token) { if ($token->getType() === Token::STRING_TYPE) { return $token->getValue(); diff --git a/TwigCS/Sniff/SniffInterface.php b/TwigCS/Sniff/SniffInterface.php index f1ed0ab..f25b0c4 100644 --- a/TwigCS/Sniff/SniffInterface.php +++ b/TwigCS/Sniff/SniffInterface.php @@ -50,5 +50,5 @@ public function getReport(); * @param int $tokenPosition * @param Token[] $stream */ - public function process(Token $token, $tokenPosition, $stream); + public function process(Token $token, int $tokenPosition, array $stream); } diff --git a/TwigCS/Tests/AbstractSniffTest.php b/TwigCS/Tests/AbstractSniffTest.php index eac2b68..e5d6c84 100644 --- a/TwigCS/Tests/AbstractSniffTest.php +++ b/TwigCS/Tests/AbstractSniffTest.php @@ -50,7 +50,7 @@ protected function checkGenericSniff(SniffInterface $sniff, array $expects) $file = __DIR__.'/Fixtures/'.$class->getShortName().'.twig'; $ruleset->addSniff($sniff); - $report = $this->lint->run($file, $ruleset); + $report = $this->lint->run([$file], $ruleset); } catch (Exception $e) { $this->fail($e->getMessage()); diff --git a/TwigCS/Token/Token.php b/TwigCS/Token/Token.php index 1c471af..7d980cb 100644 --- a/TwigCS/Token/Token.php +++ b/TwigCS/Token/Token.php @@ -63,8 +63,13 @@ class Token * @param string $filename * @param string|null $value */ - public function __construct($type, $line, $position, $filename, $value = null) - { + public function __construct( + int $type, + int $line, + int $position, + string $filename, + string $value = null + ) { $this->type = $type; $this->line = $line; $this->position = $position; diff --git a/TwigCS/Token/TokenParser.php b/TwigCS/Token/TokenParser.php index afe83a8..0114498 100644 --- a/TwigCS/Token/TokenParser.php +++ b/TwigCS/Token/TokenParser.php @@ -21,7 +21,7 @@ class TokenParser extends AbstractTokenParser /** * @param string $name */ - public function __construct($name) + public function __construct(string $name) { $this->name = $name; } diff --git a/TwigCS/Token/Tokenizer.php b/TwigCS/Token/Tokenizer.php index 5a14fe5..27c52cd 100644 --- a/TwigCS/Token/Tokenizer.php +++ b/TwigCS/Token/Tokenizer.php @@ -190,7 +190,7 @@ protected function getState() /** * @param int $state */ - protected function pushState($state) + protected function pushState(int $state) { $this->state[] = $state; } @@ -209,7 +209,7 @@ protected function popState() /** * @param string $code */ - protected function preflightSource($code) + protected function preflightSource(string $code) { $tokenPositions = []; preg_match_all($this->regexes['lex_tokens_start'], $code, $tokenPositions, PREG_OFFSET_CAPTURE); @@ -231,7 +231,7 @@ protected function preflightSource($code) * * @return array|null */ - protected function getTokenPosition($offset = 0) + protected function getTokenPosition(int $offset = 0) { if (empty($this->tokenPositions) || !isset($this->tokenPositions[$this->currentPosition + $offset]) @@ -245,7 +245,7 @@ protected function getTokenPosition($offset = 0) /** * @param int $value */ - protected function moveCurrentPosition($value = 1) + protected function moveCurrentPosition(int $value = 1) { $this->currentPosition += $value; } @@ -253,7 +253,7 @@ protected function moveCurrentPosition($value = 1) /** * @param string $value */ - protected function moveCursor($value) + protected function moveCursor(string $value) { $this->cursor += strlen($value); $this->line += substr_count($value, "\n"); @@ -263,7 +263,7 @@ protected function moveCursor($value) * @param int $type * @param string|null $value */ - protected function pushToken($type, $value = null) + protected function pushToken(int $type, string $value = null) { $tokenPositionInLine = $this->cursor - strrpos(substr($this->code, 0, $this->cursor), PHP_EOL); $this->tokens[] = new Token($type, $this->line, $tokenPositionInLine, $this->filename, $value); @@ -384,7 +384,7 @@ protected function lexComment() /** * @param int $limit */ - protected function lexData($limit = 0) + protected function lexData(int $limit = 0) { $nextToken = $this->getTokenPosition(); if (0 === $limit && null !== $nextToken) { From 43adc2214ec23ffb67345b5f76530ff5ef14f3ba Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Sun, 2 Jun 2019 23:40:19 +0200 Subject: [PATCH 13/13] :lipstick: Fix lint --- TwigCS/Token/Tokenizer.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/TwigCS/Token/Tokenizer.php b/TwigCS/Token/Tokenizer.php index 27c52cd..4341041 100644 --- a/TwigCS/Token/Tokenizer.php +++ b/TwigCS/Token/Tokenizer.php @@ -370,7 +370,8 @@ protected function lexComment() if (!isset($match[0])) { throw new Exception('Unclosed comment'); - } elseif ($match[0][1] === $this->cursor) { + } + if ($match[0][1] === $this->cursor) { $this->pushToken(Token::COMMENT_END_TYPE, $match[0][0]); $this->moveCursor($match[0][0]); $this->moveCurrentPosition();