Skip to content

Commit

Permalink
Add rector and raise minimum PHP version to 8.0 (#69)
Browse files Browse the repository at this point in the history
* Add rector files yiisoft/yii-dev-tool#232

* Add rector/rector dependecy

* [rector] Apply fixes

* Apply fixes from StyleCI

* Use predefined rector action

* fix

* 8.0

Co-authored-by: rector-bot <[email protected]>
Co-authored-by: StyleCI Bot <[email protected]>
Co-authored-by: Sergei Predvoditelev <[email protected]>
  • Loading branch information
4 people authored Oct 17, 2022
1 parent 6093538 commit bbc2d60
Show file tree
Hide file tree
Showing 32 changed files with 121 additions and 251 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ jobs:
os: >-
['ubuntu-latest', 'windows-latest']
php: >-
['7.4', '8.0', '8.1']
['8.0', '8.1']
21 changes: 21 additions & 0 deletions .github/workflows/rector.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
on:
pull_request:
paths-ignore:
- 'docs/**'
- 'README.md'
- 'CHANGELOG.md'
- '.gitignore'
- '.gitattributes'
- 'infection.json.dist'
- 'psalm.xml'

name: rector

jobs:
rector:
uses: yiisoft/actions/.github/workflows/rector.yml@master
with:
os: >-
['ubuntu-latest']
php: >-
['8.0']
2 changes: 1 addition & 1 deletion .github/workflows/static.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ jobs:
os: >-
['ubuntu-latest']
php: >-
['7.4', '8.0', '8.1']
['8.0', '8.1']
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Enh #74: Dispatch `MissingTranslationCategoryEvent` once per category (@vjik)
- Enh #73: Set `en_US` as default locale for translator (@vjik)
- Enh #72: Use `SimpleMessageFormatter` to format messages when missing translation category (@vjik)
- Enh #69: Raise minimum PHP version to 8.0 (@xepozz, @vjik)

## 1.1.1 September 09, 2022

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ standalone PHP applications.

## Requirements

- PHP 7.4 or higher.
- PHP 8.0 or higher.

## Installation

Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@
"source": "https://github.com/yiisoft/translator"
},
"require": {
"php": "^7.4|^8.0",
"php": "^8.0",
"psr/event-dispatcher": "1.0.0",
"yiisoft/files": "^1.0|^2.0",
"yiisoft/i18n": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "^9.5",
"rector/rector": "^0.14.3",
"roave/infection-static-analysis-plugin": "^1.16",
"spatie/phpunit-watcher": "^1.23",
"vimeo/psalm": "^4.18"
Expand Down
27 changes: 27 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

use Rector\CodeQuality\Rector\Class_\InlineConstructorDefaultToPropertyRector;
use Rector\Config\RectorConfig;
use Rector\Php80\Rector\FuncCall\TokenGetAllToObjectRector;
use Rector\Set\ValueObject\LevelSetList;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->paths([
__DIR__ . '/src',
__DIR__ . '/tests',
]);

// register a single rule
$rectorConfig->rule(InlineConstructorDefaultToPropertyRector::class);

// define sets of rules
$rectorConfig->sets([
LevelSetList::UP_TO_PHP_80,
]);

$rectorConfig->skip([
TokenGetAllToObjectRector::class,
]);
};
6 changes: 1 addition & 5 deletions src/CategorySource.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,18 @@
final class CategorySource
{
private string $name;
private MessageReaderInterface $reader;
private MessageFormatterInterface $formatter;

/**
* @param string $name Category name.
* @param MessageReaderInterface $reader Message reader to get messages from for this category.
* @param MessageFormatterInterface $formatter Message formatter to format messages with for this category.
*/
public function __construct(string $name, MessageReaderInterface $reader, MessageFormatterInterface $formatter)
public function __construct(string $name, private MessageReaderInterface $reader, private MessageFormatterInterface $formatter)
{
if (!preg_match('/^[a-z0-9_-]+$/i', $name)) {
throw new RuntimeException('Category name is invalid. Only letters and numbers are allowed.');
}
$this->name = $name;
$this->reader = $reader;
$this->formatter = $formatter;
}

/**
Expand Down
5 changes: 1 addition & 4 deletions src/Event/MissingTranslationCategoryEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,11 @@
*/
final class MissingTranslationCategoryEvent
{
private string $category;

/**
* @param string $category Category that is missing.
*/
public function __construct(string $category)
public function __construct(private string $category)
{
$this->category = $category;
}

/**
Expand Down
9 changes: 1 addition & 8 deletions src/Event/MissingTranslationEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,13 @@
*/
final class MissingTranslationEvent
{
private string $category;
private string $language;
private string $message;

/**
* @param string $category Category of the missing translation.
* @param string $language Language of the missing translation.
* @param string $message Message of the missing translation.
*/
public function __construct(string $category, string $language, string $message)
public function __construct(private string $category, private string $language, private string $message)
{
$this->category = $category;
$this->language = $language;
$this->message = $message;
}

/**
Expand Down
5 changes: 1 addition & 4 deletions src/Extractor/ContentParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ final class ContentParser

private int $translatorTokenCount = 0;

private string $defaultCategory;

private static array $brackets = [
')' => '(',
']' => '[',
Expand All @@ -43,9 +41,8 @@ final class ContentParser
* @param string|null $translator A string containing a method call that translates the message. If not specified,
* "->translate" is assumed.
*/
public function __construct(string $defaultCategory, ?string $translator = null)
public function __construct(private string $defaultCategory, ?string $translator = null)
{
$this->defaultCategory = $defaultCategory;
$this->setTranslator($translator ?? $this->translatorCall);
}

Expand Down
10 changes: 1 addition & 9 deletions src/SimpleMessageFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,7 @@ public function format(string $message, array $parameters, string $locale = 'en_
return strtr($message, $replacements);
}

/**
* @param mixed $value
* @param string $options
*
* @return string
*/
private static function pluralize($value, string $options): string
private static function pluralize(mixed $value, string $options): string
{
if (!$options) {
throw new InvalidArgumentException('Missing plural keys: ' . self::formatList(self::PLURAL_KEYS) . '.');
Expand Down Expand Up @@ -93,8 +87,6 @@ private static function pluralize($value, string $options): string

/**
* @param string[] $items
*
* @return string
*/
private static function formatList(array $items): string
{
Expand Down
16 changes: 5 additions & 11 deletions src/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@
final class Translator implements TranslatorInterface
{
private string $defaultCategory = 'app';
private string $locale;
private ?string $fallbackLocale;
private ?EventDispatcherInterface $eventDispatcher;

/**
* @var CategorySource[][] Array of category message sources indexed by category names.
Expand All @@ -38,13 +35,10 @@ final class Translator implements TranslatorInterface
* @param EventDispatcherInterface|null $eventDispatcher Event dispatcher for translation events. Null for none.
*/
public function __construct(
string $locale = 'en_US',
?string $fallbackLocale = null,
?EventDispatcherInterface $eventDispatcher = null
private string $locale = 'en_US',
private ?string $fallbackLocale = null,
private ?EventDispatcherInterface $eventDispatcher = null
) {
$this->locale = $locale;
$this->fallbackLocale = $fallbackLocale;
$this->eventDispatcher = $eventDispatcher;
}

public function addCategorySource(CategorySource $category): void
Expand Down Expand Up @@ -79,9 +73,9 @@ public function translate(
string $category = null,
string $locale = null
): string {
$locale = $locale ?? $this->locale;
$locale ??= $this->locale;

$category = $category ?? $this->defaultCategory;
$category ??= $this->defaultCategory;

if (empty($this->categorySources[$category])) {
$this->dispatchMissingTranslationCategoryEvent($category);
Expand Down
13 changes: 1 addition & 12 deletions tests/TranslatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -302,12 +302,6 @@ public function manyTranslations(): array

/**
* @dataProvider manyTranslations
*
* @param string $expected
* @param string $id
* @param array $params
* @param string|null $category
* @param string|null $locale
*/
public function testManySourcesForSingleCategory(
string $expected,
Expand Down Expand Up @@ -572,13 +566,8 @@ private function createCategory(string $categoryName, array $messages = []): Cat
private function createMessageReader(string $category, array $messages): MessageReaderInterface
{
return new class ($category, $messages) implements MessageReaderInterface {
private string $category;
private array $messages;

public function __construct(string $category, array $messages)
public function __construct(private string $category, private array $messages)
{
$this->category = $category;
$this->messages = $messages;
}

public function getMessage(string $id, string $category, string $locale, array $parameters = []): ?string
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
$translator::translate('messageId1', array(1, 2));
$translator::translate('messageId1', [1, 2]);
$translator::translate('messageId2', [1, 2]);
$translator::translate('message' . 'Id3', array(1, 2));
$translator::translate('message' . 'Id3', [1, 2]);
$translator::translate('message' . 'Id4', [1, 2]);

18 changes: 9 additions & 9 deletions tests/extractorExamples/synthetic/correctSamples/test.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
->translate('');

// withoutCategory
$translator->translate('messageId1', array(1, 2));
$translator->translate('messageId1', [1, 2]);
$translator->translate('messageId2', [1, 2]);
$translator->translate('message' . 'Id3', array(1, 2));
$translator->translate('message' . 'Id3', [1, 2]);
$translator->translate('message' . 'Id4', [1, 2]);

$translator->translate('message' . 'Id' . 5, array(1, 2));
$translator->translate('message' . 'Id' . 5, [1, 2]);
$translator->translate('message' . 'Id' . 6, [1, 2]);

$translator->translate('messageId7');
Expand All @@ -32,12 +32,12 @@
$translator->translate('message' . 'Id' . 1 . '7', ['test' => $translator->translate('messageId' . 18)]);

// With categoryName
$translator->translate('messageId1', array(1, 2), 'categoryName');
$translator->translate('messageId1', [1, 2], 'categoryName');
$translator->translate('messageId2', [1, 2], 'categoryName');
$translator->translate('message' . 'Id3', array(1, 2), 'categoryName');
$translator->translate('message' . 'Id3', [1, 2], 'categoryName');
$translator->translate('message' . 'Id4', [1, 2], 'categoryName');

$translator->translate('message' . 'Id' . 5, array(1, 2), 'categoryName');
$translator->translate('message' . 'Id' . 5, [1, 2], 'categoryName');
$translator->translate('message' . 'Id' . 6, [1, 2], 'categoryName');

$translator->translate('messageId7', [], 'categoryName');
Expand All @@ -55,12 +55,12 @@
$translator->translate('message' . 'Id' . 1 . '7', ['test' => $translator->translate('messageId' . 19)], 'categoryName');

// With categoryName and complex params
$translator->translate('messageId1', array('1', 2), 'categoryName2');
$translator->translate('messageId1', ['1', 2], 'categoryName2');
$translator->translate('messageId2', [1, '2'], 'categoryName2');
$translator->translate('message' . 'Id3', array("1", '2'), 'categoryName2');
$translator->translate('message' . 'Id3', ["1", '2'], 'categoryName2');
$translator->translate('message' . 'Id4', ["1", '2'], 'categoryName2');

$translator->translate('message' . "Id" . 5, array(null, 2), 'categoryName2');
$translator->translate('message' . "Id" . 5, [null, 2], 'categoryName2');
$translator->translate("message" . 'Id' . 6, [null, 2], 'categoryName2');

$translator->translate('messageId7', ['n' => 1], 'categoryName2');
Expand Down
37 changes: 11 additions & 26 deletions tests/extractorExamples/user-main/src/ActiveRecord/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,38 +54,23 @@ public function getUserId(): string

public function toUrl(): string
{
switch ($this->getAttribute('type')) {
case self::TYPE_CONFIRMATION:
$route = 'confirm';
break;
case self::TYPE_RECOVERY:
$route = 'reset';
break;
case self::TYPE_CONFIRM_NEW_EMAIL:
case self::TYPE_CONFIRM_OLD_EMAIL:
$route = 'email/attempt';
break;
default:
throw new RuntimeException('Url not available.');
}
$route = match ($this->getAttribute('type')) {
self::TYPE_CONFIRMATION => 'confirm',
self::TYPE_RECOVERY => 'reset',
self::TYPE_CONFIRM_NEW_EMAIL, self::TYPE_CONFIRM_OLD_EMAIL => 'email/attempt',
default => throw new RuntimeException('Url not available.'),
};

return $route;
}

public function isExpired(int $tokenConfirmWithin = 0, int $tokenRecoverWithin = 0): bool
{
switch ($this->getAttribute('type')) {
case self::TYPE_CONFIRMATION:
case self::TYPE_CONFIRM_NEW_EMAIL:
case self::TYPE_CONFIRM_OLD_EMAIL:
$expirationTime = $tokenConfirmWithin;
break;
case self::TYPE_RECOVERY:
$expirationTime = $tokenRecoverWithin;
break;
default:
throw new RuntimeException('Expired not available.');
}
$expirationTime = match ($this->getAttribute('type')) {
self::TYPE_CONFIRMATION, self::TYPE_CONFIRM_NEW_EMAIL, self::TYPE_CONFIRM_OLD_EMAIL => $tokenConfirmWithin,
self::TYPE_RECOVERY => $tokenRecoverWithin,
default => throw new RuntimeException('Expired not available.'),
};

return ($this->created_at + $expirationTime) < time();
}
Expand Down
Loading

0 comments on commit bbc2d60

Please sign in to comment.