diff --git a/src/FallbackResolver.php b/src/FallbackResolver.php index f3ccddb7..a928356c 100644 --- a/src/FallbackResolver.php +++ b/src/FallbackResolver.php @@ -18,6 +18,7 @@ public function setFallbackLocales( ): self { $this->fallbackLocales = $array; + return $this; } diff --git a/src/Helpers.php b/src/Helpers.php index 49aadbba..38ccf936 100644 --- a/src/Helpers.php +++ b/src/Helpers.php @@ -43,14 +43,12 @@ public static function isAbsoluteMessage( } /** - * @param mixed $message * @param array|null $prefix - * @return mixed */ public static function prefixMessage( - $message, + mixed $message, ?array $prefix - ) + ): mixed { if (is_string($message) && $prefix !== null && !self::isAbsoluteMessage($message)) { $message = implode('.', $prefix) . '.' . $message; diff --git a/src/Latte/Filters.php b/src/Latte/Filters.php index 3c077a11..06ebd0af 100644 --- a/src/Latte/Filters.php +++ b/src/Latte/Filters.php @@ -17,15 +17,10 @@ public function __construct( $this->translator = $translator; } - /** - * @param \Latte\Runtime\FilterInfo $filterInfo - * @param mixed $message - * @param mixed ...$parameters - */ public function translate( FilterInfo $filterInfo, - $message, - ...$parameters + mixed $message, + mixed ...$parameters ): string { return $this->translator->translate($message, ...$parameters); diff --git a/src/Latte/Macros.php b/src/Latte/Macros.php index 87c3c2c9..63318bb5 100644 --- a/src/Latte/Macros.php +++ b/src/Latte/Macros.php @@ -21,16 +21,6 @@ final public function __construct( parent::__construct($compiler); } - public static function install( - Compiler $compiler - ): void - { - $me = new static($compiler); - - $me->addMacro('_', [$me, 'macroTranslate'], [$me, 'macroTranslate']); - $me->addMacro('translator', [$me, 'macroPrefix'], [$me, 'macroPrefix']); - } - /** * {_ ...} * @@ -124,12 +114,23 @@ public function macroPrefix( ', $tempPrefixProp, $tempPrefixProp, $prefixProp, $tempPrefixProp, $prefixProp, $prefixProp)); } + public static function install( + Compiler $compiler + ): void + { + $me = new static($compiler); + + $me->addMacro('_', [$me, 'macroTranslate'], [$me, 'macroTranslate']); + $me->addMacro('translator', [$me, 'macroPrefix'], [$me, 'macroPrefix']); + } + public static function macroWithoutParameters( MacroNode $node ): bool { $result = Strings::trim($node->tokenizer->joinUntil(',')) === Strings::trim($node->args); $node->tokenizer->reset(); + return $result; } diff --git a/src/Latte/Nodes/TranslateNode.php b/src/Latte/Nodes/TranslateNode.php index 8c370adc..aa1d47bd 100644 --- a/src/Latte/Nodes/TranslateNode.php +++ b/src/Latte/Nodes/TranslateNode.php @@ -19,6 +19,42 @@ class TranslateNode extends StatementNode public ModifierNode $modifier; + public function print( + PrintContext $context + ): string + { + return $this->content instanceof TextNode ? $context->format( + ' + $ʟ_fi = new LR\FilterInfo(%dump); + echo %modifyContent(%dump) %line; + ', + $context->getEscaper()->export(), + $this->modifier, + $this->content->content, + $this->position, + ) : $context->format( + ' + ob_start(fn() => ""); try { + %node + } finally { + $ʟ_tmp = ob_get_clean(); + } + $ʟ_fi = new LR\FilterInfo(%dump); + echo %modifyContent($ʟ_tmp) %line; + ', + $this->content, + $context->getEscaper()->export(), + $this->modifier, + $this->position, + ); + } + + public function getIterator(): \Generator + { + yield $this->content; + yield $this->modifier; + } + /** @return \Generator, array{AreaNode, ?Tag}, TranslateNode|NopNode> */ public static function create( Tag $tag @@ -45,47 +81,4 @@ public static function create( return $node; } - - public function print( - PrintContext $context - ): string - { - if ($this->content instanceof TextNode) { - return $context->format( - ' - $ʟ_fi = new LR\FilterInfo(%dump); - echo %modifyContent(%dump) %line; - ', - $context->getEscaper()->export(), - $this->modifier, - $this->content->content, - $this->position, - ); - - } else { - return $context->format( - ' - ob_start(fn() => ""); try { - %node - } finally { - $ʟ_tmp = ob_get_clean(); - } - $ʟ_fi = new LR\FilterInfo(%dump); - echo %modifyContent($ʟ_tmp) %line; - ', - $this->content, - $context->getEscaper()->export(), - $this->modifier, - $this->position, - ); - } - } - - - public function &getIterator(): \Generator - { - yield $this->content; - yield $this->modifier; - } - } diff --git a/src/Latte/Nodes/TranslatorNode.php b/src/Latte/Nodes/TranslatorNode.php index 4364dee0..d21b501d 100644 --- a/src/Latte/Nodes/TranslatorNode.php +++ b/src/Latte/Nodes/TranslatorNode.php @@ -16,21 +16,6 @@ class TranslatorNode extends StatementNode public AreaNode $content; - /** @return \Generator, array{AreaNode, ?Tag}, TranslatorNode> */ - public static function create( - Tag $tag - ): \Generator - { - $tag->expectArguments(); - $variable = $tag->parser->parseUnquotedStringOrExpression(); - - $node = new TranslatorNode(); - $node->prefix = $variable; - [$node->content] = yield; - return $node; - } - - public function print( PrintContext $context ): string @@ -54,11 +39,25 @@ public function print( ); } - - public function &getIterator(): \Generator + public function getIterator(): \Generator { yield $this->prefix; yield $this->content; } + /** @return \Generator, array{AreaNode, ?Tag}, TranslatorNode> */ + public static function create( + Tag $tag + ): \Generator + { + $tag->expectArguments(); + $variable = $tag->parser->parseUnquotedStringOrExpression(); + + $node = new TranslatorNode(); + $node->prefix = $variable; + [$node->content] = yield; + + return $node; + } + } diff --git a/src/Latte/TranslatorExtension.php b/src/Latte/TranslatorExtension.php index f436eff9..2c48eb81 100644 --- a/src/Latte/TranslatorExtension.php +++ b/src/Latte/TranslatorExtension.php @@ -33,6 +33,9 @@ public function __construct( $this->translator = $translator; } + /** + * @return array{_:callable(Tag):Node, translate:callable(Tag):\Generator, translator:callable(Tag):\Generator} + */ public function getTags(): array { return [ @@ -42,13 +45,19 @@ public function getTags(): array ]; } + /** + * @return array{translate:callable(FilterInfo $fi, string ...$args):string} + */ public function getFilters(): array { return [ - 'translate' => fn(FilterInfo $fi, ...$args): string => (string) $this->translator->translate(...$args), + 'translate' => fn (FilterInfo $fi, ...$args): string => (string) $this->translator->translate(...$args), ]; } + /** + * @return array{translator:ITranslator} + */ public function getProviders(): array { return [ @@ -84,6 +93,7 @@ public function parseTranslate( $outputNode->modifier->escape = true; $outputNode->expression = $messageNode; array_unshift($outputNode->modifier->filters, new FilterNode(new IdentifierNode('translate'), $args->toArguments())); + return $outputNode; } diff --git a/src/Loaders/DatabaseAbstract.php b/src/Loaders/DatabaseAbstract.php index 8176c8bb..d2543868 100644 --- a/src/Loaders/DatabaseAbstract.php +++ b/src/Loaders/DatabaseAbstract.php @@ -19,6 +19,17 @@ abstract class DatabaseAbstract extends ArrayLoader implements LoaderInterface 'message' => 'message', ]; + /** + * @param array{table: string, id: string, locale: string, message: string} $config + * @return array + */ + abstract protected function getMessages( + array $config, + string $resource, + string $locale, + string $domain + ): array; + /** * {@inheritdoc} * @@ -64,15 +75,4 @@ public function load( return $catalogue; } - /** - * @param array{table: string, id: string, locale: string, message: string} $config - * @return array - */ - abstract protected function getMessages( - array $config, - string $resource, - string $locale, - string $domain - ): array; - } diff --git a/src/LocaleResolver.php b/src/LocaleResolver.php index 272bfd7f..db7e89e9 100644 --- a/src/LocaleResolver.php +++ b/src/LocaleResolver.php @@ -30,13 +30,13 @@ public function getResolvers(): array /** * @param class-string $resolver - * @return self */ public function addResolver( string $resolver ): self { $this->resolvers[] = $resolver; + return $this; } diff --git a/src/LocalesResolvers/Session.php b/src/LocalesResolvers/Session.php index 316f3fde..094bdfc7 100644 --- a/src/LocalesResolvers/Session.php +++ b/src/LocalesResolvers/Session.php @@ -33,6 +33,7 @@ public function setLocale( ): self { $this->sessionSection[self::$parameter] = $locale; + return $this; } @@ -42,6 +43,7 @@ public function resolve( { if (!$this->session->isStarted() && $this->httpResponse->isSent()) { trigger_error('The advice of session locale resolver is required but the session has not been started and headers had been already sent. Either start your sessions earlier or disable the SessionResolver.', E_USER_WARNING); + return null; } diff --git a/src/PrefixedTranslator.php b/src/PrefixedTranslator.php index bddb7545..538114ca 100644 --- a/src/PrefixedTranslator.php +++ b/src/PrefixedTranslator.php @@ -30,18 +30,15 @@ public function getPrefix(): string return $this->prefix; } - /** - * @param mixed $message - * @param mixed ...$parameters - */ public function translate( - $message, - ...$parameters + mixed $message, + mixed ...$parameters ): string { $this->translator->addPrefix($this->prefix); $message = $this->translator->translate($message, ...$parameters); $this->translator->removePrefix(); + return $message; } diff --git a/src/Tracy/Panel.php b/src/Tracy/Panel.php index 09eb84b8..33618a45 100644 --- a/src/Tracy/Panel.php +++ b/src/Tracy/Panel.php @@ -43,6 +43,7 @@ public function getTab(): string // https://postimg.cc/jC4Nq2wg $icon = ''; $errMsg = ($this->missingTranslationCount > 1) ? 'errors' : 'error'; + return '' . $icon . ' ' . @@ -123,30 +124,6 @@ public function getPanel(): string return implode('', $panel); } - /** - * @param array> $resources - */ - private static function createResourcePanelHelper( - array $resources, - string $class - ): string - { - $string = ''; - $string .= ''; - - foreach ($resources as $k1 => $v1) { - foreach ($v1 as $k2 => $v2) { - $string .= ''; - $string .= ''; - $string .= ''; - $string .= ''; - $string .= ''; - } - } - - return $string . '
LocaleDomainFile name
' . htmlspecialchars($k1) . '' . htmlspecialchars($v2) . '' . htmlspecialchars(dirname($k2)) . '/' . htmlspecialchars(basename($k2)) . '
'; - } - public function addMissingTranslation( string $id, string $domain @@ -220,4 +197,28 @@ public function getIgnoredResources(): array return $this->ignoredResources; } + /** + * @param array> $resources + */ + private static function createResourcePanelHelper( + array $resources, + string $class + ): string + { + $string = ''; + $string .= ''; + + foreach ($resources as $k1 => $v1) { + foreach ($v1 as $k2 => $v2) { + $string .= ''; + $string .= ''; + $string .= ''; + $string .= ''; + $string .= ''; + } + } + + return $string . '
LocaleDomainFile name
' . htmlspecialchars($k1) . '' . htmlspecialchars($v2) . '' . htmlspecialchars(dirname($k2)) . '/' . htmlspecialchars(basename($k2)) . '
'; + } + } diff --git a/src/Translator.php b/src/Translator.php index e8658650..932a12a0 100644 --- a/src/Translator.php +++ b/src/Translator.php @@ -16,6 +16,8 @@ class Translator extends SymfonyTranslator implements ITranslator { + public bool $returnOriginalMessage = true; + private LocaleResolver $localeResolver; private FallbackResolver $fallbackResolver; @@ -26,8 +28,6 @@ class Translator extends SymfonyTranslator implements ITranslator private bool $debug; - public bool $returnOriginalMessage = true; - /** @var array|null */ private ?array $localesWhitelist = null; @@ -155,6 +155,7 @@ public function addPrefix( ): self { $this->prefix[] = $string; + return $this; } @@ -176,6 +177,7 @@ public function setTranslateKeyGenerator( ): self { $this->translateKeyGenerator = $generator; + return $this; } @@ -224,12 +226,13 @@ public function getAvailableLocales(): array { $locales = array_keys($this->resourcesLocales); sort($locales); + return $locales; } public function addResource( string $format, - $resource, + mixed $resource, string $locale, ?string $domain = null ): void @@ -271,6 +274,7 @@ public function setPsrLogger( ): self { $this->psrLogger = $psrLogger; + return $this; } @@ -284,16 +288,13 @@ public function setTracyPanel( ): self { $this->tracyPanel = $tracyPanel; + return $this; } - /** - * @param mixed $message - * @param mixed ...$parameters - */ public function translate( - $message, - ...$parameters + mixed $message, + mixed ...$parameters ): string { if ($message === null || $message === '') { diff --git a/src/Wrappers/Message.php b/src/Wrappers/Message.php index a9a980d7..3d8bb8e9 100644 --- a/src/Wrappers/Message.php +++ b/src/Wrappers/Message.php @@ -10,12 +10,9 @@ class Message /** @var array */ public array $parameters; - /** - * @param array ...$parameters - */ public function __construct( string $message, - ...$parameters + mixed ...$parameters ) { $this->message = $message; diff --git a/tests/Tests/DI/TranslationExtensionTest.phpt b/tests/Tests/DI/TranslationExtensionTest.phpt index cf771b18..9a9e85b6 100644 --- a/tests/Tests/DI/TranslationExtensionTest.phpt +++ b/tests/Tests/DI/TranslationExtensionTest.phpt @@ -297,7 +297,7 @@ final class TranslationExtensionTest extends TestAbstract public function test12(): void { - Assert::exception(static function () { + Assert::exception(static function (): void { Container::of() ->withDefaults() ->withCompiler(function (Compiler $compiler): void { diff --git a/tests/Tests/Loaders/NetteDatabaseTest.phpt b/tests/Tests/Loaders/NetteDatabaseTest.phpt index 14246de8..8f771ff8 100644 --- a/tests/Tests/Loaders/NetteDatabaseTest.phpt +++ b/tests/Tests/Loaders/NetteDatabaseTest.phpt @@ -20,7 +20,19 @@ final class NetteDatabaseTest extends TestAbstract private Connection $connection; - protected function setUp() + public function test01(): void + { + $this->connection->query(file_get_contents(__DIR__ . '/../../sql.sql')); + $this->translator->setLocale('cs_CZ'); + + Assert::same('Ahoj', $this->translator->translate('db_table.hello')); + + $this->translator->setLocale('en_US'); + + Assert::same('Hello', $this->translator->translate('db_table.hello')); + } + + protected function setUp(): void { parent::setUp(); @@ -53,18 +65,6 @@ final class NetteDatabaseTest extends TestAbstract } } - public function test01(): void - { - $this->connection->query(file_get_contents(__DIR__ . '/../../sql.sql')); - $this->translator->setLocale('cs_CZ'); - - Assert::same('Ahoj', $this->translator->translate('db_table.hello')); - - $this->translator->setLocale('en_US'); - - Assert::same('Hello', $this->translator->translate('db_table.hello')); - } - } (new NetteDatabaseTest($container))->run(); diff --git a/tests/Tests/LocaleResolverMock.php b/tests/Tests/LocaleResolverMock.php index aaaf72c3..0846c8ae 100644 --- a/tests/Tests/LocaleResolverMock.php +++ b/tests/Tests/LocaleResolverMock.php @@ -15,6 +15,7 @@ public function setLocale( ): self { $this->locale = $locale; + return $this; } diff --git a/tests/Tests/LocalesResolvers/HeaderTest.phpt b/tests/Tests/LocalesResolvers/HeaderTest.phpt index e521b2b8..d353efde 100644 --- a/tests/Tests/LocalesResolvers/HeaderTest.phpt +++ b/tests/Tests/LocalesResolvers/HeaderTest.phpt @@ -51,32 +51,23 @@ final class HeaderTest extends TestAbstract return new UrlScript(); } - /** - * @return mixed - */ public function getQuery( ?string $key = null - ) + ): mixed { return null; } - /** - * @return mixed - */ public function getPost( ?string $key = null - ) + ): mixed { return null; } - /** - * @return mixed - */ public function getFile( string $key - ) + ): mixed { return null; } @@ -89,12 +80,9 @@ final class HeaderTest extends TestAbstract return []; } - /** - * @return mixed - */ public function getCookie( string $key - ) + ): mixed { return null; } @@ -118,6 +106,7 @@ final class HeaderTest extends TestAbstract { return true; } + public function getHeader( string $header ): ?string diff --git a/tests/Tests/PsrLoggerMock.php b/tests/Tests/PsrLoggerMock.php index 33c1f67d..9b75d9ec 100644 --- a/tests/Tests/PsrLoggerMock.php +++ b/tests/Tests/PsrLoggerMock.php @@ -16,6 +16,7 @@ public function log( array $context = [] ) { + // testing mock } } diff --git a/tests/Toolkit/Container.php b/tests/Toolkit/Container.php index fb6c3ddf..97b58f27 100644 --- a/tests/Toolkit/Container.php +++ b/tests/Toolkit/Container.php @@ -11,11 +11,10 @@ final class Container { - /** @var string */ - private $key; + private string $key; /** @var callable[] */ - private $onCompile = []; + private array $onCompile = []; public function __construct(string $key) {