4 * This file is part of Twig.
9 * For the full copyright and license information, please view the LICENSE
10 * file that was distributed with this source code.
14 * Exception thrown when a syntax error occurs during lexing or parsing of a template.
16 * @author Fabien Potencier <fabien@symfony.com>
18 class Twig_Error_Syntax extends Twig_Error
21 * Tweaks the error message to include suggestions.
23 * @param string $name The original name of the item that does not exist
24 * @param array $items An array of possible items
26 public function addSuggestions($name, array $items)
28 if (!$alternatives = self::computeAlternatives($name, $items)) {
32 $this->appendMessage(sprintf(' Did you mean "%s"?', implode('", "', $alternatives)));
38 * To be merged with the addSuggestions() method in 2.0.
40 public static function computeAlternatives($name, $items)
42 $alternatives = array();
43 foreach ($items as $item) {
44 $lev = levenshtein($name, $item);
45 if ($lev <= strlen($name) / 3 || false !== strpos($item, $name)) {
46 $alternatives[$item] = $lev;
51 return array_keys($alternatives);
55 class_alias('Twig_Error_Syntax', 'Twig\Error\SyntaxError', false);