diff --git a/src/Twig.php b/src/Twig.php index 14045d6..0be8222 100644 --- a/src/Twig.php +++ b/src/Twig.php @@ -245,6 +245,9 @@ public function offsetExists($key): bool */ public function offsetGet($key) { + if (!$this->offsetExists($key)) { + return null; + } return $this->defaultVariables[$key]; } diff --git a/tests/TwigTest.php b/tests/TwigTest.php index de69fe3..963e854 100644 --- a/tests/TwigTest.php +++ b/tests/TwigTest.php @@ -332,6 +332,13 @@ public function testOffsetGet() $this->assertEquals('bar', $view->offsetGet('foo')); } + public function testOffsetGetUndefined() + { + $loader = $this->createMock(LoaderInterface::class); + $view = new Twig($loader); + $this->assertNull($view->offsetGet('foo')); + } + public function testOffsetUnset() { $loader = $this->createMock(LoaderInterface::class);