Skip to content

✨ Add Whitespace check for delimiter #63

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 13 commits into from
Jun 2, 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
2 changes: 1 addition & 1 deletion SymfonyCustom/Sniffs/Commenting/FunctionCommentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ protected function processParams(
*
* @return bool True if the return does not return anything
*/
protected function isMatchingReturn($tokens, $returnPos)
protected function isMatchingReturn(array $tokens, $returnPos)
{
do {
$returnPos++;
Expand Down
25 changes: 11 additions & 14 deletions TwigCS/Command/TwigCSCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Twig\Loader\ArrayLoader;
use TwigCS\Config\Config;
use TwigCS\Environment\StubbedEnvironment;
use TwigCS\Linter;
use TwigCS\Report\TextFormatter;
use TwigCS\Ruleset\RulesetFactory;
use TwigCS\Ruleset\Ruleset;
use TwigCS\Token\Tokenizer;

/**
Expand All @@ -33,16 +32,16 @@ protected function configure()
new InputOption(
'exclude',
'e',
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
'Excludes, based on regex, paths of files and folders from parsing',
['vendor/']
),
new InputOption(
'level',
'l',
InputOption::VALUE_OPTIONAL,
'Allowed values are: warning, error',
'warning'
'Allowed values are notice, warning or error',
'notice'
),
new InputOption(
'working-dir',
Expand Down Expand Up @@ -81,26 +80,24 @@ protected function execute(InputInterface $input, OutputInterface $output)
'workingDirectory' => $currentDir,
]);

$twig = new StubbedEnvironment(new ArrayLoader(), ['stub_tags' => $config->get('stub')]);
$linter = new Linter($twig, new Tokenizer($twig));
$factory = new RulesetFactory();
$reporter = new TextFormatter($input, $output);
$exitCode = 0;

// Get the rules to apply.
$ruleset = $factory->createStandardRuleset();
$ruleset = new Ruleset();
$ruleset->addStandard();

// Execute the linter.
$twig = new StubbedEnvironment();
$linter = new Linter($twig, new Tokenizer($twig));
$report = $linter->run($config->findFiles(), $ruleset);

// Format the output.
$reporter = new TextFormatter($input, $output);
$reporter->display($report, $level);

// Return a meaningful error code.
if ($report->getTotalErrors()) {
$exitCode = 1;
return 1;
}

return $exitCode;
return 0;
}
}
17 changes: 7 additions & 10 deletions TwigCS/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ class Config
'exclude' => [],
'pattern' => '*.twig',
'paths' => [],
'stub' => [],
'workingDirectory' => '',
];

Expand All @@ -30,14 +29,12 @@ class Config
*/
protected $config;

public function __construct()
/**
* @param array $config
*/
public function __construct(array $config = [])
{
$args = func_get_args();

$this->config = $this::$defaultConfig;
foreach ($args as $arg) {
$this->config = array_merge($this->config, $arg);
}
$this->config = array_merge($this::$defaultConfig, $config);
}

/**
Expand Down Expand Up @@ -68,7 +65,7 @@ public function findFiles()
$files->exclude($exclude);
}

return $files;
return iterator_to_array($files, false);
}

/**
Expand All @@ -80,7 +77,7 @@ public function findFiles()
*
* @throws Exception
*/
public function get($key)
public function get(string $key)
{
if (!isset($this->config[$key])) {
throw new Exception(sprintf('Configuration key "%s" does not exist', $key));
Expand Down
55 changes: 10 additions & 45 deletions TwigCS/Environment/StubbedEnvironment.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,17 @@

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\Loader\ArrayLoader;
use Twig\TwigFilter;
use Twig\TwigFunction;
use Twig\TwigTest;
use TwigCS\Extension\SniffsExtension;
use TwigCS\Token\TokenParser;

/**
* Provide stubs for all filters, functions, tests and tags that are not defined in twig's core.
Expand All @@ -37,18 +34,9 @@ class StubbedEnvironment extends Environment
*/
private $stubTests;

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

/**
* @param LoaderInterface|null $loader
* @param array $options
*/
public function __construct(LoaderInterface $loader = null, $options = [])
public function __construct()
{
parent::__construct($loader, $options);
parent::__construct(new ArrayLoader());

$this->addTokenParser(new DumpTokenParser());
$this->addTokenParser(new FormThemeTokenParser());
Expand All @@ -57,27 +45,9 @@ public function __construct(LoaderInterface $loader = null, $options = [])
$this->addTokenParser(new TransDefaultDomainTokenParser());
$this->addTokenParser(new TransTokenParser());

$this->stubCallable = function () {
/* This will be used as stub filter, function or test */
};

$this->stubFilters = [];
$this->stubFilters = [];
$this->stubFunctions = [];

if (isset($options['stub_tags'])) {
foreach ($options['stub_tags'] as $tag) {
$this->addTokenParser(new TokenParser($tag));
}
}

$this->stubTests = [];
if (isset($options['stub_tests'])) {
foreach ($options['stub_tests'] as $test) {
$this->stubTests[$test] = new TwigTest('stub', $this->stubCallable);
}
}

$this->addExtension(new SniffsExtension());
}

/**
Expand All @@ -88,7 +58,7 @@ public function __construct(LoaderInterface $loader = null, $options = [])
public function getFilter($name)
{
if (!isset($this->stubFilters[$name])) {
$this->stubFilters[$name] = new TwigFilter('stub', $this->stubCallable);
$this->stubFilters[$name] = new TwigFilter('stub');
}

return $this->stubFilters[$name];
Expand All @@ -102,7 +72,7 @@ public function getFilter($name)
public function getFunction($name)
{
if (!isset($this->stubFunctions[$name])) {
$this->stubFunctions[$name] = new TwigFunction('stub', $this->stubCallable);
$this->stubFunctions[$name] = new TwigFunction('stub');
}

return $this->stubFunctions[$name];
Expand All @@ -111,19 +81,14 @@ public function getFunction($name)
/**
* @param string $name
*
* @return false|TwigTest
* @return TwigTest
*/
public function getTest($name)
{
$test = parent::getTest($name);
if ($test) {
return $test;
}

if (isset($this->stubTests[$name])) {
return $this->stubTests[$name];
if (!isset($this->stubTests[$name])) {
$this->stubTests[$name] = new TwigTest('stub');
}

return false;
return $this->stubTests[$name];
}
}
64 changes: 0 additions & 64 deletions TwigCS/Extension/SniffsExtension.php

This file was deleted.

Loading