Skip to content

Commit

Permalink
added deprecation notices for deprecated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Aug 18, 2015
1 parent 65e70a2 commit efcfe8e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 15 deletions.
38 changes: 23 additions & 15 deletions lib/Twig/Extension/Core.php
Expand Up @@ -233,11 +233,11 @@ public function getTests()
new Twig_SimpleTest('even', null, array('node_class' => 'Twig_Node_Expression_Test_Even')),
new Twig_SimpleTest('odd', null, array('node_class' => 'Twig_Node_Expression_Test_Odd')),
new Twig_SimpleTest('defined', null, array('node_class' => 'Twig_Node_Expression_Test_Defined')),
new Twig_SimpleTest('sameas', null, array('node_class' => 'Twig_Node_Expression_Test_Sameas')),
new Twig_SimpleTest('sameas', null, array('node_class' => 'Twig_Node_Expression_Test_Sameas', 'deprecated' => true, 'alternative' => 'same as')),
new Twig_SimpleTest('same as', null, array('node_class' => 'Twig_Node_Expression_Test_Sameas')),
new Twig_SimpleTest('none', null, array('node_class' => 'Twig_Node_Expression_Test_Null')),
new Twig_SimpleTest('null', null, array('node_class' => 'Twig_Node_Expression_Test_Null')),
new Twig_SimpleTest('divisibleby', null, array('node_class' => 'Twig_Node_Expression_Test_Divisibleby')),
new Twig_SimpleTest('divisibleby', null, array('node_class' => 'Twig_Node_Expression_Test_Divisibleby', 'deprecated' => true, 'alternative' => 'divisible by')),
new Twig_SimpleTest('divisible by', null, array('node_class' => 'Twig_Node_Expression_Test_Divisibleby')),
new Twig_SimpleTest('constant', null, array('node_class' => 'Twig_Node_Expression_Test_Constant')),
new Twig_SimpleTest('empty', 'twig_test_empty'),
Expand Down Expand Up @@ -298,25 +298,36 @@ public function parseNotTestExpression(Twig_Parser $parser, Twig_NodeInterface $
public function parseTestExpression(Twig_Parser $parser, Twig_NodeInterface $node)
{
$stream = $parser->getStream();
$name = $this->getTestName($parser, $node->getLine());
$class = $this->getTestNodeClass($parser, $name);
$test = $this->getTest($parser, $node->getLine());

if ($test instanceof Twig_SimpleTest && $test->isDeprecated()) {
$message = sprintf('Twig Test "%s" is deprecated', $test->getName());
if ($test->getAlternative()) {
$message .= sprintf('. Use "%s" instead', $test->getAlternative());
}
$message .= sprintf(' in %s at line %d.', $stream->getFilename(), $stream->getCurrent()->getLine());

@trigger_error($message, E_USER_DEPRECATED);
}

$class = $this->getTestNodeClass($parser, $test);
$arguments = null;
if ($stream->test(Twig_Token::PUNCTUATION_TYPE, '(')) {
$arguments = $parser->getExpressionParser()->parseArguments(true);
}

return new $class($node, $name, $arguments, $parser->getCurrentToken()->getLine());
return new $class($node, $test->getName(), $arguments, $parser->getCurrentToken()->getLine());
}

protected function getTestName(Twig_Parser $parser, $line)
protected function getTest(Twig_Parser $parser, $line)
{
$stream = $parser->getStream();
$name = $stream->expect(Twig_Token::NAME_TYPE)->getValue();
$env = $parser->getEnvironment();
$testMap = $env->getTests();

if (isset($testMap[$name])) {
return $name;
return $testMap[$name];
}

if ($stream->test(Twig_Token::NAME_TYPE)) {
Expand All @@ -326,7 +337,7 @@ protected function getTestName(Twig_Parser $parser, $line)
if (isset($testMap[$name])) {
$parser->getStream()->next();

return $name;
return $testMap[$name];
}
}

Expand All @@ -338,16 +349,13 @@ protected function getTestName(Twig_Parser $parser, $line)
throw new Twig_Error_Syntax($message, $line, $parser->getFilename());
}

protected function getTestNodeClass(Twig_Parser $parser, $name)
protected function getTestNodeClass(Twig_Parser $parser, $test)
{
$env = $parser->getEnvironment();
$testMap = $env->getTests();

if ($testMap[$name] instanceof Twig_SimpleTest) {
return $testMap[$name]->getNodeClass();
if ($test instanceof Twig_SimpleTest) {
return $test->getNodeClass();
}

return $testMap[$name] instanceof Twig_Test_Node ? $testMap[$name]->getClass() : 'Twig_Node_Expression_Test';
return $test instanceof Twig_Test_Node ? $test->getClass() : 'Twig_Node_Expression_Test';
}

/**
Expand Down
12 changes: 12 additions & 0 deletions lib/Twig/SimpleTest.php
Expand Up @@ -27,6 +27,8 @@ public function __construct($name, $callable, array $options = array())
$this->options = array_merge(array(
'is_variadic' => false,
'node_class' => 'Twig_Node_Expression_Test',
'deprecated' => false,
'alternative' => null,
), $options);
}

Expand All @@ -49,4 +51,14 @@ public function isVariadic()
{
return $this->options['is_variadic'];
}

public function isDeprecated()
{
return $this->options['deprecated'];
}

public function getAlternative()
{
return $this->options['alternative'];
}
}

0 comments on commit efcfe8e

Please sign in to comment.