From 6f4e6e7b8e71e1fa8137fa7e6113f42fffd190d2 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sat, 4 May 2024 19:46:29 +0200 Subject: [PATCH] Make Environment::getGlobals() private --- CHANGELOG | 1 + src/Environment.php | 4 +--- tests/EnvironmentTest.php | 38 +++++++++++++++++++++----------------- 3 files changed, 23 insertions(+), 20 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 929180044..c7569ba9d 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,4 @@ # 4.0.0 (2024-XX-XX) + * Make `Environment::getGlobals()` private * Drop support for PHP < 8.2 diff --git a/src/Environment.php b/src/Environment.php index be655e916..03badf951 100644 --- a/src/Environment.php +++ b/src/Environment.php @@ -792,11 +792,9 @@ class Environment } /** - * @internal - * * @return array */ - public function getGlobals(): array + private function getGlobals(): array { if ($this->extensionSet->isInitialized()) { if (null === $this->resolvedGlobals) { diff --git a/tests/EnvironmentTest.php b/tests/EnvironmentTest.php index cea37aaf3..fd3e85efa 100644 --- a/tests/EnvironmentTest.php +++ b/tests/EnvironmentTest.php @@ -62,45 +62,47 @@ class EnvironmentTest extends TestCase $loader = $this->createMock(LoaderInterface::class); $loader->expects($this->any())->method('getSourceContext')->willReturn(new Source('', '')); + $getGlobals = new \ReflectionMethod(Environment::class, 'getGlobals'); + // globals can be added after calling getGlobals $twig = new Environment($loader); $twig->addGlobal('foo', 'foo'); - $twig->getGlobals(); + $getGlobals->invoke($twig); $twig->addGlobal('foo', 'bar'); - $globals = $twig->getGlobals(); + $globals = $getGlobals->invoke($twig); $this->assertEquals('bar', $globals['foo']); // globals can be modified after a template has been loaded $twig = new Environment($loader); $twig->addGlobal('foo', 'foo'); - $twig->getGlobals(); + $getGlobals->invoke($twig); $twig->load('index'); $twig->addGlobal('foo', 'bar'); - $globals = $twig->getGlobals(); + $globals = $getGlobals->invoke($twig); $this->assertEquals('bar', $globals['foo']); // globals can be modified after extensions init $twig = new Environment($loader); $twig->addGlobal('foo', 'foo'); - $twig->getGlobals(); + $getGlobals->invoke($twig); $twig->getFunctions(); $twig->addGlobal('foo', 'bar'); - $globals = $twig->getGlobals(); + $globals = $getGlobals->invoke($twig); $this->assertEquals('bar', $globals['foo']); // globals can be modified after extensions and a template has been loaded $arrayLoader = new ArrayLoader(['index' => '{{foo}}']); $twig = new Environment($arrayLoader); $twig->addGlobal('foo', 'foo'); - $twig->getGlobals(); + $getGlobals->invoke($twig); $twig->getFunctions(); $twig->load('index'); $twig->addGlobal('foo', 'bar'); - $globals = $twig->getGlobals(); + $globals = $getGlobals->invoke($twig); $this->assertEquals('bar', $globals['foo']); $twig = new Environment($arrayLoader); - $twig->getGlobals(); + $getGlobals->invoke($twig); $twig->addGlobal('foo', 'bar'); $template = $twig->load('index'); $this->assertEquals('bar', $template->render([])); @@ -108,38 +110,38 @@ class EnvironmentTest extends TestCase // globals cannot be added after a template has been loaded $twig = new Environment($loader); $twig->addGlobal('foo', 'foo'); - $twig->getGlobals(); + $getGlobals->invoke($twig); $twig->load('index'); try { $twig->addGlobal('bar', 'bar'); $this->fail(); } catch (\LogicException $e) { - $this->assertArrayNotHasKey('bar', $twig->getGlobals()); + $this->assertArrayNotHasKey('bar', $getGlobals->invoke($twig)); } // globals cannot be added after extensions init $twig = new Environment($loader); $twig->addGlobal('foo', 'foo'); - $twig->getGlobals(); + $getGlobals->invoke($twig); $twig->getFunctions(); try { $twig->addGlobal('bar', 'bar'); $this->fail(); } catch (\LogicException $e) { - $this->assertArrayNotHasKey('bar', $twig->getGlobals()); + $this->assertArrayNotHasKey('bar', $getGlobals->invoke($twig)); } // globals cannot be added after extensions and a template has been loaded $twig = new Environment($loader); $twig->addGlobal('foo', 'foo'); - $twig->getGlobals(); + $getGlobals->invoke($twig); $twig->getFunctions(); $twig->load('index'); try { $twig->addGlobal('bar', 'bar'); $this->fail(); } catch (\LogicException $e) { - $this->assertArrayNotHasKey('bar', $twig->getGlobals()); + $this->assertArrayNotHasKey('bar', $getGlobals->invoke($twig)); } // test adding globals after a template has been loaded without call to getGlobals @@ -149,7 +151,7 @@ class EnvironmentTest extends TestCase $twig->addGlobal('bar', 'bar'); $this->fail(); } catch (\LogicException $e) { - $this->assertArrayNotHasKey('bar', $twig->getGlobals()); + $this->assertArrayNotHasKey('bar', $getGlobals->invoke($twig)); } } @@ -277,13 +279,15 @@ class EnvironmentTest extends TestCase $twig = new Environment($this->createMock(LoaderInterface::class)); $twig->addExtension(new EnvironmentTest_Extension()); + $getGlobals = new \ReflectionMethod(Environment::class, 'getGlobals'); + $this->assertArrayHasKey('test', $twig->getTokenParsers()); $this->assertArrayHasKey('foo_filter', $twig->getFilters()); $this->assertArrayHasKey('foo_function', $twig->getFunctions()); $this->assertArrayHasKey('foo_test', $twig->getTests()); $this->assertArrayHasKey('foo_unary', $twig->getUnaryOperators()); $this->assertArrayHasKey('foo_binary', $twig->getBinaryOperators()); - $this->assertArrayHasKey('foo_global', $twig->getGlobals()); + $this->assertArrayHasKey('foo_global', $getGlobals->invoke($twig)); $visitors = $twig->getNodeVisitors(); $found = false; foreach ($visitors as $visitor) {