Skip to content
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

Remove Twig_TokenParserBroker (deprecated) + all deprecation warnings #36

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 7 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
language: php

dist: trusty

sudo: false

php:
- 5.3
- 5.4
- 5.5
- 5.6
Expand All @@ -13,3 +14,8 @@ php:
before_script: composer install

script: phpunit

matrix:
include: # https://github.com/travis-ci/travis-ci/issues/7712#issuecomment-300553336
- php: "5.3"
dist: precise
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
"symfony/finder": "^2.1 || ^3.0",
"twig/twig": "^1.16.2"
},
"require-dev": {
"phpunit/phpunit": "^4.8.36 || ^5.5 || ^6.2"
},
"autoload": {
"psr-0": { "Asm89\\Twig\\Lint\\": "src/" }
},
Expand Down
36 changes: 29 additions & 7 deletions src/Asm89/Twig/Lint/Command/LintCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,20 @@ protected function configure()
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
'Excludes, based on regex, paths of files and folders from parsing'
),
new InputOption(
'stub-tag',
'',
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
'List of tags that the lint command has to provide stub for',
array()
),
new InputOption(
'stub-test',
'',
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
'List of tests that the lint command has to provide stub for',
array()
),
new InputOption('only-print-errors', '', InputOption::VALUE_NONE),
new InputOption('summary', '', InputOption::VALUE_NONE)
))
Expand Down Expand Up @@ -81,12 +95,20 @@ protected function configure()

protected function execute(InputInterface $input, CliOutputInterface $output)
{
$twig = new StubbedEnvironment(new \Twig_Loader_String());
$template = null;
$filename = $input->getArgument('filename');
$exclude = $input->getOption('exclude');
$summary = $input->getOption('summary');
$output = $this->getOutput($output, $input->getOption('format'));
$template = null;
$filename = $input->getArgument('filename');
$exclude = $input->getOption('exclude');
$stubTagList = $input->getOption('stub-tag');
$stubTestsList = $input->getOption('stub-test');
$summary = $input->getOption('summary');
$output = $this->getOutput($output, $input->getOption('format'));
$twig = new StubbedEnvironment(
new \Twig_Loader_Array(),
array(
'stub_tags' => $stubTagList,
'stub_tests' => $stubTestsList,
)
);

if (!$filename) {
if (0 !== ftell(STDIN)) {
Expand Down Expand Up @@ -151,7 +173,7 @@ protected function validateTemplate(
)
{
try {
$twig->parse($twig->tokenize($template, $file ? (string) $file : null));
$twig->parse($twig->tokenize(new \Twig_Source($template, $file ? (string) $file : null)));
if (false === $onlyPrintErrors) {
$output->ok($template, $file);
}
Expand Down
43 changes: 0 additions & 43 deletions src/Asm89/Twig/Lint/Extension/StubbedCore.php

This file was deleted.

41 changes: 30 additions & 11 deletions src/Asm89/Twig/Lint/StubbedEnvironment.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Asm89\Twig\Lint;

use Asm89\Twig\Lint\Extension\StubbedCore;
use Asm89\Twig\Lint\TokenParser\CatchAll;
use Twig_LoaderInterface;

/**
Expand All @@ -25,7 +25,7 @@ class StubbedEnvironment extends \Twig_Environment
private $stubFilters;
private $stubFunctions;
private $stubTests;
protected $parsers;
private $stubCallable;

/**
* {@inheritDoc}
Expand All @@ -34,11 +34,25 @@ public function __construct(Twig_LoaderInterface $loader = null, $options = arra
{
parent::__construct($loader, $options);

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

$broker = new StubbedTokenParserBroker();
$this->parsers->addTokenParserBroker($broker);
$this->stubFilters = array();
$this->stubFunctions = array();

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

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

/**
Expand All @@ -47,7 +61,7 @@ public function __construct(Twig_LoaderInterface $loader = null, $options = arra
public function getFilter($name)
{
if (!isset($this->stubFilters[$name])) {
$this->stubFilters[$name] = new \Twig_Filter_Function('stub');
$this->stubFilters[$name] = new \Twig_SimpleFilter('stub', $this->stubCallable);
}

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

return $this->stubFunctions[$name];
Expand All @@ -70,10 +84,15 @@ public function getFunction($name)
*/
public function getTest($name)
{
if (!isset($this->stubTests[$name])) {
$this->stubTests[$name] = new \Twig_SimpleTest('stub', function(){});
$test = parent::getTest($name);
if ($test) {
return $test;
}

if (isset($this->stubTests[$name])) {
return $this->stubTests[$name];
}

return $this->stubTests[$name];
return false;
}
}
55 changes: 0 additions & 55 deletions src/Asm89/Twig/Lint/StubbedTokenParserBroker.php

This file was deleted.

4 changes: 4 additions & 0 deletions tests/Asm89/Twig/Lint/Test/Fixtures/undefined_test.twig
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@
{% if foo is some_undefined_test_with_args(bar) %}
{{ foo }}
{% endif %}

{% if foo is created by('me') %}
{{ foo }}
{% endif %}
23 changes: 15 additions & 8 deletions tests/Asm89/Twig/Lint/Test/StubbedEnvironmentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,39 @@
namespace Asm89\Twig\Lint\Test;

use Asm89\Twig\Lint\StubbedEnvironment;
use Twig_Error;
use PHPUnit\Framework\TestCase;
use \Twig_Error;

/**
* @author Alexander <[email protected]>
*/
class StubbedEnvironmentTest extends \PHPUnit_Framework_TestCase
class StubbedEnvironmentTest extends TestCase
{
private $env;

public function setup()
{
$this->env = new StubbedEnvironment();
$this->env = new StubbedEnvironment(
$this->getMockBuilder('Twig_LoaderInterface')->getMock(),
array(
'stub_tags' => array('meh', 'render', 'some_other_block', 'stylesheets', 'trans'),
'stub_tests' => array('created by', 'sometest', 'some_undefined_test', 'some_undefined_test_with_args'),
)
);
}

public function testGetFilterAlwaysReturnsAFilter()
{
$filter = $this->env->getFilter('foo');

$this->assertInstanceOf('Twig_Filter', $filter);
$this->assertInstanceOf('Twig_SimpleFilter', $filter);
}

public function testGetFunctionAlwaysReturnsAFunction()
{
$function = $this->env->getFunction('foo');

$this->assertInstanceOf('Twig_Function', $function);
$this->assertInstanceOf('Twig_SimpleFunction', $function);
}

/**
Expand All @@ -48,12 +55,12 @@ public function testParseTemplatesWithUndefinedElements($filename)
$file = __DIR__ . '/Fixtures/' . $filename;
$template = file_get_contents($file);
try {
$this->env->parse($this->env->tokenize($template, $file));
$this->env->parse($this->env->tokenize(new \Twig_Source($template, $file)));
} catch (Twig_Error $exception) {
$this->assertTrue(false, "Was unable to parse the template.");
$this->assertTrue(false, sprintf('Was unable to parse the template: "%s"', $exception->getMessage()));
}

$this->assertTrue(true, "Was able to parse the template.");
$this->assertTrue(true, 'Was able to parse the template.');
}

public function templateFixtures()
Expand Down