Skip to content

Commit

Permalink
added a way to trigger deprecation notices for filters and functions
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Aug 18, 2015
1 parent 21f536b commit d16b7bf
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
20 changes: 20 additions & 0 deletions lib/Twig/ExpressionParser.php
Expand Up @@ -578,6 +578,16 @@ protected function getFunctionNodeClass($name, $line)
throw new Twig_Error_Syntax($message, $line, $this->parser->getFilename());
}

if ($function instanceof Twig_SimpleFunction && $function->isDeprecated()) {
$message = sprintf('Twig Function "%s" is deprecated', $function->getName());
if ($test->getAlternative()) {
$message .= sprintf('. Use "%s" instead', $function->getAlternative());
}
$message .= sprintf(' in %s at line %d.', $this->parser->getFilename(), $line);

@trigger_error($message, E_USER_DEPRECATED);
}

if ($function instanceof Twig_SimpleFunction) {
return $function->getNodeClass();
}
Expand All @@ -598,6 +608,16 @@ protected function getFilterNodeClass($name, $line)
throw new Twig_Error_Syntax($message, $line, $this->parser->getFilename());
}

if ($filter instanceof Twig_SimpleFilter && $filter->isDeprecated()) {
$message = sprintf('Twig Filter "%s" is deprecated', $filter->getName());
if ($test->getAlternative()) {
$message .= sprintf('. Use "%s" instead', $filter->getAlternative());
}
$message .= sprintf(' in %s at line %d.', $this->parser->getFilename(), $line);

@trigger_error($message, E_USER_DEPRECATED);
}

This comment has been minimized.

Copy link
@stof

stof Aug 31, 2015

Member

@fabpot this code has been lost in the master branch. Please re-add it. We should be able to trigger deprecation warnings in Twig 2 when marking a function as deprecated too.

This comment has been minimized.

Copy link
@fabpot

fabpot Sep 1, 2015

Author Contributor

I don't understand how this happened.

This comment has been minimized.

Copy link
@fabpot

fabpot Sep 1, 2015

Author Contributor

fixed in 90c9bc3

This comment has been minimized.

Copy link
@stof

stof Sep 1, 2015

Member

probably a bad conflict resolution when merging branches


if ($filter instanceof Twig_SimpleFilter) {
return $filter->getNodeClass();
}
Expand Down
12 changes: 12 additions & 0 deletions lib/Twig/SimpleFilter.php
Expand Up @@ -34,6 +34,8 @@ public function __construct($name, $callable, array $options = array())
'pre_escape' => null,
'preserves_safety' => null,
'node_class' => 'Twig_Node_Expression_Filter',
'deprecated' => false,
'alternative' => null,
), $options);
}

Expand Down Expand Up @@ -97,4 +99,14 @@ public function isVariadic()
{
return $this->options['is_variadic'];
}

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

public function getAlternative()
{
return $this->options['alternative'];
}
}
12 changes: 12 additions & 0 deletions lib/Twig/SimpleFunction.php
Expand Up @@ -32,6 +32,8 @@ public function __construct($name, $callable, array $options = array())
'is_safe' => null,
'is_safe_callback' => null,
'node_class' => 'Twig_Node_Expression_Function',
'deprecated' => false,
'alternative' => null,
), $options);
}

Expand Down Expand Up @@ -87,4 +89,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 d16b7bf

Please sign in to comment.