diff --git a/.github/workflows/static.yml b/.github/workflows/static.yml index e199408..2589fa4 100644 --- a/.github/workflows/static.yml +++ b/.github/workflows/static.yml @@ -6,7 +6,7 @@ on: - '.github/workflows/static.yml' - 'src/**' - 'config/**' - - 'psalm.xml' + - 'phpstan.neon' - 'composer.json' push: branches: ['master'] @@ -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'] diff --git a/CHANGELOG.md b/CHANGELOG.md index d8457d2..2600865 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index ca84869..5fcb8a2 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/composer.json b/composer.json index 7eb6181..f31a5ae 100644 --- a/composer.json +++ b/composer.json @@ -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", @@ -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" } } diff --git a/docs/internals.md b/docs/internals.md index b77907c..4ae9aeb 100644 --- a/docs/internals.md +++ b/docs/internals.md @@ -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 diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..776ccd8 --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,4 @@ +parameters: + level: max + paths: + - src diff --git a/psalm.xml b/psalm.xml deleted file mode 100644 index 2b2e0b2..0000000 --- a/psalm.xml +++ /dev/null @@ -1,21 +0,0 @@ - - - - - - - - - - - - - diff --git a/src/CategorySource.php b/src/CategorySource.php index fa5034c..5fc827a 100644 --- a/src/CategorySource.php +++ b/src/CategorySource.php @@ -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. */ @@ -57,9 +57,7 @@ public function getMessage(string $id, string $locale, array $parameters = []): /** * @param string $locale Locale of messages to get. * - * @psalm-return array> - * - * @return array All messages from category. The format is the following: + * @return array> All messages from category. The format is the following: * * ```php * [ @@ -86,10 +84,8 @@ public function getMessages(string $locale): array /** * Writes a set of messages for a specified category and locale. * - * @psalm-param array> $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> $messages A set of messages to write. The format is the following: * ```php * [ * 'key1' => [ @@ -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. diff --git a/src/Extractor/ContentParser.php b/src/Extractor/ContentParser.php index 7fd83ec..e76f897 100644 --- a/src/Extractor/ContentParser.php +++ b/src/Extractor/ContentParser.php @@ -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 */ private static array $brackets = [ ')' => '(', ']' => '[', '}' => '{', ]; + /** @var array */ private array $skippedLines = []; /** @@ -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> Extracted messages. */ public function extract(string $content): array { @@ -82,17 +84,15 @@ public function hasSkippedLines(): bool } /** - * @return array Lines that were skipped during parsing. + * @return array Lines that were skipped during parsing. * * The format is: * * ```php * return [ - * 'fileName' => [ - * [ - * int $numberOfLine, - * string $incorrectLine, - * ], + * [ + * int $numberOfLine, + * string $incorrectLine, * ], * ] * ``` @@ -116,9 +116,9 @@ private function setTranslator(string $translatorCall): void } /** - * @psalm-param array $tokens + * @param TranslatorToken[] $tokens * - * @psalm-suppress UnusedVariable See https://github.com/vimeo/psalm/issues/9080 + * @return array> */ private function extractMessagesFromTokens(array $tokens): array { @@ -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> $messages */ $messages = array_merge_recursive($messages, $result); } $startTranslatorTokenIndex = 0; @@ -172,7 +173,9 @@ private function extractMessagesFromTokens(array $tokens): array } /** - * @psalm-param TranslatorToken[] $tokens + * @param TranslatorToken[] $tokens + * + * @return array>|null */ private function extractParametersFromTokens(array $tokens): ?array { @@ -186,6 +189,7 @@ private function extractParametersFromTokens(array $tokens): ?array // Get translation messages from parameters if (isset($parameters['parameters'])) { + /** @var array> $messages */ $messages = array_merge_recursive($messages, $this->extractMessagesFromTokens($parameters['parameters'])); } @@ -193,9 +197,9 @@ private function extractParametersFromTokens(array $tokens): ?array } /** - * @psalm-param TranslatorToken[] $tokens + * @param TranslatorToken[] $tokens * - * @psalm-return array{ + * @return array{ * category?: null|string, * id?: null|string, * parameters?: null|list @@ -230,7 +234,7 @@ private function splitTokensAsParams(array $tokens): array } /** - * @psalm-param TranslatorToken[] $tokens + * @param TranslatorToken[] $tokens */ private function getMessageStringFromTokens(array $tokens): ?string { @@ -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 { @@ -273,7 +277,9 @@ 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 { @@ -281,9 +287,7 @@ private function getLinesData(array $tokens): array $codeLines = ''; foreach ($tokens as $token) { if (is_array($token)) { - if ($startLine === null) { - $startLine = $token[2]; - } + $startLine ??= $token[2]; $codeLines .= $token[1]; } else { $codeLines .= $token; diff --git a/src/Extractor/TranslationExtractor.php b/src/Extractor/TranslationExtractor.php index 3382363..9a0b7dd 100644 --- a/src/Extractor/TranslationExtractor.php +++ b/src/Extractor/TranslationExtractor.php @@ -20,6 +20,7 @@ final class TranslationExtractor /** @var string[] */ private array $only = ['**.php']; + /** @var array> */ private array $skippedLines = []; /** @var string[] */ @@ -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> Extracted messages. */ public function extract(string $defaultCategory = 'app', ?string $translatorCall = null): array { @@ -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> $messages */ $messages = array_merge_recursive($messages, $parser->extract($fileContent)); if ($parser->hasSkippedLines()) { $this->skippedLines[$file] = $parser->getSkippedLines(); @@ -101,7 +103,7 @@ public function hasSkippedLines(): bool } /** - * @return array Lines that were skipped during parsing. + * @return array> Lines that were skipped during parsing. * * The format is: * diff --git a/src/IdMessageReader.php b/src/IdMessageReader.php index f077b3e..5fb5b27 100644 --- a/src/IdMessageReader.php +++ b/src/IdMessageReader.php @@ -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; diff --git a/src/InMemoryMessageSource.php b/src/InMemoryMessageSource.php index 25c0c12..2ad5c25 100644 --- a/src/InMemoryMessageSource.php +++ b/src/InMemoryMessageSource.php @@ -12,7 +12,7 @@ final class InMemoryMessageSource implements MessageReaderInterface, MessageWriterInterface { /** - * @psalm-var array>>> + * @var array>>> */ private array $messages = []; @@ -27,7 +27,7 @@ public function getMessages(string $category, string $locale): array } /** - * @psalm-param array|string> $messages + * @param array|string> $messages */ public function write(string $category, string $locale, array $messages): void { diff --git a/src/MessageFormatterInterface.php b/src/MessageFormatterInterface.php index a0baa5f..c1f5e71 100644 --- a/src/MessageFormatterInterface.php +++ b/src/MessageFormatterInterface.php @@ -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 $parameters * * @param string $locale Locale to use when formatting. Usually affects formatting numbers, dates etc. * diff --git a/src/MessageReaderInterface.php b/src/MessageReaderInterface.php index 4ba9c53..97b66e6 100644 --- a/src/MessageReaderInterface.php +++ b/src/MessageReaderInterface.php @@ -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. */ @@ -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> - * - * @return array All messages from category. The format is the following: + * @return array> All messages from category. The format is the following: * * ```php * [ diff --git a/src/MessageWriterInterface.php b/src/MessageWriterInterface.php index 76fcc64..2de11cb 100644 --- a/src/MessageWriterInterface.php +++ b/src/MessageWriterInterface.php @@ -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> $messages A set of messages to write. The format is the following: * * ```php * [ @@ -30,8 +30,6 @@ interface MessageWriterInterface * ], * ] * ``` - * - * @psalm-param array> $messages */ public function write(string $category, string $locale, array $messages): void; } diff --git a/src/SimpleMessageFormatter.php b/src/SimpleMessageFormatter.php index 7c9623a..926c192 100644 --- a/src/SimpleMessageFormatter.php +++ b/src/SimpleMessageFormatter.php @@ -17,19 +17,8 @@ */ class SimpleMessageFormatter implements MessageFormatterInterface { - /** - * @psalm-suppress MissingClassConstType - */ private const PLURAL_ONE = 'one'; - - /** - * @psalm-suppress MissingClassConstType - */ private const PLURAL_OTHER = 'other'; - - /** - * @psalm-suppress MissingClassConstType - */ private const PLURAL_KEYS = [self::PLURAL_ONE, self::PLURAL_OTHER]; public function format(string $message, array $parameters, string $locale = 'en-US'): string diff --git a/src/Translator.php b/src/Translator.php index cb6b335..b64af4a 100644 --- a/src/Translator.php +++ b/src/Translator.php @@ -19,13 +19,12 @@ final class Translator implements TranslatorInterface private MessageFormatterInterface $defaultMessageFormatter; /** - * @var array Array of category message sources indexed by category names. - * @psalm-var array> + * @var array> Array of category message sources indexed by category names. */ private array $categorySources = []; /** - * @psalm-var array + * @var array */ private array $dispatchedMissingTranslationCategoryEvents = []; @@ -105,6 +104,9 @@ public function withLocale(string $locale): static return $new; } + /** + * @param mixed[] $parameters Message parameters. + */ private function translateUsingCategorySources( string $id, array $parameters, diff --git a/src/TranslatorInterface.php b/src/TranslatorInterface.php index f2f95e0..55673c0 100644 --- a/src/TranslatorInterface.php +++ b/src/TranslatorInterface.php @@ -35,8 +35,7 @@ public function getLocale(): string; * * @param string|Stringable $id The ID of the message to be translated. It can be either artificial ID or the source * message. - * @param array $parameters An array of parameters for the message. - * @psalm-param array $parameters + * @param mixed[] $parameters An array of parameters for the message. * * @param string|null $category The message category. Null means default category. * @param string|null $locale The target locale. Null means default locale. @@ -52,15 +51,11 @@ public function translate( /** * Get a new translator instance with category to be used by default in case category isn't specified explicitly. - * - * @return $this */ public function withDefaultCategory(string $category): static; /** * Get a new translator instance with locale to be used by default in case locale isn't specified explicitly. - * - * @return $this */ public function withLocale(string $locale): static; } diff --git a/tests/ConfigTest.php b/tests/ConfigTest.php index 420b384..74e0e9d 100644 --- a/tests/ConfigTest.php +++ b/tests/ConfigTest.php @@ -59,9 +59,7 @@ private function createContainer(?array $params = null, bool $withCategorySource private function getContainerDefinitions(?array $params, bool $withCategorySources): array { - if ($params === null) { - $params = $this->getParams(); - } + $params ??= $this->getParams(); $common = require dirname(__DIR__) . '/config/di.php'; if ($withCategorySources) { diff --git a/tests/extractorExamples/user-main/src/Form/FormEmailChange.php b/tests/extractorExamples/user-main/src/Form/FormEmailChange.php index af9b63a..7dc8c3c 100644 --- a/tests/extractorExamples/user-main/src/Form/FormEmailChange.php +++ b/tests/extractorExamples/user-main/src/Form/FormEmailChange.php @@ -20,9 +20,6 @@ final class FormEmailChange extends FormModel private string $oldEmail = ''; private User $identity; - /** - * @psalm-suppress PropertyTypeCoercion - */ public function __construct( Identity $identity, private RepositoryUser $repositoryUser, diff --git a/tests/extractorExamples/user-main/src/Repository/RepositoryToken.php b/tests/extractorExamples/user-main/src/Repository/RepositoryToken.php index 00d3630..cf01187 100644 --- a/tests/extractorExamples/user-main/src/Repository/RepositoryToken.php +++ b/tests/extractorExamples/user-main/src/Repository/RepositoryToken.php @@ -59,7 +59,6 @@ public function register(string $id, int $typeToken): bool /** @var Connection $db */ $db = $this->activeRecordFactory->getConnection(); - /** @psalm-suppress UndefinedInterfaceMethod */ $transaction = $db->beginTransaction(); try { diff --git a/tests/extractorExamples/user-main/src/Repository/RepositoryUser.php b/tests/extractorExamples/user-main/src/Repository/RepositoryUser.php index 11f077c..f1940a9 100644 --- a/tests/extractorExamples/user-main/src/Repository/RepositoryUser.php +++ b/tests/extractorExamples/user-main/src/Repository/RepositoryUser.php @@ -39,9 +39,6 @@ final class RepositoryUser implements IdentityRepositoryInterface private Token $token; private User $user; - /** - * @psalm-suppress PropertyTypeCoercion - */ public function __construct( private ActiveRecordFactory $activeRecordFactory, private Aliases $aliases, @@ -55,7 +52,6 @@ public function __construct( /** * @return IdentityInterface|null - * @psalm-suppress InvalidReturnType, InvalidReturnStatement */ public function findIdentity(string $id): ?IdentityInterface { @@ -185,7 +181,6 @@ public function register(FormRegister $formRegister, bool $isConfirmation, bool * @param int $length * * {@see https://gist.github.com/tylerhall/521810} - * @psalm-suppress MixedOperand, PossiblyInvalidArrayOffset */ private function generate(int $length): string { diff --git a/tools/psalm/composer.json b/tools/psalm/composer.json deleted file mode 100644 index 0cbdf97..0000000 --- a/tools/psalm/composer.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "require-dev": { - "vimeo/psalm": "^5.26.1 || ^6.8.9" - } -}