From 3a94244ede914e278ec0cd8d8987ba246bf9bf73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Tue, 24 Dec 2024 11:38:29 +0100 Subject: [PATCH] Add deprecation message for ExtensionInterface::getLastModified() method not implemented --- src/ExtensionSet.php | 15 ++++++++++----- tests/CustomExtensionTest.php | 5 +++++ tests/EnvironmentTest.php | 2 +- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/ExtensionSet.php b/src/ExtensionSet.php index 3276023de7b..cca258c6c3f 100644 --- a/src/ExtensionSet.php +++ b/src/ExtensionSet.php @@ -119,11 +119,16 @@ public function getLastModified(): int $lastModified = 0; foreach ($this->extensions as $extension) { $r = new \ReflectionObject($extension); - $lastModified = max(match(true) { - $r->hasMethod('getLastModified') => $extension->getLastModified(), - is_file($r->getFileName()) => filemtime($r->getFileName()), - default => 0, - }, $lastModified); + + if ($r->hasMethod('getLastModified')) { + $lastModified = max($extension->getLastModified(), $lastModified); + } else { + trigger_deprecation('twig/twig', '3.18', 'Not implementing "%s::getLastModified()" in "%s" is deprecated.', ExtensionInterface::class, $extension::class); + + if (is_file($r->getFileName())) { + $lastModified = max(filemtime($r->getFileName()), $lastModified); + } + } } return $this->lastModified = $lastModified; diff --git a/tests/CustomExtensionTest.php b/tests/CustomExtensionTest.php index 40be3b38293..13489c264ba 100644 --- a/tests/CustomExtensionTest.php +++ b/tests/CustomExtensionTest.php @@ -78,4 +78,9 @@ public function getOperators(): array { return $this->operators; } + + public function getLastModified(): int + { + return 0; + } } diff --git a/tests/EnvironmentTest.php b/tests/EnvironmentTest.php index f378b6b3895..7071b3417f1 100644 --- a/tests/EnvironmentTest.php +++ b/tests/EnvironmentTest.php @@ -322,7 +322,7 @@ public function testAddExtension() public function testAddMockExtension() { - $extension = $this->createMock(ExtensionInterface::class); + $extension = $this->createMock(AbstractExtension::class); $loader = new ArrayLoader(['page' => 'hey']); $twig = new Environment($loader);