Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use contributte/qa and fix build QA #125

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ vendor: composer.json composer.lock
qa: lint phpstan cs

lint: vendor
vendor/bin/linter src tests
vendor/bin/parallel-lint src tests

cs: vendor
vendor/bin/codesniffer src tests
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,16 @@
"symfony/config": "^6.0"
},
"require-dev": {
"contributte/qa": "^0.2.0",
"doctrine/orm": "^2.8",
"mockery/mockery": "^1.4",
"mockery/mockery": "^1.5",
"nette/application": "^3.1.0",
"nette/bootstrap": "^3.0",
"nette/database": "^3.1.1",
"nette/robot-loader": "^3.4.0|~4.0.0",
"nette/tester": "^2.3.1",
"ninjify/nunjuck": "^0.3.0",
"ninjify/qa": "^0.13",
"php-parallel-lint/php-parallel-lint": "^1.3",
"phpstan/phpstan": "^1.8",
"phpstan/phpstan-deprecation-rules": "^1.0",
"phpstan/phpstan-nette": "^1.0",
Expand Down
10 changes: 2 additions & 8 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ parameters:
excludePaths:
- src/Latte/Filters.php
- src/Latte/Macros.php
bootstrapFiles:
- vendor/latte/latte/src/Latte/Compiler/Nodes/Php/Expression/StaticMethodCallNode.php
ignoreErrors:
-
count: 2
Expand Down Expand Up @@ -60,14 +62,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
6 changes: 5 additions & 1 deletion ruleset.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
<ruleset>
<rule ref="./vendor/ninjify/coding-standard/contributte.xml">
<rule ref="./vendor/contributte/qa/ruleset-8.0.xml">
<exclude name="SlevomatCodingStandard.Classes.SuperfluousAbstractClassNaming.SuperfluousSuffix" />
<exclude name="SlevomatCodingStandard.Classes.SuperfluousInterfaceNaming.SuperfluousSuffix" />
<exclude name="SlevomatCodingStandard.Classes.RequireMultiLineMethodSignature.RequiredMultiLineSignature" />
Expand All @@ -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;
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
Loading
Loading