Skip to content

Commit

Permalink
renamed Twig_Node::getLine() to Twig_Node::getTemplateLine()
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Oct 19, 2016
1 parent e231aa5 commit 39d94df
Show file tree
Hide file tree
Showing 26 changed files with 59 additions and 44 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
@@ -1,5 +1,6 @@
* 1.27.0 (2016-XX-XX)

* deprecated Twig_Node::getLine() in favor of Twig_Node::getTemplateLine()
* deprecated Twig_Template::getSource() in favor of Twig_Template::getSourceContext()
* deprecated Twig_Node::getFilename() in favor of Twig_Node::getTemplateName()
* deprecated the "filename" escaping strategy (use "name" instead)
Expand Down
5 changes: 3 additions & 2 deletions doc/deprecated.rst
Expand Up @@ -122,8 +122,9 @@ Nodes
* As of Twig 1.27, the ``filename`` attribute on ``Twig_Node_Module`` is
deprecated. Use ``getName()`` instead.

* As of Twig 1.27, the ``Twig_Node::getFilename()`` method is deprecated, use
``Twig_Node::getName()`` instead.
* As of Twig 1.27, the ``Twig_Node::getFilename()/Twig_Node::getLine()``
methods are deprecated, use
``Twig_Node::getTemplateName()/Twig_Node::getTemplateLine()`` instead.

Interfaces
----------
Expand Down
8 changes: 4 additions & 4 deletions lib/Twig/Compiler.php
Expand Up @@ -213,8 +213,8 @@ public function repr($value)
*/
public function addDebugInfo(Twig_NodeInterface $node)
{
if ($node->getLine() != $this->lastLine) {
$this->write(sprintf("// line %d\n", $node->getLine()));
if ($node->getTemplateLine() != $this->lastLine) {
$this->write(sprintf("// line %d\n", $node->getTemplateLine()));

// when mbstring.func_overload is set to 2
// mb_substr_count() replaces substr_count()
Expand All @@ -226,9 +226,9 @@ public function addDebugInfo(Twig_NodeInterface $node)
$this->sourceLine += substr_count($this->source, "\n", $this->sourceOffset);
}
$this->sourceOffset = strlen($this->source);
$this->debugInfo[$this->sourceLine] = $node->getLine();
$this->debugInfo[$this->sourceLine] = $node->getTemplateLine();

$this->lastLine = $node->getLine();
$this->lastLine = $node->getTemplateLine();
}

return $this;
Expand Down
2 changes: 1 addition & 1 deletion lib/Twig/ExpressionParser.php
Expand Up @@ -216,7 +216,7 @@ public function parseStringExpression()

$expr = array_shift($nodes);
foreach ($nodes as $node) {
$expr = new Twig_Node_Expression_Binary_Concat($expr, $node, $node->getLine());
$expr = new Twig_Node_Expression_Binary_Concat($expr, $node, $node->getTemplateLine());
}

return $expr;
Expand Down
2 changes: 1 addition & 1 deletion lib/Twig/Extension/Core.php
Expand Up @@ -274,7 +274,7 @@ public function parseNotTestExpression(Twig_Parser $parser, Twig_NodeInterface $
public function parseTestExpression(Twig_Parser $parser, Twig_NodeInterface $node)
{
$stream = $parser->getStream();
list($name, $test) = $this->getTest($parser, $node->getLine());
list($name, $test) = $this->getTest($parser, $node->getTemplateLine());

if ($test instanceof Twig_SimpleTest && $test->isDeprecated()) {
$message = sprintf('Twig Test "%s" is deprecated', $name);
Expand Down
10 changes: 10 additions & 0 deletions lib/Twig/Node.php
Expand Up @@ -118,8 +118,18 @@ public function compile(Twig_Compiler $compiler)
}
}

public function getTemplateLine()
{
return $this->lineno;
}

/**
* @deprecated since 1.27 (to be removed in 2.0)
*/
public function getLine()
{
@trigger_error('The '.__METHOD__.' method is deprecated since version 1.27 and will be removed in 2.0. Use getTemplateName() instead.', E_USER_DEPRECATED);

return $this->lineno;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Twig/Node/CheckSecurity.php
Expand Up @@ -33,7 +33,7 @@ public function compile(Twig_Compiler $compiler)
foreach (array('tags', 'filters', 'functions') as $type) {
foreach ($this->{'used'.ucfirst($type)} as $name => $node) {
if ($node instanceof Twig_Node) {
${$type}[$name] = $node->getLine();
${$type}[$name] = $node->getTemplateLine();
} else {
${$type}[$node] = null;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Twig/Node/Embed.php
Expand Up @@ -35,7 +35,7 @@ protected function addGetTemplate(Twig_Compiler $compiler)
->raw(', ')
->repr($this->getTemplateName())
->raw(', ')
->repr($this->getLine())
->repr($this->getTemplateLine())
->raw(', ')
->string($this->getAttribute('index'))
->raw(')')
Expand Down
2 changes: 1 addition & 1 deletion lib/Twig/Node/Expression/Array.php
Expand Up @@ -54,7 +54,7 @@ public function hasElement(Twig_Node_Expression $key)
public function addElement(Twig_Node_Expression $value, Twig_Node_Expression $key = null)
{
if (null === $key) {
$key = new Twig_Node_Expression_Constant(++$this->index, $value->getLine());
$key = new Twig_Node_Expression_Constant(++$this->index, $value->getTemplateLine());
}

array_push($this->nodes, $key, $value);
Expand Down
2 changes: 1 addition & 1 deletion lib/Twig/Node/Expression/Call.php
Expand Up @@ -205,7 +205,7 @@ protected function getArguments($callable, $arguments)
throw new Twig_Error_Syntax(sprintf(
'Unknown argument%s "%s" for %s "%s(%s)".',
count($parameters) > 1 ? 's' : '', implode('", "', array_keys($parameters)), $callType, $callName, implode(', ', $names)
), $unknownParameter ? $unknownParameter->getLine() : -1);
), $unknownParameter ? $unknownParameter->getTemplateLine() : -1);
}

return $arguments;
Expand Down
8 changes: 4 additions & 4 deletions lib/Twig/Node/Expression/Filter/Default.php
Expand Up @@ -22,13 +22,13 @@ class Twig_Node_Expression_Filter_Default extends Twig_Node_Expression_Filter
{
public function __construct(Twig_NodeInterface $node, Twig_Node_Expression_Constant $filterName, Twig_NodeInterface $arguments, $lineno, $tag = null)
{
$default = new Twig_Node_Expression_Filter($node, new Twig_Node_Expression_Constant('default', $node->getLine()), $arguments, $node->getLine());
$default = new Twig_Node_Expression_Filter($node, new Twig_Node_Expression_Constant('default', $node->getTemplateLine()), $arguments, $node->getTemplateLine());

if ('default' === $filterName->getAttribute('value') && ($node instanceof Twig_Node_Expression_Name || $node instanceof Twig_Node_Expression_GetAttr)) {
$test = new Twig_Node_Expression_Test_Defined(clone $node, 'defined', new Twig_Node(), $node->getLine());
$false = count($arguments) ? $arguments->getNode(0) : new Twig_Node_Expression_Constant('', $node->getLine());
$test = new Twig_Node_Expression_Test_Defined(clone $node, 'defined', new Twig_Node(), $node->getTemplateLine());
$false = count($arguments) ? $arguments->getNode(0) : new Twig_Node_Expression_Constant('', $node->getTemplateLine());

$node = new Twig_Node_Expression_Conditional($test, $default, $false, $node->getLine());
$node = new Twig_Node_Expression_Conditional($test, $default, $false, $node->getTemplateLine());
} else {
$node = $default;
}
Expand Down
6 changes: 3 additions & 3 deletions lib/Twig/Node/Expression/NullCoalesce.php
Expand Up @@ -13,9 +13,9 @@ class Twig_Node_Expression_NullCoalesce extends Twig_Node_Expression_Conditional
public function __construct(Twig_NodeInterface $left, Twig_NodeInterface $right, $lineno)
{
$test = new Twig_Node_Expression_Binary_And(
new Twig_Node_Expression_Test_Defined(clone $left, 'defined', new Twig_Node(), $left->getLine()),
new Twig_Node_Expression_Unary_Not(new Twig_Node_Expression_Test_Null($left, 'null', new Twig_Node(), $left->getLine()), $left->getLine()),
$left->getLine()
new Twig_Node_Expression_Test_Defined(clone $left, 'defined', new Twig_Node(), $left->getTemplateLine()),
new Twig_Node_Expression_Unary_Not(new Twig_Node_Expression_Test_Null($left, 'null', new Twig_Node(), $left->getTemplateLine()), $left->getTemplateLine()),
$left->getTemplateLine()
);

parent::__construct($test, $left, $right, $lineno);
Expand Down
4 changes: 2 additions & 2 deletions lib/Twig/Node/Expression/Test/Defined.php
Expand Up @@ -32,9 +32,9 @@ public function __construct(Twig_NodeInterface $node, $name, Twig_NodeInterface

$this->changeIgnoreStrictCheck($node);
} elseif ($node instanceof Twig_Node_Expression_Constant || $node instanceof Twig_Node_Expression_Array) {
$node = new Twig_Node_Expression_Constant(true, $node->getLine());
$node = new Twig_Node_Expression_Constant(true, $node->getTemplateLine());
} else {
throw new Twig_Error_Syntax('The "defined" test only works with simple variables.', $this->getLine());
throw new Twig_Error_Syntax('The "defined" test only works with simple variables.', $this->getTemplateLine());
}

parent::__construct($node, $name, $arguments, $lineno);
Expand Down
2 changes: 1 addition & 1 deletion lib/Twig/Node/Import.php
Expand Up @@ -39,7 +39,7 @@ public function compile(Twig_Compiler $compiler)
->raw(', ')
->repr($this->getTemplateName())
->raw(', ')
->repr($this->getLine())
->repr($this->getTemplateLine())
->raw(')')
;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Twig/Node/Include.php
Expand Up @@ -66,7 +66,7 @@ protected function addGetTemplate(Twig_Compiler $compiler)
->raw(', ')
->repr($this->getTemplateName())
->raw(', ')
->repr($this->getLine())
->repr($this->getTemplateLine())
->raw(')')
;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Twig/Node/Macro.php
Expand Up @@ -22,7 +22,7 @@ public function __construct($name, Twig_NodeInterface $body, Twig_NodeInterface
{
foreach ($arguments as $argumentName => $argument) {
if (self::VARARGS_NAME === $argumentName) {
throw new Twig_Error_Syntax(sprintf('The argument "%s" in macro "%s" cannot be defined because the variable "%s" is reserved for arbitrary arguments.', self::VARARGS_NAME, $name, self::VARARGS_NAME), $argument->getLine());
throw new Twig_Error_Syntax(sprintf('The argument "%s" in macro "%s" cannot be defined because the variable "%s" is reserved for arbitrary arguments.', self::VARARGS_NAME, $name, self::VARARGS_NAME), $argument->getTemplateLine());
}
}

Expand Down
6 changes: 3 additions & 3 deletions lib/Twig/Node/Module.php
Expand Up @@ -138,7 +138,7 @@ protected function compileGetParent(Twig_Compiler $compiler)
->raw(', ')
->repr($this->source->getName())
->raw(', ')
->repr($parent->getLine())
->repr($parent->getTemplateLine())
->raw(')')
;
}
Expand Down Expand Up @@ -183,7 +183,7 @@ protected function compileConstructor(Twig_Compiler $compiler)
->raw(', ')
->repr($this->source->getName())
->raw(', ')
->repr($parent->getLine())
->repr($parent->getTemplateLine())
->raw(");\n")
;
}
Expand Down Expand Up @@ -449,7 +449,7 @@ protected function compileLoadTemplate(Twig_Compiler $compiler, $node, $var)
->raw(', ')
->repr($node->getTemplateName())
->raw(', ')
->repr($node->getLine())
->repr($node->getTemplateLine())
->raw(");\n")
;
} else {
Expand Down
2 changes: 1 addition & 1 deletion lib/Twig/Node/Set.php
Expand Up @@ -30,7 +30,7 @@ public function __construct($capture, Twig_NodeInterface $names, Twig_NodeInterf

$values = $this->getNode('values');
if ($values instanceof Twig_Node_Text) {
$this->setNode('values', new Twig_Node_Expression_Constant($values->getAttribute('data'), $values->getLine()));
$this->setNode('values', new Twig_Node_Expression_Constant($values->getAttribute('data'), $values->getTemplateLine()));
$this->setAttribute('capture', false);
}
}
Expand Down
3 changes: 3 additions & 0 deletions lib/Twig/NodeInterface.php
Expand Up @@ -25,6 +25,9 @@ interface Twig_NodeInterface extends Countable, IteratorAggregate
*/
public function compile(Twig_Compiler $compiler);

/**
* @deprecated since 1.27 (to be removed in 2.0)
*/
public function getLine();

public function getNodeTag();
Expand Down
4 changes: 2 additions & 2 deletions lib/Twig/NodeVisitor/Escaper.php
Expand Up @@ -90,7 +90,7 @@ protected function escapePrintNode(Twig_Node_Print $node, Twig_Environment $env,

return new $class(
$this->getEscaperFilter($type, $expression),
$node->getLine()
$node->getTemplateLine()
);
}

Expand Down Expand Up @@ -142,7 +142,7 @@ protected function needEscaping(Twig_Environment $env)

protected function getEscaperFilter($type, Twig_NodeInterface $node)
{
$line = $node->getLine();
$line = $node->getTemplateLine();
$name = new Twig_Node_Expression_Constant('escape', $line);
$args = new Twig_Node(array(new Twig_Node_Expression_Constant((string) $type, $line), new Twig_Node_Expression_Constant(null, $line), new Twig_Node_Expression_Constant(true, $line)));

Expand Down
4 changes: 2 additions & 2 deletions lib/Twig/NodeVisitor/Optimizer.php
Expand Up @@ -97,7 +97,7 @@ protected function doLeaveNode(Twig_Node $node, Twig_Environment $env)
if (!$expression && get_class($node) !== 'Twig_Node' && $prependedNodes = array_shift($this->prependedNodes)) {
$nodes = array();
foreach (array_unique($prependedNodes) as $name) {
$nodes[] = new Twig_Node_SetTemp($name, $node->getLine());
$nodes[] = new Twig_Node_SetTemp($name, $node->getTemplateLine());
}

$nodes[] = $node;
Expand All @@ -114,7 +114,7 @@ protected function optimizeVariables(Twig_NodeInterface $node, Twig_Environment
if ('Twig_Node_Expression_Name' === get_class($node) && $node->isSimple()) {
$this->prependedNodes[0][] = $node->getAttribute('name');

return new Twig_Node_Expression_TempName($node->getAttribute('name'), $node->getLine());
return new Twig_Node_Expression_TempName($node->getAttribute('name'), $node->getTemplateLine());
}

return $node;
Expand Down
2 changes: 1 addition & 1 deletion lib/Twig/NodeVisitor/Sandbox.php
Expand Up @@ -51,7 +51,7 @@ protected function doEnterNode(Twig_Node $node, Twig_Environment $env)

// wrap print to check __toString() calls
if ($node instanceof Twig_Node_Print) {
return new Twig_Node_SandboxedPrint($node->getNode('expr'), $node->getLine(), $node->getNodeTag());
return new Twig_Node_SandboxedPrint($node->getNode('expr'), $node->getTemplateLine(), $node->getNodeTag());
}
}

Expand Down
8 changes: 4 additions & 4 deletions lib/Twig/Parser.php
Expand Up @@ -249,7 +249,7 @@ public function getBlock($name)

public function setBlock($name, Twig_Node_Block $value)
{
$this->blocks[$name] = new Twig_Node_Body(array($value), array(), $value->getLine());
$this->blocks[$name] = new Twig_Node_Body(array($value), array(), $value->getTemplateLine());
}

public function hasMacro($name)
Expand All @@ -260,7 +260,7 @@ public function hasMacro($name)
public function setMacro($name, Twig_Node_Macro $node)
{
if ($this->isReservedMacroName($name)) {
throw new Twig_Error_Syntax(sprintf('"%s" cannot be used as a macro name as it is a reserved keyword.', $name), $node->getLine(), $this->stream->getSourceContext()->getName());
throw new Twig_Error_Syntax(sprintf('"%s" cannot be used as a macro name as it is a reserved keyword.', $name), $node->getTemplateLine(), $this->stream->getSourceContext()->getName());
}

$this->macros[$name] = $node;
Expand Down Expand Up @@ -378,10 +378,10 @@ protected function filterBodyNodes(Twig_NodeInterface $node)
(!$node instanceof Twig_Node_Text && !$node instanceof Twig_Node_BlockReference && $node instanceof Twig_NodeOutputInterface)
) {
if (false !== strpos((string) $node, chr(0xEF).chr(0xBB).chr(0xBF))) {
throw new Twig_Error_Syntax('A template that extends another one cannot start with a byte order mark (BOM); it must be removed.', $node->getLine(), $this->stream->getSourceContext()->getName());
throw new Twig_Error_Syntax('A template that extends another one cannot start with a byte order mark (BOM); it must be removed.', $node->getTemplateLine(), $this->stream->getSourceContext()->getName());
}

throw new Twig_Error_Syntax('A template that extends another one cannot include contents outside Twig blocks. Did you forget to put the contents inside a {% block %} tag?', $node->getLine(), $this->stream->getSourceContext()->getName());
throw new Twig_Error_Syntax('A template that extends another one cannot include contents outside Twig blocks. Did you forget to put the contents inside a {% block %} tag?', $node->getTemplateLine(), $this->stream->getSourceContext()->getName());
}

// bypass "set" nodes as they "capture" the output
Expand Down
2 changes: 1 addition & 1 deletion lib/Twig/TokenParser/Block.php
Expand Up @@ -28,7 +28,7 @@ public function parse(Twig_Token $token)
$stream = $this->parser->getStream();
$name = $stream->expect(Twig_Token::NAME_TYPE)->getValue();
if ($this->parser->hasBlock($name)) {
throw new Twig_Error_Syntax(sprintf("The block '%s' has already been defined line %d.", $name, $this->parser->getBlock($name)->getLine()), $stream->getCurrent()->getLine(), $stream->getSourceContext()->getName());
throw new Twig_Error_Syntax(sprintf("The block '%s' has already been defined line %d.", $name, $this->parser->getBlock($name)->getTemplateLine()), $stream->getCurrent()->getLine(), $stream->getSourceContext()->getName());
}
$this->parser->setBlock($name, $block = new Twig_Node_Block($name, new Twig_Node(array()), $lineno));
$this->parser->pushLocalScope();
Expand Down
10 changes: 5 additions & 5 deletions lib/Twig/TokenParser/For.php
Expand Up @@ -48,13 +48,13 @@ public function parse(Twig_Token $token)

if (count($targets) > 1) {
$keyTarget = $targets->getNode(0);
$keyTarget = new Twig_Node_Expression_AssignName($keyTarget->getAttribute('name'), $keyTarget->getLine());
$keyTarget = new Twig_Node_Expression_AssignName($keyTarget->getAttribute('name'), $keyTarget->getTemplateLine());
$valueTarget = $targets->getNode(1);
$valueTarget = new Twig_Node_Expression_AssignName($valueTarget->getAttribute('name'), $valueTarget->getLine());
$valueTarget = new Twig_Node_Expression_AssignName($valueTarget->getAttribute('name'), $valueTarget->getTemplateLine());
} else {
$keyTarget = new Twig_Node_Expression_AssignName('_key', $lineno);
$valueTarget = $targets->getNode(0);
$valueTarget = new Twig_Node_Expression_AssignName($valueTarget->getAttribute('name'), $valueTarget->getLine());
$valueTarget = new Twig_Node_Expression_AssignName($valueTarget->getAttribute('name'), $valueTarget->getTemplateLine());
}

if ($ifexpr) {
Expand All @@ -79,7 +79,7 @@ public function decideForEnd(Twig_Token $token)
protected function checkLoopUsageCondition(Twig_TokenStream $stream, Twig_NodeInterface $node)
{
if ($node instanceof Twig_Node_Expression_GetAttr && $node->getNode('node') instanceof Twig_Node_Expression_Name && 'loop' == $node->getNode('node')->getAttribute('name')) {
throw new Twig_Error_Syntax('The "loop" variable cannot be used in a looping condition.', $node->getLine(), $stream->getSourceContext()->getName());
throw new Twig_Error_Syntax('The "loop" variable cannot be used in a looping condition.', $node->getTemplateLine(), $stream->getSourceContext()->getName());
}

foreach ($node as $n) {
Expand All @@ -98,7 +98,7 @@ protected function checkLoopUsageBody(Twig_TokenStream $stream, Twig_NodeInterfa
if ($node instanceof Twig_Node_Expression_GetAttr && $node->getNode('node') instanceof Twig_Node_Expression_Name && 'loop' == $node->getNode('node')->getAttribute('name')) {
$attribute = $node->getNode('attribute');
if ($attribute instanceof Twig_Node_Expression_Constant && in_array($attribute->getAttribute('value'), array('length', 'revindex0', 'revindex', 'last'))) {
throw new Twig_Error_Syntax(sprintf('The "loop.%s" variable is not defined when looping with a condition.', $attribute->getAttribute('value')), $node->getLine(), $stream->getSourceContext()->getName());
throw new Twig_Error_Syntax(sprintf('The "loop.%s" variable is not defined when looping with a condition.', $attribute->getAttribute('value')), $node->getTemplateLine(), $stream->getSourceContext()->getName());
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Twig/TokenParser/Sandbox.php
Expand Up @@ -37,7 +37,7 @@ public function parse(Twig_Token $token)
}

if (!$node instanceof Twig_Node_Include) {
throw new Twig_Error_Syntax('Only "include" tags are allowed within a "sandbox" section.', $node->getLine(), $stream->getSourceContext()->getName());
throw new Twig_Error_Syntax('Only "include" tags are allowed within a "sandbox" section.', $node->getTemplateLine(), $stream->getSourceContext()->getName());
}
}
}
Expand Down

0 comments on commit 39d94df

Please sign in to comment.