Skip to content

Commit

Permalink
Fix: PHP Unit test in php 8.1
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasjanicek committed Jan 9, 2025
1 parent bdcc6f5 commit 693710f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
27 changes: 23 additions & 4 deletions src/Compiler/ComponentTagCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,25 @@ 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 ?: "\t\n\r\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);
}

return rtrim($value, $characters);
}

/**
* Compile the opening tags within the given string.
*/
Expand Down Expand Up @@ -169,16 +188,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;
Expand Down Expand Up @@ -231,7 +250,7 @@ protected function getAttributesFromAttributeString(string $attributeString): st
$out .= "$key: $value,";
}

return mb_rtrim($out, ',') . '}';
return $this->rtrim($out, ',') . '}';
}

/**
Expand Down

0 comments on commit 693710f

Please sign in to comment.