From 577c052aff605697cd173967751828a181cca4ad Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Fri, 29 Nov 2019 19:38:28 +0100 Subject: [PATCH] Twig update to 3.0.0 and Renderer type hinting --- composer.json | 2 +- src/Controllers/Metrics/MetricsEngine.php | 2 +- src/Renderer/Engine.php | 2 +- src/Renderer/EngineInterface.php | 2 +- src/Renderer/Renderer.php | 4 ++-- src/Renderer/Twig/Extensions/Assets.php | 6 +++--- src/Renderer/Twig/Extensions/Authentication.php | 6 +++--- src/Renderer/Twig/Extensions/Config.php | 2 +- src/Renderer/Twig/Extensions/Csrf.php | 6 +++--- src/Renderer/Twig/Extensions/Globals.php | 2 +- src/Renderer/Twig/Extensions/Legacy.php | 4 ++-- src/Renderer/Twig/Extensions/Markdown.php | 2 +- src/Renderer/Twig/Extensions/Session.php | 2 +- src/Renderer/Twig/Extensions/Translation.php | 4 ++-- src/Renderer/Twig/Extensions/Url.php | 6 +++--- src/Renderer/TwigLoader.php | 4 ++-- src/Renderer/TwigServiceProvider.php | 2 +- tests/Unit/Renderer/TwigServiceProviderTest.php | 13 ++++++++++--- 18 files changed, 39 insertions(+), 32 deletions(-) diff --git a/composer.json b/composer.json index 2beeeb68..4bd7ae40 100644 --- a/composer.json +++ b/composer.json @@ -40,7 +40,7 @@ "swiftmailer/swiftmailer": "^6.2", "symfony/http-foundation": "^4.3", "symfony/psr-http-message-bridge": "^1.2", - "twig/twig": "^2.11", + "twig/twig": "^3.0", "vlucas/phpdotenv": "^3.3" }, "require-dev": { diff --git a/src/Controllers/Metrics/MetricsEngine.php b/src/Controllers/Metrics/MetricsEngine.php index 8301e388..375bb096 100644 --- a/src/Controllers/Metrics/MetricsEngine.php +++ b/src/Controllers/Metrics/MetricsEngine.php @@ -142,7 +142,7 @@ class MetricsEngine implements EngineInterface * @param string|mixed[] $key * @param mixed $value */ - public function share($key, $value = null) + public function share($key, $value = null): void { } } diff --git a/src/Renderer/Engine.php b/src/Renderer/Engine.php index 60f1d686..e2ff23f2 100644 --- a/src/Renderer/Engine.php +++ b/src/Renderer/Engine.php @@ -11,7 +11,7 @@ abstract class Engine implements EngineInterface * @param mixed[]|string $key * @param null $value */ - public function share($key, $value = null) + public function share($key, $value = null): void { if (!is_array($key)) { $key = [$key => $value]; diff --git a/src/Renderer/EngineInterface.php b/src/Renderer/EngineInterface.php index 3bce9c02..dd713029 100644 --- a/src/Renderer/EngineInterface.php +++ b/src/Renderer/EngineInterface.php @@ -23,5 +23,5 @@ interface EngineInterface * @param string|mixed[] $key * @param mixed $value */ - public function share($key, $value = null); + public function share($key, $value = null): void; } diff --git a/src/Renderer/Renderer.php b/src/Renderer/Renderer.php index de31ca74..ccda6881 100644 --- a/src/Renderer/Renderer.php +++ b/src/Renderer/Renderer.php @@ -18,7 +18,7 @@ class Renderer * @param mixed[] $data * @return string */ - public function render($template, $data = []) + public function render(string $template, array $data = []): string { foreach ($this->renderer as $renderer) { if (!$renderer->canRender($template)) { @@ -40,7 +40,7 @@ class Renderer * * @param EngineInterface $renderer */ - public function addRenderer(EngineInterface $renderer) + public function addRenderer(EngineInterface $renderer): void { $this->renderer[] = $renderer; } diff --git a/src/Renderer/Twig/Extensions/Assets.php b/src/Renderer/Twig/Extensions/Assets.php index b77e4755..04d6c9c8 100644 --- a/src/Renderer/Twig/Extensions/Assets.php +++ b/src/Renderer/Twig/Extensions/Assets.php @@ -22,7 +22,7 @@ class Assets extends TwigExtension /** * @return TwigFunction[] */ - public function getFunctions() + public function getFunctions(): array { return [ new TwigFunction('asset', [$this, 'getAsset']), @@ -31,9 +31,9 @@ class Assets extends TwigExtension /** * @param string $path - * @return UrlGenerator|string + * @return string */ - public function getAsset($path) + public function getAsset(string $path): string { $path = ltrim($path, '/'); diff --git a/src/Renderer/Twig/Extensions/Authentication.php b/src/Renderer/Twig/Extensions/Authentication.php index 33268ee6..061a9018 100644 --- a/src/Renderer/Twig/Extensions/Authentication.php +++ b/src/Renderer/Twig/Extensions/Authentication.php @@ -22,7 +22,7 @@ class Authentication extends TwigExtension /** * @return TwigFunction[] */ - public function getFunctions() + public function getFunctions(): array { return [ new TwigFunction('is_user', [$this, 'isAuthenticated']), @@ -34,7 +34,7 @@ class Authentication extends TwigExtension /** * @return bool */ - public function isAuthenticated() + public function isAuthenticated(): bool { return (bool)$this->auth->user(); } @@ -42,7 +42,7 @@ class Authentication extends TwigExtension /** * @return bool */ - public function isGuest() + public function isGuest(): bool { return !$this->isAuthenticated(); } diff --git a/src/Renderer/Twig/Extensions/Config.php b/src/Renderer/Twig/Extensions/Config.php index a82d2f73..da106957 100644 --- a/src/Renderer/Twig/Extensions/Config.php +++ b/src/Renderer/Twig/Extensions/Config.php @@ -22,7 +22,7 @@ class Config extends TwigExtension /** * @return TwigFunction[] */ - public function getFunctions() + public function getFunctions(): array { return [ new TwigFunction('config', [$this->config, 'get']), diff --git a/src/Renderer/Twig/Extensions/Csrf.php b/src/Renderer/Twig/Extensions/Csrf.php index 6e326824..fee1c1f9 100644 --- a/src/Renderer/Twig/Extensions/Csrf.php +++ b/src/Renderer/Twig/Extensions/Csrf.php @@ -22,7 +22,7 @@ class Csrf extends TwigExtension /** * @return TwigFunction[] */ - public function getFunctions() + public function getFunctions(): array { return [ new TwigFunction('csrf', [$this, 'getCsrfField'], ['is_safe' => ['html']]), @@ -33,7 +33,7 @@ class Csrf extends TwigExtension /** * @return string */ - public function getCsrfField() + public function getCsrfField(): string { return sprintf('', $this->getCsrfToken()); } @@ -41,7 +41,7 @@ class Csrf extends TwigExtension /** * @return string */ - public function getCsrfToken() + public function getCsrfToken(): string { return $this->session->get('_token'); } diff --git a/src/Renderer/Twig/Extensions/Globals.php b/src/Renderer/Twig/Extensions/Globals.php index a7914623..a809682c 100644 --- a/src/Renderer/Twig/Extensions/Globals.php +++ b/src/Renderer/Twig/Extensions/Globals.php @@ -24,7 +24,7 @@ class Globals extends TwigExtension implements GlobalsInterface * * @return array An array of global variables */ - public function getGlobals() + public function getGlobals(): array { $user = $this->auth->user(); diff --git a/src/Renderer/Twig/Extensions/Legacy.php b/src/Renderer/Twig/Extensions/Legacy.php index 41b00468..dc0c4775 100644 --- a/src/Renderer/Twig/Extensions/Legacy.php +++ b/src/Renderer/Twig/Extensions/Legacy.php @@ -22,7 +22,7 @@ class Legacy extends TwigExtension /** * @return TwigFunction[] */ - public function getFunctions() + public function getFunctions(): array { $isSafeHtml = ['is_safe' => ['html']]; return [ @@ -39,7 +39,7 @@ class Legacy extends TwigExtension /** * @return string */ - public function getPage() + public function getPage(): string { if ($this->request->has('p')) { return $this->request->get('p'); diff --git a/src/Renderer/Twig/Extensions/Markdown.php b/src/Renderer/Twig/Extensions/Markdown.php index 3d523f57..c23a0031 100644 --- a/src/Renderer/Twig/Extensions/Markdown.php +++ b/src/Renderer/Twig/Extensions/Markdown.php @@ -36,7 +36,7 @@ class Markdown extends TwigExtension * @param string $text * @return string */ - public function render($text): string + public function render(string $text): string { return $this->renderer->text(htmlspecialchars($text)); } diff --git a/src/Renderer/Twig/Extensions/Session.php b/src/Renderer/Twig/Extensions/Session.php index 378e977e..77b5b55a 100644 --- a/src/Renderer/Twig/Extensions/Session.php +++ b/src/Renderer/Twig/Extensions/Session.php @@ -22,7 +22,7 @@ class Session extends TwigExtension /** * @return TwigFunction[] */ - public function getFunctions() + public function getFunctions(): array { return [ new TwigFunction('session_get', [$this->session, 'get']), diff --git a/src/Renderer/Twig/Extensions/Translation.php b/src/Renderer/Twig/Extensions/Translation.php index 63b6fa5e..fff3bfa9 100644 --- a/src/Renderer/Twig/Extensions/Translation.php +++ b/src/Renderer/Twig/Extensions/Translation.php @@ -23,7 +23,7 @@ class Translation extends TwigExtension /** * @return array */ - public function getFilters() + public function getFilters(): array { return [ new TwigFilter('trans', [$this->translator, 'translate']), @@ -33,7 +33,7 @@ class Translation extends TwigExtension /** * @return TwigFunction[] */ - public function getFunctions() + public function getFunctions(): array { return [ new TwigFunction('__', [$this->translator, 'translate']), diff --git a/src/Renderer/Twig/Extensions/Url.php b/src/Renderer/Twig/Extensions/Url.php index 84c6aa4d..17fd6ff0 100644 --- a/src/Renderer/Twig/Extensions/Url.php +++ b/src/Renderer/Twig/Extensions/Url.php @@ -22,7 +22,7 @@ class Url extends TwigExtension /** * @return TwigFunction[] */ - public function getFunctions() + public function getFunctions(): array { return [ new TwigFunction('url', [$this, 'getUrl']), @@ -32,9 +32,9 @@ class Url extends TwigExtension /** * @param string $path * @param array $parameters - * @return UrlGenerator|string + * @return string */ - public function getUrl($path, $parameters = []) + public function getUrl(string $path, array $parameters = []): string { $path = str_replace('_', '-', $path); diff --git a/src/Renderer/TwigLoader.php b/src/Renderer/TwigLoader.php index 3c4c8ab8..1bcbbd71 100644 --- a/src/Renderer/TwigLoader.php +++ b/src/Renderer/TwigLoader.php @@ -10,10 +10,10 @@ class TwigLoader extends FilesystemLoader /** * @param string $name * @param bool $throw - * @return string|false|null + * @return string|null * @throws ErrorLoader */ - public function findTemplate($name, $throw = true) + public function findTemplate(string $name, bool $throw = true): ?string { $extension = '.twig'; $extensionLength = mb_strlen($extension); diff --git a/src/Renderer/TwigServiceProvider.php b/src/Renderer/TwigServiceProvider.php index 3a365a5e..d4e4a939 100644 --- a/src/Renderer/TwigServiceProvider.php +++ b/src/Renderer/TwigServiceProvider.php @@ -95,7 +95,7 @@ class TwigServiceProvider extends ServiceProvider * @param string $class * @param string $alias */ - protected function registerTwigExtensions($class, $alias) + protected function registerTwigExtensions(string $class, string $alias) { $alias = 'twig.extension.' . $alias; diff --git a/tests/Unit/Renderer/TwigServiceProviderTest.php b/tests/Unit/Renderer/TwigServiceProviderTest.php index 7696d28f..3336e015 100644 --- a/tests/Unit/Renderer/TwigServiceProviderTest.php +++ b/tests/Unit/Renderer/TwigServiceProviderTest.php @@ -12,6 +12,7 @@ use ReflectionClass as Reflection; use ReflectionException; use stdClass; use Twig\Environment as Twig; +use Twig\Extension\AbstractExtension; use Twig\Extension\CoreExtension as TwigCore; use Twig\Extension\ExtensionInterface as ExtensionInterface; use Twig\Loader\LoaderInterface as TwigLoaderInterface; @@ -103,9 +104,15 @@ class TwigServiceProviderTest extends ServiceProviderTest /** @var Config|MockObject $config */ $config = $this->createMock(Config::class); /** @var TwigCore|MockObject $twigCore */ - $twigCore = $this->getMockBuilder(stdClass::class) - ->addMethods(['setTimezone']) - ->getMock(); + $twigCore = $this->getMockForAbstractClass( + AbstractExtension::class, + [], + '', + true, + true, + true, + ['setTimezone'] + ); $app = $this->getApp(['make', 'instance', 'tag', 'get']);