mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-15 20:06:31 +00:00
deprecated Twig_Environment::computeAlternatives()
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
* 1.23.0 (2015-XX-XX)
|
* 1.23.0 (2015-XX-XX)
|
||||||
|
|
||||||
* n/a
|
* deprecated Twig_Environment::computeAlternatives()
|
||||||
|
|
||||||
* 1.22.3 (2015-10-13)
|
* 1.22.3 (2015-10-13)
|
||||||
|
|
||||||
|
|||||||
@@ -1235,18 +1235,14 @@ class Twig_Environment
|
|||||||
return $this->binaryOperators;
|
return $this->binaryOperators;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated since 1.23 (to be removed in 2.0)
|
||||||
|
*/
|
||||||
public function computeAlternatives($name, $items)
|
public function computeAlternatives($name, $items)
|
||||||
{
|
{
|
||||||
$alternatives = array();
|
@trigger_error(sprintf('The %s method is deprecated and will be removed in Twig 2.0.', __METHOD__), E_USER_DEPRECATED);
|
||||||
foreach ($items as $item) {
|
|
||||||
$lev = levenshtein($name, $item);
|
|
||||||
if ($lev <= strlen($name) / 3 || false !== strpos($item, $name)) {
|
|
||||||
$alternatives[$item] = $lev;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
asort($alternatives);
|
|
||||||
|
|
||||||
return array_keys($alternatives);
|
return Twig_Error_Syntax::getAlternatives($name, $items);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function initGlobals()
|
protected function initGlobals()
|
||||||
|
|||||||
@@ -155,6 +155,9 @@ class Twig_Error extends Exception
|
|||||||
throw new BadMethodCallException(sprintf('Method "Twig_Error::%s()" does not exist.', $method));
|
throw new BadMethodCallException(sprintf('Method "Twig_Error::%s()" does not exist.', $method));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
protected function updateRepr()
|
protected function updateRepr()
|
||||||
{
|
{
|
||||||
$this->message = $this->rawMessage;
|
$this->message = $this->rawMessage;
|
||||||
@@ -165,6 +168,12 @@ class Twig_Error extends Exception
|
|||||||
$dot = true;
|
$dot = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$questionMark = false;
|
||||||
|
if ('?' === substr($this->message, -1)) {
|
||||||
|
$this->message = substr($this->message, 0, -1);
|
||||||
|
$questionMark = true;
|
||||||
|
}
|
||||||
|
|
||||||
if ($this->filename) {
|
if ($this->filename) {
|
||||||
if (is_string($this->filename) || (is_object($this->filename) && method_exists($this->filename, '__toString'))) {
|
if (is_string($this->filename) || (is_object($this->filename) && method_exists($this->filename, '__toString'))) {
|
||||||
$filename = sprintf('"%s"', $this->filename);
|
$filename = sprintf('"%s"', $this->filename);
|
||||||
@@ -181,8 +190,15 @@ class Twig_Error extends Exception
|
|||||||
if ($dot) {
|
if ($dot) {
|
||||||
$this->message .= '.';
|
$this->message .= '.';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($questionMark) {
|
||||||
|
$this->message .= '?';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
protected function guessTemplateInfo()
|
protected function guessTemplateInfo()
|
||||||
{
|
{
|
||||||
$template = null;
|
$template = null;
|
||||||
|
|||||||
@@ -17,4 +17,38 @@
|
|||||||
*/
|
*/
|
||||||
class Twig_Error_Syntax extends Twig_Error
|
class Twig_Error_Syntax extends Twig_Error
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* Tweaks the error message to include suggestions.
|
||||||
|
*
|
||||||
|
* @param string $name The original name of the item that does not exist
|
||||||
|
* @param array $items An array of possible items
|
||||||
|
*/
|
||||||
|
public function addMessageSuggestions($name, array $items)
|
||||||
|
{
|
||||||
|
if (!$alternatives = self::computeAlternatives($name, $items)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->rawMessage .= sprintf(' Did you mean "%s"?', implode('", "', $alternatives));
|
||||||
|
$this->updateRepr();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @internal
|
||||||
|
*
|
||||||
|
* To be merged with the addMessageSuggestions() method in 2.0.
|
||||||
|
*/
|
||||||
|
public static function computeAlternatives($name, $items)
|
||||||
|
{
|
||||||
|
$alternatives = array();
|
||||||
|
foreach ($items as $item) {
|
||||||
|
$lev = levenshtein($name, $item);
|
||||||
|
if ($lev <= strlen($name) / 3 || false !== strpos($item, $name)) {
|
||||||
|
$alternatives[$item] = $lev;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
asort($alternatives);
|
||||||
|
|
||||||
|
return array_keys($alternatives);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -570,12 +570,10 @@ class Twig_ExpressionParser
|
|||||||
$env = $this->parser->getEnvironment();
|
$env = $this->parser->getEnvironment();
|
||||||
|
|
||||||
if (false === $function = $env->getFunction($name)) {
|
if (false === $function = $env->getFunction($name)) {
|
||||||
$message = sprintf('The function "%s" does not exist', $name);
|
$e = new Twig_Error_Syntax(sprintf('Unknown "%s" function.', $name), $line, $this->parser->getFilename());
|
||||||
if ($alternatives = $env->computeAlternatives($name, array_keys($env->getFunctions()))) {
|
$e->addMessageSuggestions($name, array_keys($env->getFunctions()));
|
||||||
$message = sprintf('%s. Did you mean "%s"', $message, implode('", "', $alternatives));
|
|
||||||
}
|
|
||||||
|
|
||||||
throw new Twig_Error_Syntax($message, $line, $this->parser->getFilename());
|
throw $e;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($function instanceof Twig_SimpleFunction && $function->isDeprecated()) {
|
if ($function instanceof Twig_SimpleFunction && $function->isDeprecated()) {
|
||||||
@@ -600,12 +598,10 @@ class Twig_ExpressionParser
|
|||||||
$env = $this->parser->getEnvironment();
|
$env = $this->parser->getEnvironment();
|
||||||
|
|
||||||
if (false === $filter = $env->getFilter($name)) {
|
if (false === $filter = $env->getFilter($name)) {
|
||||||
$message = sprintf('The filter "%s" does not exist', $name);
|
$e = new Twig_Error_Syntax(sprintf('Unknown "%s" filter.', $name), $line, $this->parser->getFilename());
|
||||||
if ($alternatives = $env->computeAlternatives($name, array_keys($env->getFilters()))) {
|
$e->addMessageSuggestions($name, array_keys($env->getFilters()));
|
||||||
$message = sprintf('%s. Did you mean "%s"', $message, implode('", "', $alternatives));
|
|
||||||
}
|
|
||||||
|
|
||||||
throw new Twig_Error_Syntax($message, $line, $this->parser->getFilename());
|
throw $e;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($filter instanceof Twig_SimpleFilter && $filter->isDeprecated()) {
|
if ($filter instanceof Twig_SimpleFilter && $filter->isDeprecated()) {
|
||||||
|
|||||||
@@ -340,12 +340,10 @@ class Twig_Extension_Core extends Twig_Extension
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$message = sprintf('The test "%s" does not exist', $name);
|
$e = new Twig_Error_Syntax(sprintf('Unknown "%s" test.', $name), $line, $parser->getFilename());
|
||||||
if ($alternatives = $env->computeAlternatives($name, array_keys($env->getTests()))) {
|
$e->addMessageSuggestions($name, array_keys($env->getTests()));
|
||||||
$message = sprintf('%s. Did you mean "%s"', $message, implode('", "', $alternatives));
|
|
||||||
}
|
|
||||||
|
|
||||||
throw new Twig_Error_Syntax($message, $line, $parser->getFilename());
|
throw $e;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function getTestNodeClass(Twig_Parser $parser, $test)
|
protected function getTestNodeClass(Twig_Parser $parser, $test)
|
||||||
|
|||||||
+3
-5
@@ -174,12 +174,10 @@ class Twig_Parser implements Twig_ParserInterface
|
|||||||
throw new Twig_Error_Syntax($error, $token->getLine(), $this->getFilename());
|
throw new Twig_Error_Syntax($error, $token->getLine(), $this->getFilename());
|
||||||
}
|
}
|
||||||
|
|
||||||
$message = sprintf('Unknown tag name "%s"', $token->getValue());
|
$e = new Twig_Error_Syntax(sprintf('Unknown "%s" tag.', $token->getValue()), $token->getLine(), $this->getFilename());
|
||||||
if ($alternatives = $this->env->computeAlternatives($token->getValue(), array_keys($this->env->getTags()))) {
|
$e->addMessageSuggestions($token->getValue(), array_keys($this->env->getTags()));
|
||||||
$message = sprintf('%s. Did you mean "%s"', $message, implode('", "', $alternatives));
|
|
||||||
}
|
|
||||||
|
|
||||||
throw new Twig_Error_Syntax($message, $token->getLine(), $this->getFilename());
|
throw $e;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->stream->next();
|
$this->stream->next();
|
||||||
|
|||||||
@@ -296,7 +296,7 @@ class Twig_Tests_ExpressionParserTest extends PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @expectedException Twig_Error_Syntax
|
* @expectedException Twig_Error_Syntax
|
||||||
* @expectedExceptionMessage The function "cycl" does not exist. Did you mean "cycle" in "index" at line 1
|
* @expectedExceptionMessage Unknown "cycl" function. Did you mean "cycle" in "index" at line 1?
|
||||||
*/
|
*/
|
||||||
public function testUnknownFunction()
|
public function testUnknownFunction()
|
||||||
{
|
{
|
||||||
@@ -308,7 +308,19 @@ class Twig_Tests_ExpressionParserTest extends PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @expectedException Twig_Error_Syntax
|
* @expectedException Twig_Error_Syntax
|
||||||
* @expectedExceptionMessage The filter "lowe" does not exist. Did you mean "lower" in "index" at line 1
|
* @expectedExceptionMessage Unknown "foobar" function in "index" at line 1.
|
||||||
|
*/
|
||||||
|
public function testUnknownFunctionWithoutSuggestions()
|
||||||
|
{
|
||||||
|
$env = new Twig_Environment($this->getMock('Twig_LoaderInterface'), array('cache' => false, 'autoescape' => false));
|
||||||
|
$parser = new Twig_Parser($env);
|
||||||
|
|
||||||
|
$parser->parse($env->tokenize('{{ foobar() }}', 'index'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @expectedException Twig_Error_Syntax
|
||||||
|
* @expectedExceptionMessage Unknown "lowe" filter. Did you mean "lower" in "index" at line 1?
|
||||||
*/
|
*/
|
||||||
public function testUnknownFilter()
|
public function testUnknownFilter()
|
||||||
{
|
{
|
||||||
@@ -320,7 +332,19 @@ class Twig_Tests_ExpressionParserTest extends PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @expectedException Twig_Error_Syntax
|
* @expectedException Twig_Error_Syntax
|
||||||
* @expectedExceptionMessage The test "nul" does not exist. Did you mean "null" in "index" at line 1
|
* @expectedExceptionMessage Unknown "foobar" filter in "index" at line 1.
|
||||||
|
*/
|
||||||
|
public function testUnknownFilterWithoutSuggestions()
|
||||||
|
{
|
||||||
|
$env = new Twig_Environment($this->getMock('Twig_LoaderInterface'), array('cache' => false, 'autoescape' => false));
|
||||||
|
$parser = new Twig_Parser($env);
|
||||||
|
|
||||||
|
$parser->parse($env->tokenize('{{ 1|foobar }}', 'index'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @expectedException Twig_Error_Syntax
|
||||||
|
* @expectedExceptionMessage Unknown "nul" test. Did you mean "null" in "index" at line 1
|
||||||
*/
|
*/
|
||||||
public function testUnknownTest()
|
public function testUnknownTest()
|
||||||
{
|
{
|
||||||
@@ -329,4 +353,16 @@ class Twig_Tests_ExpressionParserTest extends PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
$parser->parse($env->tokenize('{{ 1 is nul }}', 'index'));
|
$parser->parse($env->tokenize('{{ 1 is nul }}', 'index'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @expectedException Twig_Error_Syntax
|
||||||
|
* @expectedExceptionMessage Unknown "foobar" test in "index" at line 1.
|
||||||
|
*/
|
||||||
|
public function testUnknownTestWithoutSuggestions()
|
||||||
|
{
|
||||||
|
$env = new Twig_Environment($this->getMock('Twig_LoaderInterface'), array('cache' => false, 'autoescape' => false));
|
||||||
|
$parser = new Twig_Parser($env);
|
||||||
|
|
||||||
|
$parser->parse($env->tokenize('{{ 1 is foobar }}', 'index'));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ class Twig_Tests_ParserTest extends PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @expectedException Twig_Error_Syntax
|
* @expectedException Twig_Error_Syntax
|
||||||
* @expectedExceptionMessage Unknown tag name "foo". Did you mean "for" at line 1
|
* @expectedExceptionMessage Unknown "foo" tag. Did you mean "for" at line 1?
|
||||||
*/
|
*/
|
||||||
public function testUnknownTag()
|
public function testUnknownTag()
|
||||||
{
|
{
|
||||||
@@ -35,6 +35,22 @@ class Twig_Tests_ParserTest extends PHPUnit_Framework_TestCase
|
|||||||
$parser->parse($stream);
|
$parser->parse($stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @expectedException Twig_Error_Syntax
|
||||||
|
* @expectedExceptionMessage Unknown "foobar" tag at line 1.
|
||||||
|
*/
|
||||||
|
public function testUnknownTagWithoutSuggestions()
|
||||||
|
{
|
||||||
|
$stream = new Twig_TokenStream(array(
|
||||||
|
new Twig_Token(Twig_Token::BLOCK_START_TYPE, '', 1),
|
||||||
|
new Twig_Token(Twig_Token::NAME_TYPE, 'foobar', 1),
|
||||||
|
new Twig_Token(Twig_Token::BLOCK_END_TYPE, '', 1),
|
||||||
|
new Twig_Token(Twig_Token::EOF_TYPE, '', 1),
|
||||||
|
));
|
||||||
|
$parser = new Twig_Parser(new Twig_Environment($this->getMock('Twig_LoaderInterface')));
|
||||||
|
$parser->parse($stream);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @dataProvider getFilterBodyNodesData
|
* @dataProvider getFilterBodyNodesData
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user