From a45a1123b8886abf19c100adf29bed2ff2e136b4 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sun, 3 Nov 2024 13:11:06 +0700 Subject: [PATCH] Update to use PHP 8.1 syntax Signed-off-by: Abdul Malik Ikhsan --- src/TwigEnvironmentFactory.php | 4 ++-- src/TwigExtension.php | 10 +++++----- src/TwigRenderer.php | 2 +- test/TwigExtensionTest.php | 2 +- test/TwigRendererFactoryTest.php | 6 +++--- test/TwigRendererTest.php | 10 +++++----- 6 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/TwigEnvironmentFactory.php b/src/TwigEnvironmentFactory.php index a2fb1a9..fedf0ed 100644 --- a/src/TwigEnvironmentFactory.php +++ b/src/TwigEnvironmentFactory.php @@ -140,8 +140,8 @@ public function __invoke(ContainerInterface $container): Environment // Add template paths $allPaths = isset($config['paths']) && is_array($config['paths']) ? $config['paths'] : []; foreach ($allPaths as $namespace => $paths) { - $namespace = is_numeric($namespace) ? null : $namespace; - $namespace = $namespace ?? FilesystemLoader::MAIN_NAMESPACE; + $namespace = is_numeric($namespace) ? null : $namespace; + $namespace ??= FilesystemLoader::MAIN_NAMESPACE; foreach ((array) $paths as $path) { $loader->addPath($path, $namespace); } diff --git a/src/TwigExtension.php b/src/TwigExtension.php index 344381b..9990603 100644 --- a/src/TwigExtension.php +++ b/src/TwigExtension.php @@ -16,11 +16,11 @@ class TwigExtension extends AbstractExtension implements GlobalsInterface { public function __construct( - private ServerUrlHelper $serverUrlHelper, - private UrlHelper $urlHelper, - private ?string $assetsUrl, - private null|string|int $assetsVersion, - private array $globals = [] + private readonly ServerUrlHelper $serverUrlHelper, + private readonly UrlHelper $urlHelper, + private readonly ?string $assetsUrl, + private readonly null|string|int $assetsVersion, + private readonly array $globals = [] ) { } diff --git a/src/TwigRenderer.php b/src/TwigRenderer.php index 6cd7a79..211fc0e 100644 --- a/src/TwigRenderer.php +++ b/src/TwigRenderer.php @@ -96,7 +96,7 @@ public function render(string $name, $params = []): string */ public function addPath(string $path, ?string $namespace = null): void { - $namespace = $namespace ?? FilesystemLoader::MAIN_NAMESPACE; + $namespace ??= FilesystemLoader::MAIN_NAMESPACE; $this->twigLoader->addPath($path, $namespace); } diff --git a/test/TwigExtensionTest.php b/test/TwigExtensionTest.php index b894be8..69047d0 100644 --- a/test/TwigExtensionTest.php +++ b/test/TwigExtensionTest.php @@ -53,7 +53,7 @@ public function createExtension(?string $assetsUrl, $assetsVersion): TwigExtensi public function assertFunctionExists(string $name, array $functions, ?string $message = null): void { - $message = $message ?? sprintf('Failed to identify function by name %s', $name); + $message ??= sprintf('Failed to identify function by name %s', $name); $function = $this->findFunction($name, $functions); $this->assertInstanceOf(TwigFunction::class, $function, $message); } diff --git a/test/TwigRendererFactoryTest.php b/test/TwigRendererFactoryTest.php index cc8c965..01f0ba9 100644 --- a/test/TwigRendererFactoryTest.php +++ b/test/TwigRendererFactoryTest.php @@ -177,7 +177,7 @@ public function getConfigurationPaths(): array public function assertPathsHasNamespace(?string $namespace, array $paths, ?string $message = null): void { - $message = $message ?? sprintf('Paths do not contain namespace %s', $namespace ?? 'null'); + $message ??= sprintf('Paths do not contain namespace %s', $namespace ?? 'null'); $found = false; foreach ($paths as $path) { @@ -196,7 +196,7 @@ public function assertPathNamespaceCount( array $paths, ?string $message = null ): void { - $message = $message ?? sprintf('Did not find %d paths with namespace %s', $expected, $namespace ?? 'null'); + $message ??= sprintf('Did not find %d paths with namespace %s', $expected, $namespace ?? 'null'); $count = 0; foreach ($paths as $path) { @@ -214,7 +214,7 @@ public function assertPathNamespaceContains( array $paths, ?string $message = null ): void { - $message = $message ?? sprintf('Did not find path %s in namespace %s', $expected, $namespace ?? 'null'); + $message ??= sprintf('Did not find path %s in namespace %s', $expected, $namespace ?? 'null'); $found = []; foreach ($paths as $path) { diff --git a/test/TwigRendererTest.php b/test/TwigRendererTest.php index 226a83e..73b9b87 100644 --- a/test/TwigRendererTest.php +++ b/test/TwigRendererTest.php @@ -33,7 +33,7 @@ public function assertEqualTemplatePath( TemplatePath $received, ?string $message = null ): void { - $message = $message ?? 'Failed to assert TemplatePaths are equal'; + $message ??= 'Failed to assert TemplatePaths are equal'; if ( $expected->getPath() !== $received->getPath() || $expected->getNamespace() !== $received->getNamespace() @@ -70,19 +70,19 @@ public function testCanAddPathWithEmptyNamespace(): void public function assertTemplatePath(string $path, TemplatePath $templatePath, ?string $message = null): void { - $message = $message ?? sprintf('Failed to assert TemplatePath contained path %s', $path); + $message ??= sprintf('Failed to assert TemplatePath contained path %s', $path); $this->assertEquals($path, $templatePath->getPath(), $message); } public function assertTemplatePathString(string $path, TemplatePath $templatePath, ?string $message = null): void { - $message = $message ?? sprintf('Failed to assert TemplatePath casts to string path %s', $path); + $message ??= sprintf('Failed to assert TemplatePath casts to string path %s', $path); $this->assertEquals($path, (string) $templatePath, $message); } public function assertEmptyTemplatePathNamespace(TemplatePath $templatePath, ?string $message = null): void { - $message = $message ?? 'Failed to assert TemplatePath namespace was empty'; + $message ??= 'Failed to assert TemplatePath namespace was empty'; $this->assertEmpty($templatePath->getNamespace(), $message); } @@ -103,7 +103,7 @@ public function assertTemplatePathNamespace( TemplatePath $templatePath, ?string $message = null ): void { - $message = $message ?? sprintf( + $message ??= sprintf( 'Failed to assert TemplatePath namespace matched %s', var_export($namespace, true) );