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 6661b0f commit e6ecbbc
Show file tree
Hide file tree
Showing 22 changed files with 161 additions and 180 deletions.
8 changes: 0 additions & 8 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,6 @@ parameters:
"""
path: src/Latte/Filters.php

-
count: 1
message: """
#^Parameter \\$translator of method Contributte\\\\Translation\\\\Latte\\\\TranslatorExtension\\:\\:__construct\\(\\) has typehint with deprecated interface Nette\\\\Localization\\\\ITranslator\\:
use Nette\\\\Localization\\\\Translator$#
"""
path: src/Latte/TranslatorExtension.php

-
count: 1
message: """
Expand Down
4 changes: 4 additions & 0 deletions ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,8 @@
</property>
</properties>
</rule>
<rule ref="SlevomatCodingStandard.PHP.DisallowReference.DisallowedReturningReference">
<exclude-pattern>src/Latte/Nodes/TranslateNode.php</exclude-pattern>
<exclude-pattern>src/Latte/Nodes/TranslatorNode.php</exclude-pattern>
</rule>
</ruleset>
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;
}

}
31 changes: 15 additions & 16 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
{
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;
}

}
18 changes: 14 additions & 4 deletions src/Latte/TranslatorExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,23 @@
use Latte\Essential\Nodes\PrintNode;
use Latte\Extension;
use Latte\Runtime\FilterInfo;
use Nette\Localization\ITranslator;
use Nette\Localization\Translator;

class TranslatorExtension extends Extension
{

private ITranslator $translator;
private Translator $translator;

public function __construct(
ITranslator $translator
Translator $translator
)
{
$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:Translator}
*/
public function getProviders(): array
{
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 e6ecbbc

Please sign in to comment.