From 0cac07b085101179c45657e4d79a3541c6df2d0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Toma=CC=81s=CC=8C=20Jani=CC=81c=CC=8Cek?= Date: Thu, 9 Jan 2025 09:39:59 +0100 Subject: [PATCH] Fix: PHP Unit test in php 8.1 --- README.md | 6 +++--- composer.json | 2 +- ecs.php | 4 ++++ src/Compiler/ComponentTagCompiler.php | 30 +++++++++++++++++++++++---- 4 files changed, 34 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index c7f7548..640e88d 100644 --- a/README.md +++ b/README.md @@ -6,9 +6,9 @@ TwigX Bundle Symfony bundle extending Twig template engine with [JSX]-like markup. ## Requirements -- PHP 7.4 || 8.1 -- Symfony 4.4+ || 5.4+ || ^6.1 -- Twig >=1.44.6 || >=2.12.5 || 3+ +- PHP ^8.1 +- Symfony ^5.4 || ^6.4 || ^7.2 +- Twig ^3 ## Changelog See [CHANGELOG](./CHANGELOG.md) diff --git a/composer.json b/composer.json index 2c5b3da..42931b5 100644 --- a/composer.json +++ b/composer.json @@ -29,7 +29,7 @@ "ext-simplexml": "*" }, "require-dev": { - "phpunit/phpunit": "^11.5", + "phpunit/phpunit": "^10.5 ||^11.5", "mockery/mockery": "^1.5", "doctrine/cache": "^1.10", "lmc/coding-standard": "^4.1", diff --git a/ecs.php b/ecs.php index acc2124..12a8350 100644 --- a/ecs.php +++ b/ecs.php @@ -2,6 +2,7 @@ declare(strict_types=1); +use PhpCsFixer\Fixer\Alias\MbStrFunctionsFixer; use PhpCsFixer\Fixer\ArrayNotation\ArraySyntaxFixer; use Symplify\EasyCodingStandard\Config\ECSConfig; use Symplify\EasyCodingStandard\ValueObject\Set\SetList; @@ -18,6 +19,9 @@ directory: sys_get_temp_dir() . '/ecs_cached_files', namespace: getcwd(), ) + ->withSkip([ + MbStrFunctionsFixer::class, + ]) ->withPaths([ __DIR__ . '/src', __DIR__ . '/tests', diff --git a/src/Compiler/ComponentTagCompiler.php b/src/Compiler/ComponentTagCompiler.php index 97bae5d..fcc6101 100644 --- a/src/Compiler/ComponentTagCompiler.php +++ b/src/Compiler/ComponentTagCompiler.php @@ -24,6 +24,28 @@ public function compile(): string return $this->compileClosingTags($value); } + private function trim(string $value, ?string $characters = null, ?string $encoding = null): string + { + if (PHP_VERSION_ID >= 80400 && function_exists('mb_trim')) { + return mb_trim($value, $characters, $encoding); + } + + $characters = $characters ?: "\n\r\t\v\0\x0B\x0C "; + + return trim($value, $characters); + } + + private function rtrim(string $value, ?string $characters = null, ?string $encoding = null): string + { + if (PHP_VERSION_ID >= 80400 && function_exists('mb_rtrim')) { + return mb_rtrim($value, $characters, $encoding); + } + + $characters = $characters ?: "\n\r\t\v\0\x0B\x0C "; + + return rtrim($value, $characters); + } + /** * Compile the opening tags within the given string. */ @@ -169,16 +191,16 @@ private function valueParser(?string $value, string $attribute): string // `"{ value } "` -> `{ value }` // `"{{value}} "` -> `{{value}}` // `"{value} "` -> `{value}` - $valueWithoutQuotes = mb_trim($this->stripQuotes($value)); + $valueWithoutQuotes = $this->trim($this->stripQuotes($value)); // `{{ value }}` or `{{value}}` if (\str_starts_with($valueWithoutQuotes, '{{') && (mb_strpos($valueWithoutQuotes, '}}') === mb_strlen($valueWithoutQuotes) - 2)) { - return mb_trim(mb_substr($valueWithoutQuotes, 2, -2)); + return $this->trim(mb_substr($valueWithoutQuotes, 2, -2)); } // `{ value }` or `{value}` if (\str_starts_with($valueWithoutQuotes, '{') && (mb_strpos($valueWithoutQuotes, '}') === mb_strlen($valueWithoutQuotes) - 1)) { - return mb_trim(mb_substr($valueWithoutQuotes, 1, -1)); + return $this->trim(mb_substr($valueWithoutQuotes, 1, -1)); } return $value; @@ -231,7 +253,7 @@ protected function getAttributesFromAttributeString(string $attributeString): st $out .= "$key: $value,"; } - return mb_rtrim($out, ',') . '}'; + return $this->rtrim($out, ',') . '}'; } /**