Skip to content

Commit

Permalink
Add typehint and final
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet committed Aug 6, 2022
1 parent 994ad25 commit 53d0ca0
Show file tree
Hide file tree
Showing 18 changed files with 185 additions and 181 deletions.
6 changes: 2 additions & 4 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,14 @@
* @author Thomas Rabaix <[email protected]>
* @author Alexander <[email protected]>
*/
class Configuration implements ConfigurationInterface
final class Configuration implements ConfigurationInterface
{
/**
* @psalm-suppress PossiblyNullReference, PossiblyUndefinedMethod
*
* @see https://github.com/psalm/psalm-plugin-symfony/issues/174
*
* @return TreeBuilder
*/
public function getConfigTreeBuilder()
public function getConfigTreeBuilder(): TreeBuilder
{
$treeBuilder = new TreeBuilder('sonata_intl');
$rootNode = $treeBuilder->getRootNode();
Expand Down
8 changes: 2 additions & 6 deletions src/DependencyInjection/SonataIntlExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,10 @@
use Symfony\Component\HttpKernel\DependencyInjection\Extension;

/**
* SonataIntlExtension.
*
* @author Thomas Rabaix <[email protected]>
* @author Alexander <[email protected]>
*/
class SonataIntlExtension extends Extension
final class SonataIntlExtension extends Extension
{
/**
* @param array<mixed> $configs
Expand All @@ -47,7 +45,7 @@ public function load(array $configs, ContainerBuilder $container): void
/**
* @param mixed[] $config
*/
protected function configureTimezone(ContainerBuilder $container, array $config): void
private function configureTimezone(ContainerBuilder $container, array $config): void
{
if (isset($config['timezone']['service'])) {
$container->setAlias('sonata.intl.timezone_detector', $config['timezone']['service']);
Expand Down Expand Up @@ -88,8 +86,6 @@ protected function configureTimezone(ContainerBuilder $container, array $config)
}

/**
* Validate timezones.
*
* @param array<string> $timezones
*
* @throws \RuntimeException If one of the locales is invalid
Expand Down
23 changes: 2 additions & 21 deletions src/Helper/BaseHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,32 +29,20 @@
*/
abstract class BaseHelper implements LocaleAwareInterface
{
protected string $charset = 'UTF-8';
private string $charset = 'UTF-8';

/**
* @var string|null
*/
protected $locale;
private ?string $locale = null;

/**
* @param string $charset The output charset of the helper
*/
public function __construct(string $charset)
{
$this->setCharset($charset);
}

/**
* Sets the default charset.
*/
public function setCharset(string $charset): void
{
$this->charset = $charset;
}

/**
* Gets the default charset.
*/
public function getCharset(): string
{
return $this->charset;
Expand All @@ -74,9 +62,6 @@ public function setLocale(string $locale): void
$this->locale = $locale;
}

/**
* @static
*/
public static function getICUDataVersion(): string
{
if (\defined('INTL_ICU_VERSION')) {
Expand Down Expand Up @@ -122,10 +107,6 @@ public static function getICUDataVersion(): string
* charset of the kernel.
*
* Precondition: the kernel charset is not UTF-8
*
* @param string $string The string to fix
*
* @return string A string with the %kernel.charset% encoding
*/
protected function fixCharset(string $string): string
{
Expand Down
43 changes: 30 additions & 13 deletions src/Helper/DateTimeFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,12 @@ public function __construct(
parent::__construct($charset);
}

public function formatDate($date, ?string $locale = null, ?string $timezone = null, ?int $dateType = null): string
{
public function formatDate(
\DateTimeInterface|string|int $date,
?string $locale = null,
?string $timezone = null,
?int $dateType = null
): string {
$date = $this->getDatetime($date, $timezone);

$formatter = self::createInstance([
Expand All @@ -51,8 +55,13 @@ public function formatDate($date, ?string $locale = null, ?string $timezone = nu
return $this->process($formatter, $date);
}

public function formatDateTime($datetime, ?string $locale = null, ?string $timezone = null, ?int $dateType = null, ?int $timeType = null): string
{
public function formatDateTime(
\DateTimeInterface|string|int $datetime,
?string $locale = null,
?string $timezone = null,
?int $dateType = null,
?int $timeType = null
): string {
$date = $this->getDatetime($datetime, $timezone);

$formatter = self::createInstance([
Expand All @@ -66,8 +75,12 @@ public function formatDateTime($datetime, ?string $locale = null, ?string $timez
return $this->process($formatter, $date);
}

public function formatTime($time, ?string $locale = null, ?string $timezone = null, ?int $timeType = null): string
{
public function formatTime(
\DateTimeInterface|string|int $time,
?string $locale = null,
?string $timezone = null,
?int $timeType = null
): string {
$date = $this->getDatetime($time, $timezone);

$formatter = self::createInstance([
Expand All @@ -81,8 +94,12 @@ public function formatTime($time, ?string $locale = null, ?string $timezone = nu
return $this->process($formatter, $date);
}

public function format($datetime, string $pattern, ?string $locale = null, ?string $timezone = null): string
{
public function format(
\DateTimeInterface|string|int $datetime,
string $pattern,
?string $locale = null,
?string $timezone = null
): string {
$date = $this->getDatetime($datetime, $timezone);

$formatter = self::createInstance([
Expand All @@ -97,8 +114,10 @@ public function format($datetime, string $pattern, ?string $locale = null, ?stri
return $this->process($formatter, $date);
}

public function getDatetime($data, ?string $timezone = null): \DateTimeInterface
{
public function getDatetime(
\DateTimeInterface|string|int $data,
?string $timezone = null
): \DateTimeInterface {
if ($data instanceof \DateTimeInterface) {
return $data;
}
Expand All @@ -124,10 +143,8 @@ public function getDatetime($data, ?string $timezone = null): \DateTimeInterface

/**
* @param mixed[] $args
*
* @return \IntlDateFormatter
*/
protected static function createInstance(array $args = [])
private static function createInstance(array $args = []): \IntlDateFormatter
{
if (null === self::$reflection) {
self::$reflection = new \ReflectionClass(\IntlDateFormatter::class);
Expand Down
51 changes: 33 additions & 18 deletions src/Helper/DateTimeFormatterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,34 +23,49 @@
interface DateTimeFormatterInterface
{
/**
* @param \DateTimeInterface|string|int $date
* @param int|null $dateType See \IntlDateFormatter::getDateType
* @param int|null $dateType See \IntlDateFormatter::getDateType
*/
public function formatDate($date, ?string $locale = null, ?string $timezone = null, ?int $dateType = null): string;
public function formatDate(
\DateTimeInterface|string|int $date,
?string $locale = null,
?string $timezone = null,
?int $dateType = null
): string;

/**
* @param \DateTimeInterface|string|int $datetime
* @param int|null $dateType See \IntlDateFormatter::getDateType
* @param int|null $timeType See \IntlDateFormatter::getTimeType
* @param int|null $dateType See \IntlDateFormatter::getDateType
* @param int|null $timeType See \IntlDateFormatter::getTimeType
*/
public function formatDateTime($datetime, ?string $locale = null, ?string $timezone = null, ?int $dateType = null, ?int $timeType = null): string;
public function formatDateTime(
\DateTimeInterface|string|int $datetime,
?string $locale = null,
?string $timezone = null,
?int $dateType = null,
?int $timeType = null
): string;

/**
* @param \DateTimeInterface|string|int $time
* @param int|null $timeType See \IntlDateFormatter::getTimeType
* @param int|null $timeType See \IntlDateFormatter::getTimeType
*/
public function formatTime($time, ?string $locale = null, ?string $timezone = null, ?int $timeType = null): string;
public function formatTime(
\DateTimeInterface|string|int $time,
?string $locale = null,
?string $timezone = null,
?int $timeType = null
): string;

/**
* @param \DateTimeInterface|string|int $datetime
*/
public function format($datetime, string $pattern, ?string $locale = null, ?string $timezone = null): string;
public function format(
\DateTimeInterface|string|int $datetime,
string $pattern,
?string $locale = null,
?string $timezone = null
): string;

/**
* Gets a date time instance by a given data and timezone.
*
* @param \DateTimeInterface|string|int $data Value representing date
* @param string|null $timezone Timezone of the date
*/
public function getDatetime($data, ?string $timezone = null): \DateTimeInterface;
public function getDatetime(
\DateTimeInterface|string|int $data,
?string $timezone = null
): \DateTimeInterface;
}
24 changes: 11 additions & 13 deletions src/Helper/NumberFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,27 +36,27 @@ public function __construct(
parent::__construct($charset);
}

public function formatPercent($number, array $attributes = [], array $textAttributes = [], array $symbols = [], ?string $locale = null): string
public function formatPercent(string|float|int $number, array $attributes = [], array $textAttributes = [], array $symbols = [], ?string $locale = null): string
{
return $this->format($number, \NumberFormatter::PERCENT, $attributes, $textAttributes, $symbols, $locale);
}

public function formatDuration($number, array $attributes = [], array $textAttributes = [], array $symbols = [], ?string $locale = null): string
public function formatDuration(string|float|int $number, array $attributes = [], array $textAttributes = [], array $symbols = [], ?string $locale = null): string
{
return $this->format($number, \NumberFormatter::DURATION, $attributes, $textAttributes, $symbols, $locale);
}

public function formatDecimal($number, array $attributes = [], array $textAttributes = [], array $symbols = [], ?string $locale = null): string
public function formatDecimal(string|float|int $number, array $attributes = [], array $textAttributes = [], array $symbols = [], ?string $locale = null): string
{
return $this->format($number, \NumberFormatter::DECIMAL, $attributes, $textAttributes, $symbols, $locale);
}

public function formatSpellout($number, array $attributes = [], array $textAttributes = [], array $symbols = [], ?string $locale = null): string
public function formatSpellout(string|float|int $number, array $attributes = [], array $textAttributes = [], array $symbols = [], ?string $locale = null): string
{
return $this->format($number, \NumberFormatter::SPELLOUT, $attributes, $textAttributes, $symbols, $locale);
}

public function formatCurrency($number, string $currency, array $attributes = [], array $textAttributes = [], array $symbols = [], ?string $locale = null): string
public function formatCurrency(string|float|int $number, string $currency, array $attributes = [], array $textAttributes = [], array $symbols = [], ?string $locale = null): string
{
$formatter = $this->getFormatter($locale ?? $this->getLocale(), \NumberFormatter::CURRENCY, $attributes, $textAttributes, $symbols);
$number = $this->parseNumericValue($number);
Expand All @@ -71,17 +71,17 @@ public function formatCurrency($number, string $currency, array $attributes = []
return $this->fixCharset($result);
}

public function formatScientific($number, array $attributes = [], array $textAttributes = [], array $symbols = [], ?string $locale = null): string
public function formatScientific(string|float|int $number, array $attributes = [], array $textAttributes = [], array $symbols = [], ?string $locale = null): string
{
return $this->format($number, \NumberFormatter::SCIENTIFIC, $attributes, $textAttributes, $symbols, $locale);
}

public function formatOrdinal($number, array $attributes = [], array $textAttributes = [], array $symbols = [], ?string $locale = null): string
public function formatOrdinal(string|float|int $number, array $attributes = [], array $textAttributes = [], array $symbols = [], ?string $locale = null): string
{
return $this->format($number, \NumberFormatter::ORDINAL, $attributes, $textAttributes, $symbols, $locale);
}

public function format($number, int $style, array $attributes = [], array $textAttributes = [], array $symbols = [], ?string $locale = null): string
public function format(string|float|int $number, int $style, array $attributes = [], array $textAttributes = [], array $symbols = [], ?string $locale = null): string
{
$number = $this->parseNumericValue($number);
$formatter = $this->getFormatter($locale ?? $this->getLocale(), $style, $attributes, $textAttributes, $symbols);
Expand All @@ -104,7 +104,7 @@ public function format($number, int $style, array $attributes = [], array $textA
* @param array<string, string> $textAttributes The text attributes used by \NumberFormatter
* @param array<string, string> $symbols The symbols used by \NumberFormatter
*/
protected function getFormatter(string $culture, int $style, array $attributes = [], array $textAttributes = [], array $symbols = []): \NumberFormatter
private function getFormatter(string $culture, int $style, array $attributes = [], array $textAttributes = [], array $symbols = []): \NumberFormatter
{
$attributes = $this->parseAttributes(array_merge($this->attributes, $attributes));
$textAttributes = $this->parseAttributes(array_merge($this->textAttributes, $textAttributes));
Expand Down Expand Up @@ -144,7 +144,7 @@ protected function getFormatter(string $culture, int $style, array $attributes =
* @phpstan-param array<string, T> $attributes
* @phpstan-return array<int, T>
*/
protected function parseAttributes(array $attributes)
private function parseAttributes(array $attributes): array
{
$result = [];

Expand All @@ -158,11 +158,9 @@ protected function parseAttributes(array $attributes)
/**
* Parse the given value trying to get a match with a \NumberFormatter constant.
*
* @param string $attribute The constant's name
*
* @throws \InvalidArgumentException If the value does not match any constant
*/
protected function parseConstantValue(string $attribute): int
private function parseConstantValue(string $attribute): int
{
$attribute = strtoupper($attribute);
$constantName = 'NumberFormatter::'.$attribute;
Expand Down
Loading

0 comments on commit 53d0ca0

Please sign in to comment.