Skip to content

🐛 Bugfix #59

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion TwigCS/Command/TwigCSCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace TwigCS\Command;

use \Exception;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
Expand Down Expand Up @@ -65,7 +66,7 @@ protected function configure()
*
* @return int
*
* @throws \Exception
* @throws Exception
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
Expand Down
7 changes: 6 additions & 1 deletion TwigCS/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace TwigCS\Config;

use \Exception;
use Symfony\Component\Finder\Finder;

/**
Expand Down Expand Up @@ -43,6 +44,8 @@ public function __construct()
* Find all files to process, based on a file or directory and exclude patterns.
*
* @return array
*
* @throws Exception
*/
public function findFiles()
{
Expand Down Expand Up @@ -74,11 +77,13 @@ public function findFiles()
* @param string $key
*
* @return mixed
*
* @throws Exception
*/
public function get($key)
{
if (!isset($this->config[$key])) {
throw new \Exception(sprintf('Configuration key "%s" does not exist', $key));
throw new Exception(sprintf('Configuration key "%s" does not exist', $key));
}

return $this->config[$key];
Expand Down
16 changes: 15 additions & 1 deletion TwigCS/Environment/StubbedEnvironment.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

namespace TwigCS\Environment;

use \Closure;
use Symfony\Bridge\Twig\TokenParser\DumpTokenParser;
use Symfony\Bridge\Twig\TokenParser\FormThemeTokenParser;
use Symfony\Bridge\Twig\TokenParser\StopwatchTokenParser;
use Symfony\Bridge\Twig\TokenParser\TransChoiceTokenParser;
use Symfony\Bridge\Twig\TokenParser\TransDefaultDomainTokenParser;
use Symfony\Bridge\Twig\TokenParser\TransTokenParser;
use Twig\Environment;
use Twig\Loader\LoaderInterface;
use Twig\TwigFilter;
Expand Down Expand Up @@ -31,7 +38,7 @@ class StubbedEnvironment extends Environment
private $stubTests;

/**
* @var \Closure
* @var Closure
*/
private $stubCallable;

Expand All @@ -43,6 +50,13 @@ public function __construct(LoaderInterface $loader = null, $options = [])
{
parent::__construct($loader, $options);

$this->addTokenParser(new DumpTokenParser());
$this->addTokenParser(new FormThemeTokenParser());
$this->addTokenParser(new StopwatchTokenParser(false));
$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 */
};
Expand Down
29 changes: 17 additions & 12 deletions TwigCS/Linter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace TwigCS;

use \Exception;
use \Traversable;
use Twig\Environment;
use Twig\Error\Error;
use Twig\Source;
Expand All @@ -12,7 +14,7 @@
use TwigCS\Sniff\PostParserSniffInterface;
use TwigCS\Sniff\PreParserSniffInterface;
use TwigCS\Sniff\SniffInterface;
use TwigCS\Token\TokenizerInterface;
use TwigCS\Token\Tokenizer;

/**
* Linter is the main class and will process twig files against a set of rules.
Expand All @@ -30,17 +32,18 @@ class Linter
protected $sniffsExtension;

/**
* @var TokenizerInterface
* @var Tokenizer
*/
protected $tokenizer;

/**
* @param Environment $env
* @param TokenizerInterface $tokenizer
* @param Environment $env
* @param Tokenizer $tokenizer
*/
public function __construct(Environment $env, TokenizerInterface $tokenizer)
public function __construct(Environment $env, Tokenizer $tokenizer)
{
$this->env = $env;

$this->sniffsExtension = $this->env->getExtension('TwigCS\Extension\SniffsExtension');
$this->tokenizer = $tokenizer;
}
Expand All @@ -52,15 +55,17 @@ public function __construct(Environment $env, TokenizerInterface $tokenizer)
* @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)
{
if (!is_array($files) && !$files instanceof \Traversable) {
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');
throw new Exception('No files to process, provide at least one file to be linted');
}

// setUp
Expand All @@ -70,8 +75,8 @@ public function run($files, Ruleset $ruleset)
$sniffViolation = new SniffViolation(
SniffInterface::MESSAGE_TYPE_NOTICE,
$msg,
'',
''
null,
null
);

$report->addMessage($sniffViolation);
Expand Down Expand Up @@ -139,11 +144,11 @@ public function processTemplate($file, $ruleset, $report)
// Tokenizer.
try {
$stream = $this->tokenizer->tokenize($twigSource);
} catch (\Exception $e) {
} catch (Exception $e) {
$sniffViolation = new SniffViolation(
SniffInterface::MESSAGE_TYPE_ERROR,
sprintf('Unable to tokenize file "%s"', (string) $file),
'',
sprintf('Unable to tokenize file'),
null,
(string) $file
);

Expand Down
6 changes: 2 additions & 4 deletions TwigCS/Report/SniffViolation.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,9 @@ public function getLevelAsString()
case SniffInterface::MESSAGE_TYPE_WARNING:
return 'WARNING';
case SniffInterface::MESSAGE_TYPE_ERROR:
default:
return 'ERROR';
}

throw new \Exception(sprintf('Unknown level "%s"', $this->level));
}

/**
Expand All @@ -112,10 +111,9 @@ public static function getLevelAsInt($level)
case 'WARNING':
return SniffInterface::MESSAGE_TYPE_WARNING;
case 'ERROR':
default:
return SniffInterface::MESSAGE_TYPE_ERROR;
}

throw new \Exception(sprintf('Unknown level "%s"', $level));
}

/**
Expand Down
39 changes: 30 additions & 9 deletions TwigCS/Report/TextFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,33 +46,40 @@ public function display(Report $report, $level = null)
'level' => $level,
]);

$this->io->text((count($fileMessages) > 0 ? '<fg=red>KO</fg=red>' : '<info>OK</info>').' '.$file);
if (count($fileMessages) > 0) {
$this->io->text('<fg=red>KO</fg=red> '.$file);
}

$rows = [];
foreach ($fileMessages as $message) {
$lines = $this->getContext(file_get_contents($file), $message->getLine(), $this::ERROR_CONTEXT_LIMIT);

$formattedText = [];
if (!$message->getLine()) {
$formattedText[] = $this->formatErrorMessage($message);
}

foreach ($lines as $no => $code) {
$formattedText[] = sprintf($this::ERROR_LINE_FORMAT, $no, wordwrap($code, $this::ERROR_LINE_WIDTH));

if ($no === $message->getLine()) {
$formattedText[] = sprintf(
'<fg=red>'.$this::ERROR_LINE_FORMAT.'</fg=red>',
$this::ERROR_CURSOR_CHAR,
wordwrap($message->getMessage(), $this::ERROR_LINE_WIDTH)
);
$formattedText[] = $this->formatErrorMessage($message);
}
}

if (count($rows) > 0) {
$rows[] = new TableSeparator();
}

$rows[] = [
new TableCell('<comment>'.$message->getLevelAsString().'</comment>', ['rowspan' => 2]),
new TableCell('<comment>'.$message->getLevelAsString().'</comment>'),
implode("\n", $formattedText),
];
$rows[] = new TableSeparator();
}

$this->io->table([], $rows);
if (count($rows) > 0) {
$this->io->table([], $rows);
}
}

$summaryString = sprintf(
Expand Down Expand Up @@ -131,4 +138,18 @@ protected function getContext($template, $line, $context)

return $result;
}

/**
* @param SniffViolation $message
*
* @return string
*/
protected function formatErrorMessage(SniffViolation $message)
{
return sprintf(
'<fg=red>'.$this::ERROR_LINE_FORMAT.'</fg=red>',
$this::ERROR_CURSOR_CHAR,
wordwrap($message->getMessage(), $this::ERROR_LINE_WIDTH)
);
}
}
5 changes: 4 additions & 1 deletion TwigCS/Ruleset/Ruleset.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace TwigCS\Ruleset;

use \Exception;
use TwigCS\Sniff\PostParserSniffInterface;
use TwigCS\Sniff\PreParserSniffInterface;
use TwigCS\Sniff\SniffInterface;
Expand Down Expand Up @@ -69,6 +70,8 @@ public function addPostParserSniff(PostParserSniffInterface $sniff)
* @param SniffInterface $sniff
*
* @return $this
*
* @throws Exception
*/
public function addSniff(SniffInterface $sniff)
{
Expand All @@ -88,7 +91,7 @@ public function addSniff(SniffInterface $sniff)
return $this;
}

throw new \Exception(sprintf(
throw new Exception(sprintf(
'Unknown type of sniff "%s", expected one of: "%s"',
$sniff->getType(),
implode(', ', [SniffInterface::TYPE_PRE_PARSER, SniffInterface::TYPE_POST_PARSER])
Expand Down
6 changes: 5 additions & 1 deletion TwigCS/Ruleset/RulesetFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace TwigCS\Ruleset;

use \Exception;
use \SplFileInfo;
use Symfony\Component\Finder\Finder;

/**
Expand All @@ -13,14 +15,16 @@ class RulesetFactory
* Create a new set of rule.
*
* @return Ruleset
*
* @throws Exception
*/
public function createStandardRuleset()
{
$ruleset = new Ruleset();

$finder = Finder::create()->in(__DIR__.'/../Sniff/Standard')->files();

/** @var \SplFileInfo $file */
/** @var SplFileInfo $file */
foreach ($finder as $file) {
$class = 'TwigCS\Sniff\Standard\\'.explode('.', $file->getFilename())[0];
$ruleset->addSniff(new $class());
Expand Down
7 changes: 5 additions & 2 deletions TwigCS/Sniff/AbstractPostParserSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace TwigCS\Sniff;

use \Exception;
use Twig\Node\Expression\Binary\ConcatBinary;
use Twig\Node\Expression\ConstantExpression;
use Twig\Node\Expression\FilterExpression;
Expand Down Expand Up @@ -38,6 +39,8 @@ public function getType()
* @param Node $node
*
* @return self
*
* @throws Exception
*/
public function addMessage($messageType, $message, Node $node)
{
Expand All @@ -56,7 +59,7 @@ public function addMessage($messageType, $message, Node $node)
/**
* @param Node $node
*
* @return string
* @return int|null
*/
public function getTemplateLine(Node $node)
{
Expand All @@ -68,7 +71,7 @@ public function getTemplateLine(Node $node)
return $node->getLine();
}

return '';
return null;
}

/**
Expand Down
3 changes: 3 additions & 0 deletions TwigCS/Sniff/AbstractPreParserSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace TwigCS\Sniff;

use \Exception;
use TwigCS\Report\SniffViolation;
use TwigCS\Token\Token;

Expand Down Expand Up @@ -46,6 +47,8 @@ public function isTokenMatching(Token $token, $type, $value = null)
* @param Token $token
*
* @return self
*
* @throws Exception
*/
public function addMessage($messageType, $message, Token $token)
{
Expand Down
5 changes: 4 additions & 1 deletion TwigCS/Sniff/AbstractSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace TwigCS\Sniff;

use \Exception;
use TwigCS\Report\Report;

/**
Expand Down Expand Up @@ -51,11 +52,13 @@ public function disable()

/**
* @return Report
*
* @throws Exception
*/
public function getReport()
{
if (null === $this->report) {
throw new \Exception('Sniff is disabled!');
throw new Exception('Sniff is disabled!');
}

return $this->report;
Expand Down
Loading