From 6ee3e08b22cdd8fe99bcafe212a1818e9ae73ce5 Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Sun, 4 Jul 2021 16:46:40 +0200 Subject: [PATCH] Remove TwigCS --- README.md | 1 - TwigCS/src/Command/TwigCSCommand.php | 95 --- TwigCS/src/Config/Config.php | 58 -- TwigCS/src/Config/TwigFileFilter.php | 20 - TwigCS/src/Environment/StubbedEnvironment.php | 101 --- TwigCS/src/Report/Report.php | 153 ---- TwigCS/src/Report/SniffViolation.php | 177 ----- TwigCS/src/Report/TextFormatter.php | 159 ---- TwigCS/src/Ruleset/Generic/BlankEOFSniff.php | 59 -- .../Ruleset/Generic/DelimiterSpacingSniff.php | 56 -- .../src/Ruleset/Generic/EmptyLinesSniff.php | 54 -- .../Ruleset/Generic/OperatorSpacingSniff.php | 99 --- .../Generic/PunctuationSpacingSniff.php | 50 -- TwigCS/src/Ruleset/Ruleset.php | 69 -- TwigCS/src/Runner/Fixer.php | 385 --------- TwigCS/src/Runner/Linter.php | 184 ----- TwigCS/src/Sniff/AbstractSniff.php | 240 ------ TwigCS/src/Sniff/AbstractSpacingSniff.php | 134 ---- TwigCS/src/Sniff/SniffInterface.php | 46 -- TwigCS/src/Token/Token.php | 145 ---- TwigCS/src/Token/TokenParser.php | 110 --- TwigCS/src/Token/Tokenizer.php | 749 ------------------ TwigCS/src/Token/TokenizerHelper.php | 155 ---- TwigCS/tests/Ruleset/AbstractSniffTest.php | 88 -- .../Generic/BlankEOF/BlankEOFTest.fixed.twig | 2 - .../Ruleset/Generic/BlankEOF/BlankEOFTest.php | 24 - .../Generic/BlankEOF/BlankEOFTest.twig | 3 - .../DelimiterSpacingTest.fixed.twig | 23 - .../DelimiterSpacing/DelimiterSpacingTest.php | 27 - .../DelimiterSpacingTest.twig | 23 - .../EmptyLines/EmptyLinesTest.fixed.twig | 3 - .../Generic/EmptyLines/EmptyLinesTest.php | 24 - .../Generic/EmptyLines/EmptyLinesTest.twig | 4 - .../OperatorSpacingTest.fixed.twig | 37 - .../OperatorSpacing/OperatorSpacingTest.php | 68 -- .../OperatorSpacing/OperatorSpacingTest.twig | 37 - .../PunctuationSpacingTest.fixed.twig | 6 - .../PunctuationSpacingTest.php | 36 - .../PunctuationSpacingTest.twig | 6 - TwigCS/tests/TestHelper.php | 76 -- .../tests/Token/Tokenizer/TokenizerTest.php | 301 ------- .../tests/Token/Tokenizer/TokenizerTest1.twig | 1 - .../tests/Token/Tokenizer/TokenizerTest2.twig | 3 - .../tests/Token/Tokenizer/TokenizerTest3.twig | 5 - .../tests/Token/Tokenizer/TokenizerTest4.twig | 1 - .../tests/Token/Tokenizer/TokenizerTest5.twig | 2 - .../tests/Token/Tokenizer/TokenizerTest6.twig | 1 - bin/twigcs | 23 - build.xml | 3 - composer.json | 19 +- docs/installation.md | 1 - docs/twig.md | 44 - phpunit.xml.dist | 3 - 53 files changed, 2 insertions(+), 4191 deletions(-) delete mode 100644 TwigCS/src/Command/TwigCSCommand.php delete mode 100644 TwigCS/src/Config/Config.php delete mode 100644 TwigCS/src/Config/TwigFileFilter.php delete mode 100644 TwigCS/src/Environment/StubbedEnvironment.php delete mode 100644 TwigCS/src/Report/Report.php delete mode 100644 TwigCS/src/Report/SniffViolation.php delete mode 100644 TwigCS/src/Report/TextFormatter.php delete mode 100644 TwigCS/src/Ruleset/Generic/BlankEOFSniff.php delete mode 100644 TwigCS/src/Ruleset/Generic/DelimiterSpacingSniff.php delete mode 100644 TwigCS/src/Ruleset/Generic/EmptyLinesSniff.php delete mode 100644 TwigCS/src/Ruleset/Generic/OperatorSpacingSniff.php delete mode 100644 TwigCS/src/Ruleset/Generic/PunctuationSpacingSniff.php delete mode 100644 TwigCS/src/Ruleset/Ruleset.php delete mode 100644 TwigCS/src/Runner/Fixer.php delete mode 100644 TwigCS/src/Runner/Linter.php delete mode 100644 TwigCS/src/Sniff/AbstractSniff.php delete mode 100644 TwigCS/src/Sniff/AbstractSpacingSniff.php delete mode 100644 TwigCS/src/Sniff/SniffInterface.php delete mode 100644 TwigCS/src/Token/Token.php delete mode 100644 TwigCS/src/Token/TokenParser.php delete mode 100644 TwigCS/src/Token/Tokenizer.php delete mode 100644 TwigCS/src/Token/TokenizerHelper.php delete mode 100644 TwigCS/tests/Ruleset/AbstractSniffTest.php delete mode 100644 TwigCS/tests/Ruleset/Generic/BlankEOF/BlankEOFTest.fixed.twig delete mode 100644 TwigCS/tests/Ruleset/Generic/BlankEOF/BlankEOFTest.php delete mode 100644 TwigCS/tests/Ruleset/Generic/BlankEOF/BlankEOFTest.twig delete mode 100644 TwigCS/tests/Ruleset/Generic/DelimiterSpacing/DelimiterSpacingTest.fixed.twig delete mode 100644 TwigCS/tests/Ruleset/Generic/DelimiterSpacing/DelimiterSpacingTest.php delete mode 100644 TwigCS/tests/Ruleset/Generic/DelimiterSpacing/DelimiterSpacingTest.twig delete mode 100644 TwigCS/tests/Ruleset/Generic/EmptyLines/EmptyLinesTest.fixed.twig delete mode 100644 TwigCS/tests/Ruleset/Generic/EmptyLines/EmptyLinesTest.php delete mode 100644 TwigCS/tests/Ruleset/Generic/EmptyLines/EmptyLinesTest.twig delete mode 100644 TwigCS/tests/Ruleset/Generic/OperatorSpacing/OperatorSpacingTest.fixed.twig delete mode 100644 TwigCS/tests/Ruleset/Generic/OperatorSpacing/OperatorSpacingTest.php delete mode 100644 TwigCS/tests/Ruleset/Generic/OperatorSpacing/OperatorSpacingTest.twig delete mode 100644 TwigCS/tests/Ruleset/Generic/PunctuationSpacing/PunctuationSpacingTest.fixed.twig delete mode 100644 TwigCS/tests/Ruleset/Generic/PunctuationSpacing/PunctuationSpacingTest.php delete mode 100644 TwigCS/tests/Ruleset/Generic/PunctuationSpacing/PunctuationSpacingTest.twig delete mode 100644 TwigCS/tests/TestHelper.php delete mode 100644 TwigCS/tests/Token/Tokenizer/TokenizerTest.php delete mode 100644 TwigCS/tests/Token/Tokenizer/TokenizerTest1.twig delete mode 100644 TwigCS/tests/Token/Tokenizer/TokenizerTest2.twig delete mode 100644 TwigCS/tests/Token/Tokenizer/TokenizerTest3.twig delete mode 100644 TwigCS/tests/Token/Tokenizer/TokenizerTest4.twig delete mode 100644 TwigCS/tests/Token/Tokenizer/TokenizerTest5.twig delete mode 100644 TwigCS/tests/Token/Tokenizer/TokenizerTest6.twig delete mode 100755 bin/twigcs delete mode 100644 docs/twig.md diff --git a/README.md b/README.md index f882a70..64642aa 100644 --- a/README.md +++ b/README.md @@ -9,4 +9,3 @@ Documentation ------------- * [Installation](docs/installation.md) * [Php Coding Standard](docs/php.md) - * [Twig Coding Standard](docs/twig.md) diff --git a/TwigCS/src/Command/TwigCSCommand.php b/TwigCS/src/Command/TwigCSCommand.php deleted file mode 100644 index a484aa6..0000000 --- a/TwigCS/src/Command/TwigCSCommand.php +++ /dev/null @@ -1,95 +0,0 @@ -setName('lint') - ->setDescription('Lints a template and outputs encountered errors') - ->setDefinition([ - new InputOption( - 'level', - 'l', - InputOption::VALUE_OPTIONAL, - 'Allowed values are notice, warning or error', - 'notice' - ), - new InputOption( - 'fix', - 'f', - InputOption::VALUE_NONE, - 'Automatically fix all the fixable violations' - ), - ]) - ->addArgument( - 'paths', - InputArgument::OPTIONAL | InputArgument::IS_ARRAY, - 'Paths of files and folders to parse', - null - ); - } - - /** - * @param InputInterface $input - * @param OutputInterface $output - * - * @return int - * - * @throws Exception - */ - protected function execute(InputInterface $input, OutputInterface $output): int - { - $paths = $input->getArgument('paths'); - $level = $input->getOption('level'); - $fix = $input->getOption('fix'); - - $config = new Config($paths); - - // Get the rules to apply. - $ruleset = new Ruleset(); - $ruleset->addStandard(); - - // Execute the linter. - $twig = new StubbedEnvironment(); - $linter = new Linter($twig, new Tokenizer($twig)); - $report = $linter->run($config->findFiles(), $ruleset, $fix); - - // Format the output. - $reporter = new TextFormatter($input, $output); - $reporter->display($report, $level); - - // Return a meaningful error code. - if ($report->getTotalErrors()) { - return 1; - } - - return 0; - } -} diff --git a/TwigCS/src/Config/Config.php b/TwigCS/src/Config/Config.php deleted file mode 100644 index 164b312..0000000 --- a/TwigCS/src/Config/Config.php +++ /dev/null @@ -1,58 +0,0 @@ -paths = $paths; - } - - /** - * @return array - * - * @throws Exception - */ - public function findFiles(): array - { - $files = []; - foreach ($this->paths as $path) { - if (is_dir($path)) { - $flags = \RecursiveDirectoryIterator::SKIP_DOTS | \FilesystemIterator::FOLLOW_SYMLINKS; - $directoryIterator = new \RecursiveDirectoryIterator($path, $flags); - $filter = new TwigFileFilter($directoryIterator); - $iterator = new \RecursiveIteratorIterator($filter); - - /** @var \SplFileInfo $file */ - foreach ($iterator as $k => $file) { - $files[] = $file->getRealPath(); - } - } elseif (is_file($path)) { - $file = new \SplFileInfo($path); - $files[] = $file->getRealPath(); - } else { - throw new Exception(sprintf('Unknown path: "%s"', $path)); - } - } - - return $files; - } -} diff --git a/TwigCS/src/Config/TwigFileFilter.php b/TwigCS/src/Config/TwigFileFilter.php deleted file mode 100644 index 2ebb586..0000000 --- a/TwigCS/src/Config/TwigFileFilter.php +++ /dev/null @@ -1,20 +0,0 @@ -current(); - - return $file->isDir() || 'twig' === $file->getExtension(); - } -} diff --git a/TwigCS/src/Environment/StubbedEnvironment.php b/TwigCS/src/Environment/StubbedEnvironment.php deleted file mode 100644 index e81e996..0000000 --- a/TwigCS/src/Environment/StubbedEnvironment.php +++ /dev/null @@ -1,101 +0,0 @@ - - */ - protected $stubFilters = []; - - /** - * @var array - */ - protected $stubFunctions = []; - - /** - * @var array - */ - protected $stubTests = [ - 'divisible' => null, // Allow 'divisible by' - 'same' => null, // Allow 'same as' - ]; - - /** - * @return void - */ - public function __construct() - { - parent::__construct(new ArrayLoader()); - - $this->addTokenParser(new DumpTokenParser()); - $this->addTokenParser(new FormThemeTokenParser()); - $this->addTokenParser(new StopwatchTokenParser(false)); - $this->addTokenParser(new TransDefaultDomainTokenParser()); - $this->addTokenParser(new TransTokenParser()); - - if (class_exists(TransChoiceTokenParser::class)) { - $this->addTokenParser(new TransChoiceTokenParser()); - } - } - - /** - * @param string $name - * - * @return TwigFilter|null - */ - public function getFilter($name): ?TwigFilter - { - if (!array_key_exists($name, $this->stubFilters)) { - $this->stubFilters[$name] = new TwigFilter('stub'); - } - - return $this->stubFilters[$name]; - } - - /** - * @param string $name - * - * @return TwigFunction|null - */ - public function getFunction($name): ?TwigFunction - { - if (!array_key_exists($name, $this->stubFunctions)) { - $this->stubFunctions[$name] = new TwigFunction('stub'); - } - - return $this->stubFunctions[$name]; - } - - /** - * @param string $name - * - * @return TwigTest|null - */ - public function getTest($name): ?TwigTest - { - if (!array_key_exists($name, $this->stubTests)) { - $this->stubTests[$name] = new TwigTest('stub'); - } - - return $this->stubTests[$name]; - } -} diff --git a/TwigCS/src/Report/Report.php b/TwigCS/src/Report/Report.php deleted file mode 100644 index 2544917..0000000 --- a/TwigCS/src/Report/Report.php +++ /dev/null @@ -1,153 +0,0 @@ -getLevel()) { - case self::MESSAGE_TYPE_NOTICE: - ++$this->totalNotices; - break; - case self::MESSAGE_TYPE_WARNING: - ++$this->totalWarnings; - break; - case self::MESSAGE_TYPE_ERROR: - case self::MESSAGE_TYPE_FATAL: - ++$this->totalErrors; - break; - } - - $this->messages[] = $sniffViolation; - - return $this; - } - - /** - * @param array $filters - * - * @return SniffViolation[] - */ - public function getMessages(array $filters = []): array - { - if (0 === count($filters)) { - // Return all messages, without filtering. - return $this->messages; - } - - return array_filter($this->messages, function (SniffViolation $message) use ($filters) { - $fileFilter = true; - $levelFilter = true; - - if (isset($filters['file']) && $filters['file']) { - $fileFilter = (string) $message->getFilename() === (string) $filters['file']; - } - - if (isset($filters['level']) && $filters['level']) { - $levelFilter = $message->getLevel() >= $message::getLevelAsInt($filters['level']); - } - - return $fileFilter && $levelFilter; - }); - } - - /** - * @param string $file - * - * @return void - */ - public function addFile(string $file): void - { - $this->files[] = $file; - } - - /** - * @return string[] - */ - public function getFiles(): array - { - return $this->files; - } - - /** - * @return int - */ - public function getTotalFiles(): int - { - return count($this->files); - } - - /** - * @return int - */ - public function getTotalMessages(): int - { - return count($this->messages); - } - - /** - * @return int - */ - public function getTotalNotices(): int - { - return $this->totalNotices; - } - - /** - * @return int - */ - public function getTotalWarnings(): int - { - return $this->totalWarnings; - } - - /** - * @return int - */ - public function getTotalErrors(): int - { - return $this->totalErrors; - } -} diff --git a/TwigCS/src/Report/SniffViolation.php b/TwigCS/src/Report/SniffViolation.php deleted file mode 100644 index 739341f..0000000 --- a/TwigCS/src/Report/SniffViolation.php +++ /dev/null @@ -1,177 +0,0 @@ -level = $level; - $this->message = $message; - $this->line = $line; - $this->filename = $filename; - } - - /** - * @return int - */ - public function getLevel(): int - { - return $this->level; - } - - /** - * @return string - */ - public function getLevelAsString(): string - { - switch ($this->level) { - case Report::MESSAGE_TYPE_NOTICE: - return self::LEVEL_NOTICE; - case Report::MESSAGE_TYPE_WARNING: - return self::LEVEL_WARNING; - case Report::MESSAGE_TYPE_ERROR: - return self::LEVEL_ERROR; - case Report::MESSAGE_TYPE_FATAL: - return self::LEVEL_FATAL; - default: - throw new LogicException(); - } - } - - /** - * @param string $level - * - * @return int - */ - public static function getLevelAsInt(string $level): int - { - switch (mb_strtoupper($level)) { - case self::LEVEL_NOTICE: - return Report::MESSAGE_TYPE_NOTICE; - case self::LEVEL_WARNING: - return Report::MESSAGE_TYPE_WARNING; - case self::LEVEL_ERROR: - return Report::MESSAGE_TYPE_ERROR; - case self::LEVEL_FATAL: - return Report::MESSAGE_TYPE_FATAL; - default: - throw new LogicException(); - } - } - - /** - * @return string - */ - public function getMessage(): string - { - return $this->message; - } - - /** - * @return int|null - */ - public function getLine(): ?int - { - return $this->line; - } - - /** - * @return string - */ - public function getFilename(): string - { - return $this->filename; - } - - /** - * @param int|null $linePosition - * - * @return self - */ - public function setLinePosition(?int $linePosition): SniffViolation - { - $this->linePosition = $linePosition; - - return $this; - } - - /** - * @return int|null - */ - public function getLinePosition(): ?int - { - return $this->linePosition; - } - - /** - * @param SniffInterface $sniff - * - * @return self - */ - public function setSniff(SniffInterface $sniff): SniffViolation - { - $this->sniff = $sniff; - - return $this; - } - - /** - * @return SniffInterface|null - */ - public function getSniff(): ?SniffInterface - { - return $this->sniff; - } -} diff --git a/TwigCS/src/Report/TextFormatter.php b/TwigCS/src/Report/TextFormatter.php deleted file mode 100644 index 1dd54af..0000000 --- a/TwigCS/src/Report/TextFormatter.php +++ /dev/null @@ -1,159 +0,0 @@ ->'; - private const ERROR_LINE_FORMAT = '%-5s| %s'; - private const ERROR_CONTEXT_LIMIT = 2; - private const ERROR_LINE_WIDTH = 120; - - /** - * @var SymfonyStyle - */ - protected $io; - - /** - * @param InputInterface $input - * @param OutputInterface $output - * - * @return void - */ - public function __construct(InputInterface $input, OutputInterface $output) - { - $this->io = new SymfonyStyle($input, $output); - } - - /** - * @param Report $report - * @param string|null $level - * - * @return void - */ - public function display(Report $report, string $level = null): void - { - foreach ($report->getFiles() as $file) { - $fileMessages = $report->getMessages([ - 'file' => $file, - 'level' => $level, - ]); - - if (count($fileMessages) > 0) { - $this->io->text('KO '.$file); - } - - $rows = []; - foreach ($fileMessages as $message) { - $lines = $this->getContext(file_get_contents($file), $message->getLine(), self::ERROR_CONTEXT_LIMIT); - - $formattedText = []; - if (!$message->getLine()) { - $formattedText[] = $this->formatErrorMessage($message); - } - - foreach ($lines as $no => $code) { - $formattedText[] = sprintf(self::ERROR_LINE_FORMAT, $no, wordwrap($code, self::ERROR_LINE_WIDTH)); - - if ($no === $message->getLine()) { - $formattedText[] = $this->formatErrorMessage($message); - } - } - - if (count($rows) > 0) { - $rows[] = new TableSeparator(); - } - - $rows[] = [ - new TableCell(''.$message->getLevelAsString().''), - implode("\n", $formattedText), - ]; - } - - if (count($rows) > 0) { - $this->io->table([], $rows); - } - } - - $summaryString = sprintf( - 'Files linted: %d, notices: %d, warnings: %d, errors: %d', - $report->getTotalFiles(), - $report->getTotalNotices(), - $report->getTotalWarnings(), - $report->getTotalErrors() - ); - - if (0 === $report->getTotalWarnings() && 0 === $report->getTotalErrors()) { - $this->io->block($summaryString, 'SUCCESS', 'fg=black;bg=green', ' ', true); - } elseif (0 < $report->getTotalWarnings() && 0 === $report->getTotalErrors()) { - $this->io->block($summaryString, 'WARNING', 'fg=black;bg=yellow', ' ', true); - } else { - $this->io->block($summaryString, 'ERROR', 'fg=black;bg=red', ' ', true); - } - } - - /** - * @param string $template - * @param int|null $line - * @param int $context - * - * @return array - */ - protected function getContext(string $template, ?int $line, int $context): array - { - $lines = explode("\n", $template); - - $position = max(0, $line - $context); - $max = min(count($lines), $line - 1 + $context); - - $result = []; - $indentCount = null; - while ($position < $max) { - if (preg_match('/^([\s\t]+)/', $lines[$position], $match)) { - if (null === $indentCount) { - $indentCount = mb_strlen($match[1]); - } - - if (mb_strlen($match[1]) < $indentCount) { - $indentCount = mb_strlen($match[1]); - } - } else { - $indentCount = 0; - } - - $result[$position + 1] = $lines[$position]; - $position++; - } - - foreach ($result as $index => $code) { - $result[$index] = mb_substr($code, $indentCount); - } - - return $result; - } - - /** - * @param SniffViolation $message - * - * @return string - */ - protected function formatErrorMessage(SniffViolation $message): string - { - return sprintf( - ''.self::ERROR_LINE_FORMAT.'', - self::ERROR_CURSOR_CHAR, - wordwrap($message->getMessage(), self::ERROR_LINE_WIDTH) - ); - } -} diff --git a/TwigCS/src/Ruleset/Generic/BlankEOFSniff.php b/TwigCS/src/Ruleset/Generic/BlankEOFSniff.php deleted file mode 100644 index 2313da9..0000000 --- a/TwigCS/src/Ruleset/Generic/BlankEOFSniff.php +++ /dev/null @@ -1,59 +0,0 @@ -isTokenMatching($token, Token::EOF_TYPE)) { - $i = 0; - while ( - isset($tokens[$tokenPosition - ($i + 1)]) - && $this->isTokenMatching($tokens[$tokenPosition - ($i + 1)], Token::EOL_TYPE) - ) { - $i++; - } - - if (1 !== $i) { - // Either 0 or 2+ blank lines. - $fix = $this->addFixableError( - sprintf('A file must end with 1 blank line; found %d', $i), - $token - ); - - if ($fix) { - if (0 === $i) { - $this->fixer->addNewlineBefore($tokenPosition); - } else { - $this->fixer->beginChangeset(); - while ($i > 1) { - $this->fixer->replaceToken($tokenPosition - $i, ''); - $i--; - } - $this->fixer->endChangeset(); - } - } - } - } - } -} diff --git a/TwigCS/src/Ruleset/Generic/DelimiterSpacingSniff.php b/TwigCS/src/Ruleset/Generic/DelimiterSpacingSniff.php deleted file mode 100644 index 7df9dfd..0000000 --- a/TwigCS/src/Ruleset/Generic/DelimiterSpacingSniff.php +++ /dev/null @@ -1,56 +0,0 @@ -isTokenMatching($token, Token::VAR_END_TYPE) - || $this->isTokenMatching($token, Token::BLOCK_END_TYPE) - || $this->isTokenMatching($token, Token::COMMENT_END_TYPE) - ) { - return 1; - } - - return null; - } - - /** - * @param int $tokenPosition - * @param Token[] $tokens - * - * @return int|null - */ - protected function shouldHaveSpaceAfter(int $tokenPosition, array $tokens): ?int - { - $token = $tokens[$tokenPosition]; - - if ( - $this->isTokenMatching($token, Token::VAR_START_TYPE) - || $this->isTokenMatching($token, Token::BLOCK_START_TYPE) - || $this->isTokenMatching($token, Token::COMMENT_START_TYPE) - ) { - return 1; - } - - return null; - } -} diff --git a/TwigCS/src/Ruleset/Generic/EmptyLinesSniff.php b/TwigCS/src/Ruleset/Generic/EmptyLinesSniff.php deleted file mode 100644 index 254e002..0000000 --- a/TwigCS/src/Ruleset/Generic/EmptyLinesSniff.php +++ /dev/null @@ -1,54 +0,0 @@ -isTokenMatching($token, Token::EOL_TYPE)) { - $i = 0; - while ( - isset($tokens[$tokenPosition - ($i + 1)]) - && $this->isTokenMatching($tokens[$tokenPosition - ($i + 1)], Token::EOL_TYPE) - ) { - $i++; - } - - if (1 < $i) { - $fix = $this->addFixableError( - sprintf('More than 1 empty lines are not allowed, found %d', $i), - $token - ); - - if ($fix) { - $this->fixer->beginChangeset(); - while ($i > 1) { - $this->fixer->replaceToken($tokenPosition - $i, ''); - $i--; - } - $this->fixer->endChangeset(); - } - } - } - } -} diff --git a/TwigCS/src/Ruleset/Generic/OperatorSpacingSniff.php b/TwigCS/src/Ruleset/Generic/OperatorSpacingSniff.php deleted file mode 100644 index aedcd11..0000000 --- a/TwigCS/src/Ruleset/Generic/OperatorSpacingSniff.php +++ /dev/null @@ -1,99 +0,0 @@ -isTokenMatching($token, Token::OPERATOR_TYPE)) { - return null; - } - - if ($this->isTokenMatching($token, Token::OPERATOR_TYPE, ['-', '+'])) { - return $this->isUnary($tokenPosition, $tokens) ? null : 1; - } - - if ($this->isTokenMatching($token, Token::OPERATOR_TYPE, '..')) { - return 0; - } - - return 1; - } - - /** - * @param int $tokenPosition - * @param Token[] $tokens - * - * @return int|null - */ - protected function shouldHaveSpaceAfter(int $tokenPosition, array $tokens): ?int - { - $token = $tokens[$tokenPosition]; - if (!$this->isTokenMatching($token, Token::OPERATOR_TYPE)) { - return null; - } - - if ($this->isTokenMatching($token, Token::OPERATOR_TYPE, ['-', '+'])) { - return $this->isUnary($tokenPosition, $tokens) ? 0 : 1; - } - - if ($this->isTokenMatching($token, Token::OPERATOR_TYPE, '..')) { - return 0; - } - - return 1; - } - - /** - * @param int $tokenPosition - * @param array $tokens - * - * @return bool - */ - private function isUnary(int $tokenPosition, array $tokens): bool - { - $previous = $this->findPrevious(Token::EMPTY_TOKENS, $tokens, $tokenPosition - 1, true); - if (false === $previous) { - return true; - } - - $previousToken = $tokens[$previous]; - if ($this->isTokenMatching($previousToken, Token::OPERATOR_TYPE)) { - // {{ 1 * -2 }} - return true; - } - - if ($this->isTokenMatching($previousToken, Token::VAR_START_TYPE)) { - // {{ -2 }} - return true; - } - - if ($this->isTokenMatching($previousToken, Token::PUNCTUATION_TYPE, ['(', '[', ':', ','])) { - // {{ 1 + (-2) }} - return true; - } - - if ($this->isTokenMatching($previousToken, Token::BLOCK_TAG_TYPE)) { - // {% if -2 ... %} - return true; - } - - return false; - } -} diff --git a/TwigCS/src/Ruleset/Generic/PunctuationSpacingSniff.php b/TwigCS/src/Ruleset/Generic/PunctuationSpacingSniff.php deleted file mode 100644 index 2bdb822..0000000 --- a/TwigCS/src/Ruleset/Generic/PunctuationSpacingSniff.php +++ /dev/null @@ -1,50 +0,0 @@ -isTokenMatching($token, Token::PUNCTUATION_TYPE, [')', ']', '}', ':', '.', ',', '|'])) { - return 0; - } - - return null; - } - - /** - * @param int $tokenPosition - * @param Token[] $tokens - * - * @return int|null - */ - protected function shouldHaveSpaceAfter(int $tokenPosition, array $tokens): ?int - { - $token = $tokens[$tokenPosition]; - if ($this->isTokenMatching($token, Token::PUNCTUATION_TYPE, [':', ','])) { - return 1; - } - - if ($this->isTokenMatching($token, Token::PUNCTUATION_TYPE, ['(', '[', '{', '.', '|'])) { - return 0; - } - - return null; - } -} diff --git a/TwigCS/src/Ruleset/Ruleset.php b/TwigCS/src/Ruleset/Ruleset.php deleted file mode 100644 index 01131f8..0000000 --- a/TwigCS/src/Ruleset/Ruleset.php +++ /dev/null @@ -1,69 +0,0 @@ -sniffs; - } - - /** - * @param SniffInterface $sniff - * - * @return $this - */ - public function addSniff(SniffInterface $sniff): Ruleset - { - $this->sniffs[get_class($sniff)] = $sniff; - - return $this; - } - - /** - * @param string $standardName - * - * @return Ruleset - * - * @throws Exception - */ - public function addStandard(string $standardName = 'Generic'): Ruleset - { - if (!is_dir(__DIR__.'/'.$standardName)) { - throw new Exception(sprintf('The standard "%s" is not found.', $standardName)); - } - - $flags = \RecursiveDirectoryIterator::SKIP_DOTS | \FilesystemIterator::FOLLOW_SYMLINKS; - $directoryIterator = new \RecursiveDirectoryIterator(__DIR__.'/'.$standardName, $flags); - $iterator = new \RecursiveIteratorIterator($directoryIterator); - - /** @var SplFileInfo $file */ - foreach ($iterator as $file) { - $class = __NAMESPACE__.'\\'.$standardName.'\\'.$file->getBasename('.php'); - - if (class_exists($class)) { - $this->addSniff(new $class()); - } - } - - return $this; - } -} diff --git a/TwigCS/src/Runner/Fixer.php b/TwigCS/src/Runner/Fixer.php deleted file mode 100644 index 5f45f3d..0000000 --- a/TwigCS/src/Runner/Fixer.php +++ /dev/null @@ -1,385 +0,0 @@ -ruleset = $ruleset; - $this->tokenizer = $tokenizer; - } - - /** - * @param array $tokens - * - * @return void - */ - public function startFile(array $tokens): void - { - $this->numFixes = 0; - $this->fixedTokens = []; - - $this->tokens = array_map(function (Token $token) { - return $token->getValue(); - }, $tokens); - - if (preg_match("/\r\n?|\n/", $this->getContents(), $matches) !== 1) { - // Assume there are no newlines. - $this->eolChar = "\n"; - } else { - $this->eolChar = $matches[0]; - } - } - - /** - * @param string $file - * - * @return bool - */ - public function fixFile(string $file): bool - { - $contents = file_get_contents($file); - - $this->loops = 0; - while ($this->loops < 50) { - $this->inConflict = false; - - try { - $twigSource = new Source($contents, 'TwigCS'); - $stream = $this->tokenizer->tokenize($twigSource); - } catch (Exception $exception) { - return false; - } - - $this->startFile($stream); - - $sniffs = $this->ruleset->getSniffs(); - foreach ($sniffs as $sniff) { - $sniff->processFile($stream); - } - - $this->loops++; - - if (0 === $this->numFixes && !$this->inConflict) { - // Nothing left to do. - break; - } - - // Only needed once file content has changed. - $contents = $this->getContents(); - } - - if ($this->numFixes > 0) { - return false; - } - - return true; - } - - /** - * @return string - */ - public function getContents(): string - { - return implode($this->tokens); - } - - /** - * Start recording actions for a changeset. - * - * @return void - */ - public function beginChangeset(): void - { - if ($this->inConflict) { - return; - } - - $this->changeset = []; - $this->inChangeset = true; - } - - /** - * Stop recording actions for a changeset, and apply logged changes. - * - * @return void - */ - public function endChangeset(): void - { - if ($this->inConflict) { - return; - } - - $this->inChangeset = false; - - $success = true; - $applied = []; - foreach ($this->changeset as $tokenPosition => $content) { - $success = $this->replaceToken($tokenPosition, $content); - if (!$success) { - break; - } else { - $applied[] = $tokenPosition; - } - } - - if (!$success) { - // Rolling back all changes. - foreach ($applied as $tokenPosition) { - $this->revertToken($tokenPosition); - } - } - - $this->changeset = []; - } - - /** - * Stop recording actions for a changeset, and discard logged changes. - * - * @return void - */ - public function rollbackChangeset(): void - { - $this->inChangeset = false; - $this->inConflict = false; - - if (count($this->changeset) > 0) { - $this->changeset = []; - } - } - - /** - * @param int $tokenPosition - * @param string $content - * - * @return bool - */ - public function replaceToken(int $tokenPosition, string $content): bool - { - if ($this->inConflict) { - return false; - } - - if (!$this->inChangeset && isset($this->fixedTokens[$tokenPosition])) { - return false; - } - - if ($this->inChangeset) { - $this->changeset[$tokenPosition] = $content; - - return true; - } - - if (!isset($this->oldTokenValues[$tokenPosition])) { - $this->oldTokenValues[$tokenPosition] = [ - 'curr' => $content, - 'prev' => $this->tokens[$tokenPosition], - 'loop' => $this->loops, - ]; - } else { - if ( - $content === $this->oldTokenValues[$tokenPosition]['prev'] - && ($this->loops - 1) === $this->oldTokenValues[$tokenPosition]['loop'] - ) { - if ($this->oldTokenValues[$tokenPosition]['loop'] >= ($this->loops - 1)) { - $this->inConflict = true; - } - - return false; - } - - $this->oldTokenValues[$tokenPosition]['prev'] = $this->oldTokenValues[$tokenPosition]['curr']; - $this->oldTokenValues[$tokenPosition]['curr'] = $content; - $this->oldTokenValues[$tokenPosition]['loop'] = $this->loops; - } - - $this->fixedTokens[$tokenPosition] = $this->tokens[$tokenPosition]; - $this->tokens[$tokenPosition] = $content; - $this->numFixes++; - - return true; - } - - /** - * @param int $tokenPosition - * - * @return bool - */ - public function addNewline(int $tokenPosition): bool - { - $current = $this->getTokenContent($tokenPosition); - - return $this->replaceToken($tokenPosition, $current.$this->eolChar); - } - - /** - * @param int $tokenPosition - * - * @return bool - */ - public function addNewlineBefore(int $tokenPosition): bool - { - $current = $this->getTokenContent($tokenPosition); - - return $this->replaceToken($tokenPosition, $this->eolChar.$current); - } - - /** - * @param int $tokenPosition - * @param string $content - * - * @return bool - */ - public function addContent(int $tokenPosition, string $content): bool - { - $current = $this->getTokenContent($tokenPosition); - - return $this->replaceToken($tokenPosition, $current.$content); - } - - /** - * @param int $tokenPosition - * @param string $content - * - * @return bool - */ - public function addContentBefore(int $tokenPosition, string $content): bool - { - $current = $this->getTokenContent($tokenPosition); - - return $this->replaceToken($tokenPosition, $content.$current); - } - - /** - * This function takes changesets into account so should be used - * instead of directly accessing the token array. - * - * @param int $tokenPosition - * - * @return string - */ - protected function getTokenContent(int $tokenPosition): string - { - if ($this->inChangeset && isset($this->changeset[$tokenPosition])) { - return $this->changeset[$tokenPosition]; - } - - return $this->tokens[$tokenPosition]; - } - - /** - * @param int $tokenPosition - * - * @return bool - */ - protected function revertToken(int $tokenPosition): bool - { - if (!isset($this->fixedTokens[$tokenPosition])) { - return false; - } - - $this->tokens[$tokenPosition] = $this->fixedTokens[$tokenPosition]; - unset($this->fixedTokens[$tokenPosition]); - $this->numFixes--; - - return true; - } -} diff --git a/TwigCS/src/Runner/Linter.php b/TwigCS/src/Runner/Linter.php deleted file mode 100644 index 26d8ff6..0000000 --- a/TwigCS/src/Runner/Linter.php +++ /dev/null @@ -1,184 +0,0 @@ -env = $env; - $this->tokenizer = $tokenizer; - } - - /** - * @param array $files - * @param Ruleset $ruleset - * @param bool $fix - * - * @return Report - * - * @throws Exception - */ - public function run(array $files, Ruleset $ruleset, bool $fix = false): Report - { - $report = new Report(); - - if ($fix) { - $this->fix($files, $ruleset); - } - - foreach ($ruleset->getSniffs() as $sniff) { - $sniff->enableReport($report); - } - - // Process - foreach ($files as $file) { - $file = strval($file); - $this->setErrorHandler($report, $file); - - $this->processTemplate($file, $ruleset, $report); - - // Add this file to the report. - $report->addFile($file); - } - restore_error_handler(); - - // tearDown - foreach ($ruleset->getSniffs() as $sniff) { - $sniff->disable(); - } - - return $report; - } - - /** - * @param iterable $files - * @param Ruleset $ruleset - * - * @return void - * - * @throws Exception - */ - protected function fix(iterable $files, Ruleset $ruleset): void - { - $fixer = new Fixer($ruleset, $this->tokenizer); - - foreach ($ruleset->getSniffs() as $sniff) { - $sniff->enableFixer($fixer); - } - - foreach ($files as $file) { - $file = strval($file); - $success = $fixer->fixFile($file); - - if (!$success) { - throw new Exception("Cannot fix the file $file."); - } - - file_put_contents($file, $fixer->getContents()); - } - } - - /** - * @param string $file - * @param Ruleset $ruleset - * @param Report $report - * - * @return bool - */ - protected function processTemplate(string $file, Ruleset $ruleset, Report $report): bool - { - $twigSource = new Source(file_get_contents($file), $file); - - // Tokenize + Parse. - try { - $this->env->parse($this->env->tokenize($twigSource)); - } catch (Error $e) { - $sniffViolation = new SniffViolation( - Report::MESSAGE_TYPE_FATAL, - $e->getRawMessage(), - $e->getSourceContext()->getName(), - $e->getTemplateLine() - ); - - $report->addMessage($sniffViolation); - - return false; - } - - // Tokenizer. - try { - $stream = $this->tokenizer->tokenize($twigSource); - } catch (Exception $exception) { - $sniffViolation = new SniffViolation( - Report::MESSAGE_TYPE_FATAL, - sprintf('Unable to tokenize file: %s', $exception->getMessage()), - $file - ); - - $report->addMessage($sniffViolation); - - return false; - } - - /** @var SniffInterface[] $sniffs */ - $sniffs = $ruleset->getSniffs(); - foreach ($sniffs as $sniff) { - $sniff->processFile($stream); - } - - return true; - } - - /** - * @param Report $report - * @param string|null $file - * - * @return void - */ - protected function setErrorHandler(Report $report, string $file = null): void - { - set_error_handler(function ($type, $message) use ($report, $file) { - if (E_USER_DEPRECATED === $type) { - $sniffViolation = new SniffViolation( - Report::MESSAGE_TYPE_NOTICE, - $message, - $file - ); - - $report->addMessage($sniffViolation); - } - }); - } -} diff --git a/TwigCS/src/Sniff/AbstractSniff.php b/TwigCS/src/Sniff/AbstractSniff.php deleted file mode 100644 index fe43578..0000000 --- a/TwigCS/src/Sniff/AbstractSniff.php +++ /dev/null @@ -1,240 +0,0 @@ -report = $report; - } - - /** - * @param Fixer $fixer - * - * @return void - */ - public function enableFixer(Fixer $fixer): void - { - $this->fixer = $fixer; - } - - /** - * @return void - */ - public function disable(): void - { - $this->report = null; - $this->fixer = null; - } - - /** - * @param array $stream - * - * @return void - */ - public function processFile(array $stream): void - { - foreach ($stream as $index => $token) { - $this->process($index, $stream); - } - } - - /** - * @param int $tokenPosition - * @param Token[] $stream - * - * @return void - */ - abstract protected function process(int $tokenPosition, array $stream): void; - - /** - * @param Token $token - * @param int|array $type - * @param string|array $value - * - * @return bool - */ - protected function isTokenMatching(Token $token, $type, $value = []): bool - { - if (!is_array($type)) { - $type = [$type]; - } - if (!is_array($value)) { - $value = [$value]; - } - - return in_array($token->getType(), $type) && ([] === $value || in_array($token->getValue(), $value)); - } - - /** - * @param int|array $type - * @param array $tokens - * @param int $start - * @param bool $exclude - * - * @return int|false - */ - protected function findNext($type, array $tokens, int $start, bool $exclude = false) - { - $i = 0; - - while (isset($tokens[$start + $i]) && $exclude === $this->isTokenMatching($tokens[$start + $i], $type)) { - $i++; - } - - if (!isset($tokens[$start + $i])) { - return false; - } - - return $start + $i; - } - - /** - * @param int|array $type - * @param array $tokens - * @param int $start - * @param bool $exclude - * - * @return int|false - */ - protected function findPrevious($type, array $tokens, int $start, bool $exclude = false) - { - $i = 0; - - while (isset($tokens[$start - $i]) && $exclude === $this->isTokenMatching($tokens[$start - $i], $type)) { - $i++; - } - - if (!isset($tokens[$start - $i])) { - return false; - } - - return $start - $i; - } - - /** - * @param string $message - * @param Token $token - * - * @return void - * - * @throws Exception - */ - protected function addWarning(string $message, Token $token): void - { - $this->addMessage(Report::MESSAGE_TYPE_WARNING, $message, $token); - } - - /** - * @param string $message - * @param Token $token - * - * @return void - * - * @throws Exception - */ - protected function addError(string $message, Token $token): void - { - $this->addMessage(Report::MESSAGE_TYPE_ERROR, $message, $token); - } - - /** - * @param string $message - * @param Token $token - * - * @return bool - * - * @throws Exception - */ - protected function addFixableWarning(string $message, Token $token): bool - { - return $this->addFixableMessage(Report::MESSAGE_TYPE_WARNING, $message, $token); - } - - /** - * @param string $message - * @param Token $token - * - * @return bool - * - * @throws Exception - */ - protected function addFixableError(string $message, Token $token): bool - { - return $this->addFixableMessage(Report::MESSAGE_TYPE_ERROR, $message, $token); - } - - /** - * @param int $messageType - * @param string $message - * @param Token $token - * - * @return void - * - * @throws Exception - */ - private function addMessage(int $messageType, string $message, Token $token): void - { - if (null === $this->report) { - if (null !== $this->fixer) { - // We are fixing the file, ignore this - return; - } - - throw new Exception('Sniff is disabled!'); - } - - $sniffViolation = new SniffViolation( - $messageType, - $message, - $token->getFilename(), - $token->getLine() - ); - $sniffViolation->setLinePosition($token->getPosition()); - - $this->report->addMessage($sniffViolation); - } - - /** - * @param int $messageType - * @param string $message - * @param Token $token - * - * @return bool - * - * @throws Exception - */ - private function addFixableMessage(int $messageType, string $message, Token $token): bool - { - $this->addMessage($messageType, $message, $token); - - return null !== $this->fixer; - } -} diff --git a/TwigCS/src/Sniff/AbstractSpacingSniff.php b/TwigCS/src/Sniff/AbstractSpacingSniff.php deleted file mode 100644 index 007654f..0000000 --- a/TwigCS/src/Sniff/AbstractSpacingSniff.php +++ /dev/null @@ -1,134 +0,0 @@ -shouldHaveSpaceAfter($tokenPosition, $tokens); - $spaceBefore = $this->shouldHaveSpaceBefore($tokenPosition, $tokens); - - if (null !== $spaceAfter) { - $this->checkSpaceAfter($tokenPosition, $tokens, $spaceAfter); - } - - if (null !== $spaceBefore) { - $this->checkSpaceBefore($tokenPosition, $tokens, $spaceBefore); - } - } - - /** - * @param int $tokenPosition - * @param Token[] $tokens - * - * @return int|null - */ - abstract protected function shouldHaveSpaceAfter(int $tokenPosition, array $tokens): ?int; - - /** - * @param int $tokenPosition - * @param Token[] $tokens - * - * @return int|null - */ - abstract protected function shouldHaveSpaceBefore(int $tokenPosition, array $tokens): ?int; - - /** - * @param int $tokenPosition - * @param Token[] $tokens - * @param int $expected - * - * @return void - * - * @throws Exception - */ - private function checkSpaceAfter(int $tokenPosition, array $tokens, int $expected): void - { - $token = $tokens[$tokenPosition]; - - // Ignore new line - $next = $this->findNext(Token::WHITESPACE_TOKENS, $tokens, $tokenPosition + 1, true); - if (false !== $next && $this->isTokenMatching($tokens[$next], [Token::EOL_TYPE, Token::COMMENT_EOL_TYPE])) { - return; - } - - if ($this->isTokenMatching($tokens[$tokenPosition + 1], Token::WHITESPACE_TOKENS)) { - $count = mb_strlen($tokens[$tokenPosition + 1]->getValue()); - } else { - $count = 0; - } - - if ($expected !== $count) { - $fix = $this->addFixableError( - sprintf('Expecting %d whitespace after "%s"; found %d', $expected, $token->getValue(), $count), - $token - ); - - if ($fix) { - if (0 === $count) { - $this->fixer->addContent($tokenPosition, str_repeat(' ', $expected)); - } else { - $this->fixer->replaceToken($tokenPosition + 1, str_repeat(' ', $expected)); - } - } - } - } - - /** - * @param int $tokenPosition - * @param Token[] $tokens - * @param int $expected - * - * @return void - * - * @throws Exception - */ - private function checkSpaceBefore(int $tokenPosition, array $tokens, int $expected): void - { - $token = $tokens[$tokenPosition]; - - // Ignore new line - $previous = $this->findPrevious(Token::WHITESPACE_TOKENS, $tokens, $tokenPosition - 1, true); - if ($this->isTokenMatching($tokens[$previous], [Token::EOL_TYPE, Token::COMMENT_EOL_TYPE])) { - return; - } - - if ($this->isTokenMatching($tokens[$tokenPosition - 1], Token::WHITESPACE_TOKENS)) { - $count = mb_strlen($tokens[$tokenPosition - 1]->getValue()); - } else { - $count = 0; - } - - if ($expected !== $count) { - $fix = $this->addFixableError( - sprintf('Expecting %d whitespace before "%s"; found %d', $expected, $token->getValue(), $count), - $token - ); - - if ($fix) { - if (0 === $count) { - $this->fixer->addContentBefore($tokenPosition, str_repeat(' ', $expected)); - } else { - $this->fixer->replaceToken($tokenPosition - 1, str_repeat(' ', $expected)); - } - } - } - } -} diff --git a/TwigCS/src/Sniff/SniffInterface.php b/TwigCS/src/Sniff/SniffInterface.php deleted file mode 100644 index 844f15c..0000000 --- a/TwigCS/src/Sniff/SniffInterface.php +++ /dev/null @@ -1,46 +0,0 @@ - self::WHITESPACE_TYPE, - self::TAB_TYPE => self::TAB_TYPE, - self::EOL_TYPE => self::EOL_TYPE, - self::COMMENT_START_TYPE => self::COMMENT_START_TYPE, - self::COMMENT_TEXT_TYPE => self::COMMENT_TEXT_TYPE, - self::COMMENT_WHITESPACE_TYPE => self::COMMENT_WHITESPACE_TYPE, - self::COMMENT_TAB_TYPE => self::COMMENT_TAB_TYPE, - self::COMMENT_EOL_TYPE => self::COMMENT_EOL_TYPE, - self::COMMENT_END_TYPE => self::COMMENT_END_TYPE, - ]; - - public const WHITESPACE_TOKENS = [ - self::WHITESPACE_TYPE => self::WHITESPACE_TYPE, - self::COMMENT_WHITESPACE_TYPE => self::COMMENT_WHITESPACE_TYPE, - ]; - - /** - * @var int - */ - private $type; - - /** - * @var int - */ - private $line; - - /** - * @var int - */ - private $position; - - /** - * @var string - */ - private $filename; - - /** - * @var string|null - */ - private $value; - - /** - * @param int $type - * @param int $line - * @param int $position - * @param string $filename - * @param string|null $value - * - * @return void - */ - public function __construct( - int $type, - int $line, - int $position, - string $filename, - string $value = null - ) { - $this->type = $type; - $this->line = $line; - $this->position = $position; - $this->filename = $filename; - $this->value = $value; - } - - /** - * @return int - */ - public function getType(): int - { - return $this->type; - } - - /** - * @return int - */ - public function getLine(): int - { - return $this->line; - } - - /** - * @return int - */ - public function getPosition(): int - { - return $this->position; - } - - /** - * @return string - */ - public function getFilename(): string - { - return $this->filename; - } - - /** - * @return string|null - */ - public function getValue(): ?string - { - return $this->value; - } -} diff --git a/TwigCS/src/Token/TokenParser.php b/TwigCS/src/Token/TokenParser.php deleted file mode 100644 index 20e86ee..0000000 --- a/TwigCS/src/Token/TokenParser.php +++ /dev/null @@ -1,110 +0,0 @@ -name = $name; - } - - /** - * @param Token $token - * - * @return bool - */ - public function decideEnd(Token $token): bool - { - return $token->test('end'.$this->name); - } - - /** - * @param Token $token - * - * @return Node - * - * @throws Exception - */ - public function parse(Token $token): Node - { - $stream = $this->parser->getStream(); - - while ($stream->getCurrent()->getType() !== Token::BLOCK_END_TYPE) { - $stream->next(); - } - - $stream->expect(Token::BLOCK_END_TYPE); - - if ($this->hasBody($stream)) { - $this->parser->subparse([$this, 'decideEnd'], true); - $stream->expect(Token::BLOCK_END_TYPE); - } - - $attributes = []; - if ($token->getValue()) { - $attributes['name'] = $token->getValue(); - } - - return new Node([], $attributes, $token->getLine(), $token->getValue() ?: null); - } - - /** - * @return string - */ - public function getTag(): string - { - return $this->name; - } - - /** - * @param TokenStream $stream - * - * @return bool - * - * @throws Exception - */ - private function hasBody(TokenStream $stream): bool - { - $look = 0; - $token = $stream->look($look); - while ($token) { - if ($token->getType() === Token::EOF_TYPE) { - return false; - } - - if ( - $token->getType() === Token::NAME_TYPE - && $token->getValue() === 'end'.$this->name - ) { - return true; - } - - $look++; - $token = $stream->look($look); - } - - return false; - } -} diff --git a/TwigCS/src/Token/Tokenizer.php b/TwigCS/src/Token/Tokenizer.php deleted file mode 100644 index d4ce9f2..0000000 --- a/TwigCS/src/Token/Tokenizer.php +++ /dev/null @@ -1,749 +0,0 @@ -options = array_merge([ - 'tag_comment' => ['{#', '#}'], - 'tag_block' => ['{%', '%}'], - 'tag_variable' => ['{{', '}}'], - 'whitespace_trim' => '-', - 'whitespace_line_trim' => '~', - 'interpolation' => ['#{', '}'], - ], $options); - - $tokenizerHelper = new TokenizerHelper($env, $this->options); - $this->regexes = [ - 'lex_block' => $tokenizerHelper->getBlockRegex(), - 'lex_comment' => $tokenizerHelper->getCommentRegex(), - 'lex_variable' => $tokenizerHelper->getVariableRegex(), - 'operator' => $tokenizerHelper->getOperatorRegex(), - 'lex_tokens_start' => $tokenizerHelper->getTokensStartRegex(), - 'interpolation_start' => $tokenizerHelper->getInterpolationStartRegex(), - 'interpolation_end' => $tokenizerHelper->getInterpolationEndRegex(), - ]; - } - - /** - * @param Source $source - * - * @return Token[] - * - * @throws Exception - */ - public function tokenize(Source $source): array - { - $this->resetState($source); - $this->preflightSource($this->code); - - while ($this->cursor < $this->end) { - $lastToken = $this->getTokenPosition(); - $nextToken = $this->getTokenPosition(1); - - while (null !== $nextToken && $nextToken['position'] < $this->cursor) { - $this->moveCurrentPosition(); - $lastToken = $nextToken; - $nextToken = $this->getTokenPosition(1); - } - - switch ($this->getState()) { - case self::STATE_BLOCK: - $this->lexBlock(); - break; - case self::STATE_VAR: - $this->lexVariable(); - break; - case self::STATE_COMMENT: - $this->lexComment(); - break; - case self::STATE_DATA: - if (null !== $lastToken && $this->cursor === $lastToken['position']) { - $this->lexStart(); - } else { - $this->lexData(); - } - break; - case self::STATE_DQ_STRING: - $this->lexDqString(); - break; - case self::STATE_INTERPOLATION: - $this->lexInterpolation(); - break; - default: - throw new Exception('Unhandled state in tokenize', 1); - } - } - - if (self::STATE_DATA !== $this->getState()) { - throw new Exception('Error Processing Request', 1); - } - - $this->pushToken(Token::EOF_TYPE); - - return $this->tokens; - } - - /** - * @param Source $source - * - * @return void - */ - protected function resetState(Source $source): void - { - $this->cursor = 0; - $this->line = 1; - $this->currentPosition = 0; - $this->tokens = []; - $this->state = []; - $this->bracketsAndTernary = []; - - $this->code = str_replace(["\r\n", "\r"], "\n", $source->getCode()); - $this->end = strlen($this->code); - $this->filename = $source->getName(); - } - - /** - * @return int - */ - protected function getState(): int - { - return count($this->state) > 0 ? $this->state[count($this->state) - 1][0] : self::STATE_DATA; - } - - /** - * @param int $state - * @param array $data - * - * @return void - */ - protected function pushState(int $state, array $data = []): void - { - $this->state[] = [$state, $data]; - } - - /** - * @param string $name - * @param mixed $value - * - * @return void - * - * @throws Exception - */ - protected function setStateParam($name, $value): void - { - if (0 === count($this->state)) { - throw new Exception('Cannot update state without a current state'); - } - - $this->state[count($this->state) - 1][1][$name] = $value; - } - - /** - * @return array - */ - protected function getStateParams(): array - { - return count($this->state) > 0 ? $this->state[count($this->state) - 1][1] : []; - } - - /** - * @return void - * - * @throws Exception - */ - protected function popState(): void - { - if (0 === count($this->state)) { - throw new Exception('Cannot pop state without a current state'); - } - array_pop($this->state); - } - - /** - * @param string $code - * - * @return void - */ - protected function preflightSource(string $code): void - { - $tokenPositions = []; - preg_match_all($this->regexes['lex_tokens_start'], $code, $tokenPositions, PREG_OFFSET_CAPTURE); - - $tokenPositionsReworked = []; - foreach ($tokenPositions[0] as $index => $tokenFullMatch) { - $tokenPositionsReworked[$index] = [ - 'fullMatch' => $tokenFullMatch[0], - 'position' => $tokenFullMatch[1], - 'match' => $tokenPositions[1][$index][0], - ]; - } - - $this->tokenPositions = $tokenPositionsReworked; - } - - /** - * @param int $offset - * - * @return array|null - */ - protected function getTokenPosition(int $offset = 0): ?array - { - if ( - count($this->tokenPositions) === 0 - || !isset($this->tokenPositions[$this->currentPosition + $offset]) - ) { - return null; - } - - return $this->tokenPositions[$this->currentPosition + $offset]; - } - - /** - * @param int $value - * - * @return void - */ - protected function moveCurrentPosition(int $value = 1): void - { - $this->currentPosition += $value; - } - - /** - * @param string $value - * - * @return void - */ - protected function moveCursor(string $value): void - { - $this->cursor += strlen($value); - $this->line += substr_count($value, "\n"); - } - - /** - * @param int $type - * @param string|null $value - * - * @return void - */ - protected function pushToken(int $type, string $value = null): void - { - $tokenPositionInLine = $this->cursor - strrpos(substr($this->code, 0, $this->cursor), PHP_EOL); - $this->tokens[] = new Token($type, $this->line, $tokenPositionInLine, $this->filename, $value); - } - - /** - * @return void - * - * @throws Exception - */ - protected function lexExpression(): void - { - $currentToken = $this->code[$this->cursor]; - $nextToken = $this->code[$this->cursor + 1] ?? null; - - if (preg_match('/\t/', $currentToken)) { - $this->lexTab(); - } elseif (' ' === $currentToken) { - $this->lexWhitespace(); - } elseif (PHP_EOL === $currentToken) { - $this->lexEOL(); - } elseif ('=' === $currentToken && '>' === $nextToken) { - $this->lexArrowFunction(); - } elseif (preg_match($this->regexes['operator'], $this->code, $match, 0, $this->cursor)) { - $this->lexOperator($match[0]); - } elseif (preg_match(self::REGEX_NAME, $this->code, $match, 0, $this->cursor)) { - $this->lexName($match[0]); - } elseif (preg_match(self::REGEX_NUMBER, $this->code, $match, 0, $this->cursor)) { - $this->lexNumber($match[0]); - } elseif (false !== strpos(self::PUNCTUATION, $this->code[$this->cursor])) { - $this->lexPunctuation(); - } elseif (preg_match(self::REGEX_STRING, $this->code, $match, 0, $this->cursor)) { - $this->lexString($match[0]); - } elseif (preg_match(self::REGEX_DQ_STRING_DELIM, $this->code, $match, 0, $this->cursor)) { - $this->lexStartDqString(); - } else { - throw new Exception(sprintf('Unexpected character "%s"', $currentToken)); - } - } - - /** - * @return void - * - * @throws Exception - */ - protected function lexBlock(): void - { - $endRegex = $this->regexes['lex_block']; - preg_match($endRegex, $this->code, $match, PREG_OFFSET_CAPTURE, $this->cursor); - - if (count($this->bracketsAndTernary) === 0 && isset($match[0])) { - $this->pushToken(Token::BLOCK_END_TYPE, $match[0][0]); - $this->moveCursor($match[0][0]); - $this->moveCurrentPosition(); - $this->popState(); - } else { - $this->lexExpression(); - } - } - - /** - * @return void - * - * @throws Exception - */ - protected function lexVariable(): void - { - $endRegex = $this->regexes['lex_variable']; - preg_match($endRegex, $this->code, $match, PREG_OFFSET_CAPTURE, $this->cursor); - - if (count($this->bracketsAndTernary) === 0 && isset($match[0])) { - $this->pushToken(Token::VAR_END_TYPE, $match[0][0]); - $this->moveCursor($match[0][0]); - $this->moveCurrentPosition(); - $this->popState(); - } else { - $this->lexExpression(); - } - } - - /** - * @return void - * - * @throws Exception - */ - protected function lexComment(): void - { - $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'); - } - if ($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]); - } - } - - /** - * @return void - * - * @throws Exception - */ - protected function lexDqString(): void - { - if (preg_match($this->regexes['interpolation_start'], $this->code, $match, 0, $this->cursor)) { - $this->lexStartInterpolation(); - } elseif ( - preg_match(self::REGEX_DQ_STRING_PART, $this->code, $match, 0, $this->cursor) - && strlen($match[0]) > 0 - ) { - $this->pushToken(Token::STRING_TYPE, $match[0]); - $this->moveCursor($match[0]); - } elseif (preg_match(self::REGEX_DQ_STRING_DELIM, $this->code, $match, 0, $this->cursor)) { - $bracket = array_pop($this->bracketsAndTernary); - - if ('"' !== $this->code[$this->cursor]) { - throw new Exception(sprintf('Unclosed "%s"', $bracket[0])); - } - - $this->popState(); - $this->pushToken(Token::DQ_STRING_END_TYPE, $match[0]); - $this->moveCursor($match[0]); - } else { - throw new Exception(sprintf('Unexpected character "%s"', $this->code[$this->cursor])); - } - } - - /** - * @return void - * - * @throws Exception - */ - protected function lexInterpolation(): void - { - $bracket = end($this->bracketsAndTernary); - - if ( - $this->options['interpolation'][0] === $bracket[0] - && preg_match($this->regexes['interpolation_end'], $this->code, $match, 0, $this->cursor) - ) { - array_pop($this->bracketsAndTernary); - $this->pushToken(Token::INTERPOLATION_END_TYPE, $match[0]); - $this->moveCursor($match[0]); - $this->popState(); - } else { - $this->lexExpression(); - } - } - - /** - * @param int $limit - * - * @return void - */ - protected function lexData(int $limit = 0): void - { - $nextToken = $this->getTokenPosition(); - if (0 === $limit && null !== $nextToken) { - $limit = $nextToken['position']; - } - - $currentToken = $this->code[$this->cursor]; - if (preg_match('/\t/', $currentToken)) { - $this->lexTab(); - } elseif (' ' === $currentToken) { - $this->lexWhitespace(); - } elseif (PHP_EOL === $currentToken) { - $this->lexEOL(); - } elseif (preg_match('/\S+/', $this->code, $match, 0, $this->cursor)) { - $value = $match[0]; - - // Stop if cursor reaches the next token start. - if (0 !== $limit && $limit <= ($this->cursor + strlen($value))) { - $value = substr($value, 0, $limit - $this->cursor); - } - - // Fixing token start among expressions and comments. - $nbTokenStart = preg_match_all($this->regexes['lex_tokens_start'], $value, $matches); - if ($nbTokenStart) { - $this->moveCurrentPosition($nbTokenStart); - } - - if (self::STATE_COMMENT === $this->getState()) { - $this->pushToken(Token::COMMENT_TEXT_TYPE, $value); - } else { - $this->pushToken(Token::TEXT_TYPE, $value); - } - - $this->moveCursor($value); - } - } - - /** - * @return void - * - * @throws Exception - */ - protected function lexStart(): void - { - $tokenStart = $this->getTokenPosition(); - if ($tokenStart['match'] === $this->options['tag_comment'][0]) { - $state = self::STATE_COMMENT; - $tokenType = Token::COMMENT_START_TYPE; - } elseif ($tokenStart['match'] === $this->options['tag_block'][0]) { - $state = self::STATE_BLOCK; - $tokenType = Token::BLOCK_START_TYPE; - } elseif ($tokenStart['match'] === $this->options['tag_variable'][0]) { - $state = self::STATE_VAR; - $tokenType = Token::VAR_START_TYPE; - } else { - throw new Exception(sprintf('Unhandled tag "%s" in lexStart', $tokenStart['match']), 1); - } - - $this->pushToken($tokenType, $tokenStart['fullMatch']); - $this->pushState($state); - $this->moveCursor($tokenStart['fullMatch']); - } - - /** - * @return void - */ - protected function lexStartDqString(): void - { - $this->bracketsAndTernary[] = ['"', $this->line]; - $this->pushToken(Token::DQ_STRING_START_TYPE, '"'); - $this->pushState(self::STATE_DQ_STRING); - $this->moveCursor('"'); - } - - /** - * @return void - */ - protected function lexStartInterpolation(): void - { - $this->bracketsAndTernary[] = [$this->options['interpolation'][0], $this->line]; - $this->pushToken(Token::INTERPOLATION_START_TYPE, '#{'); - $this->pushState(self::STATE_INTERPOLATION); - $this->moveCursor($this->options['interpolation'][0]); - } - - /** - * @return void - */ - protected function lexTab(): void - { - $currentToken = $this->code[$this->cursor]; - $whitespace = ''; - - while (preg_match('/\t/', $currentToken)) { - $whitespace .= $currentToken; - $this->moveCursor($currentToken); - $currentToken = $this->code[$this->cursor]; - } - - if (self::STATE_COMMENT === $this->getState()) { - $this->pushToken(Token::COMMENT_TAB_TYPE, $whitespace); - } else { - $this->pushToken(Token::TAB_TYPE, $whitespace); - } - } - - /** - * @return void - */ - protected function lexWhitespace(): void - { - $currentToken = $this->code[$this->cursor]; - $whitespace = ''; - - while (' ' === $currentToken) { - $whitespace .= $currentToken; - $this->moveCursor($currentToken); - $currentToken = $this->code[$this->cursor]; - } - - if (self::STATE_COMMENT === $this->getState()) { - $this->pushToken(Token::COMMENT_WHITESPACE_TYPE, $whitespace); - } else { - $this->pushToken(Token::WHITESPACE_TYPE, $whitespace); - } - } - - /** - * @return void - */ - protected function lexEOL(): void - { - if (self::STATE_COMMENT === $this->getState()) { - $this->pushToken(Token::COMMENT_EOL_TYPE, $this->code[$this->cursor]); - } else { - $this->pushToken(Token::EOL_TYPE, $this->code[$this->cursor]); - } - - $this->moveCursor($this->code[$this->cursor]); - } - - /** - * @return void - */ - protected function lexArrowFunction(): void - { - $this->pushToken(Token::ARROW_TYPE, '=>'); - $this->moveCursor('=>'); - } - - /** - * @param string $operator - * - * @return void - */ - protected function lexOperator(string $operator): void - { - if ('?' === $operator) { - $this->bracketsAndTernary[] = [$operator, $this->line]; - } elseif (':' === $operator) { - array_pop($this->bracketsAndTernary); - } - - $this->pushToken(Token::OPERATOR_TYPE, $operator); - $this->moveCursor($operator); - } - - /** - * @param string $name - * - * @return void - * - * @throws Exception - */ - protected function lexName(string $name): void - { - if (self::STATE_BLOCK === $this->getState() && !isset($this->getStateParams()['tag'])) { - $this->pushToken(Token::BLOCK_TAG_TYPE, $name); - $this->setStateParam('tag', $name); - } else { - $this->pushToken(Token::NAME_TYPE, $name); - } - - $this->moveCursor($name); - } - - /** - * @param string $numberAsString - * - * @return void - */ - protected function lexNumber(string $numberAsString): void - { - $number = (float) $numberAsString; // floats - if (ctype_digit($numberAsString) && $number <= PHP_INT_MAX) { - $number = (int) $numberAsString; // integers lower than the maximum - } - - $this->pushToken(Token::NUMBER_TYPE, (string) $number); - $this->moveCursor($numberAsString); - } - - /** - * @return void - * - * @throws Exception - */ - protected function lexPunctuation(): void - { - $currentToken = $this->code[$this->cursor]; - - $lastBracket = end($this->bracketsAndTernary); - if (false !== $lastBracket && '?' === $lastBracket[0]) { - if (':' === $currentToken) { - // This is a ternary instead - $this->lexOperator($currentToken); - - return; - } - if (false !== strpos(',)]}', $currentToken)) { - // Because {{ foo ? 'yes' }} is the same as {{ foo ? 'yes' : '' }} - do { - array_pop($this->bracketsAndTernary); - $lastBracket = end($this->bracketsAndTernary); - } while (false !== $lastBracket && '?' === $lastBracket[0]); - - // This is maybe the end of the variable, start again. - $this->lexVariable(); - - return; - } - } - - if (false !== strpos('([{', $currentToken)) { - $this->bracketsAndTernary[] = [$currentToken, $this->line]; - } elseif (false !== strpos(')]}', $currentToken)) { - if (0 === count($this->bracketsAndTernary)) { - throw new Exception(sprintf('Unexpected "%s"', $currentToken)); - } - - $bracket = array_pop($this->bracketsAndTernary); - if (strtr($bracket[0], '([{', ')]}') !== $currentToken) { - throw new Exception(sprintf('Unclosed "%s"', $bracket[0])); - } - } - - $this->pushToken(Token::PUNCTUATION_TYPE, $currentToken); - $this->moveCursor($currentToken); - } - - /** - * @param string $string - * - * @return void - */ - protected function lexString(string $string): void - { - $this->pushToken(Token::STRING_TYPE, $string); - $this->moveCursor($string); - } -} diff --git a/TwigCS/src/Token/TokenizerHelper.php b/TwigCS/src/Token/TokenizerHelper.php deleted file mode 100644 index 15160d4..0000000 --- a/TwigCS/src/Token/TokenizerHelper.php +++ /dev/null @@ -1,155 +0,0 @@ -env = $env; - $this->options = $options; - } - - /** - * @return string - */ - public function getBlockRegex(): string - { - return '/' - .'(' - .preg_quote($this->options['whitespace_trim']) - .'|'.preg_quote($this->options['whitespace_line_trim']) - .')?' - .'(' - .preg_quote($this->options['tag_block'][1]) - .')' - .'/A'; - } - - /** - * @return string - */ - public function getCommentRegex(): string - { - return '/' - .'(' - .preg_quote($this->options['whitespace_trim']) - .'|'.preg_quote($this->options['whitespace_line_trim']) - .')?' - .'(' - .preg_quote($this->options['tag_comment'][1]) - .')' - .'/'; // Should not be anchored - } - - /** - * @return string - */ - public function getVariableRegex(): string - { - return '/' - .'(' - .preg_quote($this->options['whitespace_trim']) - .'|'.preg_quote($this->options['whitespace_line_trim']) - .')?' - .'(' - .preg_quote($this->options['tag_variable'][1]) - .')' - .'/A'; - } - - /** - * @return string - */ - public function getTokensStartRegex(): string - { - return '/' - .'(' - .preg_quote($this->options['tag_variable'][0]) - .'|'.preg_quote($this->options['tag_block'][0]) - .'|'.preg_quote($this->options['tag_comment'][0]) - .')' - .'(' - .preg_quote($this->options['whitespace_trim']) - .'|'.preg_quote($this->options['whitespace_line_trim']) - .')?' - .'/'; - } - - /** - * @return string - */ - public function getInterpolationStartRegex(): string - { - return '/' - .'(' - .preg_quote($this->options['interpolation'][0]) - .')' - .'/A'; - } - - /** - * @return string - */ - public function getInterpolationEndRegex(): string - { - return '/' - .'(' - .preg_quote($this->options['interpolation'][1]) - .')' - .'/A'; - } - - /** - * @return string - */ - public function getOperatorRegex(): string - { - $operators = array_merge( - ['=', '?', '?:'], - array_keys($this->env->getUnaryOperators()), - array_keys($this->env->getBinaryOperators()) - ); - - $operators = array_combine($operators, array_map('strlen', $operators)); - arsort($operators); - - $regex = []; - foreach ($operators as $operator => $length) { - if (ctype_alpha($operator[$length - 1])) { - $r = preg_quote($operator, '/').'(?=[\s()])'; - } else { - $r = preg_quote($operator, '/'); - } - - $r = preg_replace('/\s+/', '\s+', $r); - - $regex[] = $r; - } - - return '/'.implode('|', $regex).'/A'; - } -} diff --git a/TwigCS/tests/Ruleset/AbstractSniffTest.php b/TwigCS/tests/Ruleset/AbstractSniffTest.php deleted file mode 100644 index 6ddb539..0000000 --- a/TwigCS/tests/Ruleset/AbstractSniffTest.php +++ /dev/null @@ -1,88 +0,0 @@ -checkSniff(new Sniff(), [...]); - * - * @return void - */ - abstract public function testSniff(): void; - - /** - * @param SniffInterface $sniff - * @param array> $expects - * - * @return void - */ - protected function checkSniff(SniffInterface $sniff, array $expects): void - { - $env = new StubbedEnvironment(); - $tokenizer = new Tokenizer($env); - $linter = new Linter($env, $tokenizer); - $ruleset = new Ruleset(); - - try { - $class = new ReflectionClass(get_called_class()); - $className = $class->getShortName(); - $directory = dirname($class->getFileName()); - $file = "$directory/$className.twig"; - - $ruleset->addSniff($sniff); - $report = $linter->run([$file], $ruleset); - } catch (Exception $exception) { - self::fail($exception->getMessage()); - - return; - } - - $fixedFile = "$directory/$className.fixed.twig"; - if (file_exists($fixedFile)) { - $fixer = new Fixer($ruleset, $tokenizer); - $sniff->enableFixer($fixer); - $fixer->fixFile($file); - - $diff = TestHelper::generateDiff($fixer->getContents(), $fixedFile); - if ('' !== $diff) { - self::fail($diff); - } - } - - $messages = $report->getMessages(); - $messagePositions = []; - - foreach ($messages as $message) { - if (Report::MESSAGE_TYPE_FATAL === $message->getLevel()) { - $errorMessage = $message->getMessage(); - $line = $message->getLine(); - - if (null !== $line) { - $errorMessage = sprintf('Line %s: %s', $line, $errorMessage); - } - self::fail($errorMessage); - } - - $messagePositions[] = [$message->getLine() => $message->getLinePosition()]; - } - self::assertSame($expects, $messagePositions); - } -} diff --git a/TwigCS/tests/Ruleset/Generic/BlankEOF/BlankEOFTest.fixed.twig b/TwigCS/tests/Ruleset/Generic/BlankEOF/BlankEOFTest.fixed.twig deleted file mode 100644 index 1936aaf..0000000 --- a/TwigCS/tests/Ruleset/Generic/BlankEOF/BlankEOFTest.fixed.twig +++ /dev/null @@ -1,2 +0,0 @@ -
-
diff --git a/TwigCS/tests/Ruleset/Generic/BlankEOF/BlankEOFTest.php b/TwigCS/tests/Ruleset/Generic/BlankEOF/BlankEOFTest.php deleted file mode 100644 index d124f0f..0000000 --- a/TwigCS/tests/Ruleset/Generic/BlankEOF/BlankEOFTest.php +++ /dev/null @@ -1,24 +0,0 @@ -checkSniff(new BlankEOFSniff(), [ - [4 => 1], - ]); - } -} diff --git a/TwigCS/tests/Ruleset/Generic/BlankEOF/BlankEOFTest.twig b/TwigCS/tests/Ruleset/Generic/BlankEOF/BlankEOFTest.twig deleted file mode 100644 index 99dfd42..0000000 --- a/TwigCS/tests/Ruleset/Generic/BlankEOF/BlankEOFTest.twig +++ /dev/null @@ -1,3 +0,0 @@ -
-
- diff --git a/TwigCS/tests/Ruleset/Generic/DelimiterSpacing/DelimiterSpacingTest.fixed.twig b/TwigCS/tests/Ruleset/Generic/DelimiterSpacing/DelimiterSpacingTest.fixed.twig deleted file mode 100644 index 5da5b66..0000000 --- a/TwigCS/tests/Ruleset/Generic/DelimiterSpacing/DelimiterSpacingTest.fixed.twig +++ /dev/null @@ -1,23 +0,0 @@ -{{ foo }} -{# comment #} -{% if foo %}{% endif %} - -{{- foo -}} -{#- comment -#} -{%- if foo -%}{%- endif -%} - -{{ - shouldNotCareAboutNewLine -}} -{# - shouldNotCareAboutNewLine -#} -{%- if foo -%}{%- endif -%} - -{{ foo({'bar': {'baz': 'shouldNotCareAboutDoubleHashes'}}) }} - -
- {{ - shouldNotCareAboutNewLine - }} -
diff --git a/TwigCS/tests/Ruleset/Generic/DelimiterSpacing/DelimiterSpacingTest.php b/TwigCS/tests/Ruleset/Generic/DelimiterSpacing/DelimiterSpacingTest.php deleted file mode 100644 index c037966..0000000 --- a/TwigCS/tests/Ruleset/Generic/DelimiterSpacing/DelimiterSpacingTest.php +++ /dev/null @@ -1,27 +0,0 @@ -checkSniff(new DelimiterSpacingSniff(), [ - [15 => 1], - [15 => 12], - [15 => 15], - [15 => 25], - ]); - } -} diff --git a/TwigCS/tests/Ruleset/Generic/DelimiterSpacing/DelimiterSpacingTest.twig b/TwigCS/tests/Ruleset/Generic/DelimiterSpacing/DelimiterSpacingTest.twig deleted file mode 100644 index c6161a3..0000000 --- a/TwigCS/tests/Ruleset/Generic/DelimiterSpacing/DelimiterSpacingTest.twig +++ /dev/null @@ -1,23 +0,0 @@ -{{ foo }} -{# comment #} -{% if foo %}{% endif %} - -{{- foo -}} -{#- comment -#} -{%- if foo -%}{%- endif -%} - -{{ - shouldNotCareAboutNewLine -}} -{# - shouldNotCareAboutNewLine -#} -{%-if foo -%}{%- endif-%} - -{{ foo({'bar': {'baz': 'shouldNotCareAboutDoubleHashes'}}) }} - -
- {{ - shouldNotCareAboutNewLine - }} -
diff --git a/TwigCS/tests/Ruleset/Generic/EmptyLines/EmptyLinesTest.fixed.twig b/TwigCS/tests/Ruleset/Generic/EmptyLines/EmptyLinesTest.fixed.twig deleted file mode 100644 index 70c6156..0000000 --- a/TwigCS/tests/Ruleset/Generic/EmptyLines/EmptyLinesTest.fixed.twig +++ /dev/null @@ -1,3 +0,0 @@ -
- -
diff --git a/TwigCS/tests/Ruleset/Generic/EmptyLines/EmptyLinesTest.php b/TwigCS/tests/Ruleset/Generic/EmptyLines/EmptyLinesTest.php deleted file mode 100644 index 50aedb2..0000000 --- a/TwigCS/tests/Ruleset/Generic/EmptyLines/EmptyLinesTest.php +++ /dev/null @@ -1,24 +0,0 @@ -checkSniff(new EmptyLinesSniff(), [ - [3 => 1], - ]); - } -} diff --git a/TwigCS/tests/Ruleset/Generic/EmptyLines/EmptyLinesTest.twig b/TwigCS/tests/Ruleset/Generic/EmptyLines/EmptyLinesTest.twig deleted file mode 100644 index 018bc05..0000000 --- a/TwigCS/tests/Ruleset/Generic/EmptyLines/EmptyLinesTest.twig +++ /dev/null @@ -1,4 +0,0 @@ -
- - -
diff --git a/TwigCS/tests/Ruleset/Generic/OperatorSpacing/OperatorSpacingTest.fixed.twig b/TwigCS/tests/Ruleset/Generic/OperatorSpacing/OperatorSpacingTest.fixed.twig deleted file mode 100644 index 416fc26..0000000 --- a/TwigCS/tests/Ruleset/Generic/OperatorSpacing/OperatorSpacingTest.fixed.twig +++ /dev/null @@ -1,37 +0,0 @@ -{{ 1 + 2 }} -{{ 1 - 2 }} -{{ 1 / 2 }} -{{ 1 * 2 }} -{{ 1 % 2 }} -{{ 1 // 2 }} -{{ 1 ** 2 }} -{{ foo ~ bar }} -{{ true ? true : false }} -{{ 1 == 2 }} -{{ not true }} -{{ false and true }} -{{ false or true }} -{{ a in array }} -{{ a is array }} - -Untouch +-/*%==: - -{{ 1 ?: 2 }} -{{ 1 ?? 2 }} - -{{ 1 + -2 }} -{{ 1 + (-2 + 3) }} -{{ a[-2] }} -{{ { 'foo': -2 } }} -{{ foo.name }} -{{ 1..10 }} -{{ -5..-2 }} -{{ [1, -2] }} -{% if -2 == -3 or -2 == -3 %}{% endif %} -{% if a - 2 == -4 %}{% endif %} -{{ a in -2..-3 }} -{{ "a_#{1 + 1}" }} - -{{ 'array' starts with 'a' }} -{{ 'array' ends with 'a' }} -{{ 'array' matches 'a' }} diff --git a/TwigCS/tests/Ruleset/Generic/OperatorSpacing/OperatorSpacingTest.php b/TwigCS/tests/Ruleset/Generic/OperatorSpacing/OperatorSpacingTest.php deleted file mode 100644 index f01cf83..0000000 --- a/TwigCS/tests/Ruleset/Generic/OperatorSpacing/OperatorSpacingTest.php +++ /dev/null @@ -1,68 +0,0 @@ -checkSniff(new OperatorSpacingSniff(), [ - [1 => 4], - [1 => 4], - [2 => 5], - [2 => 5], - [3 => 5], - [3 => 5], - [4 => 5], - [4 => 5], - [5 => 5], - [5 => 5], - [6 => 5], - [6 => 5], - [7 => 5], - [7 => 5], - [8 => 7], - [8 => 7], - [9 => 10], - [9 => 10], - [9 => 19], - [9 => 19], - [10 => 5], - [10 => 5], - [11 => 6], - [11 => 6], - [12 => 11], - [12 => 11], - [13 => 11], - [13 => 11], - [14 => 7], - [14 => 7], - [15 => 7], - [15 => 7], - [19 => 5], - [19 => 5], - [20 => 5], - [20 => 5], - [22 => 6], - [33 => 10], - [33 => 10], - [35 => 13], - [35 => 13], - [36 => 13], - [36 => 13], - [37 => 13], - [37 => 13], - ]); - } -} diff --git a/TwigCS/tests/Ruleset/Generic/OperatorSpacing/OperatorSpacingTest.twig b/TwigCS/tests/Ruleset/Generic/OperatorSpacing/OperatorSpacingTest.twig deleted file mode 100644 index fa9e3f6..0000000 --- a/TwigCS/tests/Ruleset/Generic/OperatorSpacing/OperatorSpacingTest.twig +++ /dev/null @@ -1,37 +0,0 @@ -{{ 1+2 }} -{{ 1-2 }} -{{ 1/2 }} -{{ 1*2 }} -{{ 1%2 }} -{{ 1//2 }} -{{ 1**2 }} -{{ foo~bar }} -{{ true ? true : false }} -{{ 1==2 }} -{{ not true }} -{{ false and true }} -{{ false or true }} -{{ a in array }} -{{ a is array }} - -Untouch +-/*%==: - -{{ 1?:2 }} -{{ 1??2 }} - -{{ 1 +-2 }} -{{ 1 + (-2 + 3) }} -{{ a[-2] }} -{{ { 'foo': -2 } }} -{{ foo.name }} -{{ 1..10 }} -{{ -5..-2 }} -{{ [1, -2] }} -{% if -2 == -3 or -2 == -3 %}{% endif %} -{% if a - 2 == -4 %}{% endif %} -{{ a in -2..-3 }} -{{ "a_#{1+1}" }} - -{{ 'array' starts with 'a' }} -{{ 'array' ends with 'a' }} -{{ 'array' matches 'a' }} diff --git a/TwigCS/tests/Ruleset/Generic/PunctuationSpacing/PunctuationSpacingTest.fixed.twig b/TwigCS/tests/Ruleset/Generic/PunctuationSpacing/PunctuationSpacingTest.fixed.twig deleted file mode 100644 index 4c5b5e2..0000000 --- a/TwigCS/tests/Ruleset/Generic/PunctuationSpacing/PunctuationSpacingTest.fixed.twig +++ /dev/null @@ -1,6 +0,0 @@ -{{ 1..10 }} -{{ foo.bar }} -{{ (foo) }} -{{ {'a': 'b', 'c': 'd'} }} -{% set a = [1, 2, 4] %} -{{ a|trans }} diff --git a/TwigCS/tests/Ruleset/Generic/PunctuationSpacing/PunctuationSpacingTest.php b/TwigCS/tests/Ruleset/Generic/PunctuationSpacing/PunctuationSpacingTest.php deleted file mode 100644 index 4ec9cc3..0000000 --- a/TwigCS/tests/Ruleset/Generic/PunctuationSpacing/PunctuationSpacingTest.php +++ /dev/null @@ -1,36 +0,0 @@ -checkSniff(new PunctuationSpacingSniff(), [ - [3 => 4], - [3 => 10], - [4 => 4], - [4 => 10], - [4 => 16], - [4 => 22], - [4 => 28], - [5 => 12], - [5 => 16], - [5 => 20], - [5 => 24], - [6 => 6], - [6 => 6], - ]); - } -} diff --git a/TwigCS/tests/Ruleset/Generic/PunctuationSpacing/PunctuationSpacingTest.twig b/TwigCS/tests/Ruleset/Generic/PunctuationSpacing/PunctuationSpacingTest.twig deleted file mode 100644 index a5aa08d..0000000 --- a/TwigCS/tests/Ruleset/Generic/PunctuationSpacing/PunctuationSpacingTest.twig +++ /dev/null @@ -1,6 +0,0 @@ -{{ 1..10 }} -{{ foo.bar }} -{{ ( foo ) }} -{{ { 'a' : 'b' , 'c' : 'd' } }} -{% set a = [ 1 , 2 , 4 ] %} -{{ a | trans }} diff --git a/TwigCS/tests/TestHelper.php b/TwigCS/tests/TestHelper.php deleted file mode 100644 index 0912427..0000000 --- a/TwigCS/tests/TestHelper.php +++ /dev/null @@ -1,76 +0,0 @@ - $expectedTokenTypes - * - * @return void - * - * @throws \Exception - * - * @dataProvider tokenizeDataProvider - */ - public function testTokenize(string $filePath, array $expectedTokenTypes): void - { - $env = new StubbedEnvironment(); - $tokenizer = new Tokenizer($env); - $source = new Source(file_get_contents($filePath), $filePath); - - $tokens = $tokenizer->tokenize($source); - - $tokenValues = array_map(function (Token $token) { - return $token->getValue(); - }, $tokens); - - $diff = TestHelper::generateDiff(implode($tokenValues), $filePath); - if ('' !== $diff) { - self::fail($diff); - } - - $tokenTypes = array_map(function (Token $token) { - return $token->getType(); - }, $tokens); - self::assertSame($expectedTokenTypes, $tokenTypes); - } - - /** - * @return array - */ - public function tokenizeDataProvider(): array - { - return [ - [ - __DIR__.'/TokenizerTest1.twig', - [ - 0 => Token::TEXT_TYPE, - 1 => Token::EOL_TYPE, - 2 => Token::EOF_TYPE, - ], - ], - [ - __DIR__.'/TokenizerTest2.twig', - [ - 0 => Token::VAR_START_TYPE, - 1 => Token::WHITESPACE_TYPE, - 2 => Token::NAME_TYPE, - 3 => Token::WHITESPACE_TYPE, - 4 => Token::VAR_END_TYPE, - 5 => Token::EOL_TYPE, - 6 => Token::COMMENT_START_TYPE, - 7 => Token::COMMENT_WHITESPACE_TYPE, - 8 => Token::COMMENT_TEXT_TYPE, - 9 => Token::COMMENT_WHITESPACE_TYPE, - 10 => Token::COMMENT_END_TYPE, - 11 => Token::EOL_TYPE, - 12 => Token::BLOCK_START_TYPE, - 13 => Token::WHITESPACE_TYPE, - 14 => Token::BLOCK_TAG_TYPE, - 15 => Token::WHITESPACE_TYPE, - 16 => Token::NAME_TYPE, - 17 => Token::WHITESPACE_TYPE, - 18 => Token::BLOCK_END_TYPE, - 19 => Token::BLOCK_START_TYPE, - 20 => Token::WHITESPACE_TYPE, - 21 => Token::BLOCK_TAG_TYPE, - 22 => Token::WHITESPACE_TYPE, - 23 => Token::BLOCK_END_TYPE, - 24 => Token::EOL_TYPE, - 25 => Token::EOF_TYPE, - ], - ], - [ - __DIR__.'/TokenizerTest3.twig', - [ - 0 => Token::VAR_START_TYPE, - 1 => Token::WHITESPACE_TYPE, - 2 => Token::NUMBER_TYPE, - 3 => Token::OPERATOR_TYPE, - 4 => Token::NUMBER_TYPE, - 5 => Token::OPERATOR_TYPE, - 6 => Token::NUMBER_TYPE, - 7 => Token::OPERATOR_TYPE, - 8 => Token::NUMBER_TYPE, - 9 => Token::OPERATOR_TYPE, - 10 => Token::NUMBER_TYPE, - 11 => Token::WHITESPACE_TYPE, - 12 => Token::VAR_END_TYPE, - 13 => Token::EOL_TYPE, - 14 => Token::VAR_START_TYPE, - 15 => Token::WHITESPACE_TYPE, - 16 => Token::NAME_TYPE, - 17 => Token::WHITESPACE_TYPE, - 18 => Token::OPERATOR_TYPE, - 19 => Token::WHITESPACE_TYPE, - 20 => Token::STRING_TYPE, - 21 => Token::WHITESPACE_TYPE, - 22 => Token::OPERATOR_TYPE, - 23 => Token::WHITESPACE_TYPE, - 24 => Token::STRING_TYPE, - 25 => Token::WHITESPACE_TYPE, - 26 => Token::VAR_END_TYPE, - 27 => Token::EOL_TYPE, - 28 => Token::VAR_START_TYPE, - 29 => Token::WHITESPACE_TYPE, - 30 => Token::NAME_TYPE, - 31 => Token::WHITESPACE_TYPE, - 32 => Token::OPERATOR_TYPE, - 33 => Token::WHITESPACE_TYPE, - 34 => Token::PUNCTUATION_TYPE, - 35 => Token::WHITESPACE_TYPE, - 36 => Token::NAME_TYPE, - 37 => Token::PUNCTUATION_TYPE, - 38 => Token::PUNCTUATION_TYPE, - 39 => Token::NUMBER_TYPE, - 40 => Token::PUNCTUATION_TYPE, - 41 => Token::WHITESPACE_TYPE, - 42 => Token::NUMBER_TYPE, - 43 => Token::PUNCTUATION_TYPE, - 44 => Token::WHITESPACE_TYPE, - 45 => Token::PUNCTUATION_TYPE, - 46 => Token::WHITESPACE_TYPE, - 47 => Token::VAR_END_TYPE, - 48 => Token::EOL_TYPE, - 49 => Token::BLOCK_START_TYPE, - 50 => Token::WHITESPACE_TYPE, - 51 => Token::BLOCK_TAG_TYPE, - 52 => Token::WHITESPACE_TYPE, - 53 => Token::NAME_TYPE, - 54 => Token::OPERATOR_TYPE, - 55 => Token::PUNCTUATION_TYPE, - 56 => Token::NAME_TYPE, - 57 => Token::PUNCTUATION_TYPE, - 58 => Token::WHITESPACE_TYPE, - 59 => Token::NAME_TYPE, - 60 => Token::WHITESPACE_TYPE, - 61 => Token::OPERATOR_TYPE, - 62 => Token::WHITESPACE_TYPE, - 63 => Token::NAME_TYPE, - 64 => Token::WHITESPACE_TYPE, - 65 => Token::OPERATOR_TYPE, - 66 => Token::WHITESPACE_TYPE, - 67 => Token::NUMBER_TYPE, - 68 => Token::PUNCTUATION_TYPE, - 69 => Token::WHITESPACE_TYPE, - 70 => Token::NAME_TYPE, - 71 => Token::PUNCTUATION_TYPE, - 72 => Token::WHITESPACE_TYPE, - 73 => Token::NUMBER_TYPE, - 74 => Token::PUNCTUATION_TYPE, - 75 => Token::WHITESPACE_TYPE, - 76 => Token::BLOCK_END_TYPE, - 77 => Token::EOL_TYPE, - 78 => Token::VAR_START_TYPE, - 79 => Token::WHITESPACE_TYPE, - 80 => Token::NAME_TYPE, - 81 => Token::WHITESPACE_TYPE, - 82 => Token::OPERATOR_TYPE, - 83 => Token::WHITESPACE_TYPE, - 84 => Token::STRING_TYPE, - 85 => Token::WHITESPACE_TYPE, - 86 => Token::VAR_END_TYPE, - 87 => Token::EOL_TYPE, - 88 => Token::EOF_TYPE, - ], - ], - [ - __DIR__.'/TokenizerTest4.twig', - [ - 0 => Token::VAR_START_TYPE, - 1 => Token::WHITESPACE_TYPE, - 2 => Token::NAME_TYPE, - 3 => Token::PUNCTUATION_TYPE, - 4 => Token::NAME_TYPE, - 5 => Token::PUNCTUATION_TYPE, - 6 => Token::NAME_TYPE, - 7 => Token::WHITESPACE_TYPE, - 8 => Token::ARROW_TYPE, - 9 => Token::WHITESPACE_TYPE, - 10 => Token::DQ_STRING_START_TYPE, - 11 => Token::INTERPOLATION_START_TYPE, - 12 => Token::NAME_TYPE, - 13 => Token::PUNCTUATION_TYPE, - 14 => Token::NAME_TYPE, - 15 => Token::INTERPOLATION_END_TYPE, - 16 => Token::STRING_TYPE, - 17 => Token::INTERPOLATION_START_TYPE, - 18 => Token::NAME_TYPE, - 19 => Token::PUNCTUATION_TYPE, - 20 => Token::NAME_TYPE, - 21 => Token::INTERPOLATION_END_TYPE, - 22 => Token::DQ_STRING_END_TYPE, - 23 => Token::PUNCTUATION_TYPE, - 24 => Token::PUNCTUATION_TYPE, - 25 => Token::NAME_TYPE, - 26 => Token::PUNCTUATION_TYPE, - 27 => Token::STRING_TYPE, - 28 => Token::PUNCTUATION_TYPE, - 29 => Token::WHITESPACE_TYPE, - 30 => Token::VAR_END_TYPE, - 31 => Token::EOL_TYPE, - 32 => Token::EOF_TYPE, - ], - ], - [ - __DIR__.'/TokenizerTest5.twig', - [ - 0 => Token::BLOCK_START_TYPE, - 1 => Token::WHITESPACE_TYPE, - 2 => Token::BLOCK_TAG_TYPE, - 3 => Token::WHITESPACE_TYPE, - 4 => Token::NAME_TYPE, - 5 => Token::WHITESPACE_TYPE, - 6 => Token::OPERATOR_TYPE, - 7 => Token::WHITESPACE_TYPE, - 8 => Token::NAME_TYPE, - 9 => Token::WHITESPACE_TYPE, - 10 => Token::NAME_TYPE, - 11 => Token::PUNCTUATION_TYPE, - 12 => Token::NAME_TYPE, - 13 => Token::PUNCTUATION_TYPE, - 14 => Token::WHITESPACE_TYPE, - 15 => Token::BLOCK_END_TYPE, - 16 => Token::BLOCK_START_TYPE, - 17 => Token::WHITESPACE_TYPE, - 18 => Token::BLOCK_TAG_TYPE, - 19 => Token::WHITESPACE_TYPE, - 20 => Token::BLOCK_END_TYPE, - 21 => Token::EOL_TYPE, - 22 => Token::BLOCK_START_TYPE, - 23 => Token::WHITESPACE_TYPE, - 24 => Token::BLOCK_TAG_TYPE, - 25 => Token::WHITESPACE_TYPE, - 26 => Token::NAME_TYPE, - 27 => Token::WHITESPACE_TYPE, - 28 => Token::OPERATOR_TYPE, - 29 => Token::WHITESPACE_TYPE, - 30 => Token::NAME_TYPE, - 31 => Token::WHITESPACE_TYPE, - 32 => Token::NAME_TYPE, - 33 => Token::PUNCTUATION_TYPE, - 34 => Token::NAME_TYPE, - 35 => Token::PUNCTUATION_TYPE, - 36 => Token::WHITESPACE_TYPE, - 37 => Token::BLOCK_END_TYPE, - 38 => Token::BLOCK_START_TYPE, - 39 => Token::WHITESPACE_TYPE, - 40 => Token::BLOCK_TAG_TYPE, - 41 => Token::WHITESPACE_TYPE, - 42 => Token::BLOCK_END_TYPE, - 43 => Token::EOL_TYPE, - 44 => Token::EOF_TYPE, - ], - ], - [ - __DIR__.'/TokenizerTest6.twig', - [ - 0 => Token::BLOCK_START_TYPE, - 1 => Token::WHITESPACE_TYPE, - 2 => Token::BLOCK_TAG_TYPE, - 3 => Token::WHITESPACE_TYPE, - 4 => Token::NAME_TYPE, - 5 => Token::WHITESPACE_TYPE, - 6 => Token::BLOCK_END_TYPE, - 7 => Token::TEXT_TYPE, - 8 => Token::BLOCK_START_TYPE, - 9 => Token::WHITESPACE_TYPE, - 10 => Token::BLOCK_TAG_TYPE, - 11 => Token::WHITESPACE_TYPE, - 12 => Token::BLOCK_END_TYPE, - 13 => Token::EOL_TYPE, - 14 => Token::EOF_TYPE, - ], - ], - ]; - } -} diff --git a/TwigCS/tests/Token/Tokenizer/TokenizerTest1.twig b/TwigCS/tests/Token/Tokenizer/TokenizerTest1.twig deleted file mode 100644 index 179f993..0000000 --- a/TwigCS/tests/Token/Tokenizer/TokenizerTest1.twig +++ /dev/null @@ -1 +0,0 @@ -
test
diff --git a/TwigCS/tests/Token/Tokenizer/TokenizerTest2.twig b/TwigCS/tests/Token/Tokenizer/TokenizerTest2.twig deleted file mode 100644 index 42e16f6..0000000 --- a/TwigCS/tests/Token/Tokenizer/TokenizerTest2.twig +++ /dev/null @@ -1,3 +0,0 @@ -{{ foo }} -{# comment #} -{% block try %}{% endblock %} diff --git a/TwigCS/tests/Token/Tokenizer/TokenizerTest3.twig b/TwigCS/tests/Token/Tokenizer/TokenizerTest3.twig deleted file mode 100644 index 2baa9b6..0000000 --- a/TwigCS/tests/Token/Tokenizer/TokenizerTest3.twig +++ /dev/null @@ -1,5 +0,0 @@ -{{ 1+2-3*4%5 }} -{{ bar ? 'string' : "string#" }} -{{ baz ?: { a:[1, 2] } }} -{% set a={foo: bar ? baz ? 1, test: 1} %} -{{ a ? ',' }} diff --git a/TwigCS/tests/Token/Tokenizer/TokenizerTest4.twig b/TwigCS/tests/Token/Tokenizer/TokenizerTest4.twig deleted file mode 100644 index 339359d..0000000 --- a/TwigCS/tests/Token/Tokenizer/TokenizerTest4.twig +++ /dev/null @@ -1 +0,0 @@ -{{ people|map(p => "#{p.first} #{p.last}")|join(', ') }} diff --git a/TwigCS/tests/Token/Tokenizer/TokenizerTest5.twig b/TwigCS/tests/Token/Tokenizer/TokenizerTest5.twig deleted file mode 100644 index e8abd45..0000000 --- a/TwigCS/tests/Token/Tokenizer/TokenizerTest5.twig +++ /dev/null @@ -1,2 +0,0 @@ -{% if foo is same as(bar) %}{% endif %} -{% if foo is divisible by(bar) %}{% endif %} diff --git a/TwigCS/tests/Token/Tokenizer/TokenizerTest6.twig b/TwigCS/tests/Token/Tokenizer/TokenizerTest6.twig deleted file mode 100644 index d5af71b..0000000 --- a/TwigCS/tests/Token/Tokenizer/TokenizerTest6.twig +++ /dev/null @@ -1 +0,0 @@ -{% block try %}⇨{% endblock %} diff --git a/bin/twigcs b/bin/twigcs deleted file mode 100755 index b94bfd7..0000000 --- a/bin/twigcs +++ /dev/null @@ -1,23 +0,0 @@ -#!/usr/bin/env php -add($command); -$application->setDefaultCommand($command->getName()); -$application->run(); diff --git a/build.xml b/build.xml index 193ef13..7d1ec9b 100644 --- a/build.xml +++ b/build.xml @@ -31,9 +31,6 @@ - - - diff --git a/composer.json b/composer.json index e1972cb..e281ca8 100644 --- a/composer.json +++ b/composer.json @@ -5,8 +5,7 @@ "keywords": [ "Symfony", "coding standard", - "phpcs", - "twigcs" + "phpcs" ], "homepage": "https://github.com/VincentLanglet/Symfony-custom-coding-standard", "license": "MIT", @@ -15,29 +14,15 @@ "name": "Vincent Langlet" } ], - "bin": ["bin/twigcs"], "require": { "php": ">=7.2", "ext-mbstring": "*", - "squizlabs/php_codesniffer": "^3.5.8", - "symfony/console": "^4.4 || ^5.0", - "symfony/twig-bridge": "^4.4 || ^5.0", - "twig/twig": "^2.12 || ^3.0" + "squizlabs/php_codesniffer": "^3.6.0" }, "require-dev": { "phpunit/phpunit": "^7.0" }, "config": { "sort-packages": true - }, - "autoload": { - "psr-4": { - "TwigCS\\": "TwigCS/src/" - } - }, - "autoload-dev": { - "psr-4": { - "TwigCS\\Tests\\": "TwigCS/tests/" - } } } diff --git a/docs/installation.md b/docs/installation.md index 2785cd6..602b594 100644 --- a/docs/installation.md +++ b/docs/installation.md @@ -27,7 +27,6 @@ bin/phpcs -i ``` bin/phpcs --standard=SymfonyCustom /path/to/code -bin/twigcs lint /path/to/code ``` 5. (optional) Set up PHPStorm diff --git a/docs/twig.md b/docs/twig.md deleted file mode 100644 index a28bc16..0000000 --- a/docs/twig.md +++ /dev/null @@ -1,44 +0,0 @@ -# Twig Coding Standard Rules - -From the [official one](http://twig.sensiolabs.org/doc/coding_standards.html). - -### Delimiter spacing - -Put one (and only one) space after the start of a delimiter (`{{`, `{%`, and `{#`) -and before the end of a delimiter (`}}`, `%}`, and `#}`). - -When using the whitespace control character, do not put any spaces between it and the delimiter - -### Operator spacing - -Put one (and only one) space before and after the following operators: -comparison operators (`==`, `!=`, `<`, `>`, `>=`, `<=`), math operators (`+`, `-`, `/`, `*`, `%`, `//`, `**`), -logic operators (`not`, `and`, `or`), `~`, `is`, `in`, and the ternary operator (`?:`) - -Do not put any spaces before and after the operator `..`. - -### Punctuation spacing - -Put one (and only one) space after the `:` sign in hashes and `,` in arrays and hashes - -Do not put any spaces after an opening parenthesis and before a closing parenthesis in expressions - -Do not put any spaces before and after the following operators: `|`, `.`, `[]` - -Do not put any spaces before and after the opening and the closing of arrays and hashes - -### Todo - -Do not put any spaces before and after string delimiters - -### Todo - -Do not put any spaces before and after the parenthesis used for filter and function calls - -### Todo - -Use lower cased and underscored variable names - -### Todo - -Indent your code inside tags (use the same indentation as the one used for the target language of the rendered template) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 7157b1e..debd0dc 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -14,8 +14,5 @@ ./vendor/squizlabs/php_codesniffer/tests/Standards/AllSniffs.php - - ./TwigCS -