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

Help the developer when they specify an invalid filter by providing some... #549

Merged
merged 2 commits into from Dec 7, 2011
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 16 additions & 1 deletion lib/Twig/Node/Expression/Filter.php
Expand Up @@ -19,8 +19,23 @@ public function __construct(Twig_NodeInterface $node, Twig_Node_Expression_Const
public function compile(Twig_Compiler $compiler)
{
$name = $this->getNode('filter')->getAttribute('value');

if (false === $filter = $compiler->getEnvironment()->getFilter($name)) {
throw new Twig_Error_Syntax(sprintf('The filter "%s" does not exist', $name), $this->getLine());
$alternativeFilters = array();

foreach ($compiler->getEnvironment()->getFilters() as $filterName => $filter) {
if (false !== strpos($filterName, $name)) {
$alternativeFilters[] = $filterName;
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may want to use levenshtein too:

<?php
foreach ($compiler->getEnvironment()->getFilters() as $filterName => $filter) {
    $diffs = levenshtein($filterName, $name);
    if ($diffs <= strlen($name)/3) {
         $alternativeFilters[$filterName] = $diffs;
    }
    if (false !== strpos($filterName, $name)) {
        $alternativeFilters[$filterName] = $diffs;
    }
}
asort($alternativeFilters);
$alternativeFilters = array_keys($alternativeFilters);

(untested)

This will suggest filters whose name is less than 1/3 different from the entered name; and will sort alternative filters by the number of differences.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

levenshtein is indeed what we need here. That's what I'm using for the Twig website when you search for an unknown filter/tag/function and it works very well.

@jonathaningram: Can you update your PRs?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fabpot I will. Any chance of posting the levenshtein code snippet from the Twig website and I'll update the PR directly? No need to reinvent the wheel.


$exceptionMessage = sprintf('The filter "%s" does not exist', $name);

if (count($alternativeFilters)) {
$exceptionMessage = sprintf('%s. Did you mean "%s"?', $exceptionMessage, implode('", "', $alternativeFilters));
}

throw new Twig_Error_Syntax($exceptionMessage, $this->getLine());
}

$this->compileFilter($compiler, $filter);
Expand Down