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

Twig2 PHP 8.1 beta compatibility #3552

Merged
merged 1 commit into from
Jul 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Markup.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public function __toString()
/**
* @return int
*/
#[\ReturnTypeWillChange]
public function count()
{
return mb_strlen($this->content, $this->charset);
Expand Down
2 changes: 2 additions & 0 deletions src/Node/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ public function removeNode($name)
/**
* @return int
*/
#[\ReturnTypeWillChange]
public function count()
{
return \count($this->nodes);
Expand All @@ -169,6 +170,7 @@ public function count()
/**
* @return \Traversable
*/
#[\ReturnTypeWillChange]
public function getIterator()
{
return new \ArrayIterator($this->nodes);
Expand Down
1 change: 1 addition & 0 deletions src/Profiler/Profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ public function reset()
$this->enter();
}

#[\ReturnTypeWillChange]
public function getIterator(): \Traversable
{
return new \ArrayIterator($this->profiles);
Expand Down
12 changes: 7 additions & 5 deletions tests/Extension/CoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ public function __construct(array $array, array $keys, $allowAccess = false, $ma
$this->iterator = new CoreTestIterator($array, $keys, $allowAccess, $maxPosition);
}

public function getIterator()
public function getIterator(): \Traversable
{
return $this->iterator;
}
Expand All @@ -277,7 +277,7 @@ public function __construct(array $array, array $keys, $allowValueAccess = false
$this->iterator = new CoreTestIteratorAggregate($array, $keys, $allowValueAccess, $maxPosition);
}

public function getIterator()
public function getIterator(): \Traversable
{
return $this->iterator;
}
Expand All @@ -300,11 +300,12 @@ public function __construct(array $values, array $keys, $allowValueAccess = fals
$this->maxPosition = false === $maxPosition ? \count($values) + 1 : $maxPosition;
}

public function rewind()
public function rewind(): void
{
$this->position = 0;
}

#[\ReturnTypeWillChange]
public function current()
{
if ($this->allowValueAccess) {
Expand All @@ -314,20 +315,21 @@ public function current()
throw new \LogicException('Code should only use the keys, not the values provided by iterator.');
}

#[\ReturnTypeWillChange]
public function key()
{
return $this->arrayKeys[$this->position];
}

public function next()
public function next(): void
{
++$this->position;
if ($this->position === $this->maxPosition) {
throw new \LogicException(sprintf('Code should not iterate beyond %d.', $this->maxPosition));
}
}

public function valid()
public function valid(): bool
{
return isset($this->arrayKeys[$this->position]);
}
Expand Down
8 changes: 5 additions & 3 deletions tests/Fixtures/tags/for/objects.test
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
class ItemsIterator implements \Iterator
{
protected $values = ['foo' => 'bar', 'bar' => 'foo'];
#[\ReturnTypeWillChange]
public function current() { return current($this->values); }
#[\ReturnTypeWillChange]
public function key() { return key($this->values); }
public function next() { return next($this->values); }
public function rewind() { return reset($this->values); }
public function valid() { return false !== current($this->values); }
public function next(): void { next($this->values); }
public function rewind(): void { reset($this->values); }
public function valid(): bool { return false !== current($this->values); }
}
return ['items' => new ItemsIterator()]
--EXPECT--
Expand Down
10 changes: 6 additions & 4 deletions tests/Fixtures/tags/for/objects_countable.test
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@
class ItemsIteratorCountable implements \Iterator, \Countable
{
protected $values = ['foo' => 'bar', 'bar' => 'foo'];
#[\ReturnTypeWillChange]
public function current() { return current($this->values); }
#[\ReturnTypeWillChange]
public function key() { return key($this->values); }
public function next() { return next($this->values); }
public function rewind() { return reset($this->values); }
public function valid() { return false !== current($this->values); }
public function count() { return count($this->values); }
public function next(): void { next($this->values); }
public function rewind(): void { reset($this->values); }
public function valid(): bool { return false !== current($this->values); }
public function count(): int { return count($this->values); }
}
return ['items' => new ItemsIteratorCountable()]
--EXPECT--
Expand Down
2 changes: 1 addition & 1 deletion tests/Fixtures/tests/empty.test
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ return [
'value_null' => null, 'value_false' => false, 'value_int_zero' => 0,
'array_empty' => [], 'array_not_empty' => [1, 2],
'magically_callable' => new \Twig\Tests\MagicCallStub(),
'countable_empty' => new \Twig\Tests\CountableStub([]), 'countable_not_empty' => new \Twig\Tests\CountableStub([1, 2]),
'countable_empty' => new \Twig\Tests\CountableStub(0), 'countable_not_empty' => new \Twig\Tests\CountableStub(2),
'tostring_empty' => new \Twig\Tests\ToStringStub(''), 'tostring_not_empty' => new \Twig\Tests\ToStringStub('0' /* edge case of using "0" as the string */),
'markup_empty' => new \Twig\Markup('', 'UTF-8'), 'markup_not_empty' => new \Twig\Markup('test', 'UTF-8'),
'iterator' => $iter = new \ArrayIterator(['bar', 'foo']),
Expand Down
20 changes: 12 additions & 8 deletions tests/IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,27 +99,29 @@ public function strToLower($value)
return strtolower($value);
}

public function rewind()
public function rewind(): void
{
$this->position = 0;
}

#[\ReturnTypeWillChange]
public function current()
{
return $this->array[$this->position];
}

#[\ReturnTypeWillChange]
public function key()
{
return 'a';
}

public function next()
public function next(): void
{
++$this->position;
}

public function valid()
public function valid(): bool
{
return isset($this->array[$this->position]);
}
Expand Down Expand Up @@ -327,7 +329,7 @@ public function __construct($count)
$this->count = $count;
}

public function count()
public function count(): int
{
return $this->count;
}
Expand All @@ -350,7 +352,7 @@ public function __construct(array $data)
$this->data = $data;
}

public function getIterator()
public function getIterator(): \Traversable
{
return new \ArrayIterator($this->data);
}
Expand All @@ -361,27 +363,29 @@ class SimpleIteratorForTesting implements \Iterator
private $data = [1, 2, 3, 4, 5, 6, 7];
private $key = 0;

#[\ReturnTypeWillChange]
public function current()
{
return $this->key;
}

public function next()
public function next(): void
{
++$this->key;
}

#[\ReturnTypeWillChange]
public function key()
{
return $this->key;
}

public function valid()
public function valid(): bool
{
return isset($this->data[$this->key]);
}

public function rewind()
public function rewind(): void
{
$this->key = 0;
}
Expand Down
23 changes: 13 additions & 10 deletions tests/TemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -471,21 +471,22 @@ class TemplateArrayAccessObject implements \ArrayAccess
'+4' => '+4',
];

public function offsetExists($name)
public function offsetExists($name) : bool
{
return \array_key_exists($name, $this->attributes);
}

#[\ReturnTypeWillChange]
public function offsetGet($name)
{
return \array_key_exists($name, $this->attributes) ? $this->attributes[$name] : null;
}

public function offsetSet($name, $value)
public function offsetSet($name, $value) : void
{
}

public function offsetUnset($name)
public function offsetUnset($name) : void
{
}
}
Expand Down Expand Up @@ -542,7 +543,7 @@ class TemplatePropertyObject

class TemplatePropertyObjectAndIterator extends TemplatePropertyObject implements \IteratorAggregate
{
public function getIterator()
public function getIterator(): \Traversable
{
return new \ArrayIterator(['foo', 'bar']);
}
Expand All @@ -560,21 +561,22 @@ class TemplatePropertyObjectAndArrayAccess extends TemplatePropertyObject implem
'baf' => 'baf',
];

public function offsetExists($offset)
public function offsetExists($offset) : bool
{
return \array_key_exists($offset, $this->data);
}

#[\ReturnTypeWillChange]
public function offsetGet($offset)
{
return $this->offsetExists($offset) ? $this->data[$offset] : 'n/a';
}

public function offsetSet($offset, $value)
public function offsetSet($offset, $value) : void
{
}

public function offsetUnset($offset)
public function offsetUnset($offset) : void
{
}
}
Expand Down Expand Up @@ -708,22 +710,23 @@ class TemplateArrayAccess implements \ArrayAccess
];
private $children = [];

public function offsetExists($offset)
public function offsetExists($offset) : bool
{
return \array_key_exists($offset, $this->children);
}

#[\ReturnTypeWillChange]
public function offsetGet($offset)
{
return $this->children[$offset];
}

public function offsetSet($offset, $value)
public function offsetSet($offset, $value) : void
{
$this->children[$offset] = $value;
}

public function offsetUnset($offset)
public function offsetUnset($offset) : void
{
unset($this->children[$offset]);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Util/DeprecationCollectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function deprec()

class Twig_Tests_Util_Iterator implements \IteratorAggregate
{
public function getIterator()
public function getIterator(): \Traversable
{
return new \ArrayIterator([
'ok.twig' => '{{ foo }}',
Expand Down