Skip to content

Commit

Permalink
Better type inference for psalm, updated pslam baseline
Browse files Browse the repository at this point in the history
Signed-off-by: Kevin Hamilton <[email protected]>
  • Loading branch information
TotalWipeOut committed May 23, 2024
1 parent 5c4f32d commit da1f695
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 37 deletions.
10 changes: 8 additions & 2 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,16 @@
<code><![CDATA[$container]]></code>
</ParamNameMismatch>
</file>
<file src="src/Translator/PlaceholderPluginManagerFactory.php">
<file src="src/Translator/FormatterPluginManagerFactory.php">
<DeprecatedClass>
<code><![CDATA[new Config($config['translator_formatter'])]]></code>
</DeprecatedClass>
<MixedArgument>
<code><![CDATA[$config['translator_placeholders']]]></code>
<code><![CDATA[$config['translator_formatter']]]></code>
</MixedArgument>
<MoreSpecificImplementedParamType>
<code><![CDATA[$options]]></code>
</MoreSpecificImplementedParamType>
</file>
<file src="src/Translator/Plural/Parser.php">
<MixedArgument>
Expand Down
4 changes: 2 additions & 2 deletions src/Translator/Formatter/FormatterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
interface FormatterInterface
{
/**
* @param iterable<array-key, string> $placeholders
* @param iterable<array-key, string> $params
*/
public function format(string $locale, string $message, iterable $placeholders = []): string;
public function format(string $locale, string $message, iterable $params = []): string;
}
4 changes: 2 additions & 2 deletions src/Translator/Formatter/HandlebarFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@

class HandlebarFormatter implements FormatterInterface
{
public function format(string $locale, string $message, iterable $placeholders = []): string
public function format(string $locale, string $message, iterable $params = []): string
{
$compiled = $message;
foreach ($placeholders as $key => $value) {
foreach ($params as $key => $value) {
$compiled = str_replace("{{{$key}}}", $value, $compiled);
}

Expand Down
8 changes: 4 additions & 4 deletions src/Translator/Formatter/IcuFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@

class IcuFormatter implements FormatterInterface
{
public function format(string $locale, string $message, iterable $placeholders = []): string
public function format(string $locale, string $message, iterable $params = []): string
{
if ($placeholders instanceof Traversable) {
$placeholders = iterator_to_array($placeholders);
if ($params instanceof Traversable) {
$params = iterator_to_array($params);
}

return MessageFormatter::formatMessage($locale, $message, $placeholders);
return MessageFormatter::formatMessage($locale, $message, $params);
}
}
8 changes: 4 additions & 4 deletions src/Translator/Formatter/PrintfFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@

class PrintfFormatter implements FormatterInterface
{
public function format(string $locale, string $message, iterable $placeholders = []): string
public function format(string $locale, string $message, iterable $params = []): string
{
if ($placeholders instanceof Traversable) {
$placeholders = iterator_to_array($placeholders);
if ($params instanceof Traversable) {
$params = iterator_to_array($params);
}

/** @var string|false $compiled */
$compiled = call_user_func_array('vsprintf', [$message, $placeholders]);
$compiled = call_user_func_array('vsprintf', [$message, $params]);
if ($compiled === false) {
throw new ParseException(
'Error occurred while processing sprintf placeholders for message "' . $message . '"'
Expand Down
14 changes: 7 additions & 7 deletions src/Translator/Formatter/SegmentFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@

class SegmentFormatter implements FormatterInterface
{
public function format(string $locale, string $message, iterable $placeholders = []): string
public function format(string $locale, string $message, iterable $params = []): string
{
if ($placeholders instanceof Traversable) {
$placeholders = iterator_to_array($placeholders);
if ($params instanceof Traversable) {
$params = iterator_to_array($params);
}

if (empty($placeholders)) {
if (empty($params)) {
return $message;
}

if (! ArrayUtils::hasStringKeys($placeholders)) {
if (! ArrayUtils::hasStringKeys($params)) {
throw new InvalidArgumentException(
'SegmentPlaceholder expects an associative array of placeholder names and values'
);
Expand All @@ -38,12 +38,12 @@ public function format(string $locale, string $message, iterable $placeholders =
try {
// Sorting the array by key length to replace placeholders with longer names first
// to avoid replacing placeholders with shorter names that are part of longer names
uksort($placeholders, static function (string|int $a, string|int $b) {
uksort($params, static function (string|int $a, string|int $b) {
return strlen((string) $a) <=> strlen((string) $b);
});

$compiled = $message;
foreach ($placeholders as $key => $value) {
foreach ($params as $key => $value) {
$key = (string) $key;
$compiled = str_replace([':' . $key, ':' . strtoupper($key), ':' . ucfirst($key)], [
$value,
Expand Down
9 changes: 4 additions & 5 deletions src/Translator/FormatterPluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
use function sprintf;

/**
* Plugin manager implementation for translation placeholder compilers.
* Plugin manager implementation for translation message formatters.
*
* Enforces that placeholder compilers retrieved are either instances of
* Placeholder\PlaceholderInterface. Additionally, it registers a number
* of default placeholder compilers.
* Formatter\FormatterInterface. Additionally, it registers a number
* of default message formatters.
*
* @template InstanceType of Formatter\FormatterInterface
* @extends AbstractPluginManager<InstanceType>
Expand Down Expand Up @@ -49,8 +49,7 @@ class FormatterPluginManager extends AbstractPluginManager
/**
* Validate the plugin.
*
* Checks that the filter loaded is an instance of
* Loader\FileLoaderInterface or Loader\RemoteLoaderInterface.
* Checks that the filter loaded is an instance of Formatter\FormatterInterface
*
* @throws Exception\RuntimeException If invalid.
* @psalm-assert RemoteLoaderInterface $instance
Expand Down
3 changes: 1 addition & 2 deletions src/Translator/FormatterPluginManagerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@ final class FormatterPluginManagerFactory implements FactoryInterface
{
/**
* @param string $requestedName
* @param array<array-key, mixed>|null $options
* @psalm-param ServiceManagerConfiguration|null $options
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @psalm-suppress DeprecatedClass
*/
public function __invoke(
ContainerInterface $container,
Expand Down
36 changes: 29 additions & 7 deletions src/Translator/TranslatorFormatterDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
namespace Laminas\I18n\Translator;

use Laminas\I18n\Translator\Formatter\FormatterInterface;
use Locale;

use function is_string;
use function method_exists;

final class TranslatorFormatterDecorator implements TranslatorInterface
{
Expand All @@ -24,7 +28,9 @@ public function translate(
$locale = null,
iterable $params = []
): string {
$locale ??= $this->translator->getLocale();
if ($locale === null) {
$locale = $this->getLocale();
}

return $this->formatMessage($this->translator->translate($message, $textDomain, $locale), $params, $locale);
}
Expand All @@ -33,8 +39,8 @@ public function translate(
* @param string $singular
* @param string $plural
* @param int $number
* @param string $textDomain
* @param string $locale
* @param string|null $textDomain
* @param string|null $locale
* @param iterable<array-key, string> $params
*/
public function translatePlural(
Expand All @@ -45,7 +51,9 @@ public function translatePlural(
$locale = null,
iterable $params = []
): string {
$locale ??= $this->translator->getLocale();
if ($locale === null) {
$locale = $this->getLocale();
}

return $this->formatMessage(
$this->translatePlural($singular, $plural, $number, $textDomain, $locale),
Expand All @@ -55,10 +63,24 @@ public function translatePlural(
}

/**
* @param iterable<string|int, string> $placeholders
* @param iterable<string|int, string> $params
*/
protected function formatMessage(string $message, iterable $placeholders, string $locale): string
protected function formatMessage(string $message, iterable $params, string $locale): string
{
return $message !== '' ? $this->formatter->format($locale, $message, $placeholders) : $message;
return $message !== '' ? $this->formatter->format($locale, $message, $params) : $message;
}

protected function getLocale(): string
{
$locale = null;
if (method_exists($this->translator, 'getLocale')) {
/** @var string|null $translatorLocale */
$translatorLocale = $this->translator->getLocale();
if (is_string($translatorLocale)) {
$locale = $translatorLocale;
}
}

return $locale ?? Locale::getDefault();
}
}
1 change: 0 additions & 1 deletion src/View/Helper/TranslatePluralWithParams.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Laminas\I18n\Exception;
use Laminas\I18n\Translator\TranslatorFormatterDecorator;
use Laminas\I18n\Translator\TranslatorWithParamsInterface;

/**
* View helper for translating messages with placeholders.
Expand Down
1 change: 0 additions & 1 deletion src/View/Helper/TranslateWithParams.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Laminas\I18n\Exception;
use Laminas\I18n\Translator\TranslatorFormatterDecorator;
use Laminas\I18n\Translator\TranslatorWithParamsInterface;

/**
* View helper for translating messages with placeholders.
Expand Down

0 comments on commit da1f695

Please sign in to comment.