Skip to content

Commit cb29109

Browse files
committed
style: apply fixes from mago
1 parent 181e55e commit cb29109

33 files changed

+466
-243
lines changed

packages/i18n/bin/plural-rules.php

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,7 @@ private function generateLanguageMethod(string $locale, array $rules): string
185185
break;
186186
}
187187

188-
$condition = $this->parseRule($rule);
189-
if ($condition) {
188+
if ($condition = $this->parseRule($rule)) {
190189
$output .= " if ({$condition}) {\n";
191190
$output .= " return '{$category}';\n";
192191
$output .= " }\n\n";
@@ -200,10 +199,9 @@ private function generateLanguageMethod(string $locale, array $rules): string
200199

201200
private function parseRule(string $rule): string
202201
{
203-
// Extract the rule condition (before @integer/@decimal examples)
204202
$rulePart = trim(explode('@', $rule)[0]);
205203

206-
if (empty($rulePart)) {
204+
if (! $rulePart) {
207205
return '';
208206
}
209207

@@ -233,7 +231,6 @@ private function parseSingleCondition(string $condition): string
233231
{
234232
$condition = trim($condition);
235233

236-
// Modulo operations "n % 100 = 3..10" or "n % 10 = 3..4,9"
237234
if (preg_match('/^([nifvet])\s*%\s*(\d+)\s*(=|!=)\s*(.+)$/', $condition, $matches)) {
238235
$var = $this->getVariable($matches[1]);
239236
$mod = $matches[2];
@@ -243,7 +240,6 @@ private function parseSingleCondition(string $condition): string
243240
return $this->parseValueCondition("({$var} % {$mod})", $op, $values);
244241
}
245242

246-
// Direct comparisons "n = 1" or "n = 0..1"
247243
if (preg_match('/^([nifvet])\s*(=|!=)\s*(.+)$/', $condition, $matches)) {
248244
$var = $this->getVariable($matches[1]);
249245
$op = $matches[2] === '=' ? '===' : '!==';
@@ -260,54 +256,47 @@ private function parseValueCondition(string $varExpression, string $operator, st
260256
$values = trim($values);
261257
$isNegative = $operator === '!==';
262258

263-
// Handle single number
264259
if (preg_match('/^\d+(?:\.\d+)?$/', $values)) {
265260
return "{$varExpression} {$operator} {$values}";
266261
}
267262

268-
// Handle single range like "3..10"
269263
if (preg_match('/^(\d+(?:\.\d+)?)\.\.(\d+(?:\.\d+)?)$/', $values, $matches)) {
270264
$start = $matches[1];
271265
$end = $matches[2];
272266
$condition = "self::inRange({$varExpression}, {$start}, {$end})";
273267
return $isNegative ? "!{$condition}" : $condition;
274268
}
275269

276-
// Handle complex values with commas and ranges like "3..4,9" or "2,22,42,62,82"
277270
if (str_contains($values, ',')) {
278271
$parts = array_map('trim', explode(',', $values));
279272
$conditions = [];
280273

281274
foreach ($parts as $part) {
282275
if (str_contains($part, '..')) {
283-
// Range like "3..4"
284276
if (preg_match('/^(\d+(?:\.\d+)?)\.\.(\d+(?:\.\d+)?)$/', $part, $matches)) {
285277
$start = $matches[1];
286278
$end = $matches[2];
287279
$conditions[] = "self::inRange({$varExpression}, {$start}, {$end})";
288280
}
289281
} elseif (str_contains($part, '~')) {
290-
// Range like "3~10"
291282
if (preg_match('/^(\d+(?:\.\d+)?)~(\d+(?:\.\d+)?)$/', $part, $matches)) {
292283
$start = $matches[1];
293284
$end = $matches[2];
294285
$conditions[] = "self::inRange({$varExpression}, {$start}, {$end})";
295286
}
296287
} else {
297-
// Single value
298288
$conditions[] = "{$varExpression} === {$part}";
299289
}
300290
}
301291

302-
if (empty($conditions)) {
292+
if (! $conditions) {
303293
return 'false';
304294
}
305295

306296
$combined = '(' . implode(' || ', $conditions) . ')';
307297
return $isNegative ? "!{$combined}" : $combined;
308298
}
309299

310-
// Handle tilde ranges like "3~10"
311300
if (str_contains($values, '~')) {
312301
if (preg_match('/^(\d+(?:\.\d+)?)~(\d+(?:\.\d+)?)$/', $values, $matches)) {
313302
$start = $matches[1];
@@ -317,8 +306,8 @@ private function parseValueCondition(string $varExpression, string $operator, st
317306
}
318307
}
319308

320-
// Fallback: use matchesValues for complex patterns
321309
$condition = "self::matchesValues({$varExpression}, '{$values}')";
310+
322311
return $isNegative ? "!{$condition}" : $condition;
323312
}
324313

packages/i18n/src/Translator/GenericTranslator.php renamed to packages/i18n/src/GenericTranslator.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
<?php
22

3-
namespace Tempest\Internationalization\Translator;
3+
namespace Tempest\Internationalization;
44

5-
use Tempest\Core\ExceptionReporter;
65
use Tempest\EventBus\EventBus;
76
use Tempest\Internationalization\Catalog\Catalog;
87
use Tempest\Internationalization\InternationalizationConfig;
98
use Tempest\Internationalization\MessageFormat\Formatter\MessageFormatter;
109
use Tempest\Support\Language\Locale;
1110

12-
final class GenericTranslator implements Translator
11+
final readonly class GenericTranslator implements Translator
1312
{
1413
public function __construct(
15-
private readonly InternationalizationConfig $config,
16-
private readonly Catalog $catalog,
17-
private readonly MessageFormatter $formatter,
18-
private readonly ?EventBus $eventBus = null,
14+
private InternationalizationConfig $config,
15+
private Catalog $catalog,
16+
private MessageFormatter $formatter,
17+
private ?EventBus $eventBus = null,
1918
) {}
2019

2120
public function translateForLocale(Locale $locale, string $key, mixed ...$arguments): string

packages/i18n/src/MessageFormat/Formatter/FormattedValue.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
namespace Tempest\Internationalization\MessageFormat\Formatter;
44

5-
final class FormattedValue
5+
final readonly class FormattedValue
66
{
77
public function __construct(
8-
public readonly mixed $value,
9-
public readonly string $formatted,
8+
public mixed $value,
9+
public string $formatted,
1010
) {}
1111
}

packages/i18n/src/MessageFormat/Formatter/MessageFormatter.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Tempest\Internationalization\MessageFormat\Formatter;
44

55
use Exception;
6-
use Tempest\Internationalization\InternationalizationConfig;
76
use Tempest\Internationalization\MessageFormat\Parser\Node\ComplexBody\ComplexBody;
87
use Tempest\Internationalization\MessageFormat\Parser\Node\ComplexBody\Matcher;
98
use Tempest\Internationalization\MessageFormat\Parser\Node\ComplexBody\SimplePatternBody;
@@ -242,7 +241,7 @@ private function evaluateExpression(Expression $expression): FormattedValue
242241
$variableName = $expression->variable->name->name;
243242

244243
if (! array_key_exists($variableName, $this->variables)) {
245-
throw new FormattingException("Variable `$variableName` not found");
244+
throw new FormattingException("Variable `{$variableName}` not found");
246245
}
247246

248247
$value = $this->variables[$variableName];
@@ -304,9 +303,9 @@ private function formatMarkup(Markup $markup): string
304303
$tag = (string) $markup->identifier;
305304

306305
return match ($markup->type) {
307-
MarkupType::OPEN => "<$tag>",
308-
MarkupType::CLOSE => "</$tag>",
309-
MarkupType::STANDALONE => "<$tag/>",
306+
MarkupType::OPEN => "<{$tag}>",
307+
MarkupType::CLOSE => "</{$tag}>",
308+
MarkupType::STANDALONE => "<{$tag}/>",
310309
default => '',
311310
};
312311
}

packages/i18n/src/MessageFormat/Parser/Node/ComplexBody/Matcher.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44

55
use Tempest\Internationalization\MessageFormat\Parser\Node\Pattern\Pattern;
66

7-
final class Matcher implements ComplexBody
7+
final readonly class Matcher implements ComplexBody
88
{
99
/**
1010
* @param Variable[] $selectors
1111
* @param Variant[] $variants
1212
*/
1313
public function __construct(
14-
public readonly array $selectors,
15-
public readonly array $variants,
14+
public array $selectors,
15+
public array $variants,
1616
) {}
1717

1818
public function getPattern(): Pattern

packages/i18n/src/MessageFormat/Parser/Node/ComplexBody/SimplePatternBody.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
use Tempest\Internationalization\MessageFormat\Parser\Node\Pattern\Pattern;
66

7-
final class SimplePatternBody implements ComplexBody
7+
final readonly class SimplePatternBody implements ComplexBody
88
{
99
public function __construct(
10-
public readonly Pattern $pattern,
10+
public Pattern $pattern,
1111
) {}
1212

1313
public function getPattern(): Pattern

packages/i18n/src/MessageFormat/Parser/Node/ComplexBody/Variant.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
use Tempest\Internationalization\MessageFormat\Parser\Node\Node;
66
use Tempest\Internationalization\MessageFormat\Parser\Node\Pattern\QuotedPattern;
77

8-
final class Variant implements Node
8+
final readonly class Variant implements Node
99
{
1010
/**
1111
* @param Key[] $keys
1212
*/
1313
public function __construct(
14-
public readonly array $keys,
15-
public readonly QuotedPattern $pattern,
14+
public array $keys,
15+
public QuotedPattern $pattern,
1616
) {}
1717
}

packages/i18n/src/MessageFormat/Parser/Node/Declaration/InputDeclaration.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
use Tempest\Internationalization\MessageFormat\Parser\Node\Expression\VariableExpression;
66

7-
final class InputDeclaration implements Declaration
7+
final readonly class InputDeclaration implements Declaration
88
{
99
public function __construct(
10-
public readonly VariableExpression $expression,
10+
public VariableExpression $expression,
1111
) {}
1212
}

packages/i18n/src/MessageFormat/Parser/Node/Declaration/LocalDeclaration.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
use Tempest\Internationalization\MessageFormat\Parser\Node\Expression\Expression;
66
use Tempest\Internationalization\MessageFormat\Parser\Node\Variable;
77

8-
final class LocalDeclaration implements Declaration
8+
final readonly class LocalDeclaration implements Declaration
99
{
1010
public function __construct(
11-
public readonly Variable $variable,
12-
public readonly Expression $expression,
11+
public Variable $variable,
12+
public Expression $expression,
1313
) {}
1414
}

packages/i18n/src/MessageFormat/Parser/Node/Expression/Attribute.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
use Tempest\Internationalization\MessageFormat\Parser\Node\Literal\Literal;
77
use Tempest\Internationalization\MessageFormat\Parser\Node\Node;
88

9-
final class Attribute implements Node
9+
final readonly class Attribute implements Node
1010
{
1111
public function __construct(
12-
public readonly Identifier $identifier,
13-
public readonly ?Literal $value,
12+
public Identifier $identifier,
13+
public ?Literal $value,
1414
) {}
1515
}

0 commit comments

Comments
 (0)