Skip to content

Commit

Permalink
Fix errors reported by contributte/qa codesniffer
Browse files Browse the repository at this point in the history
  • Loading branch information
spaze committed Jan 16, 2024
1 parent 3750937 commit a690412
Show file tree
Hide file tree
Showing 20 changed files with 155 additions and 167 deletions.
1 change: 1 addition & 0 deletions src/FallbackResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public function setFallbackLocales(
): self
{
$this->fallbackLocales = $array;

return $this;
}

Expand Down
6 changes: 2 additions & 4 deletions src/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,12 @@ public static function isAbsoluteMessage(
}

/**
* @param mixed $message
* @param array<string>|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;
Expand Down
9 changes: 2 additions & 7 deletions src/Latte/Filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
21 changes: 11 additions & 10 deletions src/Latte/Macros.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}

/**
* {_ ...}
*
Expand Down Expand Up @@ -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;
}

Expand Down
79 changes: 36 additions & 43 deletions src/Latte/Nodes/TranslateNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<int, ?array<mixed>, array{AreaNode, ?Tag}, TranslateNode|NopNode> */
public static function create(
Tag $tag
Expand All @@ -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;
}

}
33 changes: 16 additions & 17 deletions src/Latte/Nodes/TranslatorNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,6 @@ class TranslatorNode extends StatementNode

public AreaNode $content;

/** @return \Generator<int, ?array<mixed>, 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
Expand All @@ -54,11 +39,25 @@ public function print(
);
}


public function &getIterator(): \Generator
public function getIterator(): \Generator
{
yield $this->prefix;
yield $this->content;
}

/** @return \Generator<int, ?array<mixed>, 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;
}

}
12 changes: 11 additions & 1 deletion src/Latte/TranslatorExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
Expand All @@ -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

Check failure on line 61 in src/Latte/TranslatorExtension.php

View workflow job for this annotation

GitHub Actions / Static analysis (phpstan)

Return type of method Contributte\Translation\Latte\TranslatorExtension::getProviders() has typehint with deprecated interface Nette\Localization\ITranslator: use Nette\Localization\Translator
{
return [
Expand Down Expand Up @@ -84,6 +93,7 @@ public function parseTranslate(
$outputNode->modifier->escape = true;
$outputNode->expression = $messageNode;

Check failure on line 94 in src/Latte/TranslatorExtension.php

View workflow job for this annotation

GitHub Actions / Static analysis (phpstan)

Property Latte\Essential\Nodes\PrintNode::$expression (Latte\Compiler\Nodes\Php\ExpressionNode) does not accept Latte\Compiler\Nodes\Php\Expression\StaticCallNode.
array_unshift($outputNode->modifier->filters, new FilterNode(new IdentifierNode('translate'), $args->toArguments()));

return $outputNode;
}

Expand Down
22 changes: 11 additions & 11 deletions src/Loaders/DatabaseAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>
*/
abstract protected function getMessages(
array $config,
string $resource,
string $locale,
string $domain
): array;

/**
* {@inheritdoc}
*
Expand Down Expand Up @@ -64,15 +75,4 @@ public function load(
return $catalogue;
}

/**
* @param array{table: string, id: string, locale: string, message: string} $config
* @return array<string>
*/
abstract protected function getMessages(
array $config,
string $resource,
string $locale,
string $domain
): array;

}
2 changes: 1 addition & 1 deletion src/LocaleResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 2 additions & 0 deletions src/LocalesResolvers/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function setLocale(
): self
{
$this->sessionSection[self::$parameter] = $locale;

return $this;
}

Expand All @@ -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;
}

Expand Down
9 changes: 3 additions & 6 deletions src/PrefixedTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Loading

0 comments on commit a690412

Please sign in to comment.