Skip to content
Merged
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
6 changes: 3 additions & 3 deletions .github/workflows/static.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:
- '.github/workflows/static.yml'
- 'src/**'
- 'config/**'
- 'psalm.xml'
- 'phpstan.neon'
- 'composer.json'
push:
branches: ['master']
Expand All @@ -20,8 +20,8 @@ concurrency:
cancel-in-progress: true

jobs:
psalm:
uses: yiisoft/actions/.github/workflows/psalm.yml@master
phpstan:
uses: yiisoft/actions/.github/workflows/phpstan.yml@master
with:
os: >-
['ubuntu-latest']
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## 3.2.2 under development

- Chg #171: Replace Psalm with PHPStan (@vjik)
- Enh #166: Explicitly import functions and constants in `use` section (@vjik)

## 3.2.1 December 06, 2025
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
[![Code Coverage](https://codecov.io/gh/yiisoft/translator/branch/master/graph/badge.svg)](https://codecov.io/gh/yiisoft/translator)
[![Mutation testing badge](https://img.shields.io/endpoint?style=flat&url=https%3A%2F%2Fbadge-api.stryker-mutator.io%2Fgithub.com%2Fyiisoft%2Ftranslator%2Fmaster)](https://dashboard.stryker-mutator.io/reports/github.com/yiisoft/translator/master)
[![static analysis](https://github.com/yiisoft/translator/workflows/static%20analysis/badge.svg)](https://github.com/yiisoft/translator/actions?query=workflow%3A%22static+analysis%22)
[![type-coverage](https://shepherd.dev/github/yiisoft/translator/coverage.svg)](https://shepherd.dev/github/yiisoft/translator)

This package allows translating messages into several languages. It can work with both Yii-based applications and
standalone PHP applications.
Expand Down
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"require-dev": {
"bamarni/composer-bin-plugin": "^1.9.1",
"friendsofphp/php-cs-fixer": "^3.95",
"phpstan/phpstan": "^2.1.14",
"phpunit/phpunit": "^9.6.29",
"rector/rector": "^2.1.7",
"shipmonk/composer-dependency-analyser": "^1.8",
Expand Down Expand Up @@ -85,6 +86,7 @@
"mutation": "infection --min-covered-msi=100 -j$(nproc)",
"cs-fix": "php-cs-fixer fix",
"rector": "rector",
"phpstan": "phpstan",
"dependency-analyser": "composer-dependency-analyser"
}
}
4 changes: 2 additions & 2 deletions docs/internals.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ The package tests are checked with [Infection](https://infection.github.io/) mut

## Static analysis

The code is statically analyzed with [Psalm](https://psalm.dev/). To run static analysis:
The code is statically analyzed with [PHPStan](https://phpstan.org/). To run static analysis:

```shell
./vendor/bin/psalm
./vendor/bin/phpstan
```

## Code style
Expand Down
4 changes: 4 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
parameters:
level: max
paths:
- src
21 changes: 0 additions & 21 deletions psalm.xml

This file was deleted.

12 changes: 4 additions & 8 deletions src/CategorySource.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function getName(): string
*
* @param string $id Message ID.
* @param string $locale Locale to get message for.
* @param array $parameters Message parameters.
* @param mixed[] $parameters Message parameters.
*
* @return string|null Message string or null if message was not found.
*/
Expand All @@ -57,9 +57,7 @@ public function getMessage(string $id, string $locale, array $parameters = []):
/**
* @param string $locale Locale of messages to get.
*
* @psalm-return array<string, array<string, string>>
*
* @return array All messages from category. The format is the following:
* @return array<string, array<string, string>> All messages from category. The format is the following:
*
* ```php
* [
Expand All @@ -86,10 +84,8 @@ public function getMessages(string $locale): array
/**
* Writes a set of messages for a specified category and locale.
*
* @psalm-param array<string, array<string, string>> $messages
*
* @param string $locale Locale to write messages for.
* @param array $messages A set of messages to write. The format is the following:
* @param array<string, array<string, string>> $messages A set of messages to write. The format is the following:
* ```php
* [
* 'key1' => [
Expand Down Expand Up @@ -119,7 +115,7 @@ public function write(string $locale, array $messages): void
* Format the message given parameters and locale.
*
* @param string $message Message to be formatted.
* @param array $parameters Parameters to use.
* @param mixed[] $parameters Parameters to use.
* @param string $locale Locale to use. Usually affects formatting numbers, dates etc.
* @param MessageFormatterInterface $defaultFormatter Message formatter that will be used if formatter not specified
* in message category.
Expand Down
46 changes: 25 additions & 21 deletions src/Extractor/ContentParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,27 @@
/**
* Extracts translation keys from a string given.
*
* @psalm-type TranslatorToken = string|array{0: int, 1: string, 2: int}
* @phpstan-type TranslatorToken = string|array{0: int, 1: string, 2: int}
*/
final class ContentParser
{
private string $translatorCall = '->translate';

/**
* @psalm-var TranslatorToken[]
* @var TranslatorToken[]
*/
private array $translatorTokens = [];

private int $translatorTokenCount = 0;

/** @var array<string, string> */
private static array $brackets = [
')' => '(',
']' => '[',
'}' => '{',
];

/** @var array<int, array{0: int|null, 1: string}> */
private array $skippedLines = [];

/**
Expand All @@ -55,7 +57,7 @@ public function __construct(private string $defaultCategory, ?string $translator
/**
* @param string $content Code to extract translation keys from.
*
* @return array Extracted messages.
* @return array<string, list<string>> Extracted messages.
*/
public function extract(string $content): array
{
Expand All @@ -82,17 +84,15 @@ public function hasSkippedLines(): bool
}

/**
* @return array Lines that were skipped during parsing.
* @return array<int, array{0: int|null, 1: string}> Lines that were skipped during parsing.
*
* The format is:
*
* ```php
* return [
* 'fileName' => [
* [
* int $numberOfLine,
* string $incorrectLine,
* ],
* [
* int $numberOfLine,
* string $incorrectLine,
* ],
* ]
* ```
Expand All @@ -116,9 +116,9 @@ private function setTranslator(string $translatorCall): void
}

/**
* @psalm-param array<integer, TranslatorToken> $tokens
* @param TranslatorToken[] $tokens
*
* @psalm-suppress UnusedVariable See https://github.com/vimeo/psalm/issues/9080
* @return array<string, list<string>>
*/
private function extractMessagesFromTokens(array $tokens): array
{
Expand All @@ -139,6 +139,7 @@ private function extractMessagesFromTokens(array $tokens): array
$skippedTokens = array_slice($tokens, $startTranslatorTokenIndex, $indexToken - $startTranslatorTokenIndex + 1);
$this->skippedLines[] = $this->getLinesData($skippedTokens);
} else {
/** @var array<string, list<string>> $messages */
$messages = array_merge_recursive($messages, $result);
}
$startTranslatorTokenIndex = 0;
Expand Down Expand Up @@ -172,7 +173,9 @@ private function extractMessagesFromTokens(array $tokens): array
}

/**
* @psalm-param TranslatorToken[] $tokens
* @param TranslatorToken[] $tokens
*
* @return array<string, list<string>>|null
*/
private function extractParametersFromTokens(array $tokens): ?array
{
Expand All @@ -186,16 +189,17 @@ private function extractParametersFromTokens(array $tokens): ?array

// Get translation messages from parameters
if (isset($parameters['parameters'])) {
/** @var array<string, list<string>> $messages */
$messages = array_merge_recursive($messages, $this->extractMessagesFromTokens($parameters['parameters']));
}

return $messages;
}

/**
* @psalm-param TranslatorToken[] $tokens
* @param TranslatorToken[] $tokens
*
* @psalm-return array{
* @return array{
* category?: null|string,
* id?: null|string,
* parameters?: null|list<TranslatorToken>
Expand Down Expand Up @@ -230,7 +234,7 @@ private function splitTokensAsParams(array $tokens): array
}

/**
* @psalm-param TranslatorToken[] $tokens
* @param TranslatorToken[] $tokens
*/
private function getMessageStringFromTokens(array $tokens): ?string
{
Expand Down Expand Up @@ -260,8 +264,8 @@ private function getMessageStringFromTokens(array $tokens): ?string
/**
* Finds out if two PHP tokens are equal.
*
* @psalm-param TranslatorToken $a
* @psalm-param TranslatorToken $b
* @param TranslatorToken $a
* @param TranslatorToken $b
*/
private function tokensEqual(array|string $a, array|string $b): bool
{
Expand All @@ -273,17 +277,17 @@ private function tokensEqual(array|string $a, array|string $b): bool
}

/**
* @psalm-param TranslatorToken[] $tokens
* @param TranslatorToken[] $tokens
*
* @return array{0: int|null, 1: string}
*/
private function getLinesData(array $tokens): array
{
$startLine = null;
$codeLines = '';
foreach ($tokens as $token) {
if (is_array($token)) {
if ($startLine === null) {
$startLine = $token[2];
}
$startLine ??= $token[2];
$codeLines .= $token[1];
} else {
$codeLines .= $token;
Expand Down
6 changes: 4 additions & 2 deletions src/Extractor/TranslationExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ final class TranslationExtractor
/** @var string[] */
private array $only = ['**.php'];

/** @var array<string, array<int, array{0: int|null, 1: string}>> */
private array $skippedLines = [];

/** @var string[] */
Expand Down Expand Up @@ -64,7 +65,7 @@ public function __construct(string $path, ?array $only = null, ?array $except =
* @param string $defaultCategory Category to use if category isn't set in translation call.
* @param string|null $translatorCall Translation call to look for.
*
* @return array Extracted messages.
* @return array<string, list<string>> Extracted messages.
*/
public function extract(string $defaultCategory = 'app', ?string $translatorCall = null): array
{
Expand All @@ -83,6 +84,7 @@ public function extract(string $defaultCategory = 'app', ?string $translatorCall
* @var string $fileContent We assume that `file_get_contents` can always read the file.
*/
$fileContent = file_get_contents($file);
/** @var array<string, list<string>> $messages */
$messages = array_merge_recursive($messages, $parser->extract($fileContent));
if ($parser->hasSkippedLines()) {
$this->skippedLines[$file] = $parser->getSkippedLines();
Expand All @@ -101,7 +103,7 @@ public function hasSkippedLines(): bool
}

/**
* @return array Lines that were skipped during parsing.
* @return array<string, array<int, array{0: int|null, 1: string}>> Lines that were skipped during parsing.
*
* The format is:
*
Expand Down
3 changes: 3 additions & 0 deletions src/IdMessageReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
*/
final class IdMessageReader implements MessageReaderInterface
{
/**
* @param mixed[] $parameters Parameters that may be used to decide about the message to obtain.
*/
public function getMessage(string $id, string $category, string $locale, array $parameters = []): string
{
return $id;
Expand Down
4 changes: 2 additions & 2 deletions src/InMemoryMessageSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
final class InMemoryMessageSource implements MessageReaderInterface, MessageWriterInterface
{
/**
* @psalm-var array<string, array<string, array<string, array<string, string>>>>
* @var array<string, array<string, array<string, array<string, string>>>>
*/
private array $messages = [];

Expand All @@ -27,7 +27,7 @@ public function getMessages(string $category, string $locale): array
}

/**
* @psalm-param array<string, array<string, string>|string> $messages
* @param array<string, array<string, string>|string> $messages
*/
public function write(string $category, string $locale, array $messages): void
{
Expand Down
3 changes: 1 addition & 2 deletions src/MessageFormatterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ interface MessageFormatterInterface
* Formats the message using parameters and taking locale into account.
*
* @param string $message A message to format.
* @param array $parameters Parameters to replace in the message in `['name1' => 'value1', 'name2' => 'value2']`
* @param mixed[] $parameters Parameters to replace in the message in `['name1' => 'value1', 'name2' => 'value2']`
* format.
* @psalm-param array<array-key, mixed> $parameters
*
* @param string $locale Locale to use when formatting. Usually affects formatting numbers, dates etc.
*
Expand Down
6 changes: 2 additions & 4 deletions src/MessageReaderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ interface MessageReaderInterface
* @param string $id ID of the message to get.
* @param string $category Category of the message to get.
* @param string $locale Locale of the message to get.
* @param array $parameters Parameters that may be used to decide about the message to obtain.
* @param mixed[] $parameters Parameters that may be used to decide about the message to obtain.
*
* @return string|null The translated message or null if translation wasn't found.
*/
Expand All @@ -26,9 +26,7 @@ public function getMessage(string $id, string $category, string $locale, array $
* @param string $category Category of messages to get.
* @param string $locale Locale of messages to get.
*
* @psalm-return array<string, array<string, string>>
*
* @return array All messages from category. The format is the following:
* @return array<string, array<string, string>> All messages from category. The format is the following:
*
* ```php
* [
Expand Down
4 changes: 1 addition & 3 deletions src/MessageWriterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ interface MessageWriterInterface
*
* @param string $category Category to write messages for.
* @param string $locale Locale to write messages for.
* @param array $messages A set of messages to write. The format is the following:
* @param array<string, array<string, string>> $messages A set of messages to write. The format is the following:
*
* ```php
* [
Expand All @@ -30,8 +30,6 @@ interface MessageWriterInterface
* ],
* ]
* ```
*
* @psalm-param array<string, array<string, string>> $messages
*/
public function write(string $category, string $locale, array $messages): void;
}
Loading